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 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1181 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1182 | |
| 1183 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1184 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1185 | { |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 1186 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1187 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1188 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1189 | |
| 1190 | if (0 == DeviceIoControl( |
| 1191 | reparse_point_handle, |
| 1192 | FSCTL_GET_REPARSE_POINT, |
| 1193 | NULL, 0, /* in buffer */ |
| 1194 | target_buffer, sizeof(target_buffer), |
| 1195 | &n_bytes_returned, |
| 1196 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1197 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1198 | |
| 1199 | if (reparse_tag) |
| 1200 | *reparse_tag = rdb->ReparseTag; |
| 1201 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1202 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1203 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1204 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1205 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1206 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1207 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1208 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1209 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1210 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1211 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1212 | */ |
| 1213 | #include <crt_externs.h> |
| 1214 | static char **environ; |
| 1215 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1216 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1217 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1218 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1219 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1220 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1221 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1222 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1223 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1224 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1225 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1226 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1227 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1228 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1229 | d = PyDict_New(); |
| 1230 | if (d == NULL) |
| 1231 | return NULL; |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1232 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1233 | if (environ == NULL) |
| 1234 | environ = *_NSGetEnviron(); |
| 1235 | #endif |
| 1236 | #ifdef MS_WINDOWS |
| 1237 | /* _wenviron must be initialized in this way if the program is started |
| 1238 | through main() instead of wmain(). */ |
| 1239 | _wgetenv(L""); |
| 1240 | if (_wenviron == NULL) |
| 1241 | return d; |
| 1242 | /* This part ignores errors */ |
| 1243 | for (e = _wenviron; *e != NULL; e++) { |
| 1244 | PyObject *k; |
| 1245 | PyObject *v; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1246 | const wchar_t *p = wcschr(*e, L'='); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1247 | if (p == NULL) |
| 1248 | continue; |
| 1249 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1250 | if (k == NULL) { |
| 1251 | PyErr_Clear(); |
| 1252 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1253 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1254 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1255 | if (v == NULL) { |
| 1256 | PyErr_Clear(); |
| 1257 | Py_DECREF(k); |
| 1258 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1259 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1260 | if (PyDict_GetItem(d, k) == NULL) { |
| 1261 | if (PyDict_SetItem(d, k, v) != 0) |
| 1262 | PyErr_Clear(); |
| 1263 | } |
| 1264 | Py_DECREF(k); |
| 1265 | Py_DECREF(v); |
| 1266 | } |
| 1267 | #else |
| 1268 | if (environ == NULL) |
| 1269 | return d; |
| 1270 | /* This part ignores errors */ |
| 1271 | for (e = environ; *e != NULL; e++) { |
| 1272 | PyObject *k; |
| 1273 | PyObject *v; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1274 | const char *p = strchr(*e, '='); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1275 | if (p == NULL) |
| 1276 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1277 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1278 | if (k == NULL) { |
| 1279 | PyErr_Clear(); |
| 1280 | continue; |
| 1281 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1282 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1283 | if (v == NULL) { |
| 1284 | PyErr_Clear(); |
| 1285 | Py_DECREF(k); |
| 1286 | continue; |
| 1287 | } |
| 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); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1294 | } |
| 1295 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1296 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1299 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1300 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1301 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1302 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1303 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1304 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1305 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1306 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1307 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1308 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1309 | win32_error(const char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1310 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1311 | /* XXX We should pass the function name along in the future. |
| 1312 | (winreg.c also wants to pass the function name.) |
| 1313 | This would however require an additional param to the |
| 1314 | Windows error object, which is non-trivial. |
| 1315 | */ |
| 1316 | errno = GetLastError(); |
| 1317 | if (filename) |
| 1318 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1319 | else |
| 1320 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1321 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1322 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1323 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1324 | win32_error_object(const char* function, PyObject* filename) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1325 | { |
| 1326 | /* XXX - see win32_error for comments on 'function' */ |
| 1327 | errno = GetLastError(); |
| 1328 | if (filename) |
| 1329 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1330 | PyExc_OSError, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1331 | errno, |
| 1332 | filename); |
| 1333 | else |
| 1334 | return PyErr_SetFromWindowsErr(errno); |
| 1335 | } |
| 1336 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1337 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1338 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1339 | static PyObject * |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1340 | path_error(path_t *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1341 | { |
| 1342 | #ifdef MS_WINDOWS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1343 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 1344 | 0, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1345 | #else |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1346 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1347 | #endif |
| 1348 | } |
| 1349 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1350 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1351 | static PyObject * |
| 1352 | path_error2(path_t *path, path_t *path2) |
| 1353 | { |
| 1354 | #ifdef MS_WINDOWS |
| 1355 | return PyErr_SetExcFromWindowsErrWithFilenameObjects(PyExc_OSError, |
| 1356 | 0, path->object, path2->object); |
| 1357 | #else |
| 1358 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, |
| 1359 | path->object, path2->object); |
| 1360 | #endif |
| 1361 | } |
| 1362 | |
| 1363 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1364 | /* POSIX generic methods */ |
| 1365 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1366 | static int |
| 1367 | fildes_converter(PyObject *o, void *p) |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1368 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1369 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1370 | int *pointer = (int *)p; |
| 1371 | fd = PyObject_AsFileDescriptor(o); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1372 | if (fd < 0) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1373 | return 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1374 | *pointer = fd; |
| 1375 | return 1; |
| 1376 | } |
| 1377 | |
| 1378 | static PyObject * |
| 1379 | posix_fildes_fd(int fd, int (*func)(int)) |
| 1380 | { |
| 1381 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1382 | int async_err = 0; |
| 1383 | |
| 1384 | do { |
| 1385 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1386 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1387 | res = (*func)(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1388 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1389 | Py_END_ALLOW_THREADS |
| 1390 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 1391 | if (res != 0) |
| 1392 | return (!async_err) ? posix_error() : NULL; |
| 1393 | Py_RETURN_NONE; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1394 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1395 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1396 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1397 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1398 | /* This is a reimplementation of the C library's chdir function, |
| 1399 | but one that produces Win32 errors instead of DOS error codes. |
| 1400 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1401 | it also needs to set "magic" environment variables indicating |
| 1402 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1403 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1404 | win32_wchdir(LPCWSTR path) |
| 1405 | { |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1406 | wchar_t path_buf[MAX_PATH], *new_path = path_buf; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1407 | int result; |
| 1408 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1409 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1410 | if(!SetCurrentDirectoryW(path)) |
| 1411 | return FALSE; |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1412 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1413 | if (!result) |
| 1414 | return FALSE; |
Victor Stinner | e847d71 | 2015-12-14 00:21:50 +0100 | [diff] [blame] | 1415 | if (result > Py_ARRAY_LENGTH(path_buf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1416 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1417 | if (!new_path) { |
| 1418 | SetLastError(ERROR_OUTOFMEMORY); |
| 1419 | return FALSE; |
| 1420 | } |
| 1421 | result = GetCurrentDirectoryW(result, new_path); |
| 1422 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1423 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1424 | return FALSE; |
| 1425 | } |
| 1426 | } |
| 1427 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1428 | wcsncmp(new_path, L"//", 2) == 0) |
| 1429 | /* UNC path, nothing to do. */ |
| 1430 | return TRUE; |
| 1431 | env[1] = new_path[0]; |
| 1432 | result = SetEnvironmentVariableW(env, new_path); |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1433 | if (new_path != path_buf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1434 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1435 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1436 | } |
| 1437 | #endif |
| 1438 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1439 | #ifdef MS_WINDOWS |
| 1440 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1441 | - time stamps are restricted to second resolution |
| 1442 | - file modification times suffer from forth-and-back conversions between |
| 1443 | UTC and local time |
| 1444 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1445 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1446 | #define HAVE_STAT_NSEC 1 |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1447 | #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1448 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1449 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1450 | find_data_to_file_info(WIN32_FIND_DATAW *pFileData, |
| 1451 | BY_HANDLE_FILE_INFORMATION *info, |
| 1452 | ULONG *reparse_tag) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1453 | { |
| 1454 | memset(info, 0, sizeof(*info)); |
| 1455 | info->dwFileAttributes = pFileData->dwFileAttributes; |
| 1456 | info->ftCreationTime = pFileData->ftCreationTime; |
| 1457 | info->ftLastAccessTime = pFileData->ftLastAccessTime; |
| 1458 | info->ftLastWriteTime = pFileData->ftLastWriteTime; |
| 1459 | info->nFileSizeHigh = pFileData->nFileSizeHigh; |
| 1460 | info->nFileSizeLow = pFileData->nFileSizeLow; |
| 1461 | /* info->nNumberOfLinks = 1; */ |
| 1462 | if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1463 | *reparse_tag = pFileData->dwReserved0; |
| 1464 | else |
| 1465 | *reparse_tag = 0; |
| 1466 | } |
| 1467 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1468 | static BOOL |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1469 | 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] | 1470 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1471 | HANDLE hFindFile; |
| 1472 | WIN32_FIND_DATAW FileData; |
| 1473 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1474 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1475 | return FALSE; |
| 1476 | FindClose(hFindFile); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1477 | find_data_to_file_info(&FileData, info, reparse_tag); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1478 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1481 | static BOOL |
| 1482 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1483 | { |
| 1484 | int buf_size, result_length; |
| 1485 | wchar_t *buf; |
| 1486 | |
| 1487 | /* We have a good handle to the target, use it to determine |
| 1488 | the target path name (then we'll call lstat on it). */ |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1489 | buf_size = GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1490 | VOLUME_NAME_DOS); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1491 | if(!buf_size) |
| 1492 | return FALSE; |
| 1493 | |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1494 | buf = (wchar_t *)PyMem_RawMalloc((buf_size + 1) * sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1495 | if (!buf) { |
| 1496 | SetLastError(ERROR_OUTOFMEMORY); |
| 1497 | return FALSE; |
| 1498 | } |
| 1499 | |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1500 | result_length = GetFinalPathNameByHandleW(hdl, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1501 | buf, buf_size, VOLUME_NAME_DOS); |
| 1502 | |
| 1503 | if(!result_length) { |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1504 | PyMem_RawFree(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1505 | return FALSE; |
| 1506 | } |
| 1507 | |
| 1508 | if(!CloseHandle(hdl)) { |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1509 | PyMem_RawFree(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1510 | return FALSE; |
| 1511 | } |
| 1512 | |
| 1513 | buf[result_length] = 0; |
| 1514 | |
| 1515 | *target_path = buf; |
| 1516 | return TRUE; |
| 1517 | } |
| 1518 | |
| 1519 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1520 | win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1521 | BOOL traverse) |
| 1522 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1523 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1524 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1525 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1526 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1527 | wchar_t *target_path; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1528 | const wchar_t *dot; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1529 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1530 | hFile = CreateFileW( |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1531 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1532 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1533 | 0, /* share mode */ |
| 1534 | NULL, /* security attributes */ |
| 1535 | OPEN_EXISTING, |
| 1536 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1537 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1538 | Because of this, calls like GetFinalPathNameByHandle will return |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 1539 | the symlink path again and not the actual final path. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1540 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1541 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1542 | NULL); |
| 1543 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1544 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1545 | /* Either the target doesn't exist, or we don't have access to |
| 1546 | get a handle to it. If the former, we need to return an error. |
| 1547 | If the latter, we can use attributes_from_dir. */ |
| 1548 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1549 | return -1; |
| 1550 | /* Could not get attributes on open file. Fall back to |
| 1551 | reading the directory. */ |
| 1552 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1553 | /* Very strange. This should not fail now */ |
| 1554 | return -1; |
| 1555 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1556 | if (traverse) { |
| 1557 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1558 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1559 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1560 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1561 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1562 | } else { |
| 1563 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1564 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1565 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1566 | } |
| 1567 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1568 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1569 | return -1; |
| 1570 | |
| 1571 | /* Close the outer open file handle now that we're about to |
| 1572 | reopen it with different flags. */ |
| 1573 | if (!CloseHandle(hFile)) |
| 1574 | return -1; |
| 1575 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1576 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1577 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1578 | the file without the reparse handling flag set. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1579 | hFile2 = CreateFileW( |
| 1580 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1581 | NULL, OPEN_EXISTING, |
| 1582 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1583 | NULL); |
| 1584 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1585 | return -1; |
| 1586 | |
| 1587 | if (!get_target_path(hFile2, &target_path)) |
| 1588 | return -1; |
| 1589 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1590 | code = win32_xstat_impl(target_path, result, FALSE); |
Victor Stinner | c36674a | 2016-03-16 14:30:16 +0100 | [diff] [blame] | 1591 | PyMem_RawFree(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1592 | return code; |
| 1593 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1594 | } else |
| 1595 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1596 | } |
Steve Dower | a2af1a5 | 2015-02-21 10:04:10 -0800 | [diff] [blame] | 1597 | _Py_attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1598 | |
| 1599 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1600 | dot = wcsrchr(path, '.'); |
| 1601 | if (dot) { |
| 1602 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1603 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1604 | result->st_mode |= 0111; |
| 1605 | } |
| 1606 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1610 | 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] | 1611 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1612 | /* Protocol violation: we explicitly clear errno, instead of |
| 1613 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1614 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1615 | errno = 0; |
| 1616 | return code; |
| 1617 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1618 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1619 | |
| 1620 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1621 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1622 | default does not traverse symlinks and instead returns attributes for |
| 1623 | the symlink. |
| 1624 | |
| 1625 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1626 | win32_stat will first explicitly resolve the symlink target and then will |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1627 | call win32_lstat on that result. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1628 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1629 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1630 | 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] | 1631 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1632 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1635 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1636 | win32_stat(const wchar_t* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1637 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1638 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1641 | #endif /* MS_WINDOWS */ |
| 1642 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1643 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1644 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1645 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1646 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1647 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1648 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1649 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1650 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1651 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1652 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1653 | |
| 1654 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1655 | {"st_mode", "protection bits"}, |
| 1656 | {"st_ino", "inode"}, |
| 1657 | {"st_dev", "device"}, |
| 1658 | {"st_nlink", "number of hard links"}, |
| 1659 | {"st_uid", "user ID of owner"}, |
| 1660 | {"st_gid", "group ID of owner"}, |
| 1661 | {"st_size", "total size, in bytes"}, |
| 1662 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1663 | {NULL, "integer time of last access"}, |
| 1664 | {NULL, "integer time of last modification"}, |
| 1665 | {NULL, "integer time of last change"}, |
| 1666 | {"st_atime", "time of last access"}, |
| 1667 | {"st_mtime", "time of last modification"}, |
| 1668 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1669 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1670 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1671 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1672 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1673 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1674 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1675 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1676 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1677 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1678 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1679 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1680 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1681 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1682 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1683 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1684 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1685 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1686 | #endif |
| 1687 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1688 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1689 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1690 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1691 | {"st_file_attributes", "Windows file attribute bits"}, |
| 1692 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1693 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1694 | }; |
| 1695 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1696 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1697 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1698 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1699 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1700 | #endif |
| 1701 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1702 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1703 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1704 | #else |
| 1705 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1706 | #endif |
| 1707 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1708 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1709 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1710 | #else |
| 1711 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1712 | #endif |
| 1713 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1714 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1715 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1716 | #else |
| 1717 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1718 | #endif |
| 1719 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1720 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1721 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1722 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1723 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1724 | #endif |
| 1725 | |
| 1726 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1727 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1728 | #else |
| 1729 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1730 | #endif |
| 1731 | |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1732 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1733 | #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1) |
| 1734 | #else |
| 1735 | #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX |
| 1736 | #endif |
| 1737 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1738 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1739 | "stat_result", /* name */ |
| 1740 | stat_result__doc__, /* doc */ |
| 1741 | stat_result_fields, |
| 1742 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1743 | }; |
| 1744 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1745 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1746 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1747 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1748 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1749 | 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] | 1750 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1751 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1752 | |
| 1753 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1754 | {"f_bsize", }, |
| 1755 | {"f_frsize", }, |
| 1756 | {"f_blocks", }, |
| 1757 | {"f_bfree", }, |
| 1758 | {"f_bavail", }, |
| 1759 | {"f_files", }, |
| 1760 | {"f_ffree", }, |
| 1761 | {"f_favail", }, |
| 1762 | {"f_flag", }, |
| 1763 | {"f_namemax",}, |
| 1764 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1765 | }; |
| 1766 | |
| 1767 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1768 | "statvfs_result", /* name */ |
| 1769 | statvfs_result__doc__, /* doc */ |
| 1770 | statvfs_result_fields, |
| 1771 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1772 | }; |
| 1773 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1774 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1775 | PyDoc_STRVAR(waitid_result__doc__, |
| 1776 | "waitid_result: Result from waitid.\n\n\ |
| 1777 | This object may be accessed either as a tuple of\n\ |
| 1778 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1779 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1780 | \n\ |
| 1781 | See os.waitid for more information."); |
| 1782 | |
| 1783 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1784 | {"si_pid", }, |
| 1785 | {"si_uid", }, |
| 1786 | {"si_signo", }, |
| 1787 | {"si_status", }, |
| 1788 | {"si_code", }, |
| 1789 | {0} |
| 1790 | }; |
| 1791 | |
| 1792 | static PyStructSequence_Desc waitid_result_desc = { |
| 1793 | "waitid_result", /* name */ |
| 1794 | waitid_result__doc__, /* doc */ |
| 1795 | waitid_result_fields, |
| 1796 | 5 |
| 1797 | }; |
| 1798 | static PyTypeObject WaitidResultType; |
| 1799 | #endif |
| 1800 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1801 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1802 | static PyTypeObject StatResultType; |
| 1803 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1804 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 1805 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1806 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1807 | static newfunc structseq_new; |
| 1808 | |
| 1809 | static PyObject * |
| 1810 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1811 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1812 | PyStructSequence *result; |
| 1813 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1814 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1815 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1816 | if (!result) |
| 1817 | return NULL; |
| 1818 | /* If we have been initialized from a tuple, |
| 1819 | st_?time might be set to None. Initialize it |
| 1820 | from the int slots. */ |
| 1821 | for (i = 7; i <= 9; i++) { |
| 1822 | if (result->ob_item[i+3] == Py_None) { |
| 1823 | Py_DECREF(Py_None); |
| 1824 | Py_INCREF(result->ob_item[i]); |
| 1825 | result->ob_item[i+3] = result->ob_item[i]; |
| 1826 | } |
| 1827 | } |
| 1828 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | |
| 1832 | |
| 1833 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1834 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1835 | |
| 1836 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1837 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1838 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1839 | \n\ |
| 1840 | If value is True, future calls to stat() return floats; if it is False,\n\ |
| 1841 | future calls return ints.\n\ |
| 1842 | If value is omitted, return the current setting.\n"); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1843 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1844 | /* 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] | 1845 | static PyObject* |
| 1846 | stat_float_times(PyObject* self, PyObject *args) |
| 1847 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1848 | int newval = -1; |
| 1849 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1850 | return NULL; |
Victor Stinner | 034d0aa | 2012-06-05 01:22:15 +0200 | [diff] [blame] | 1851 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 1852 | "stat_float_times() is deprecated", |
| 1853 | 1)) |
| 1854 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1855 | if (newval == -1) |
| 1856 | /* Return old value */ |
| 1857 | return PyBool_FromLong(_stat_float_times); |
| 1858 | _stat_float_times = newval; |
| 1859 | Py_INCREF(Py_None); |
| 1860 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1861 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1862 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1863 | static PyObject *billion = NULL; |
| 1864 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1865 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1866 | 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] | 1867 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1868 | PyObject *s = _PyLong_FromTime_t(sec); |
| 1869 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 1870 | PyObject *s_in_ns = NULL; |
| 1871 | PyObject *ns_total = NULL; |
| 1872 | PyObject *float_s = NULL; |
| 1873 | |
| 1874 | if (!(s && ns_fractional)) |
| 1875 | goto exit; |
| 1876 | |
| 1877 | s_in_ns = PyNumber_Multiply(s, billion); |
| 1878 | if (!s_in_ns) |
| 1879 | goto exit; |
| 1880 | |
| 1881 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 1882 | if (!ns_total) |
| 1883 | goto exit; |
| 1884 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1885 | if (_stat_float_times) { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1886 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1887 | if (!float_s) |
| 1888 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1889 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1890 | else { |
| 1891 | float_s = s; |
| 1892 | Py_INCREF(float_s); |
| 1893 | } |
| 1894 | |
| 1895 | PyStructSequence_SET_ITEM(v, index, s); |
| 1896 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 1897 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 1898 | s = NULL; |
| 1899 | float_s = NULL; |
| 1900 | ns_total = NULL; |
| 1901 | exit: |
| 1902 | Py_XDECREF(s); |
| 1903 | Py_XDECREF(ns_fractional); |
| 1904 | Py_XDECREF(s_in_ns); |
| 1905 | Py_XDECREF(ns_total); |
| 1906 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1909 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1910 | (used by posix_stat() and posix_fstat()) */ |
| 1911 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1912 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1913 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1914 | unsigned long ansec, mnsec, cnsec; |
| 1915 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 1916 | if (v == NULL) |
| 1917 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1918 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1919 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1920 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1921 | PyStructSequence_SET_ITEM(v, 1, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1922 | PyLong_FromLongLong((long long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1923 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1924 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1925 | #endif |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 1926 | #ifdef MS_WINDOWS |
| 1927 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1928 | #else |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 1929 | PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1930 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1931 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 1932 | #if defined(MS_WINDOWS) |
| 1933 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 1934 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 1935 | #else |
| 1936 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 1937 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 1938 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1939 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1940 | PyStructSequence_SET_ITEM(v, 6, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1941 | PyLong_FromLongLong((long long)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1942 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1943 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1944 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1945 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1946 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1947 | ansec = st->st_atim.tv_nsec; |
| 1948 | mnsec = st->st_mtim.tv_nsec; |
| 1949 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1950 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1951 | ansec = st->st_atimespec.tv_nsec; |
| 1952 | mnsec = st->st_mtimespec.tv_nsec; |
| 1953 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1954 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1955 | ansec = st->st_atime_nsec; |
| 1956 | mnsec = st->st_mtime_nsec; |
| 1957 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1958 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1959 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1960 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1961 | fill_time(v, 7, st->st_atime, ansec); |
| 1962 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1963 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1964 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1965 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1966 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 1967 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1968 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1969 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1970 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 1971 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1972 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1973 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1974 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 1975 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1976 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1977 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1978 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 1979 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1980 | #endif |
| 1981 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1982 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1983 | PyObject *val; |
| 1984 | unsigned long bsec,bnsec; |
| 1985 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1986 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1987 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1988 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1989 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1990 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1991 | if (_stat_float_times) { |
| 1992 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1993 | } else { |
| 1994 | val = PyLong_FromLong((long)bsec); |
| 1995 | } |
| 1996 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1997 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1998 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1999 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2000 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2001 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2002 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2003 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2004 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2005 | PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX, |
| 2006 | PyLong_FromUnsignedLong(st->st_file_attributes)); |
| 2007 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2008 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2009 | if (PyErr_Occurred()) { |
| 2010 | Py_DECREF(v); |
| 2011 | return NULL; |
| 2012 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2013 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2014 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2017 | /* POSIX methods */ |
| 2018 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2019 | |
| 2020 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2021 | posix_do_stat(const char *function_name, path_t *path, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2022 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2023 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2024 | STRUCT_STAT st; |
| 2025 | int result; |
| 2026 | |
| 2027 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2028 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2029 | return NULL; |
| 2030 | #endif |
| 2031 | |
| 2032 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2033 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2034 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2035 | return NULL; |
| 2036 | |
| 2037 | Py_BEGIN_ALLOW_THREADS |
| 2038 | if (path->fd != -1) |
| 2039 | result = FSTAT(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2040 | #ifdef MS_WINDOWS |
Steve Dower | 513d747 | 2016-09-08 10:41:50 -0700 | [diff] [blame] | 2041 | else if (follow_symlinks) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2042 | result = win32_stat(path->wide, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2043 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2044 | result = win32_lstat(path->wide, &st); |
| 2045 | #else |
| 2046 | else |
| 2047 | #if defined(HAVE_LSTAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2048 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2049 | result = LSTAT(path->narrow, &st); |
| 2050 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2051 | #endif /* HAVE_LSTAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2052 | #ifdef HAVE_FSTATAT |
| 2053 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2054 | result = fstatat(dir_fd, path->narrow, &st, |
| 2055 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2056 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2057 | #endif /* HAVE_FSTATAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2058 | result = STAT(path->narrow, &st); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2059 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2060 | Py_END_ALLOW_THREADS |
| 2061 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2062 | if (result != 0) { |
| 2063 | return path_error(path); |
| 2064 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2065 | |
| 2066 | return _pystat_fromstructstat(&st); |
| 2067 | } |
| 2068 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2069 | /*[python input] |
| 2070 | |
| 2071 | for s in """ |
| 2072 | |
| 2073 | FACCESSAT |
| 2074 | FCHMODAT |
| 2075 | FCHOWNAT |
| 2076 | FSTATAT |
| 2077 | LINKAT |
| 2078 | MKDIRAT |
| 2079 | MKFIFOAT |
| 2080 | MKNODAT |
| 2081 | OPENAT |
| 2082 | READLINKAT |
| 2083 | SYMLINKAT |
| 2084 | UNLINKAT |
| 2085 | |
| 2086 | """.strip().split(): |
| 2087 | s = s.strip() |
| 2088 | print(""" |
| 2089 | #ifdef HAVE_{s} |
| 2090 | #define {s}_DIR_FD_CONVERTER dir_fd_converter |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2091 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2092 | #define {s}_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2093 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2094 | """.rstrip().format(s=s)) |
| 2095 | |
| 2096 | for s in """ |
| 2097 | |
| 2098 | FCHDIR |
| 2099 | FCHMOD |
| 2100 | FCHOWN |
| 2101 | FDOPENDIR |
| 2102 | FEXECVE |
| 2103 | FPATHCONF |
| 2104 | FSTATVFS |
| 2105 | FTRUNCATE |
| 2106 | |
| 2107 | """.strip().split(): |
| 2108 | s = s.strip() |
| 2109 | print(""" |
| 2110 | #ifdef HAVE_{s} |
| 2111 | #define PATH_HAVE_{s} 1 |
| 2112 | #else |
| 2113 | #define PATH_HAVE_{s} 0 |
| 2114 | #endif |
| 2115 | |
| 2116 | """.rstrip().format(s=s)) |
| 2117 | [python start generated code]*/ |
| 2118 | |
| 2119 | #ifdef HAVE_FACCESSAT |
| 2120 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter |
| 2121 | #else |
| 2122 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2123 | #endif |
| 2124 | |
| 2125 | #ifdef HAVE_FCHMODAT |
| 2126 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2127 | #else |
| 2128 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2129 | #endif |
| 2130 | |
| 2131 | #ifdef HAVE_FCHOWNAT |
| 2132 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter |
| 2133 | #else |
| 2134 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2135 | #endif |
| 2136 | |
| 2137 | #ifdef HAVE_FSTATAT |
| 2138 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter |
| 2139 | #else |
| 2140 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2141 | #endif |
| 2142 | |
| 2143 | #ifdef HAVE_LINKAT |
| 2144 | #define LINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2145 | #else |
| 2146 | #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2147 | #endif |
| 2148 | |
| 2149 | #ifdef HAVE_MKDIRAT |
| 2150 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter |
| 2151 | #else |
| 2152 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2153 | #endif |
| 2154 | |
| 2155 | #ifdef HAVE_MKFIFOAT |
| 2156 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter |
| 2157 | #else |
| 2158 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2159 | #endif |
| 2160 | |
| 2161 | #ifdef HAVE_MKNODAT |
| 2162 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2163 | #else |
| 2164 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2165 | #endif |
| 2166 | |
| 2167 | #ifdef HAVE_OPENAT |
| 2168 | #define OPENAT_DIR_FD_CONVERTER dir_fd_converter |
| 2169 | #else |
| 2170 | #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2171 | #endif |
| 2172 | |
| 2173 | #ifdef HAVE_READLINKAT |
| 2174 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2175 | #else |
| 2176 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2177 | #endif |
| 2178 | |
| 2179 | #ifdef HAVE_SYMLINKAT |
| 2180 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2181 | #else |
| 2182 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2183 | #endif |
| 2184 | |
| 2185 | #ifdef HAVE_UNLINKAT |
| 2186 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2187 | #else |
| 2188 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2189 | #endif |
| 2190 | |
| 2191 | #ifdef HAVE_FCHDIR |
| 2192 | #define PATH_HAVE_FCHDIR 1 |
| 2193 | #else |
| 2194 | #define PATH_HAVE_FCHDIR 0 |
| 2195 | #endif |
| 2196 | |
| 2197 | #ifdef HAVE_FCHMOD |
| 2198 | #define PATH_HAVE_FCHMOD 1 |
| 2199 | #else |
| 2200 | #define PATH_HAVE_FCHMOD 0 |
| 2201 | #endif |
| 2202 | |
| 2203 | #ifdef HAVE_FCHOWN |
| 2204 | #define PATH_HAVE_FCHOWN 1 |
| 2205 | #else |
| 2206 | #define PATH_HAVE_FCHOWN 0 |
| 2207 | #endif |
| 2208 | |
| 2209 | #ifdef HAVE_FDOPENDIR |
| 2210 | #define PATH_HAVE_FDOPENDIR 1 |
| 2211 | #else |
| 2212 | #define PATH_HAVE_FDOPENDIR 0 |
| 2213 | #endif |
| 2214 | |
| 2215 | #ifdef HAVE_FEXECVE |
| 2216 | #define PATH_HAVE_FEXECVE 1 |
| 2217 | #else |
| 2218 | #define PATH_HAVE_FEXECVE 0 |
| 2219 | #endif |
| 2220 | |
| 2221 | #ifdef HAVE_FPATHCONF |
| 2222 | #define PATH_HAVE_FPATHCONF 1 |
| 2223 | #else |
| 2224 | #define PATH_HAVE_FPATHCONF 0 |
| 2225 | #endif |
| 2226 | |
| 2227 | #ifdef HAVE_FSTATVFS |
| 2228 | #define PATH_HAVE_FSTATVFS 1 |
| 2229 | #else |
| 2230 | #define PATH_HAVE_FSTATVFS 0 |
| 2231 | #endif |
| 2232 | |
| 2233 | #ifdef HAVE_FTRUNCATE |
| 2234 | #define PATH_HAVE_FTRUNCATE 1 |
| 2235 | #else |
| 2236 | #define PATH_HAVE_FTRUNCATE 0 |
| 2237 | #endif |
| 2238 | /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2239 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 2240 | #ifdef MS_WINDOWS |
| 2241 | #undef PATH_HAVE_FTRUNCATE |
| 2242 | #define PATH_HAVE_FTRUNCATE 1 |
| 2243 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2244 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2245 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2246 | |
| 2247 | class path_t_converter(CConverter): |
| 2248 | |
| 2249 | type = "path_t" |
| 2250 | impl_by_reference = True |
| 2251 | parse_by_reference = True |
| 2252 | |
| 2253 | converter = 'path_converter' |
| 2254 | |
| 2255 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2256 | # right now path_t doesn't support default values. |
| 2257 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2258 | if self.default not in (unspecified, None): |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2259 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2260 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2261 | if self.c_default not in (None, 'Py_None'): |
| 2262 | 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] | 2263 | |
| 2264 | self.nullable = nullable |
| 2265 | self.allow_fd = allow_fd |
| 2266 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2267 | def pre_render(self): |
| 2268 | def strify(value): |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2269 | if isinstance(value, str): |
| 2270 | return value |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2271 | return str(int(bool(value))) |
| 2272 | |
| 2273 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2274 | self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format( |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2275 | self.function.name, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2276 | self.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2277 | strify(self.nullable), |
| 2278 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2279 | ) |
| 2280 | |
| 2281 | def cleanup(self): |
| 2282 | return "path_cleanup(&" + self.name + ");\n" |
| 2283 | |
| 2284 | |
| 2285 | class dir_fd_converter(CConverter): |
| 2286 | type = 'int' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2287 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2288 | def converter_init(self, requires=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2289 | if self.default in (unspecified, None): |
| 2290 | self.c_default = 'DEFAULT_DIR_FD' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2291 | if isinstance(requires, str): |
| 2292 | self.converter = requires.upper() + '_DIR_FD_CONVERTER' |
| 2293 | else: |
| 2294 | self.converter = 'dir_fd_converter' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2295 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2296 | class fildes_converter(CConverter): |
| 2297 | type = 'int' |
| 2298 | converter = 'fildes_converter' |
| 2299 | |
| 2300 | class uid_t_converter(CConverter): |
| 2301 | type = "uid_t" |
| 2302 | converter = '_Py_Uid_Converter' |
| 2303 | |
| 2304 | class gid_t_converter(CConverter): |
| 2305 | type = "gid_t" |
| 2306 | converter = '_Py_Gid_Converter' |
| 2307 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2308 | class dev_t_converter(CConverter): |
| 2309 | type = 'dev_t' |
| 2310 | converter = '_Py_Dev_Converter' |
| 2311 | |
| 2312 | class dev_t_return_converter(unsigned_long_return_converter): |
| 2313 | type = 'dev_t' |
| 2314 | conversion_fn = '_PyLong_FromDev' |
| 2315 | unsigned_cast = '(dev_t)' |
| 2316 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2317 | class FSConverter_converter(CConverter): |
| 2318 | type = 'PyObject *' |
| 2319 | converter = 'PyUnicode_FSConverter' |
| 2320 | def converter_init(self): |
| 2321 | if self.default is not unspecified: |
| 2322 | fail("FSConverter_converter does not support default values") |
| 2323 | self.c_default = 'NULL' |
| 2324 | |
| 2325 | def cleanup(self): |
| 2326 | return "Py_XDECREF(" + self.name + ");\n" |
| 2327 | |
| 2328 | class pid_t_converter(CConverter): |
| 2329 | type = 'pid_t' |
| 2330 | format_unit = '" _Py_PARSE_PID "' |
| 2331 | |
| 2332 | class idtype_t_converter(int_converter): |
| 2333 | type = 'idtype_t' |
| 2334 | |
| 2335 | class id_t_converter(CConverter): |
| 2336 | type = 'id_t' |
| 2337 | format_unit = '" _Py_PARSE_PID "' |
| 2338 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 2339 | class intptr_t_converter(CConverter): |
| 2340 | type = 'intptr_t' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2341 | format_unit = '" _Py_PARSE_INTPTR "' |
| 2342 | |
| 2343 | class Py_off_t_converter(CConverter): |
| 2344 | type = 'Py_off_t' |
| 2345 | converter = 'Py_off_t_converter' |
| 2346 | |
| 2347 | class Py_off_t_return_converter(long_return_converter): |
| 2348 | type = 'Py_off_t' |
| 2349 | conversion_fn = 'PyLong_FromPy_off_t' |
| 2350 | |
| 2351 | class path_confname_converter(CConverter): |
| 2352 | type="int" |
| 2353 | converter="conv_path_confname" |
| 2354 | |
| 2355 | class confstr_confname_converter(path_confname_converter): |
| 2356 | converter='conv_confstr_confname' |
| 2357 | |
| 2358 | class sysconf_confname_converter(path_confname_converter): |
| 2359 | converter="conv_sysconf_confname" |
| 2360 | |
| 2361 | class sched_param_converter(CConverter): |
| 2362 | type = 'struct sched_param' |
| 2363 | converter = 'convert_sched_param' |
| 2364 | impl_by_reference = True; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2365 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2366 | [python start generated code]*/ |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 2367 | /*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2368 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2369 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2370 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2371 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2372 | |
| 2373 | path : path_t(allow_fd=True) |
| 2374 | Path to be examined; can be string, bytes, or open-file-descriptor int. |
| 2375 | |
| 2376 | * |
| 2377 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2378 | dir_fd : dir_fd(requires='fstatat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2379 | If not None, it should be a file descriptor open to a directory, |
| 2380 | and path should be a relative string; path will then be relative to |
| 2381 | that directory. |
| 2382 | |
| 2383 | follow_symlinks: bool = True |
| 2384 | If False, and the last element of the path is a symbolic link, |
| 2385 | stat will examine the symbolic link itself instead of the file |
| 2386 | the link points to. |
| 2387 | |
| 2388 | Perform a stat system call on the given path. |
| 2389 | |
| 2390 | dir_fd and follow_symlinks may not be implemented |
| 2391 | on your platform. If they are unavailable, using them will raise a |
| 2392 | NotImplementedError. |
| 2393 | |
| 2394 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2395 | an open file descriptor. |
| 2396 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2397 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2398 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2399 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2400 | os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks) |
| 2401 | /*[clinic end generated code: output=7d4976e6f18a59c5 input=099d356c306fa24a]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2402 | { |
| 2403 | return posix_do_stat("stat", path, dir_fd, follow_symlinks); |
| 2404 | } |
| 2405 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2406 | |
| 2407 | /*[clinic input] |
| 2408 | os.lstat |
| 2409 | |
| 2410 | path : path_t |
| 2411 | |
| 2412 | * |
| 2413 | |
| 2414 | dir_fd : dir_fd(requires='fstatat') = None |
| 2415 | |
| 2416 | Perform a stat system call on the given path, without following symbolic links. |
| 2417 | |
| 2418 | Like stat(), but do not follow symbolic links. |
| 2419 | Equivalent to stat(path, follow_symlinks=False). |
| 2420 | [clinic start generated code]*/ |
| 2421 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2422 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2423 | os_lstat_impl(PyObject *module, path_t *path, int dir_fd) |
| 2424 | /*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2425 | { |
| 2426 | int follow_symlinks = 0; |
| 2427 | return posix_do_stat("lstat", path, dir_fd, follow_symlinks); |
| 2428 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2429 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2430 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2431 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2432 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2433 | |
Benjamin Peterson | 768f3b4 | 2016-09-05 15:29:33 -0700 | [diff] [blame] | 2434 | path: path_t |
| 2435 | Path to be tested; can be string or bytes |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2436 | |
| 2437 | mode: int |
| 2438 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2439 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2440 | |
| 2441 | * |
| 2442 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2443 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2444 | If not None, it should be a file descriptor open to a directory, |
| 2445 | and path should be relative; path will then be relative to that |
| 2446 | directory. |
| 2447 | |
| 2448 | effective_ids: bool = False |
| 2449 | If True, access will use the effective uid/gid instead of |
| 2450 | the real uid/gid. |
| 2451 | |
| 2452 | follow_symlinks: bool = True |
| 2453 | If False, and the last element of the path is a symbolic link, |
| 2454 | access will examine the symbolic link itself instead of the file |
| 2455 | the link points to. |
| 2456 | |
| 2457 | Use the real uid/gid to test for access to a path. |
| 2458 | |
| 2459 | {parameters} |
| 2460 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2461 | on your platform. If they are unavailable, using them will raise a |
| 2462 | NotImplementedError. |
| 2463 | |
| 2464 | Note that most operations will use the effective uid/gid, therefore this |
| 2465 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2466 | has the specified access to the path. |
| 2467 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2468 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2469 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2470 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2471 | 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] | 2472 | int effective_ids, int follow_symlinks) |
Benjamin Peterson | 768f3b4 | 2016-09-05 15:29:33 -0700 | [diff] [blame] | 2473 | /*[clinic end generated code: output=cf84158bc90b1a77 input=8e8c3a6ba791fee3]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2474 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2475 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2476 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2477 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2478 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2479 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2480 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2481 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2482 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2483 | #ifndef HAVE_FACCESSAT |
| 2484 | if (follow_symlinks_specified("access", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2485 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2486 | |
| 2487 | if (effective_ids) { |
| 2488 | argument_unavailable_error("access", "effective_ids"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2489 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2490 | } |
| 2491 | #endif |
| 2492 | |
| 2493 | #ifdef MS_WINDOWS |
| 2494 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2495 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2496 | Py_END_ALLOW_THREADS |
| 2497 | |
| 2498 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2499 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2500 | * * we didn't get a -1, and |
| 2501 | * * write access wasn't requested, |
| 2502 | * * or the file isn't read-only, |
| 2503 | * * or it's a directory. |
| 2504 | * (Directories cannot be read-only on Windows.) |
| 2505 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2506 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2507 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2508 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2509 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2510 | #else |
| 2511 | |
| 2512 | Py_BEGIN_ALLOW_THREADS |
| 2513 | #ifdef HAVE_FACCESSAT |
| 2514 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2515 | effective_ids || |
| 2516 | !follow_symlinks) { |
| 2517 | int flags = 0; |
| 2518 | if (!follow_symlinks) |
| 2519 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2520 | if (effective_ids) |
| 2521 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2522 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2523 | } |
| 2524 | else |
| 2525 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2526 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2527 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2528 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2529 | #endif |
| 2530 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2531 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2534 | #ifndef F_OK |
| 2535 | #define F_OK 0 |
| 2536 | #endif |
| 2537 | #ifndef R_OK |
| 2538 | #define R_OK 4 |
| 2539 | #endif |
| 2540 | #ifndef W_OK |
| 2541 | #define W_OK 2 |
| 2542 | #endif |
| 2543 | #ifndef X_OK |
| 2544 | #define X_OK 1 |
| 2545 | #endif |
| 2546 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2547 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2548 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2549 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2550 | os.ttyname -> DecodeFSDefault |
| 2551 | |
| 2552 | fd: int |
| 2553 | Integer file descriptor handle. |
| 2554 | |
| 2555 | / |
| 2556 | |
| 2557 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2558 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2559 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2560 | static char * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2561 | os_ttyname_impl(PyObject *module, int fd) |
| 2562 | /*[clinic end generated code: output=ed16ad216d813591 input=5f72ca83e76b3b45]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2563 | { |
| 2564 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2565 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2566 | ret = ttyname(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2567 | if (ret == NULL) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2568 | posix_error(); |
| 2569 | return ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2570 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2571 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2572 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2573 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2574 | /*[clinic input] |
| 2575 | os.ctermid |
| 2576 | |
| 2577 | Return the name of the controlling terminal for this process. |
| 2578 | [clinic start generated code]*/ |
| 2579 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2580 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2581 | os_ctermid_impl(PyObject *module) |
| 2582 | /*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2583 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2584 | char *ret; |
| 2585 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2586 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2587 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2588 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2589 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2590 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2591 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2592 | if (ret == NULL) |
| 2593 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2594 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2595 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2596 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2597 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2598 | |
| 2599 | /*[clinic input] |
| 2600 | os.chdir |
| 2601 | |
| 2602 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') |
| 2603 | |
| 2604 | Change the current working directory to the specified path. |
| 2605 | |
| 2606 | path may always be specified as a string. |
| 2607 | On some platforms, path may also be specified as an open file descriptor. |
| 2608 | If this functionality is unavailable, using it raises an exception. |
| 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_chdir_impl(PyObject *module, path_t *path) |
| 2613 | /*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2614 | { |
| 2615 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2616 | |
| 2617 | Py_BEGIN_ALLOW_THREADS |
| 2618 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2619 | /* on unix, success = 0, on windows, success = !0 */ |
| 2620 | result = !win32_wchdir(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2621 | #else |
| 2622 | #ifdef HAVE_FCHDIR |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2623 | if (path->fd != -1) |
| 2624 | result = fchdir(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2625 | else |
| 2626 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2627 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2628 | #endif |
| 2629 | Py_END_ALLOW_THREADS |
| 2630 | |
| 2631 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2632 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2633 | } |
| 2634 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2635 | Py_RETURN_NONE; |
| 2636 | } |
| 2637 | |
| 2638 | |
| 2639 | #ifdef HAVE_FCHDIR |
| 2640 | /*[clinic input] |
| 2641 | os.fchdir |
| 2642 | |
| 2643 | fd: fildes |
| 2644 | |
| 2645 | Change to the directory of the given file descriptor. |
| 2646 | |
| 2647 | fd must be opened on a directory, not a file. |
| 2648 | Equivalent to os.chdir(fd). |
| 2649 | |
| 2650 | [clinic start generated code]*/ |
| 2651 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2652 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2653 | os_fchdir_impl(PyObject *module, int fd) |
| 2654 | /*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2655 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2656 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2657 | } |
| 2658 | #endif /* HAVE_FCHDIR */ |
| 2659 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2660 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2661 | /*[clinic input] |
| 2662 | os.chmod |
| 2663 | |
| 2664 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
| 2665 | Path to be modified. May always be specified as a str or bytes. |
| 2666 | On some platforms, path may also be specified as an open file descriptor. |
| 2667 | If this functionality is unavailable, using it raises an exception. |
| 2668 | |
| 2669 | mode: int |
| 2670 | Operating-system mode bitfield. |
| 2671 | |
| 2672 | * |
| 2673 | |
| 2674 | dir_fd : dir_fd(requires='fchmodat') = None |
| 2675 | If not None, it should be a file descriptor open to a directory, |
| 2676 | and path should be relative; path will then be relative to that |
| 2677 | directory. |
| 2678 | |
| 2679 | follow_symlinks: bool = True |
| 2680 | If False, and the last element of the path is a symbolic link, |
| 2681 | chmod will modify the symbolic link itself instead of the file |
| 2682 | the link points to. |
| 2683 | |
| 2684 | Change the access permissions of a file. |
| 2685 | |
| 2686 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 2687 | an open file descriptor. |
| 2688 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 2689 | If they are unavailable, using them will raise a NotImplementedError. |
| 2690 | |
| 2691 | [clinic start generated code]*/ |
| 2692 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2693 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2694 | 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] | 2695 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2696 | /*[clinic end generated code: output=5cf6a94915cc7bff input=7f1618e5e15cc196]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2697 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2698 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2699 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2700 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2701 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2702 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2703 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2704 | #ifdef HAVE_FCHMODAT |
| 2705 | int fchmodat_nofollow_unsupported = 0; |
| 2706 | #endif |
| 2707 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2708 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2709 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2710 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2711 | #endif |
| 2712 | |
| 2713 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2714 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2715 | attr = GetFileAttributesW(path->wide); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 2716 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2717 | result = 0; |
| 2718 | else { |
| 2719 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2720 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2721 | else |
| 2722 | attr |= FILE_ATTRIBUTE_READONLY; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2723 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2724 | } |
| 2725 | Py_END_ALLOW_THREADS |
| 2726 | |
| 2727 | if (!result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2728 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2729 | } |
| 2730 | #else /* MS_WINDOWS */ |
| 2731 | Py_BEGIN_ALLOW_THREADS |
| 2732 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2733 | if (path->fd != -1) |
| 2734 | result = fchmod(path->fd, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2735 | else |
| 2736 | #endif |
| 2737 | #ifdef HAVE_LCHMOD |
| 2738 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2739 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2740 | else |
| 2741 | #endif |
| 2742 | #ifdef HAVE_FCHMODAT |
| 2743 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2744 | /* |
| 2745 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2746 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2747 | * and then says it isn't implemented yet. |
| 2748 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2749 | * |
| 2750 | * Once it is supported, os.chmod will automatically |
| 2751 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2752 | * Until then, we need to be careful what exception we raise. |
| 2753 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2754 | result = fchmodat(dir_fd, path->narrow, mode, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2755 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2756 | /* |
| 2757 | * But wait! We can't throw the exception without allowing threads, |
| 2758 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2759 | */ |
| 2760 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2761 | result && |
| 2762 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2763 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2764 | } |
| 2765 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2766 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2767 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2768 | Py_END_ALLOW_THREADS |
| 2769 | |
| 2770 | if (result) { |
| 2771 | #ifdef HAVE_FCHMODAT |
| 2772 | if (fchmodat_nofollow_unsupported) { |
| 2773 | if (dir_fd != DEFAULT_DIR_FD) |
| 2774 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 2775 | dir_fd, follow_symlinks); |
| 2776 | else |
| 2777 | follow_symlinks_specified("chmod", follow_symlinks); |
| 2778 | } |
| 2779 | else |
| 2780 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2781 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2782 | } |
| 2783 | #endif |
| 2784 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2785 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2788 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2789 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2790 | /*[clinic input] |
| 2791 | os.fchmod |
| 2792 | |
| 2793 | fd: int |
| 2794 | mode: int |
| 2795 | |
| 2796 | Change the access permissions of the file given by file descriptor fd. |
| 2797 | |
| 2798 | Equivalent to os.chmod(fd, mode). |
| 2799 | [clinic start generated code]*/ |
| 2800 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2801 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2802 | os_fchmod_impl(PyObject *module, int fd, int mode) |
| 2803 | /*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2804 | { |
| 2805 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2806 | int async_err = 0; |
| 2807 | |
| 2808 | do { |
| 2809 | Py_BEGIN_ALLOW_THREADS |
| 2810 | res = fchmod(fd, mode); |
| 2811 | Py_END_ALLOW_THREADS |
| 2812 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 2813 | if (res != 0) |
| 2814 | return (!async_err) ? posix_error() : NULL; |
| 2815 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2816 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2817 | } |
| 2818 | #endif /* HAVE_FCHMOD */ |
| 2819 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2820 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2821 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2822 | /*[clinic input] |
| 2823 | os.lchmod |
| 2824 | |
| 2825 | path: path_t |
| 2826 | mode: int |
| 2827 | |
| 2828 | Change the access permissions of a file, without following symbolic links. |
| 2829 | |
| 2830 | If path is a symlink, this affects the link itself rather than the target. |
| 2831 | Equivalent to chmod(path, mode, follow_symlinks=False)." |
| 2832 | [clinic start generated code]*/ |
| 2833 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2834 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2835 | os_lchmod_impl(PyObject *module, path_t *path, int mode) |
| 2836 | /*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2837 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2838 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2839 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 2840 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2841 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2842 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2843 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2844 | return NULL; |
| 2845 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2846 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2847 | } |
| 2848 | #endif /* HAVE_LCHMOD */ |
| 2849 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2850 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2851 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2852 | /*[clinic input] |
| 2853 | os.chflags |
| 2854 | |
| 2855 | path: path_t |
| 2856 | flags: unsigned_long(bitwise=True) |
| 2857 | follow_symlinks: bool=True |
| 2858 | |
| 2859 | Set file flags. |
| 2860 | |
| 2861 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 2862 | link, chflags will change flags on the symbolic link itself instead of the |
| 2863 | file the link points to. |
| 2864 | follow_symlinks may not be implemented on your platform. If it is |
| 2865 | unavailable, using it will raise a NotImplementedError. |
| 2866 | |
| 2867 | [clinic start generated code]*/ |
| 2868 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2869 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2870 | os_chflags_impl(PyObject *module, path_t *path, unsigned long flags, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2871 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2872 | /*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2873 | { |
| 2874 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2875 | |
| 2876 | #ifndef HAVE_LCHFLAGS |
| 2877 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2878 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2879 | #endif |
| 2880 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2881 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2882 | #ifdef HAVE_LCHFLAGS |
| 2883 | if (!follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2884 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2885 | else |
| 2886 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2887 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2888 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2889 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2890 | if (result) |
| 2891 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2892 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2893 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2894 | } |
| 2895 | #endif /* HAVE_CHFLAGS */ |
| 2896 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2897 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2898 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2899 | /*[clinic input] |
| 2900 | os.lchflags |
| 2901 | |
| 2902 | path: path_t |
| 2903 | flags: unsigned_long(bitwise=True) |
| 2904 | |
| 2905 | Set file flags. |
| 2906 | |
| 2907 | This function will not follow symbolic links. |
| 2908 | Equivalent to chflags(path, flags, follow_symlinks=False). |
| 2909 | [clinic start generated code]*/ |
| 2910 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2911 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2912 | os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags) |
| 2913 | /*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2914 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2915 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2916 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 2917 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2918 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2919 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2920 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2921 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2922 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2923 | } |
| 2924 | #endif /* HAVE_LCHFLAGS */ |
| 2925 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2926 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2927 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2928 | /*[clinic input] |
| 2929 | os.chroot |
| 2930 | path: path_t |
| 2931 | |
| 2932 | Change root directory to path. |
| 2933 | |
| 2934 | [clinic start generated code]*/ |
| 2935 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2936 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2937 | os_chroot_impl(PyObject *module, path_t *path) |
| 2938 | /*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2939 | { |
| 2940 | int res; |
| 2941 | Py_BEGIN_ALLOW_THREADS |
| 2942 | res = chroot(path->narrow); |
| 2943 | Py_END_ALLOW_THREADS |
| 2944 | if (res < 0) |
| 2945 | return path_error(path); |
| 2946 | Py_RETURN_NONE; |
| 2947 | } |
| 2948 | #endif /* HAVE_CHROOT */ |
| 2949 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2950 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2951 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2952 | /*[clinic input] |
| 2953 | os.fsync |
| 2954 | |
| 2955 | fd: fildes |
| 2956 | |
| 2957 | Force write of fd to disk. |
| 2958 | [clinic start generated code]*/ |
| 2959 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2960 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2961 | os_fsync_impl(PyObject *module, int fd) |
| 2962 | /*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2963 | { |
| 2964 | return posix_fildes_fd(fd, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2965 | } |
| 2966 | #endif /* HAVE_FSYNC */ |
| 2967 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2968 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2969 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2970 | /*[clinic input] |
| 2971 | os.sync |
| 2972 | |
| 2973 | Force write of everything to disk. |
| 2974 | [clinic start generated code]*/ |
| 2975 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2976 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2977 | os_sync_impl(PyObject *module) |
| 2978 | /*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2979 | { |
| 2980 | Py_BEGIN_ALLOW_THREADS |
| 2981 | sync(); |
| 2982 | Py_END_ALLOW_THREADS |
| 2983 | Py_RETURN_NONE; |
| 2984 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2985 | #endif /* HAVE_SYNC */ |
| 2986 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2987 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2988 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2989 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2990 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2991 | #endif |
| 2992 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2993 | /*[clinic input] |
| 2994 | os.fdatasync |
| 2995 | |
| 2996 | fd: fildes |
| 2997 | |
| 2998 | Force write of fd to disk without forcing update of metadata. |
| 2999 | [clinic start generated code]*/ |
| 3000 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3001 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3002 | os_fdatasync_impl(PyObject *module, int fd) |
| 3003 | /*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3004 | { |
| 3005 | return posix_fildes_fd(fd, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3006 | } |
| 3007 | #endif /* HAVE_FDATASYNC */ |
| 3008 | |
| 3009 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3010 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3011 | /*[clinic input] |
| 3012 | os.chown |
| 3013 | |
| 3014 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') |
| 3015 | Path to be examined; can be string, bytes, or open-file-descriptor int. |
| 3016 | |
| 3017 | uid: uid_t |
| 3018 | |
| 3019 | gid: gid_t |
| 3020 | |
| 3021 | * |
| 3022 | |
| 3023 | dir_fd : dir_fd(requires='fchownat') = None |
| 3024 | If not None, it should be a file descriptor open to a directory, |
| 3025 | and path should be relative; path will then be relative to that |
| 3026 | directory. |
| 3027 | |
| 3028 | follow_symlinks: bool = True |
| 3029 | If False, and the last element of the path is a symbolic link, |
| 3030 | stat will examine the symbolic link itself instead of the file |
| 3031 | the link points to. |
| 3032 | |
| 3033 | Change the owner and group id of path to the numeric uid and gid.\ |
| 3034 | |
| 3035 | path may always be specified as a string. |
| 3036 | On some platforms, path may also be specified as an open file descriptor. |
| 3037 | If this functionality is unavailable, using it raises an exception. |
| 3038 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3039 | and path should be relative; path will then be relative to that directory. |
| 3040 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3041 | link, chown will modify the symbolic link itself instead of the file the |
| 3042 | link points to. |
| 3043 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3044 | an open file descriptor. |
| 3045 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3046 | If they are unavailable, using them will raise a NotImplementedError. |
| 3047 | |
| 3048 | [clinic start generated code]*/ |
| 3049 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3050 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3051 | 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] | 3052 | int dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3053 | /*[clinic end generated code: output=4beadab0db5f70cd input=a61cc35574814d5d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3054 | { |
| 3055 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3056 | |
| 3057 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3058 | if (follow_symlinks_specified("chown", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3059 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3060 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3061 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
| 3062 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) |
| 3063 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3064 | |
| 3065 | #ifdef __APPLE__ |
| 3066 | /* |
| 3067 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3068 | * (But we still have an lchown symbol because of weak-linking.) |
| 3069 | * It doesn't have fchownat either. So there's no possibility |
| 3070 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3071 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3072 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3073 | follow_symlinks_specified("chown", follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3074 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3075 | } |
| 3076 | #endif |
| 3077 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3078 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3079 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3080 | if (path->fd != -1) |
| 3081 | result = fchown(path->fd, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3082 | else |
| 3083 | #endif |
| 3084 | #ifdef HAVE_LCHOWN |
| 3085 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3086 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3087 | else |
| 3088 | #endif |
| 3089 | #ifdef HAVE_FCHOWNAT |
| 3090 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3091 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3092 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3093 | else |
| 3094 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3095 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3096 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3097 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3098 | if (result) |
| 3099 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3100 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3101 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3102 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3103 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3104 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3105 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3106 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3107 | /*[clinic input] |
| 3108 | os.fchown |
| 3109 | |
| 3110 | fd: int |
| 3111 | uid: uid_t |
| 3112 | gid: gid_t |
| 3113 | |
| 3114 | Change the owner and group id of the file specified by file descriptor. |
| 3115 | |
| 3116 | Equivalent to os.chown(fd, uid, gid). |
| 3117 | |
| 3118 | [clinic start generated code]*/ |
| 3119 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3120 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3121 | os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid) |
| 3122 | /*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3123 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3124 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3125 | int async_err = 0; |
| 3126 | |
| 3127 | do { |
| 3128 | Py_BEGIN_ALLOW_THREADS |
| 3129 | res = fchown(fd, uid, gid); |
| 3130 | Py_END_ALLOW_THREADS |
| 3131 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3132 | if (res != 0) |
| 3133 | return (!async_err) ? posix_error() : NULL; |
| 3134 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3135 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3136 | } |
| 3137 | #endif /* HAVE_FCHOWN */ |
| 3138 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3139 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3140 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3141 | /*[clinic input] |
| 3142 | os.lchown |
| 3143 | |
| 3144 | path : path_t |
| 3145 | uid: uid_t |
| 3146 | gid: gid_t |
| 3147 | |
| 3148 | Change the owner and group id of path to the numeric uid and gid. |
| 3149 | |
| 3150 | This function will not follow symbolic links. |
| 3151 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). |
| 3152 | [clinic start generated code]*/ |
| 3153 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3154 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3155 | os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid) |
| 3156 | /*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3157 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3158 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3159 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3160 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3161 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3162 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3163 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3164 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3165 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3166 | } |
| 3167 | #endif /* HAVE_LCHOWN */ |
| 3168 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3169 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3170 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3171 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3172 | { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3173 | char *buf, *tmpbuf; |
| 3174 | char *cwd; |
| 3175 | const size_t chunk = 1024; |
| 3176 | size_t buflen = 0; |
| 3177 | PyObject *obj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3178 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3179 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3180 | if (!use_bytes) { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3181 | wchar_t wbuf[MAXPATHLEN]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3182 | wchar_t *wbuf2 = wbuf; |
| 3183 | PyObject *resobj; |
| 3184 | DWORD len; |
| 3185 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3186 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3187 | /* If the buffer is large enough, len does not include the |
| 3188 | terminating \0. If the buffer is too small, len includes |
| 3189 | the space needed for the terminator. */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3190 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3191 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3192 | if (wbuf2) |
| 3193 | len = GetCurrentDirectoryW(len, wbuf2); |
| 3194 | } |
| 3195 | Py_END_ALLOW_THREADS |
| 3196 | if (!wbuf2) { |
| 3197 | PyErr_NoMemory(); |
| 3198 | return NULL; |
| 3199 | } |
| 3200 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3201 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3202 | PyMem_RawFree(wbuf2); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3203 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3204 | } |
| 3205 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3206 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3207 | PyMem_RawFree(wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3208 | return resobj; |
| 3209 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3210 | |
| 3211 | if (win32_warn_bytes_api()) |
| 3212 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3213 | #endif |
| 3214 | |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3215 | buf = cwd = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3216 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3217 | do { |
| 3218 | buflen += chunk; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3219 | #ifdef MS_WINDOWS |
| 3220 | if (buflen > INT_MAX) { |
| 3221 | PyErr_NoMemory(); |
| 3222 | break; |
| 3223 | } |
| 3224 | #endif |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3225 | tmpbuf = PyMem_RawRealloc(buf, buflen); |
| 3226 | if (tmpbuf == NULL) |
| 3227 | break; |
| 3228 | |
| 3229 | buf = tmpbuf; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3230 | #ifdef MS_WINDOWS |
| 3231 | cwd = getcwd(buf, (int)buflen); |
| 3232 | #else |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3233 | cwd = getcwd(buf, buflen); |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3234 | #endif |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3235 | } while (cwd == NULL && errno == ERANGE); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3236 | Py_END_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3237 | |
| 3238 | if (cwd == NULL) { |
| 3239 | PyMem_RawFree(buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3240 | return posix_error(); |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3241 | } |
| 3242 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3243 | if (use_bytes) |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3244 | obj = PyBytes_FromStringAndSize(buf, strlen(buf)); |
| 3245 | else |
| 3246 | obj = PyUnicode_DecodeFSDefault(buf); |
| 3247 | PyMem_RawFree(buf); |
| 3248 | |
| 3249 | return obj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3250 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3251 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3252 | |
| 3253 | /*[clinic input] |
| 3254 | os.getcwd |
| 3255 | |
| 3256 | Return a unicode string representing the current working directory. |
| 3257 | [clinic start generated code]*/ |
| 3258 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3259 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3260 | os_getcwd_impl(PyObject *module) |
| 3261 | /*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3262 | { |
| 3263 | return posix_getcwd(0); |
| 3264 | } |
| 3265 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3266 | |
| 3267 | /*[clinic input] |
| 3268 | os.getcwdb |
| 3269 | |
| 3270 | Return a bytes string representing the current working directory. |
| 3271 | [clinic start generated code]*/ |
| 3272 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3273 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3274 | os_getcwdb_impl(PyObject *module) |
| 3275 | /*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3276 | { |
| 3277 | return posix_getcwd(1); |
| 3278 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3279 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3280 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3281 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3282 | #define HAVE_LINK 1 |
| 3283 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3284 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3285 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3286 | /*[clinic input] |
| 3287 | |
| 3288 | os.link |
| 3289 | |
| 3290 | src : path_t |
| 3291 | dst : path_t |
| 3292 | * |
| 3293 | src_dir_fd : dir_fd = None |
| 3294 | dst_dir_fd : dir_fd = None |
| 3295 | follow_symlinks: bool = True |
| 3296 | |
| 3297 | Create a hard link to a file. |
| 3298 | |
| 3299 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 3300 | descriptor open to a directory, and the respective path string (src or dst) |
| 3301 | should be relative; the path will then be relative to that directory. |
| 3302 | If follow_symlinks is False, and the last element of src is a symbolic |
| 3303 | link, link will create a link to the symbolic link itself instead of the |
| 3304 | file the link points to. |
| 3305 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 3306 | platform. If they are unavailable, using them will raise a |
| 3307 | NotImplementedError. |
| 3308 | [clinic start generated code]*/ |
| 3309 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3310 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3311 | 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] | 3312 | int dst_dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3313 | /*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3314 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3315 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3316 | BOOL result = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3317 | #else |
| 3318 | int result; |
| 3319 | #endif |
| 3320 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3321 | #ifndef HAVE_LINKAT |
| 3322 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3323 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3324 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3325 | } |
| 3326 | #endif |
| 3327 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3328 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3329 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3330 | PyErr_SetString(PyExc_NotImplementedError, |
| 3331 | "link: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3332 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3333 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3334 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3335 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3336 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3337 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3338 | if (src->wide) |
| 3339 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3340 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3341 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3342 | if (!result) |
| 3343 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3344 | #else |
| 3345 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3346 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3347 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3348 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3349 | (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3350 | result = linkat(src_dir_fd, src->narrow, |
| 3351 | dst_dir_fd, dst->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3352 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3353 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3354 | #endif /* HAVE_LINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3355 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3356 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3357 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3358 | if (result) |
| 3359 | return path_error2(src, dst); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3360 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3361 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3362 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3363 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3364 | #endif |
| 3365 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3366 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3367 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3368 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3369 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3370 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3371 | PyObject *v; |
| 3372 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3373 | BOOL result; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3374 | wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3375 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3376 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3377 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3378 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3379 | WIN32_FIND_DATAW wFileData; |
| 3380 | const wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3381 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3382 | if (!path->wide) { /* Default arg: "." */ |
| 3383 | po_wchars = L"."; |
| 3384 | len = 1; |
| 3385 | } else { |
| 3386 | po_wchars = path->wide; |
| 3387 | len = wcslen(path->wide); |
| 3388 | } |
| 3389 | /* The +5 is so we can append "\\*.*\0" */ |
| 3390 | wnamebuf = PyMem_New(wchar_t, len + 5); |
| 3391 | if (!wnamebuf) { |
| 3392 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3393 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3394 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3395 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3396 | if (len > 0) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3397 | wchar_t wch = wnamebuf[len-1]; |
| 3398 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3399 | wnamebuf[len++] = SEP; |
| 3400 | wcscpy(wnamebuf + len, L"*.*"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3401 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3402 | if ((list = PyList_New(0)) == NULL) { |
| 3403 | goto exit; |
| 3404 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3405 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3406 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3407 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3408 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3409 | int error = GetLastError(); |
| 3410 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3411 | goto exit; |
| 3412 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3413 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3414 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3415 | } |
| 3416 | do { |
| 3417 | /* Skip over . and .. */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3418 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3419 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 3420 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3421 | wcslen(wFileData.cFileName)); |
| 3422 | if (path->narrow && v) { |
| 3423 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 3424 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3425 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3426 | Py_DECREF(list); |
| 3427 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3428 | break; |
| 3429 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3430 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3431 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3432 | Py_DECREF(list); |
| 3433 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3434 | break; |
| 3435 | } |
| 3436 | Py_DECREF(v); |
| 3437 | } |
| 3438 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3439 | result = FindNextFileW(hFindFile, &wFileData); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3440 | Py_END_ALLOW_THREADS |
| 3441 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3442 | it got to the end of the directory. */ |
| 3443 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3444 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3445 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3446 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3447 | } |
| 3448 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3449 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3450 | exit: |
| 3451 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3452 | if (FindClose(hFindFile) == FALSE) { |
| 3453 | if (list != NULL) { |
| 3454 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3455 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3456 | } |
| 3457 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3458 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3459 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3460 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3461 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3462 | } /* end of _listdir_windows_no_opendir */ |
| 3463 | |
| 3464 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3465 | |
| 3466 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3467 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3468 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3469 | PyObject *v; |
| 3470 | DIR *dirp = NULL; |
| 3471 | struct dirent *ep; |
| 3472 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3473 | #ifdef HAVE_FDOPENDIR |
| 3474 | int fd = -1; |
| 3475 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3476 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3477 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3478 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3479 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3480 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3481 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3482 | if (fd == -1) |
| 3483 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3484 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3485 | return_str = 1; |
| 3486 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3487 | Py_BEGIN_ALLOW_THREADS |
| 3488 | dirp = fdopendir(fd); |
| 3489 | Py_END_ALLOW_THREADS |
| 3490 | } |
| 3491 | else |
| 3492 | #endif |
| 3493 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3494 | const char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3495 | if (path->narrow) { |
| 3496 | name = path->narrow; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3497 | /* only return bytes if they specified a bytes object */ |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3498 | return_str = !(PyBytes_Check(path->object)); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3499 | } |
| 3500 | else { |
| 3501 | name = "."; |
| 3502 | return_str = 1; |
| 3503 | } |
| 3504 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3505 | Py_BEGIN_ALLOW_THREADS |
| 3506 | dirp = opendir(name); |
| 3507 | Py_END_ALLOW_THREADS |
| 3508 | } |
| 3509 | |
| 3510 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3511 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3512 | #ifdef HAVE_FDOPENDIR |
| 3513 | if (fd != -1) { |
| 3514 | Py_BEGIN_ALLOW_THREADS |
| 3515 | close(fd); |
| 3516 | Py_END_ALLOW_THREADS |
| 3517 | } |
| 3518 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3519 | goto exit; |
| 3520 | } |
| 3521 | if ((list = PyList_New(0)) == NULL) { |
| 3522 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3523 | } |
| 3524 | for (;;) { |
| 3525 | errno = 0; |
| 3526 | Py_BEGIN_ALLOW_THREADS |
| 3527 | ep = readdir(dirp); |
| 3528 | Py_END_ALLOW_THREADS |
| 3529 | if (ep == NULL) { |
| 3530 | if (errno == 0) { |
| 3531 | break; |
| 3532 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3533 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3534 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3535 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3536 | } |
| 3537 | } |
| 3538 | if (ep->d_name[0] == '.' && |
| 3539 | (NAMLEN(ep) == 1 || |
| 3540 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3541 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3542 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3543 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3544 | else |
| 3545 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3546 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3547 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3548 | break; |
| 3549 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3550 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3551 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3552 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3553 | break; |
| 3554 | } |
| 3555 | Py_DECREF(v); |
| 3556 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3557 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3558 | exit: |
| 3559 | if (dirp != NULL) { |
| 3560 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3561 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3562 | if (fd > -1) |
| 3563 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3564 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3565 | closedir(dirp); |
| 3566 | Py_END_ALLOW_THREADS |
| 3567 | } |
| 3568 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3569 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3570 | } /* end of _posix_listdir */ |
| 3571 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3572 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3573 | |
| 3574 | /*[clinic input] |
| 3575 | os.listdir |
| 3576 | |
| 3577 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
| 3578 | |
| 3579 | Return a list containing the names of the files in the directory. |
| 3580 | |
| 3581 | path can be specified as either str or bytes. If path is bytes, |
| 3582 | the filenames returned will also be bytes; in all other circumstances |
| 3583 | the filenames returned will be str. |
| 3584 | If path is None, uses the path='.'. |
| 3585 | On some platforms, path may also be specified as an open file descriptor;\ |
| 3586 | the file descriptor must refer to a directory. |
| 3587 | If this functionality is unavailable, using it raises NotImplementedError. |
| 3588 | |
| 3589 | The list is in arbitrary order. It does not include the special |
| 3590 | entries '.' and '..' even if they are present in the directory. |
| 3591 | |
| 3592 | |
| 3593 | [clinic start generated code]*/ |
| 3594 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3595 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3596 | os_listdir_impl(PyObject *module, path_t *path) |
| 3597 | /*[clinic end generated code: output=293045673fcd1a75 input=09e300416e3cd729]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3598 | { |
| 3599 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3600 | return _listdir_windows_no_opendir(path, NULL); |
| 3601 | #else |
| 3602 | return _posix_listdir(path, NULL); |
| 3603 | #endif |
| 3604 | } |
| 3605 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3606 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3607 | /* A helper function for abspath on win32 */ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3608 | /*[clinic input] |
| 3609 | os._getfullpathname |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3610 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3611 | path: path_t |
| 3612 | / |
| 3613 | |
| 3614 | [clinic start generated code]*/ |
| 3615 | |
| 3616 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3617 | os__getfullpathname_impl(PyObject *module, path_t *path) |
| 3618 | /*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3619 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3620 | wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf; |
| 3621 | wchar_t *wtemp; |
| 3622 | DWORD result; |
| 3623 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3624 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3625 | result = GetFullPathNameW(path->wide, |
| 3626 | Py_ARRAY_LENGTH(woutbuf), |
| 3627 | woutbuf, &wtemp); |
| 3628 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
| 3629 | woutbufp = PyMem_New(wchar_t, result); |
| 3630 | if (!woutbufp) |
| 3631 | return PyErr_NoMemory(); |
| 3632 | result = GetFullPathNameW(path->wide, result, woutbufp, &wtemp); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3633 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3634 | if (result) { |
| 3635 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
| 3636 | if (path->narrow) |
| 3637 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 3638 | } else |
| 3639 | v = win32_error_object("GetFullPathNameW", path->object); |
| 3640 | if (woutbufp != woutbuf) |
| 3641 | PyMem_Free(woutbufp); |
| 3642 | return v; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3643 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3644 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3645 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3646 | /*[clinic input] |
| 3647 | os._getfinalpathname |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3648 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3649 | path: unicode |
| 3650 | / |
| 3651 | |
| 3652 | A helper function for samepath on windows. |
| 3653 | [clinic start generated code]*/ |
| 3654 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3655 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3656 | os__getfinalpathname_impl(PyObject *module, PyObject *path) |
| 3657 | /*[clinic end generated code: output=9bd78d0e52782e75 input=71d5e89334891bf4]*/ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3658 | { |
| 3659 | HANDLE hFile; |
| 3660 | int buf_size; |
| 3661 | wchar_t *target_path; |
| 3662 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3663 | PyObject *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3664 | const wchar_t *path_wchar; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3665 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3666 | path_wchar = PyUnicode_AsUnicode(path); |
| 3667 | if (path_wchar == NULL) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3668 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3669 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3670 | hFile = CreateFileW( |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3671 | path_wchar, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3672 | 0, /* desired access */ |
| 3673 | 0, /* share mode */ |
| 3674 | NULL, /* security attributes */ |
| 3675 | OPEN_EXISTING, |
| 3676 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3677 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3678 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3679 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3680 | if(hFile == INVALID_HANDLE_VALUE) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3681 | return win32_error_object("CreateFileW", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3682 | |
| 3683 | /* We have a good handle to the target, use it to determine the |
| 3684 | target path name. */ |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 3685 | buf_size = GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3686 | |
| 3687 | if(!buf_size) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3688 | return win32_error_object("GetFinalPathNameByHandle", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3689 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3690 | target_path = PyMem_New(wchar_t, buf_size+1); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3691 | if(!target_path) |
| 3692 | return PyErr_NoMemory(); |
| 3693 | |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 3694 | result_length = GetFinalPathNameByHandleW(hFile, target_path, |
| 3695 | buf_size, VOLUME_NAME_DOS); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3696 | if(!result_length) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3697 | return win32_error_object("GetFinalPathNamyByHandle", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3698 | |
| 3699 | if(!CloseHandle(hFile)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3700 | return win32_error_object("CloseHandle", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3701 | |
| 3702 | target_path[result_length] = 0; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3703 | result = PyUnicode_FromWideChar(target_path, result_length); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3704 | PyMem_Free(target_path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3705 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3706 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3707 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3708 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3709 | "Return true if the pathname refers to an existing directory."); |
| 3710 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3711 | /*[clinic input] |
| 3712 | os._isdir |
| 3713 | |
| 3714 | path: path_t |
| 3715 | / |
| 3716 | |
| 3717 | [clinic start generated code]*/ |
| 3718 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3719 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3720 | os__isdir_impl(PyObject *module, path_t *path) |
| 3721 | /*[clinic end generated code: output=75f56f32720836cb input=e794f12faab62a2a]*/ |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3722 | { |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3723 | DWORD attributes; |
| 3724 | |
Steve Dower | b22a677 | 2016-07-17 20:49:38 -0700 | [diff] [blame] | 3725 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3726 | attributes = GetFileAttributesW(path->wide); |
Steve Dower | b22a677 | 2016-07-17 20:49:38 -0700 | [diff] [blame] | 3727 | Py_END_ALLOW_THREADS |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3728 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3729 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3730 | Py_RETURN_FALSE; |
| 3731 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3732 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3733 | Py_RETURN_TRUE; |
| 3734 | else |
| 3735 | Py_RETURN_FALSE; |
| 3736 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3737 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3738 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3739 | /*[clinic input] |
| 3740 | os._getvolumepathname |
| 3741 | |
| 3742 | path: unicode |
| 3743 | |
| 3744 | A helper function for ismount on Win32. |
| 3745 | [clinic start generated code]*/ |
| 3746 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3747 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3748 | os__getvolumepathname_impl(PyObject *module, PyObject *path) |
| 3749 | /*[clinic end generated code: output=cbdcbd1059ceef4c input=7eacadc40acbda6b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3750 | { |
| 3751 | PyObject *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3752 | const wchar_t *path_wchar; |
| 3753 | wchar_t *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3754 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3755 | BOOL ret; |
| 3756 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3757 | path_wchar = PyUnicode_AsUnicodeAndSize(path, &buflen); |
| 3758 | if (path_wchar == NULL) |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3759 | return NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3760 | buflen += 1; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3761 | |
| 3762 | /* Volume path should be shorter than entire path */ |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3763 | buflen = Py_MAX(buflen, MAX_PATH); |
| 3764 | |
| 3765 | if (buflen > DWORD_MAX) { |
| 3766 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 3767 | return NULL; |
| 3768 | } |
| 3769 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3770 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3771 | if (mountpath == NULL) |
| 3772 | return PyErr_NoMemory(); |
| 3773 | |
| 3774 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3775 | ret = GetVolumePathNameW(path_wchar, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3776 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3777 | Py_END_ALLOW_THREADS |
| 3778 | |
| 3779 | if (!ret) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3780 | result = win32_error_object("_getvolumepathname", path); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3781 | goto exit; |
| 3782 | } |
| 3783 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
| 3784 | |
| 3785 | exit: |
| 3786 | PyMem_Free(mountpath); |
| 3787 | return result; |
| 3788 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3789 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3790 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3791 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3792 | |
| 3793 | /*[clinic input] |
| 3794 | os.mkdir |
| 3795 | |
| 3796 | path : path_t |
| 3797 | |
| 3798 | mode: int = 0o777 |
| 3799 | |
| 3800 | * |
| 3801 | |
| 3802 | dir_fd : dir_fd(requires='mkdirat') = None |
| 3803 | |
| 3804 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 3805 | |
| 3806 | Create a directory. |
| 3807 | |
| 3808 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3809 | and path should be relative; path will then be relative to that directory. |
| 3810 | dir_fd may not be implemented on your platform. |
| 3811 | If it is unavailable, using it will raise a NotImplementedError. |
| 3812 | |
| 3813 | The mode argument is ignored on Windows. |
| 3814 | [clinic start generated code]*/ |
| 3815 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3816 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3817 | os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 3818 | /*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3819 | { |
| 3820 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3821 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3822 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3823 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3824 | result = CreateDirectoryW(path->wide, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3825 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3826 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3827 | if (!result) |
| 3828 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3829 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3830 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3831 | #if HAVE_MKDIRAT |
| 3832 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3833 | result = mkdirat(dir_fd, path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3834 | else |
| 3835 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3836 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3837 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3838 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3839 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3840 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3841 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3842 | if (result < 0) |
| 3843 | return path_error(path); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3844 | #endif /* MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3845 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3846 | } |
| 3847 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3848 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3849 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3850 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3851 | #include <sys/resource.h> |
| 3852 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3853 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3854 | |
| 3855 | #ifdef HAVE_NICE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3856 | /*[clinic input] |
| 3857 | os.nice |
| 3858 | |
| 3859 | increment: int |
| 3860 | / |
| 3861 | |
| 3862 | Add increment to the priority of process and return the new priority. |
| 3863 | [clinic start generated code]*/ |
| 3864 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3865 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3866 | os_nice_impl(PyObject *module, int increment) |
| 3867 | /*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3868 | { |
| 3869 | int value; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3870 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3871 | /* There are two flavours of 'nice': one that returns the new |
| 3872 | priority (as required by almost all standards out there) and the |
| 3873 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 3874 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3875 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3876 | If we are of the nice family that returns the new priority, we |
| 3877 | need to clear errno before the call, and check if errno is filled |
| 3878 | before calling posix_error() on a returnvalue of -1, because the |
| 3879 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3880 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3881 | errno = 0; |
| 3882 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3883 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3884 | if (value == 0) |
| 3885 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3886 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3887 | if (value == -1 && errno != 0) |
| 3888 | /* either nice() or getpriority() returned an error */ |
| 3889 | return posix_error(); |
| 3890 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3891 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3892 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3893 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3894 | |
| 3895 | #ifdef HAVE_GETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3896 | /*[clinic input] |
| 3897 | os.getpriority |
| 3898 | |
| 3899 | which: int |
| 3900 | who: int |
| 3901 | |
| 3902 | Return program scheduling priority. |
| 3903 | [clinic start generated code]*/ |
| 3904 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3905 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3906 | os_getpriority_impl(PyObject *module, int which, int who) |
| 3907 | /*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3908 | { |
| 3909 | int retval; |
| 3910 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3911 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3912 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3913 | if (errno != 0) |
| 3914 | return posix_error(); |
| 3915 | return PyLong_FromLong((long)retval); |
| 3916 | } |
| 3917 | #endif /* HAVE_GETPRIORITY */ |
| 3918 | |
| 3919 | |
| 3920 | #ifdef HAVE_SETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3921 | /*[clinic input] |
| 3922 | os.setpriority |
| 3923 | |
| 3924 | which: int |
| 3925 | who: int |
| 3926 | priority: int |
| 3927 | |
| 3928 | Set program scheduling priority. |
| 3929 | [clinic start generated code]*/ |
| 3930 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3931 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3932 | os_setpriority_impl(PyObject *module, int which, int who, int priority) |
| 3933 | /*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3934 | { |
| 3935 | int retval; |
| 3936 | |
| 3937 | retval = setpriority(which, who, priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3938 | if (retval == -1) |
| 3939 | return posix_error(); |
| 3940 | Py_RETURN_NONE; |
| 3941 | } |
| 3942 | #endif /* HAVE_SETPRIORITY */ |
| 3943 | |
| 3944 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3945 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3946 | 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] | 3947 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3948 | const char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3949 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3950 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3951 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3952 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3953 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3954 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3955 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3956 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3957 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3958 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 3959 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 3960 | #ifndef HAVE_RENAMEAT |
| 3961 | if (dir_fd_specified) { |
| 3962 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3963 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3964 | } |
| 3965 | #endif |
| 3966 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3967 | #ifdef MS_WINDOWS |
| 3968 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3969 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3970 | Py_END_ALLOW_THREADS |
| 3971 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3972 | if (!result) |
| 3973 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3974 | |
| 3975 | #else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3976 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 3977 | PyErr_Format(PyExc_ValueError, |
| 3978 | "%s: src and dst must be the same type", function_name); |
| 3979 | return NULL; |
| 3980 | } |
| 3981 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3982 | Py_BEGIN_ALLOW_THREADS |
| 3983 | #ifdef HAVE_RENAMEAT |
| 3984 | if (dir_fd_specified) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3985 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3986 | else |
| 3987 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3988 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3989 | Py_END_ALLOW_THREADS |
| 3990 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3991 | if (result) |
| 3992 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3993 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3994 | Py_RETURN_NONE; |
| 3995 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3996 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3997 | |
| 3998 | /*[clinic input] |
| 3999 | os.rename |
| 4000 | |
| 4001 | src : path_t |
| 4002 | dst : path_t |
| 4003 | * |
| 4004 | src_dir_fd : dir_fd = None |
| 4005 | dst_dir_fd : dir_fd = None |
| 4006 | |
| 4007 | Rename a file or directory. |
| 4008 | |
| 4009 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4010 | descriptor open to a directory, and the respective path string (src or dst) |
| 4011 | should be relative; the path will then be relative to that directory. |
| 4012 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4013 | If they are unavailable, using them will raise a NotImplementedError. |
| 4014 | [clinic start generated code]*/ |
| 4015 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4016 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4017 | 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] | 4018 | int dst_dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4019 | /*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4020 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4021 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4022 | } |
| 4023 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4024 | |
| 4025 | /*[clinic input] |
| 4026 | os.replace = os.rename |
| 4027 | |
| 4028 | Rename a file or directory, overwriting the destination. |
| 4029 | |
| 4030 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4031 | descriptor open to a directory, and the respective path string (src or dst) |
| 4032 | should be relative; the path will then be relative to that directory. |
| 4033 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4034 | If they are unavailable, using them will raise a NotImplementedError." |
| 4035 | [clinic start generated code]*/ |
| 4036 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4037 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4038 | os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd, |
| 4039 | int dst_dir_fd) |
| 4040 | /*[clinic end generated code: output=1968c02e7857422b input=25515dfb107c8421]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4041 | { |
| 4042 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); |
| 4043 | } |
| 4044 | |
| 4045 | |
| 4046 | /*[clinic input] |
| 4047 | os.rmdir |
| 4048 | |
| 4049 | path: path_t |
| 4050 | * |
| 4051 | dir_fd: dir_fd(requires='unlinkat') = None |
| 4052 | |
| 4053 | Remove a directory. |
| 4054 | |
| 4055 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4056 | and path should be relative; path will then be relative to that directory. |
| 4057 | dir_fd may not be implemented on your platform. |
| 4058 | If it is unavailable, using it will raise a NotImplementedError. |
| 4059 | [clinic start generated code]*/ |
| 4060 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4061 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4062 | os_rmdir_impl(PyObject *module, path_t *path, int dir_fd) |
| 4063 | /*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4064 | { |
| 4065 | int result; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4066 | |
| 4067 | Py_BEGIN_ALLOW_THREADS |
| 4068 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4069 | /* Windows, success=1, UNIX, success=0 */ |
| 4070 | result = !RemoveDirectoryW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4071 | #else |
| 4072 | #ifdef HAVE_UNLINKAT |
| 4073 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4074 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4075 | else |
| 4076 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4077 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4078 | #endif |
| 4079 | Py_END_ALLOW_THREADS |
| 4080 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4081 | if (result) |
| 4082 | return path_error(path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4083 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4084 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4085 | } |
| 4086 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4087 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4088 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4089 | #ifdef MS_WINDOWS |
| 4090 | /*[clinic input] |
| 4091 | os.system -> long |
| 4092 | |
| 4093 | command: Py_UNICODE |
| 4094 | |
| 4095 | Execute the command in a subshell. |
| 4096 | [clinic start generated code]*/ |
| 4097 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4098 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4099 | os_system_impl(PyObject *module, Py_UNICODE *command) |
| 4100 | /*[clinic end generated code: output=96c4dffee36dfb48 input=303f5ce97df606b0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4101 | { |
| 4102 | long result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4103 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4104 | result = _wsystem(command); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4105 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4106 | return result; |
| 4107 | } |
| 4108 | #else /* MS_WINDOWS */ |
| 4109 | /*[clinic input] |
| 4110 | os.system -> long |
| 4111 | |
| 4112 | command: FSConverter |
| 4113 | |
| 4114 | Execute the command in a subshell. |
| 4115 | [clinic start generated code]*/ |
| 4116 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4117 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4118 | os_system_impl(PyObject *module, PyObject *command) |
| 4119 | /*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4120 | { |
| 4121 | long result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4122 | const char *bytes = PyBytes_AsString(command); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4123 | Py_BEGIN_ALLOW_THREADS |
| 4124 | result = system(bytes); |
| 4125 | Py_END_ALLOW_THREADS |
| 4126 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4127 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4128 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4129 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4130 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4131 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4132 | /*[clinic input] |
| 4133 | os.umask |
| 4134 | |
| 4135 | mask: int |
| 4136 | / |
| 4137 | |
| 4138 | Set the current numeric umask and return the previous umask. |
| 4139 | [clinic start generated code]*/ |
| 4140 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4141 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4142 | os_umask_impl(PyObject *module, int mask) |
| 4143 | /*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4144 | { |
| 4145 | int i = (int)umask(mask); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4146 | if (i < 0) |
| 4147 | return posix_error(); |
| 4148 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4151 | #ifdef MS_WINDOWS |
| 4152 | |
| 4153 | /* override the default DeleteFileW behavior so that directory |
| 4154 | symlinks can be removed with this function, the same as with |
| 4155 | Unix symlinks */ |
| 4156 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4157 | { |
| 4158 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4159 | WIN32_FIND_DATAW find_data; |
| 4160 | HANDLE find_data_handle; |
| 4161 | int is_directory = 0; |
| 4162 | int is_link = 0; |
| 4163 | |
| 4164 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4165 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4166 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4167 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4168 | it is a symlink */ |
| 4169 | if(is_directory && |
| 4170 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4171 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4172 | |
| 4173 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4174 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 4175 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 4176 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 4177 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4178 | FindClose(find_data_handle); |
| 4179 | } |
| 4180 | } |
| 4181 | } |
| 4182 | |
| 4183 | if (is_directory && is_link) |
| 4184 | return RemoveDirectoryW(lpFileName); |
| 4185 | |
| 4186 | return DeleteFileW(lpFileName); |
| 4187 | } |
| 4188 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4189 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4190 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4191 | /*[clinic input] |
| 4192 | os.unlink |
| 4193 | |
| 4194 | path: path_t |
| 4195 | * |
| 4196 | dir_fd: dir_fd(requires='unlinkat')=None |
| 4197 | |
| 4198 | Remove a file (same as remove()). |
| 4199 | |
| 4200 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4201 | and path should be relative; path will then be relative to that directory. |
| 4202 | dir_fd may not be implemented on your platform. |
| 4203 | If it is unavailable, using it will raise a NotImplementedError. |
| 4204 | |
| 4205 | [clinic start generated code]*/ |
| 4206 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4207 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4208 | os_unlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 4209 | /*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4210 | { |
| 4211 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4212 | |
| 4213 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4214 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4215 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4216 | /* Windows, success=1, UNIX, success=0 */ |
| 4217 | result = !Py_DeleteFileW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4218 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4219 | #ifdef HAVE_UNLINKAT |
| 4220 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4221 | result = unlinkat(dir_fd, path->narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4222 | else |
| 4223 | #endif /* HAVE_UNLINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4224 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4225 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4226 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4227 | Py_END_ALLOW_THREADS |
| 4228 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4229 | if (result) |
| 4230 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4231 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4232 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4235 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4236 | /*[clinic input] |
| 4237 | os.remove = os.unlink |
| 4238 | |
| 4239 | Remove a file (same as unlink()). |
| 4240 | |
| 4241 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4242 | and path should be relative; path will then be relative to that directory. |
| 4243 | dir_fd may not be implemented on your platform. |
| 4244 | If it is unavailable, using it will raise a NotImplementedError. |
| 4245 | [clinic start generated code]*/ |
| 4246 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4247 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4248 | os_remove_impl(PyObject *module, path_t *path, int dir_fd) |
| 4249 | /*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4250 | { |
| 4251 | return os_unlink_impl(module, path, dir_fd); |
| 4252 | } |
| 4253 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4254 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4255 | static PyStructSequence_Field uname_result_fields[] = { |
| 4256 | {"sysname", "operating system name"}, |
| 4257 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4258 | {"release", "operating system release"}, |
| 4259 | {"version", "operating system version"}, |
| 4260 | {"machine", "hardware identifier"}, |
| 4261 | {NULL} |
| 4262 | }; |
| 4263 | |
| 4264 | PyDoc_STRVAR(uname_result__doc__, |
| 4265 | "uname_result: Result from os.uname().\n\n\ |
| 4266 | This object may be accessed either as a tuple of\n\ |
| 4267 | (sysname, nodename, release, version, machine),\n\ |
| 4268 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4269 | \n\ |
| 4270 | See os.uname for more information."); |
| 4271 | |
| 4272 | static PyStructSequence_Desc uname_result_desc = { |
| 4273 | "uname_result", /* name */ |
| 4274 | uname_result__doc__, /* doc */ |
| 4275 | uname_result_fields, |
| 4276 | 5 |
| 4277 | }; |
| 4278 | |
| 4279 | static PyTypeObject UnameResultType; |
| 4280 | |
| 4281 | |
| 4282 | #ifdef HAVE_UNAME |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4283 | /*[clinic input] |
| 4284 | os.uname |
| 4285 | |
| 4286 | Return an object identifying the current operating system. |
| 4287 | |
| 4288 | The object behaves like a named tuple with the following fields: |
| 4289 | (sysname, nodename, release, version, machine) |
| 4290 | |
| 4291 | [clinic start generated code]*/ |
| 4292 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4293 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4294 | os_uname_impl(PyObject *module) |
| 4295 | /*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4296 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4297 | struct utsname u; |
| 4298 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4299 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4300 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4301 | Py_BEGIN_ALLOW_THREADS |
| 4302 | res = uname(&u); |
| 4303 | Py_END_ALLOW_THREADS |
| 4304 | if (res < 0) |
| 4305 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4306 | |
| 4307 | value = PyStructSequence_New(&UnameResultType); |
| 4308 | if (value == NULL) |
| 4309 | return NULL; |
| 4310 | |
| 4311 | #define SET(i, field) \ |
| 4312 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4313 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4314 | if (!o) { \ |
| 4315 | Py_DECREF(value); \ |
| 4316 | return NULL; \ |
| 4317 | } \ |
| 4318 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4319 | } \ |
| 4320 | |
| 4321 | SET(0, u.sysname); |
| 4322 | SET(1, u.nodename); |
| 4323 | SET(2, u.release); |
| 4324 | SET(3, u.version); |
| 4325 | SET(4, u.machine); |
| 4326 | |
| 4327 | #undef SET |
| 4328 | |
| 4329 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4330 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4331 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4332 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4333 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4334 | |
| 4335 | typedef struct { |
| 4336 | int now; |
| 4337 | time_t atime_s; |
| 4338 | long atime_ns; |
| 4339 | time_t mtime_s; |
| 4340 | long mtime_ns; |
| 4341 | } utime_t; |
| 4342 | |
| 4343 | /* |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4344 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4345 | * they also intentionally leak the declaration of a pointer named "time" |
| 4346 | */ |
| 4347 | #define UTIME_TO_TIMESPEC \ |
| 4348 | struct timespec ts[2]; \ |
| 4349 | struct timespec *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4350 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4351 | time = NULL; \ |
| 4352 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4353 | ts[0].tv_sec = ut->atime_s; \ |
| 4354 | ts[0].tv_nsec = ut->atime_ns; \ |
| 4355 | ts[1].tv_sec = ut->mtime_s; \ |
| 4356 | ts[1].tv_nsec = ut->mtime_ns; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4357 | time = ts; \ |
| 4358 | } \ |
| 4359 | |
| 4360 | #define UTIME_TO_TIMEVAL \ |
| 4361 | struct timeval tv[2]; \ |
| 4362 | struct timeval *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4363 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4364 | time = NULL; \ |
| 4365 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4366 | tv[0].tv_sec = ut->atime_s; \ |
| 4367 | tv[0].tv_usec = ut->atime_ns / 1000; \ |
| 4368 | tv[1].tv_sec = ut->mtime_s; \ |
| 4369 | tv[1].tv_usec = ut->mtime_ns / 1000; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4370 | time = tv; \ |
| 4371 | } \ |
| 4372 | |
| 4373 | #define UTIME_TO_UTIMBUF \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4374 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4375 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4376 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4377 | time = NULL; \ |
| 4378 | else { \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4379 | u.actime = ut->atime_s; \ |
| 4380 | u.modtime = ut->mtime_s; \ |
| 4381 | time = &u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4382 | } |
| 4383 | |
| 4384 | #define UTIME_TO_TIME_T \ |
| 4385 | time_t timet[2]; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4386 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4387 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4388 | time = NULL; \ |
| 4389 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4390 | timet[0] = ut->atime_s; \ |
| 4391 | timet[1] = ut->mtime_s; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4392 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4393 | } \ |
| 4394 | |
| 4395 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4396 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4397 | |
| 4398 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4399 | 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] | 4400 | { |
| 4401 | #ifdef HAVE_UTIMENSAT |
| 4402 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4403 | UTIME_TO_TIMESPEC; |
| 4404 | return utimensat(dir_fd, path, time, flags); |
| 4405 | #elif defined(HAVE_FUTIMESAT) |
| 4406 | UTIME_TO_TIMEVAL; |
| 4407 | /* |
| 4408 | * follow_symlinks will never be false here; |
| 4409 | * we only allow !follow_symlinks and dir_fd together |
| 4410 | * if we have utimensat() |
| 4411 | */ |
| 4412 | assert(follow_symlinks); |
| 4413 | return futimesat(dir_fd, path, time); |
| 4414 | #endif |
| 4415 | } |
| 4416 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4417 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
| 4418 | #else |
| 4419 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4420 | #endif |
| 4421 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4422 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4423 | |
| 4424 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4425 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4426 | { |
| 4427 | #ifdef HAVE_FUTIMENS |
| 4428 | UTIME_TO_TIMESPEC; |
| 4429 | return futimens(fd, time); |
| 4430 | #else |
| 4431 | UTIME_TO_TIMEVAL; |
| 4432 | return futimes(fd, time); |
| 4433 | #endif |
| 4434 | } |
| 4435 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4436 | #define PATH_UTIME_HAVE_FD 1 |
| 4437 | #else |
| 4438 | #define PATH_UTIME_HAVE_FD 0 |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4439 | #endif |
| 4440 | |
Victor Stinner | 5ebae87 | 2015-09-22 01:29:33 +0200 | [diff] [blame] | 4441 | #if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES) |
| 4442 | # define UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4443 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4444 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4445 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4446 | |
| 4447 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4448 | utime_nofollow_symlinks(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4449 | { |
| 4450 | #ifdef HAVE_UTIMENSAT |
| 4451 | UTIME_TO_TIMESPEC; |
| 4452 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4453 | #else |
| 4454 | UTIME_TO_TIMEVAL; |
| 4455 | return lutimes(path, time); |
| 4456 | #endif |
| 4457 | } |
| 4458 | |
| 4459 | #endif |
| 4460 | |
| 4461 | #ifndef MS_WINDOWS |
| 4462 | |
| 4463 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4464 | utime_default(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4465 | { |
| 4466 | #ifdef HAVE_UTIMENSAT |
| 4467 | UTIME_TO_TIMESPEC; |
| 4468 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4469 | #elif defined(HAVE_UTIMES) |
| 4470 | UTIME_TO_TIMEVAL; |
| 4471 | return utimes(path, time); |
| 4472 | #elif defined(HAVE_UTIME_H) |
| 4473 | UTIME_TO_UTIMBUF; |
| 4474 | return utime(path, time); |
| 4475 | #else |
| 4476 | UTIME_TO_TIME_T; |
| 4477 | return utime(path, time); |
| 4478 | #endif |
| 4479 | } |
| 4480 | |
| 4481 | #endif |
| 4482 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4483 | static int |
| 4484 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4485 | { |
| 4486 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4487 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4488 | divmod = PyNumber_Divmod(py_long, billion); |
| 4489 | if (!divmod) |
| 4490 | goto exit; |
| 4491 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4492 | if ((*s == -1) && PyErr_Occurred()) |
| 4493 | goto exit; |
| 4494 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4495 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4496 | goto exit; |
| 4497 | |
| 4498 | result = 1; |
| 4499 | exit: |
| 4500 | Py_XDECREF(divmod); |
| 4501 | return result; |
| 4502 | } |
| 4503 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4504 | |
| 4505 | /*[clinic input] |
| 4506 | os.utime |
| 4507 | |
| 4508 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') |
| 4509 | times: object = NULL |
| 4510 | * |
| 4511 | ns: object = NULL |
| 4512 | dir_fd: dir_fd(requires='futimensat') = None |
| 4513 | follow_symlinks: bool=True |
| 4514 | |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4515 | # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4516 | |
| 4517 | Set the access and modified time of path. |
| 4518 | |
| 4519 | path may always be specified as a string. |
| 4520 | On some platforms, path may also be specified as an open file descriptor. |
| 4521 | If this functionality is unavailable, using it raises an exception. |
| 4522 | |
| 4523 | If times is not None, it must be a tuple (atime, mtime); |
| 4524 | atime and mtime should be expressed as float seconds since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4525 | 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] | 4526 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 4527 | since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4528 | 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] | 4529 | Specifying tuples for both times and ns is an error. |
| 4530 | |
| 4531 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4532 | and path should be relative; path will then be relative to that directory. |
| 4533 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 4534 | link, utime will modify the symbolic link itself instead of the file the |
| 4535 | link points to. |
| 4536 | It is an error to use dir_fd or follow_symlinks when specifying path |
| 4537 | as an open file descriptor. |
| 4538 | dir_fd and follow_symlinks may not be available on your platform. |
| 4539 | If they are unavailable, using them will raise a NotImplementedError. |
| 4540 | |
| 4541 | [clinic start generated code]*/ |
| 4542 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4543 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4544 | os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns, |
| 4545 | int dir_fd, int follow_symlinks) |
| 4546 | /*[clinic end generated code: output=cfcac69d027b82cf input=081cdc54ca685385]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4547 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4548 | #ifdef MS_WINDOWS |
| 4549 | HANDLE hFile; |
| 4550 | FILETIME atime, mtime; |
| 4551 | #else |
| 4552 | int result; |
| 4553 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4554 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4555 | PyObject *return_value = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4556 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4557 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4558 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4559 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4560 | if (times && (times != Py_None) && ns) { |
| 4561 | PyErr_SetString(PyExc_ValueError, |
| 4562 | "utime: you may specify either 'times'" |
| 4563 | " or 'ns' but not both"); |
| 4564 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4565 | } |
| 4566 | |
| 4567 | if (times && (times != Py_None)) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4568 | time_t a_sec, m_sec; |
| 4569 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4570 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4571 | PyErr_SetString(PyExc_TypeError, |
| 4572 | "utime: 'times' must be either" |
| 4573 | " a tuple of two ints or None"); |
| 4574 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4575 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4576 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4577 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4578 | &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4579 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4580 | &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4581 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4582 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4583 | utime.atime_s = a_sec; |
| 4584 | utime.atime_ns = a_nsec; |
| 4585 | utime.mtime_s = m_sec; |
| 4586 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4587 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4588 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4589 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4590 | PyErr_SetString(PyExc_TypeError, |
| 4591 | "utime: 'ns' must be a tuple of two ints"); |
| 4592 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4593 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4594 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4595 | 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] | 4596 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4597 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4598 | &utime.mtime_s, &utime.mtime_ns)) { |
| 4599 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4600 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4601 | } |
| 4602 | else { |
| 4603 | /* times and ns are both None/unspecified. use "now". */ |
| 4604 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4605 | } |
| 4606 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4607 | #if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4608 | if (follow_symlinks_specified("utime", follow_symlinks)) |
| 4609 | goto exit; |
| 4610 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4611 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4612 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
| 4613 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || |
| 4614 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4615 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4616 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4617 | #if !defined(HAVE_UTIMENSAT) |
| 4618 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4619 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4620 | "utime: cannot use dir_fd and follow_symlinks " |
| 4621 | "together on this platform"); |
| 4622 | goto exit; |
| 4623 | } |
| 4624 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4625 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4626 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4627 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4628 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
| 4629 | NULL, OPEN_EXISTING, |
| 4630 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4631 | Py_END_ALLOW_THREADS |
| 4632 | if (hFile == INVALID_HANDLE_VALUE) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4633 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4634 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4635 | } |
| 4636 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4637 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 4638 | GetSystemTimeAsFileTime(&mtime); |
| 4639 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4640 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4641 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 4642 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4643 | _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] | 4644 | } |
| 4645 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4646 | /* Avoid putting the file name into the error here, |
| 4647 | as that may confuse the user into believing that |
| 4648 | something is wrong with the file, when it also |
| 4649 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4650 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4651 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4652 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4653 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4654 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4655 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4656 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4657 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4658 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4659 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4660 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4661 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4662 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4663 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4664 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4665 | else |
| 4666 | #endif |
| 4667 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4668 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4669 | if (path->fd != -1) |
| 4670 | result = utime_fd(&utime, path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4671 | else |
| 4672 | #endif |
| 4673 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4674 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4675 | |
| 4676 | Py_END_ALLOW_THREADS |
| 4677 | |
| 4678 | if (result < 0) { |
| 4679 | /* see previous comment about not putting filename in error here */ |
| 4680 | return_value = posix_error(); |
| 4681 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4682 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4683 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4684 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4685 | |
| 4686 | Py_INCREF(Py_None); |
| 4687 | return_value = Py_None; |
| 4688 | |
| 4689 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4690 | #ifdef MS_WINDOWS |
| 4691 | if (hFile != INVALID_HANDLE_VALUE) |
| 4692 | CloseHandle(hFile); |
| 4693 | #endif |
| 4694 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4695 | } |
| 4696 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4697 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4698 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4699 | |
| 4700 | /*[clinic input] |
| 4701 | os._exit |
| 4702 | |
| 4703 | status: int |
| 4704 | |
| 4705 | Exit to the system with specified status, without normal exit processing. |
| 4706 | [clinic start generated code]*/ |
| 4707 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4708 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4709 | os__exit_impl(PyObject *module, int status) |
| 4710 | /*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4711 | { |
| 4712 | _exit(status); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4713 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4714 | } |
| 4715 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4716 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 4717 | #define EXECV_CHAR wchar_t |
| 4718 | #else |
| 4719 | #define EXECV_CHAR char |
| 4720 | #endif |
| 4721 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4722 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 4723 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4724 | free_string_array(EXECV_CHAR **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4725 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4726 | Py_ssize_t i; |
| 4727 | for (i = 0; i < count; i++) |
| 4728 | PyMem_Free(array[i]); |
| 4729 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4730 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4731 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 4732 | static |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4733 | int fsconvert_strdup(PyObject *o, EXECV_CHAR**out) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4734 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4735 | Py_ssize_t size; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4736 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 4737 | *out = PyUnicode_AsWideCharString(o, &size); |
| 4738 | if (!*out) |
| 4739 | return 0; |
| 4740 | #else |
| 4741 | PyObject *bytes; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4742 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 4743 | return 0; |
| 4744 | size = PyBytes_GET_SIZE(bytes); |
| 4745 | *out = PyMem_Malloc(size+1); |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 4746 | if (!*out) { |
| 4747 | PyErr_NoMemory(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4748 | return 0; |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 4749 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4750 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 4751 | Py_DECREF(bytes); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4752 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4753 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4754 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4755 | #endif |
| 4756 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4757 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4758 | static EXECV_CHAR** |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4759 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 4760 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4761 | Py_ssize_t i, pos, envc; |
| 4762 | PyObject *keys=NULL, *vals=NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4763 | PyObject *key, *val, *keyval; |
| 4764 | EXECV_CHAR **envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4765 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4766 | i = PyMapping_Size(env); |
| 4767 | if (i < 0) |
| 4768 | return NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4769 | envlist = PyMem_NEW(EXECV_CHAR *, i + 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4770 | if (envlist == NULL) { |
| 4771 | PyErr_NoMemory(); |
| 4772 | return NULL; |
| 4773 | } |
| 4774 | envc = 0; |
| 4775 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4776 | if (!keys) |
| 4777 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4778 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4779 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4780 | goto error; |
| 4781 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 4782 | PyErr_Format(PyExc_TypeError, |
| 4783 | "env.keys() or env.values() is not a list"); |
| 4784 | goto error; |
| 4785 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4786 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4787 | for (pos = 0; pos < i; pos++) { |
| 4788 | key = PyList_GetItem(keys, pos); |
| 4789 | val = PyList_GetItem(vals, pos); |
| 4790 | if (!key || !val) |
| 4791 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4792 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4793 | keyval = PyUnicode_FromFormat("%U=%U", key, val); |
| 4794 | if (!keyval) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4795 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4796 | |
| 4797 | if (!fsconvert_strdup(keyval, &envlist[envc++])) { |
| 4798 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4799 | goto error; |
| 4800 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4801 | |
| 4802 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4803 | } |
| 4804 | Py_DECREF(vals); |
| 4805 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4806 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4807 | envlist[envc] = 0; |
| 4808 | *envc_ptr = envc; |
| 4809 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4810 | |
| 4811 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4812 | Py_XDECREF(keys); |
| 4813 | Py_XDECREF(vals); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4814 | free_string_array(envlist, envc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4815 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4816 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4817 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4818 | static EXECV_CHAR** |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4819 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 4820 | { |
| 4821 | int i; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4822 | EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4823 | if (argvlist == NULL) { |
| 4824 | PyErr_NoMemory(); |
| 4825 | return NULL; |
| 4826 | } |
| 4827 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4828 | PyObject* item = PySequence_ITEM(argv, i); |
| 4829 | if (item == NULL) |
| 4830 | goto fail; |
| 4831 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 4832 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4833 | goto fail; |
| 4834 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4835 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4836 | } |
| 4837 | argvlist[*argc] = NULL; |
| 4838 | return argvlist; |
| 4839 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4840 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4841 | free_string_array(argvlist, *argc); |
| 4842 | return NULL; |
| 4843 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4844 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4845 | #endif |
| 4846 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4847 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4848 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4849 | /*[clinic input] |
| 4850 | os.execv |
| 4851 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4852 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4853 | Path of executable file. |
| 4854 | argv: object |
| 4855 | Tuple or list of strings. |
| 4856 | / |
| 4857 | |
| 4858 | Execute an executable path with arguments, replacing current process. |
| 4859 | [clinic start generated code]*/ |
| 4860 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4861 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4862 | os_execv_impl(PyObject *module, path_t *path, PyObject *argv) |
| 4863 | /*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4864 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4865 | EXECV_CHAR **argvlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4866 | Py_ssize_t argc; |
| 4867 | |
| 4868 | /* execv has two arguments: (path, argv), where |
| 4869 | argv is a list or tuple of strings. */ |
| 4870 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4871 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4872 | PyErr_SetString(PyExc_TypeError, |
| 4873 | "execv() arg 2 must be a tuple or list"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4874 | return NULL; |
| 4875 | } |
| 4876 | argc = PySequence_Size(argv); |
| 4877 | if (argc < 1) { |
| 4878 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4879 | return NULL; |
| 4880 | } |
| 4881 | |
| 4882 | argvlist = parse_arglist(argv, &argc); |
| 4883 | if (argvlist == NULL) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4884 | return NULL; |
| 4885 | } |
| 4886 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4887 | #ifdef HAVE_WEXECV |
| 4888 | _wexecv(path->wide, argvlist); |
| 4889 | #else |
| 4890 | execv(path->narrow, argvlist); |
| 4891 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4892 | |
| 4893 | /* If we get here it's definitely an error */ |
| 4894 | |
| 4895 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4896 | return posix_error(); |
| 4897 | } |
| 4898 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4899 | |
| 4900 | /*[clinic input] |
| 4901 | os.execve |
| 4902 | |
| 4903 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') |
| 4904 | Path of executable file. |
| 4905 | argv: object |
| 4906 | Tuple or list of strings. |
| 4907 | env: object |
| 4908 | Dictionary of strings mapping to strings. |
| 4909 | |
| 4910 | Execute an executable path with arguments, replacing current process. |
| 4911 | [clinic start generated code]*/ |
| 4912 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4913 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4914 | os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env) |
| 4915 | /*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4916 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4917 | EXECV_CHAR **argvlist = NULL; |
| 4918 | EXECV_CHAR **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4919 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4920 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4921 | /* execve has three arguments: (path, argv, env), where |
| 4922 | argv is a list or tuple of strings and env is a dictionary |
| 4923 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4924 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4925 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4926 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4927 | "execve: argv must be a tuple or list"); |
| 4928 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4929 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4930 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4931 | if (!PyMapping_Check(env)) { |
| 4932 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4933 | "execve: environment must be a mapping object"); |
| 4934 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4935 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4936 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4937 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4938 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4939 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4940 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4941 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4942 | envlist = parse_envlist(env, &envc); |
| 4943 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4944 | goto fail; |
| 4945 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4946 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4947 | if (path->fd > -1) |
| 4948 | fexecve(path->fd, argvlist, envlist); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4949 | else |
| 4950 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4951 | #ifdef HAVE_WEXECV |
| 4952 | _wexecve(path->wide, argvlist, envlist); |
| 4953 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4954 | execve(path->narrow, argvlist, envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4955 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4956 | |
| 4957 | /* If we get here it's definitely an error */ |
| 4958 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4959 | path_error(path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4960 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4961 | free_string_array(envlist, envc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4962 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4963 | if (argvlist) |
| 4964 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4965 | return NULL; |
| 4966 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4967 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4968 | #endif /* HAVE_EXECV */ |
| 4969 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4970 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4971 | #if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4972 | /*[clinic input] |
| 4973 | os.spawnv |
| 4974 | |
| 4975 | mode: int |
| 4976 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4977 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4978 | Path of executable file. |
| 4979 | argv: object |
| 4980 | Tuple or list of strings. |
| 4981 | / |
| 4982 | |
| 4983 | Execute the program specified by path in a new process. |
| 4984 | [clinic start generated code]*/ |
| 4985 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4986 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4987 | os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv) |
| 4988 | /*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4989 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4990 | EXECV_CHAR **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4991 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4992 | Py_ssize_t argc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 4993 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4994 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4995 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4996 | /* spawnv has three arguments: (mode, path, argv), where |
| 4997 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4998 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4999 | if (PyList_Check(argv)) { |
| 5000 | argc = PyList_Size(argv); |
| 5001 | getitem = PyList_GetItem; |
| 5002 | } |
| 5003 | else if (PyTuple_Check(argv)) { |
| 5004 | argc = PyTuple_Size(argv); |
| 5005 | getitem = PyTuple_GetItem; |
| 5006 | } |
| 5007 | else { |
| 5008 | PyErr_SetString(PyExc_TypeError, |
| 5009 | "spawnv() arg 2 must be a tuple or list"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5010 | return NULL; |
| 5011 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5012 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5013 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5014 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5015 | return PyErr_NoMemory(); |
| 5016 | } |
| 5017 | for (i = 0; i < argc; i++) { |
| 5018 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5019 | &argvlist[i])) { |
| 5020 | free_string_array(argvlist, i); |
| 5021 | PyErr_SetString( |
| 5022 | PyExc_TypeError, |
| 5023 | "spawnv() arg 2 must contain only strings"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5024 | return NULL; |
| 5025 | } |
| 5026 | } |
| 5027 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5028 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5029 | if (mode == _OLD_P_OVERLAY) |
| 5030 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5031 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5032 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5033 | #ifdef HAVE_WSPAWNV |
| 5034 | spawnval = _wspawnv(mode, path->wide, argvlist); |
| 5035 | #else |
| 5036 | spawnval = _spawnv(mode, path->narrow, argvlist); |
| 5037 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5038 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5039 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5040 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5041 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5042 | if (spawnval == -1) |
| 5043 | return posix_error(); |
| 5044 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5045 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5046 | } |
| 5047 | |
| 5048 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5049 | /*[clinic input] |
| 5050 | os.spawnve |
| 5051 | |
| 5052 | mode: int |
| 5053 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5054 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5055 | Path of executable file. |
| 5056 | argv: object |
| 5057 | Tuple or list of strings. |
| 5058 | env: object |
| 5059 | Dictionary of strings mapping to strings. |
| 5060 | / |
| 5061 | |
| 5062 | Execute the program specified by path in a new process. |
| 5063 | [clinic start generated code]*/ |
| 5064 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5065 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5066 | os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv, |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5067 | PyObject *env) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5068 | /*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5069 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5070 | EXECV_CHAR **argvlist; |
| 5071 | EXECV_CHAR **envlist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5072 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5073 | Py_ssize_t argc, i, envc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5074 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5075 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 5076 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5077 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5078 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5079 | argv is a list or tuple of strings and env is a dictionary |
| 5080 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5081 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5082 | if (PyList_Check(argv)) { |
| 5083 | argc = PyList_Size(argv); |
| 5084 | getitem = PyList_GetItem; |
| 5085 | } |
| 5086 | else if (PyTuple_Check(argv)) { |
| 5087 | argc = PyTuple_Size(argv); |
| 5088 | getitem = PyTuple_GetItem; |
| 5089 | } |
| 5090 | else { |
| 5091 | PyErr_SetString(PyExc_TypeError, |
| 5092 | "spawnve() arg 2 must be a tuple or list"); |
| 5093 | goto fail_0; |
| 5094 | } |
| 5095 | if (!PyMapping_Check(env)) { |
| 5096 | PyErr_SetString(PyExc_TypeError, |
| 5097 | "spawnve() arg 3 must be a mapping object"); |
| 5098 | goto fail_0; |
| 5099 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5100 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5101 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5102 | if (argvlist == NULL) { |
| 5103 | PyErr_NoMemory(); |
| 5104 | goto fail_0; |
| 5105 | } |
| 5106 | for (i = 0; i < argc; i++) { |
| 5107 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5108 | &argvlist[i])) |
| 5109 | { |
| 5110 | lastarg = i; |
| 5111 | goto fail_1; |
| 5112 | } |
| 5113 | } |
| 5114 | lastarg = argc; |
| 5115 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5116 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5117 | envlist = parse_envlist(env, &envc); |
| 5118 | if (envlist == NULL) |
| 5119 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5120 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5121 | if (mode == _OLD_P_OVERLAY) |
| 5122 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5123 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5124 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5125 | #ifdef HAVE_WSPAWNV |
| 5126 | spawnval = _wspawnve(mode, path->wide, argvlist, envlist); |
| 5127 | #else |
| 5128 | spawnval = _spawnve(mode, path->narrow, argvlist, envlist); |
| 5129 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5130 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5131 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5132 | if (spawnval == -1) |
| 5133 | (void) posix_error(); |
| 5134 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5135 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5136 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5137 | while (--envc >= 0) |
| 5138 | PyMem_DEL(envlist[envc]); |
| 5139 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5140 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5141 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5142 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5143 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5144 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5145 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5146 | #endif /* HAVE_SPAWNV */ |
| 5147 | |
| 5148 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5149 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5150 | /*[clinic input] |
| 5151 | os.fork1 |
| 5152 | |
| 5153 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 5154 | |
| 5155 | Return 0 to child process and PID of child to parent process. |
| 5156 | [clinic start generated code]*/ |
| 5157 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5158 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5159 | os_fork1_impl(PyObject *module) |
| 5160 | /*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5161 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5162 | pid_t pid; |
| 5163 | int result = 0; |
| 5164 | _PyImport_AcquireLock(); |
| 5165 | pid = fork1(); |
| 5166 | if (pid == 0) { |
| 5167 | /* child: this clobbers and resets the import lock. */ |
| 5168 | PyOS_AfterFork(); |
| 5169 | } else { |
| 5170 | /* parent: release the import lock. */ |
| 5171 | result = _PyImport_ReleaseLock(); |
| 5172 | } |
| 5173 | if (pid == -1) |
| 5174 | return posix_error(); |
| 5175 | if (result < 0) { |
| 5176 | /* Don't clobber the OSError if the fork failed. */ |
| 5177 | PyErr_SetString(PyExc_RuntimeError, |
| 5178 | "not holding the import lock"); |
| 5179 | return NULL; |
| 5180 | } |
| 5181 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5182 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5183 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5184 | |
| 5185 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5186 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5187 | /*[clinic input] |
| 5188 | os.fork |
| 5189 | |
| 5190 | Fork a child process. |
| 5191 | |
| 5192 | Return 0 to child process and PID of child to parent process. |
| 5193 | [clinic start generated code]*/ |
| 5194 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5195 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5196 | os_fork_impl(PyObject *module) |
| 5197 | /*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5198 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5199 | pid_t pid; |
| 5200 | int result = 0; |
| 5201 | _PyImport_AcquireLock(); |
| 5202 | pid = fork(); |
| 5203 | if (pid == 0) { |
| 5204 | /* child: this clobbers and resets the import lock. */ |
| 5205 | PyOS_AfterFork(); |
| 5206 | } else { |
| 5207 | /* parent: release the import lock. */ |
| 5208 | result = _PyImport_ReleaseLock(); |
| 5209 | } |
| 5210 | if (pid == -1) |
| 5211 | return posix_error(); |
| 5212 | if (result < 0) { |
| 5213 | /* Don't clobber the OSError if the fork failed. */ |
| 5214 | PyErr_SetString(PyExc_RuntimeError, |
| 5215 | "not holding the import lock"); |
| 5216 | return NULL; |
| 5217 | } |
| 5218 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5219 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5220 | #endif /* HAVE_FORK */ |
| 5221 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5222 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5223 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5224 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5225 | /*[clinic input] |
| 5226 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5227 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5228 | policy: int |
| 5229 | |
| 5230 | Get the maximum scheduling priority for policy. |
| 5231 | [clinic start generated code]*/ |
| 5232 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5233 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5234 | os_sched_get_priority_max_impl(PyObject *module, int policy) |
| 5235 | /*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5236 | { |
| 5237 | int max; |
| 5238 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5239 | max = sched_get_priority_max(policy); |
| 5240 | if (max < 0) |
| 5241 | return posix_error(); |
| 5242 | return PyLong_FromLong(max); |
| 5243 | } |
| 5244 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5245 | |
| 5246 | /*[clinic input] |
| 5247 | os.sched_get_priority_min |
| 5248 | |
| 5249 | policy: int |
| 5250 | |
| 5251 | Get the minimum scheduling priority for policy. |
| 5252 | [clinic start generated code]*/ |
| 5253 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5254 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5255 | os_sched_get_priority_min_impl(PyObject *module, int policy) |
| 5256 | /*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5257 | { |
| 5258 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5259 | if (min < 0) |
| 5260 | return posix_error(); |
| 5261 | return PyLong_FromLong(min); |
| 5262 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5263 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 5264 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5265 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5266 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5267 | /*[clinic input] |
| 5268 | os.sched_getscheduler |
| 5269 | pid: pid_t |
| 5270 | / |
| 5271 | |
| 5272 | Get the scheduling policy for the process identifiedy by pid. |
| 5273 | |
| 5274 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 5275 | [clinic start generated code]*/ |
| 5276 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5277 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5278 | os_sched_getscheduler_impl(PyObject *module, pid_t pid) |
| 5279 | /*[clinic end generated code: output=dce4c0bd3f1b34c8 input=5f14cfd1f189e1a0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5280 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5281 | int policy; |
| 5282 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5283 | policy = sched_getscheduler(pid); |
| 5284 | if (policy < 0) |
| 5285 | return posix_error(); |
| 5286 | return PyLong_FromLong(policy); |
| 5287 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5288 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5289 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5290 | |
| 5291 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5292 | /*[clinic input] |
| 5293 | class os.sched_param "PyObject *" "&SchedParamType" |
| 5294 | |
| 5295 | @classmethod |
| 5296 | os.sched_param.__new__ |
| 5297 | |
| 5298 | sched_priority: object |
| 5299 | A scheduling parameter. |
| 5300 | |
| 5301 | Current has only one field: sched_priority"); |
| 5302 | [clinic start generated code]*/ |
| 5303 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5304 | static PyObject * |
| 5305 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5306 | /*[clinic end generated code: output=48f4067d60f48c13 input=73a4c22f7071fc62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5307 | { |
| 5308 | PyObject *res; |
| 5309 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5310 | res = PyStructSequence_New(type); |
| 5311 | if (!res) |
| 5312 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5313 | Py_INCREF(sched_priority); |
| 5314 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5315 | return res; |
| 5316 | } |
| 5317 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5318 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5319 | PyDoc_VAR(os_sched_param__doc__); |
| 5320 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5321 | static PyStructSequence_Field sched_param_fields[] = { |
| 5322 | {"sched_priority", "the scheduling priority"}, |
| 5323 | {0} |
| 5324 | }; |
| 5325 | |
| 5326 | static PyStructSequence_Desc sched_param_desc = { |
| 5327 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5328 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5329 | sched_param_fields, |
| 5330 | 1 |
| 5331 | }; |
| 5332 | |
| 5333 | static int |
| 5334 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 5335 | { |
| 5336 | long priority; |
| 5337 | |
| 5338 | if (Py_TYPE(param) != &SchedParamType) { |
| 5339 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 5340 | return 0; |
| 5341 | } |
| 5342 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 5343 | if (priority == -1 && PyErr_Occurred()) |
| 5344 | return 0; |
| 5345 | if (priority > INT_MAX || priority < INT_MIN) { |
| 5346 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 5347 | return 0; |
| 5348 | } |
| 5349 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 5350 | return 1; |
| 5351 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5352 | #endif /* defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5353 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5354 | |
| 5355 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5356 | /*[clinic input] |
| 5357 | os.sched_setscheduler |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5358 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5359 | pid: pid_t |
| 5360 | policy: int |
| 5361 | param: sched_param |
| 5362 | / |
| 5363 | |
| 5364 | Set the scheduling policy for the process identified by pid. |
| 5365 | |
| 5366 | If pid is 0, the calling process is changed. |
| 5367 | param is an instance of sched_param. |
| 5368 | [clinic start generated code]*/ |
| 5369 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5370 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5371 | os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 5372 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5373 | /*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5374 | { |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5375 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 5376 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 5377 | ** scheduling policy under Solaris/Illumos, and others. |
| 5378 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5379 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5380 | if (sched_setscheduler(pid, policy, param) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5381 | return posix_error(); |
| 5382 | Py_RETURN_NONE; |
| 5383 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5384 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5385 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5386 | |
| 5387 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5388 | /*[clinic input] |
| 5389 | os.sched_getparam |
| 5390 | pid: pid_t |
| 5391 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5392 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5393 | Returns scheduling parameters for the process identified by pid. |
| 5394 | |
| 5395 | If pid is 0, returns parameters for the calling process. |
| 5396 | Return value is an instance of sched_param. |
| 5397 | [clinic start generated code]*/ |
| 5398 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5399 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5400 | os_sched_getparam_impl(PyObject *module, pid_t pid) |
| 5401 | /*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5402 | { |
| 5403 | struct sched_param param; |
| 5404 | PyObject *result; |
| 5405 | PyObject *priority; |
| 5406 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5407 | if (sched_getparam(pid, ¶m)) |
| 5408 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5409 | result = PyStructSequence_New(&SchedParamType); |
| 5410 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5411 | return NULL; |
| 5412 | priority = PyLong_FromLong(param.sched_priority); |
| 5413 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5414 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5415 | return NULL; |
| 5416 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5417 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 5418 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5419 | } |
| 5420 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5421 | |
| 5422 | /*[clinic input] |
| 5423 | os.sched_setparam |
| 5424 | pid: pid_t |
| 5425 | param: sched_param |
| 5426 | / |
| 5427 | |
| 5428 | Set scheduling parameters for the process identified by pid. |
| 5429 | |
| 5430 | If pid is 0, sets parameters for the calling process. |
| 5431 | param should be an instance of sched_param. |
| 5432 | [clinic start generated code]*/ |
| 5433 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5434 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5435 | os_sched_setparam_impl(PyObject *module, pid_t pid, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 5436 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5437 | /*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5438 | { |
| 5439 | if (sched_setparam(pid, param)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5440 | return posix_error(); |
| 5441 | Py_RETURN_NONE; |
| 5442 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5443 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5444 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5445 | |
| 5446 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5447 | /*[clinic input] |
| 5448 | os.sched_rr_get_interval -> double |
| 5449 | pid: pid_t |
| 5450 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5451 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5452 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 5453 | |
| 5454 | Value returned is a float. |
| 5455 | [clinic start generated code]*/ |
| 5456 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5457 | static double |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5458 | os_sched_rr_get_interval_impl(PyObject *module, pid_t pid) |
| 5459 | /*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5460 | { |
| 5461 | struct timespec interval; |
| 5462 | if (sched_rr_get_interval(pid, &interval)) { |
| 5463 | posix_error(); |
| 5464 | return -1.0; |
| 5465 | } |
| 5466 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 5467 | } |
| 5468 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5469 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5470 | |
| 5471 | /*[clinic input] |
| 5472 | os.sched_yield |
| 5473 | |
| 5474 | Voluntarily relinquish the CPU. |
| 5475 | [clinic start generated code]*/ |
| 5476 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5477 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5478 | os_sched_yield_impl(PyObject *module) |
| 5479 | /*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5480 | { |
| 5481 | if (sched_yield()) |
| 5482 | return posix_error(); |
| 5483 | Py_RETURN_NONE; |
| 5484 | } |
| 5485 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5486 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5487 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 5488 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5489 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5490 | /*[clinic input] |
| 5491 | os.sched_setaffinity |
| 5492 | pid: pid_t |
| 5493 | mask : object |
| 5494 | / |
| 5495 | |
| 5496 | Set the CPU affinity of the process identified by pid to mask. |
| 5497 | |
| 5498 | mask should be an iterable of integers identifying CPUs. |
| 5499 | [clinic start generated code]*/ |
| 5500 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5501 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5502 | os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask) |
| 5503 | /*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5504 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5505 | int ncpus; |
| 5506 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5507 | cpu_set_t *cpu_set = NULL; |
| 5508 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5509 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5510 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5511 | if (iterator == NULL) |
| 5512 | return NULL; |
| 5513 | |
| 5514 | ncpus = NCPUS_START; |
| 5515 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5516 | cpu_set = CPU_ALLOC(ncpus); |
| 5517 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5518 | PyErr_NoMemory(); |
| 5519 | goto error; |
| 5520 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5521 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5522 | |
| 5523 | while ((item = PyIter_Next(iterator))) { |
| 5524 | long cpu; |
| 5525 | if (!PyLong_Check(item)) { |
| 5526 | PyErr_Format(PyExc_TypeError, |
| 5527 | "expected an iterator of ints, " |
| 5528 | "but iterator yielded %R", |
| 5529 | Py_TYPE(item)); |
| 5530 | Py_DECREF(item); |
| 5531 | goto error; |
| 5532 | } |
| 5533 | cpu = PyLong_AsLong(item); |
| 5534 | Py_DECREF(item); |
| 5535 | if (cpu < 0) { |
| 5536 | if (!PyErr_Occurred()) |
| 5537 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 5538 | goto error; |
| 5539 | } |
| 5540 | if (cpu > INT_MAX - 1) { |
| 5541 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 5542 | goto error; |
| 5543 | } |
| 5544 | if (cpu >= ncpus) { |
| 5545 | /* Grow CPU mask to fit the CPU number */ |
| 5546 | int newncpus = ncpus; |
| 5547 | cpu_set_t *newmask; |
| 5548 | size_t newsetsize; |
| 5549 | while (newncpus <= cpu) { |
| 5550 | if (newncpus > INT_MAX / 2) |
| 5551 | newncpus = cpu + 1; |
| 5552 | else |
| 5553 | newncpus = newncpus * 2; |
| 5554 | } |
| 5555 | newmask = CPU_ALLOC(newncpus); |
| 5556 | if (newmask == NULL) { |
| 5557 | PyErr_NoMemory(); |
| 5558 | goto error; |
| 5559 | } |
| 5560 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 5561 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5562 | memcpy(newmask, cpu_set, setsize); |
| 5563 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5564 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5565 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5566 | ncpus = newncpus; |
| 5567 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5568 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5569 | } |
| 5570 | Py_CLEAR(iterator); |
| 5571 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5572 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5573 | posix_error(); |
| 5574 | goto error; |
| 5575 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5576 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5577 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5578 | |
| 5579 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5580 | if (cpu_set) |
| 5581 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5582 | Py_XDECREF(iterator); |
| 5583 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5584 | } |
| 5585 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5586 | |
| 5587 | /*[clinic input] |
| 5588 | os.sched_getaffinity |
| 5589 | pid: pid_t |
| 5590 | / |
| 5591 | |
Charles-François Natali | dc87e4b | 2015-07-13 21:01:39 +0100 | [diff] [blame] | 5592 | 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] | 5593 | |
| 5594 | The affinity is returned as a set of CPU identifiers. |
| 5595 | [clinic start generated code]*/ |
| 5596 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5597 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5598 | os_sched_getaffinity_impl(PyObject *module, pid_t pid) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 5599 | /*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5600 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5601 | int cpu, ncpus, count; |
| 5602 | size_t setsize; |
| 5603 | cpu_set_t *mask = NULL; |
| 5604 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5605 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5606 | ncpus = NCPUS_START; |
| 5607 | while (1) { |
| 5608 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5609 | mask = CPU_ALLOC(ncpus); |
| 5610 | if (mask == NULL) |
| 5611 | return PyErr_NoMemory(); |
| 5612 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 5613 | break; |
| 5614 | CPU_FREE(mask); |
| 5615 | if (errno != EINVAL) |
| 5616 | return posix_error(); |
| 5617 | if (ncpus > INT_MAX / 2) { |
| 5618 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 5619 | "a large enough CPU set"); |
| 5620 | return NULL; |
| 5621 | } |
| 5622 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5623 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5624 | |
| 5625 | res = PySet_New(NULL); |
| 5626 | if (res == NULL) |
| 5627 | goto error; |
| 5628 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 5629 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 5630 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 5631 | --count; |
| 5632 | if (cpu_num == NULL) |
| 5633 | goto error; |
| 5634 | if (PySet_Add(res, cpu_num)) { |
| 5635 | Py_DECREF(cpu_num); |
| 5636 | goto error; |
| 5637 | } |
| 5638 | Py_DECREF(cpu_num); |
| 5639 | } |
| 5640 | } |
| 5641 | CPU_FREE(mask); |
| 5642 | return res; |
| 5643 | |
| 5644 | error: |
| 5645 | if (mask) |
| 5646 | CPU_FREE(mask); |
| 5647 | Py_XDECREF(res); |
| 5648 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5649 | } |
| 5650 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5651 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5652 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5653 | #endif /* HAVE_SCHED_H */ |
| 5654 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5655 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5656 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5657 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5658 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5659 | #define DEV_PTY_FILE "/dev/ptc" |
| 5660 | #define HAVE_DEV_PTMX |
| 5661 | #else |
| 5662 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5663 | #endif |
| 5664 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5665 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5666 | #ifdef HAVE_PTY_H |
| 5667 | #include <pty.h> |
| 5668 | #else |
| 5669 | #ifdef HAVE_LIBUTIL_H |
| 5670 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5671 | #else |
| 5672 | #ifdef HAVE_UTIL_H |
| 5673 | #include <util.h> |
| 5674 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5675 | #endif /* HAVE_LIBUTIL_H */ |
| 5676 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5677 | #ifdef HAVE_STROPTS_H |
| 5678 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5679 | #endif |
| 5680 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5681 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5682 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5683 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5684 | /*[clinic input] |
| 5685 | os.openpty |
| 5686 | |
| 5687 | Open a pseudo-terminal. |
| 5688 | |
| 5689 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 5690 | for both the master and slave ends. |
| 5691 | [clinic start generated code]*/ |
| 5692 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5693 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5694 | os_openpty_impl(PyObject *module) |
| 5695 | /*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5696 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5697 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5698 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5699 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5700 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5701 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5702 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5703 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5704 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5705 | #endif |
| 5706 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5707 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5708 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5709 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5710 | goto posix_error; |
| 5711 | |
| 5712 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5713 | goto error; |
| 5714 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 5715 | goto error; |
| 5716 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5717 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5718 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5719 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5720 | goto posix_error; |
| 5721 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5722 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5723 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5724 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5725 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 5726 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5727 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5728 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 5729 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5730 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5731 | goto posix_error; |
| 5732 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5733 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5734 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5735 | /* change permission of slave */ |
| 5736 | if (grantpt(master_fd) < 0) { |
| 5737 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5738 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5739 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5740 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5741 | /* unlock slave */ |
| 5742 | if (unlockpt(master_fd) < 0) { |
| 5743 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5744 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5745 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5746 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5747 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5748 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5749 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 5750 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5751 | goto posix_error; |
| 5752 | |
| 5753 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 5754 | if (slave_fd == -1) |
| 5755 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 5756 | |
| 5757 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5758 | goto posix_error; |
| 5759 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 5760 | #if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5761 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 5762 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5763 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5764 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5765 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5766 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 5767 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5768 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5769 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5770 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5771 | posix_error: |
| 5772 | posix_error(); |
| 5773 | error: |
| 5774 | if (master_fd != -1) |
| 5775 | close(master_fd); |
| 5776 | if (slave_fd != -1) |
| 5777 | close(slave_fd); |
| 5778 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5779 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5780 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5781 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5782 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5783 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5784 | /*[clinic input] |
| 5785 | os.forkpty |
| 5786 | |
| 5787 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 5788 | |
| 5789 | Returns a tuple of (pid, master_fd). |
| 5790 | Like fork(), return pid of 0 to the child process, |
| 5791 | and pid of child to the parent process. |
| 5792 | To both, return fd of newly opened pseudo-terminal. |
| 5793 | [clinic start generated code]*/ |
| 5794 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5795 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5796 | os_forkpty_impl(PyObject *module) |
| 5797 | /*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5798 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5799 | int master_fd = -1, result = 0; |
| 5800 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5801 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5802 | _PyImport_AcquireLock(); |
| 5803 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 5804 | if (pid == 0) { |
| 5805 | /* child: this clobbers and resets the import lock. */ |
| 5806 | PyOS_AfterFork(); |
| 5807 | } else { |
| 5808 | /* parent: release the import lock. */ |
| 5809 | result = _PyImport_ReleaseLock(); |
| 5810 | } |
| 5811 | if (pid == -1) |
| 5812 | return posix_error(); |
| 5813 | if (result < 0) { |
| 5814 | /* Don't clobber the OSError if the fork failed. */ |
| 5815 | PyErr_SetString(PyExc_RuntimeError, |
| 5816 | "not holding the import lock"); |
| 5817 | return NULL; |
| 5818 | } |
| 5819 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5820 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5821 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5822 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5823 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5824 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5825 | /*[clinic input] |
| 5826 | os.getegid |
| 5827 | |
| 5828 | Return the current process's effective group id. |
| 5829 | [clinic start generated code]*/ |
| 5830 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5831 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5832 | os_getegid_impl(PyObject *module) |
| 5833 | /*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5834 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5835 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5836 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5837 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5838 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5839 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5840 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5841 | /*[clinic input] |
| 5842 | os.geteuid |
| 5843 | |
| 5844 | Return the current process's effective user id. |
| 5845 | [clinic start generated code]*/ |
| 5846 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5847 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5848 | os_geteuid_impl(PyObject *module) |
| 5849 | /*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5850 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5851 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5852 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5853 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5854 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5855 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5856 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5857 | /*[clinic input] |
| 5858 | os.getgid |
| 5859 | |
| 5860 | Return the current process's group id. |
| 5861 | [clinic start generated code]*/ |
| 5862 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5863 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5864 | os_getgid_impl(PyObject *module) |
| 5865 | /*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5866 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5867 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5868 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5869 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5870 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5871 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5872 | /*[clinic input] |
| 5873 | os.getpid |
| 5874 | |
| 5875 | Return the current process 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_getpid_impl(PyObject *module) |
| 5880 | /*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5881 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5882 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5883 | } |
| 5884 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5885 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5886 | |
| 5887 | /* AC 3.5: funny apple logic below */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5888 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 5889 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 5890 | Returns a list of groups to which a user belongs.\n\n\ |
| 5891 | user: username to lookup\n\ |
| 5892 | group: base group id of the user"); |
| 5893 | |
| 5894 | static PyObject * |
| 5895 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 5896 | { |
| 5897 | #ifdef NGROUPS_MAX |
| 5898 | #define MAX_GROUPS NGROUPS_MAX |
| 5899 | #else |
| 5900 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 5901 | #define MAX_GROUPS 64 |
| 5902 | #endif |
| 5903 | |
| 5904 | const char *user; |
| 5905 | int i, ngroups; |
| 5906 | PyObject *list; |
| 5907 | #ifdef __APPLE__ |
| 5908 | int *groups, basegid; |
| 5909 | #else |
| 5910 | gid_t *groups, basegid; |
| 5911 | #endif |
| 5912 | ngroups = MAX_GROUPS; |
| 5913 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5914 | #ifdef __APPLE__ |
| 5915 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5916 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5917 | #else |
| 5918 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 5919 | _Py_Gid_Converter, &basegid)) |
| 5920 | return NULL; |
| 5921 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5922 | |
| 5923 | #ifdef __APPLE__ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 5924 | groups = PyMem_New(int, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5925 | #else |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 5926 | groups = PyMem_New(gid_t, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5927 | #endif |
| 5928 | if (groups == NULL) |
| 5929 | return PyErr_NoMemory(); |
| 5930 | |
| 5931 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 5932 | PyMem_Del(groups); |
| 5933 | return posix_error(); |
| 5934 | } |
| 5935 | |
| 5936 | list = PyList_New(ngroups); |
| 5937 | if (list == NULL) { |
| 5938 | PyMem_Del(groups); |
| 5939 | return NULL; |
| 5940 | } |
| 5941 | |
| 5942 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5943 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5944 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5945 | #else |
| 5946 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 5947 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5948 | if (o == NULL) { |
| 5949 | Py_DECREF(list); |
| 5950 | PyMem_Del(groups); |
| 5951 | return NULL; |
| 5952 | } |
| 5953 | PyList_SET_ITEM(list, i, o); |
| 5954 | } |
| 5955 | |
| 5956 | PyMem_Del(groups); |
| 5957 | |
| 5958 | return list; |
| 5959 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5960 | #endif /* HAVE_GETGROUPLIST */ |
| 5961 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5962 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5963 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5964 | /*[clinic input] |
| 5965 | os.getgroups |
| 5966 | |
| 5967 | Return list of supplemental group IDs for the process. |
| 5968 | [clinic start generated code]*/ |
| 5969 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5970 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5971 | os_getgroups_impl(PyObject *module) |
| 5972 | /*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5973 | { |
| 5974 | PyObject *result = NULL; |
| 5975 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5976 | #ifdef NGROUPS_MAX |
| 5977 | #define MAX_GROUPS NGROUPS_MAX |
| 5978 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5979 | /* 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] | 5980 | #define MAX_GROUPS 64 |
| 5981 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5982 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5983 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5984 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5985 | * This is a helper variable to store the intermediate result when |
| 5986 | * that happens. |
| 5987 | * |
| 5988 | * To keep the code readable the OSX behaviour is unconditional, |
| 5989 | * according to the POSIX spec this should be safe on all unix-y |
| 5990 | * systems. |
| 5991 | */ |
| 5992 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5993 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5994 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 5995 | #ifdef __APPLE__ |
| 5996 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 5997 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 5998 | * always first call getgroups with length 0 to get the actual number |
| 5999 | * of groups. |
| 6000 | */ |
| 6001 | n = getgroups(0, NULL); |
| 6002 | if (n < 0) { |
| 6003 | return posix_error(); |
| 6004 | } else if (n <= MAX_GROUPS) { |
| 6005 | /* groups will fit in existing array */ |
| 6006 | alt_grouplist = grouplist; |
| 6007 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6008 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6009 | if (alt_grouplist == NULL) { |
| 6010 | errno = EINVAL; |
| 6011 | return posix_error(); |
| 6012 | } |
| 6013 | } |
| 6014 | |
| 6015 | n = getgroups(n, alt_grouplist); |
| 6016 | if (n == -1) { |
| 6017 | if (alt_grouplist != grouplist) { |
| 6018 | PyMem_Free(alt_grouplist); |
| 6019 | } |
| 6020 | return posix_error(); |
| 6021 | } |
| 6022 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6023 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6024 | if (n < 0) { |
| 6025 | if (errno == EINVAL) { |
| 6026 | n = getgroups(0, NULL); |
| 6027 | if (n == -1) { |
| 6028 | return posix_error(); |
| 6029 | } |
| 6030 | if (n == 0) { |
| 6031 | /* Avoid malloc(0) */ |
| 6032 | alt_grouplist = grouplist; |
| 6033 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6034 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6035 | if (alt_grouplist == NULL) { |
| 6036 | errno = EINVAL; |
| 6037 | return posix_error(); |
| 6038 | } |
| 6039 | n = getgroups(n, alt_grouplist); |
| 6040 | if (n == -1) { |
| 6041 | PyMem_Free(alt_grouplist); |
| 6042 | return posix_error(); |
| 6043 | } |
| 6044 | } |
| 6045 | } else { |
| 6046 | return posix_error(); |
| 6047 | } |
| 6048 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6049 | #endif |
| 6050 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6051 | result = PyList_New(n); |
| 6052 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6053 | int i; |
| 6054 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6055 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6056 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6057 | Py_DECREF(result); |
| 6058 | result = NULL; |
| 6059 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6060 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6061 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6062 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6063 | } |
| 6064 | |
| 6065 | if (alt_grouplist != grouplist) { |
| 6066 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6067 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6068 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6069 | return result; |
| 6070 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6071 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6072 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6073 | #ifdef HAVE_INITGROUPS |
| 6074 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 6075 | "initgroups(username, gid) -> None\n\n\ |
| 6076 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 6077 | the groups of which the specified username is a member, plus the specified\n\ |
| 6078 | group id."); |
| 6079 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6080 | /* AC 3.5: funny apple logic */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6081 | static PyObject * |
| 6082 | posix_initgroups(PyObject *self, PyObject *args) |
| 6083 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6084 | PyObject *oname; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 6085 | const char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6086 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6087 | #ifdef __APPLE__ |
| 6088 | int gid; |
| 6089 | #else |
| 6090 | gid_t gid; |
| 6091 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6092 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6093 | #ifdef __APPLE__ |
| 6094 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 6095 | PyUnicode_FSConverter, &oname, |
| 6096 | &gid)) |
| 6097 | #else |
| 6098 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 6099 | PyUnicode_FSConverter, &oname, |
| 6100 | _Py_Gid_Converter, &gid)) |
| 6101 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6102 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6103 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6104 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6105 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6106 | Py_DECREF(oname); |
| 6107 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6108 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6109 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6110 | Py_INCREF(Py_None); |
| 6111 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6112 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6113 | #endif /* HAVE_INITGROUPS */ |
| 6114 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6115 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6116 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6117 | /*[clinic input] |
| 6118 | os.getpgid |
| 6119 | |
| 6120 | pid: pid_t |
| 6121 | |
| 6122 | Call the system call getpgid(), and return the result. |
| 6123 | [clinic start generated code]*/ |
| 6124 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6125 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6126 | os_getpgid_impl(PyObject *module, pid_t pid) |
| 6127 | /*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6128 | { |
| 6129 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6130 | if (pgid < 0) |
| 6131 | return posix_error(); |
| 6132 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6133 | } |
| 6134 | #endif /* HAVE_GETPGID */ |
| 6135 | |
| 6136 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6137 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6138 | /*[clinic input] |
| 6139 | os.getpgrp |
| 6140 | |
| 6141 | Return the current process group id. |
| 6142 | [clinic start generated code]*/ |
| 6143 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6144 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6145 | os_getpgrp_impl(PyObject *module) |
| 6146 | /*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6147 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6148 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6149 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6150 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6151 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6152 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6153 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6154 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6155 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6156 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6157 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6158 | /*[clinic input] |
| 6159 | os.setpgrp |
| 6160 | |
| 6161 | Make the current process the leader of its process group. |
| 6162 | [clinic start generated code]*/ |
| 6163 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6164 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6165 | os_setpgrp_impl(PyObject *module) |
| 6166 | /*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6167 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6168 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6169 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6170 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6171 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6172 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6173 | return posix_error(); |
| 6174 | Py_INCREF(Py_None); |
| 6175 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6176 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6177 | #endif /* HAVE_SETPGRP */ |
| 6178 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6179 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6180 | |
| 6181 | #ifdef MS_WINDOWS |
| 6182 | #include <tlhelp32.h> |
| 6183 | |
| 6184 | static PyObject* |
| 6185 | win32_getppid() |
| 6186 | { |
| 6187 | HANDLE snapshot; |
| 6188 | pid_t mypid; |
| 6189 | PyObject* result = NULL; |
| 6190 | BOOL have_record; |
| 6191 | PROCESSENTRY32 pe; |
| 6192 | |
| 6193 | mypid = getpid(); /* This function never fails */ |
| 6194 | |
| 6195 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 6196 | if (snapshot == INVALID_HANDLE_VALUE) |
| 6197 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 6198 | |
| 6199 | pe.dwSize = sizeof(pe); |
| 6200 | have_record = Process32First(snapshot, &pe); |
| 6201 | while (have_record) { |
| 6202 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 6203 | /* We could cache the ulong value in a static variable. */ |
| 6204 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 6205 | break; |
| 6206 | } |
| 6207 | |
| 6208 | have_record = Process32Next(snapshot, &pe); |
| 6209 | } |
| 6210 | |
| 6211 | /* If our loop exits and our pid was not found (result will be NULL) |
| 6212 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 6213 | * error anyway, so let's raise it. */ |
| 6214 | if (!result) |
| 6215 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6216 | |
| 6217 | CloseHandle(snapshot); |
| 6218 | |
| 6219 | return result; |
| 6220 | } |
| 6221 | #endif /*MS_WINDOWS*/ |
| 6222 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6223 | |
| 6224 | /*[clinic input] |
| 6225 | os.getppid |
| 6226 | |
| 6227 | Return the parent's process id. |
| 6228 | |
| 6229 | If the parent process has already exited, Windows machines will still |
| 6230 | return its id; others systems will return the id of the 'init' process (1). |
| 6231 | [clinic start generated code]*/ |
| 6232 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6233 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6234 | os_getppid_impl(PyObject *module) |
| 6235 | /*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6236 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6237 | #ifdef MS_WINDOWS |
| 6238 | return win32_getppid(); |
| 6239 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6240 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6241 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6242 | } |
| 6243 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6244 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6245 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6246 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6247 | /*[clinic input] |
| 6248 | os.getlogin |
| 6249 | |
| 6250 | Return the actual login name. |
| 6251 | [clinic start generated code]*/ |
| 6252 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6253 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6254 | os_getlogin_impl(PyObject *module) |
| 6255 | /*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6256 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6257 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6258 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6259 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6260 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6261 | |
| 6262 | if (GetUserNameW(user_name, &num_chars)) { |
| 6263 | /* num_chars is the number of unicode chars plus null terminator */ |
| 6264 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6265 | } |
| 6266 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6267 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6268 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6269 | char *name; |
| 6270 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6271 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6272 | errno = 0; |
| 6273 | name = getlogin(); |
| 6274 | if (name == NULL) { |
| 6275 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6276 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6277 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6278 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6279 | } |
| 6280 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6281 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6282 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6283 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6284 | return result; |
| 6285 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6286 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6287 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6288 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6289 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6290 | /*[clinic input] |
| 6291 | os.getuid |
| 6292 | |
| 6293 | Return the current process's user id. |
| 6294 | [clinic start generated code]*/ |
| 6295 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6296 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6297 | os_getuid_impl(PyObject *module) |
| 6298 | /*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6299 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6300 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6301 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6302 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6303 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6304 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6305 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6306 | #define HAVE_KILL |
| 6307 | #endif /* MS_WINDOWS */ |
| 6308 | |
| 6309 | #ifdef HAVE_KILL |
| 6310 | /*[clinic input] |
| 6311 | os.kill |
| 6312 | |
| 6313 | pid: pid_t |
| 6314 | signal: Py_ssize_t |
| 6315 | / |
| 6316 | |
| 6317 | Kill a process with a signal. |
| 6318 | [clinic start generated code]*/ |
| 6319 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6320 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6321 | os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal) |
| 6322 | /*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6323 | #ifndef MS_WINDOWS |
| 6324 | { |
| 6325 | if (kill(pid, (int)signal) == -1) |
| 6326 | return posix_error(); |
| 6327 | Py_RETURN_NONE; |
| 6328 | } |
| 6329 | #else /* !MS_WINDOWS */ |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6330 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 6331 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6332 | DWORD sig = (DWORD)signal; |
| 6333 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6334 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6335 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6336 | /* Console processes which share a common console can be sent CTRL+C or |
| 6337 | CTRL+BREAK events, provided they handle said events. */ |
| 6338 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6339 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6340 | err = GetLastError(); |
| 6341 | PyErr_SetFromWindowsErr(err); |
| 6342 | } |
| 6343 | else |
| 6344 | Py_RETURN_NONE; |
| 6345 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6346 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6347 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 6348 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6349 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6350 | if (handle == NULL) { |
| 6351 | err = GetLastError(); |
| 6352 | return PyErr_SetFromWindowsErr(err); |
| 6353 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6354 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6355 | if (TerminateProcess(handle, sig) == 0) { |
| 6356 | err = GetLastError(); |
| 6357 | result = PyErr_SetFromWindowsErr(err); |
| 6358 | } else { |
| 6359 | Py_INCREF(Py_None); |
| 6360 | result = Py_None; |
| 6361 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6362 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6363 | CloseHandle(handle); |
| 6364 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6365 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6366 | #endif /* !MS_WINDOWS */ |
| 6367 | #endif /* HAVE_KILL */ |
| 6368 | |
| 6369 | |
| 6370 | #ifdef HAVE_KILLPG |
| 6371 | /*[clinic input] |
| 6372 | os.killpg |
| 6373 | |
| 6374 | pgid: pid_t |
| 6375 | signal: int |
| 6376 | / |
| 6377 | |
| 6378 | Kill a process group with a signal. |
| 6379 | [clinic start generated code]*/ |
| 6380 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6381 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6382 | os_killpg_impl(PyObject *module, pid_t pgid, int signal) |
| 6383 | /*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6384 | { |
| 6385 | /* XXX some man pages make the `pgid` parameter an int, others |
| 6386 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 6387 | take the same type. Moreover, pid_t is always at least as wide as |
| 6388 | int (else compilation of this module fails), which is safe. */ |
| 6389 | if (killpg(pgid, signal) == -1) |
| 6390 | return posix_error(); |
| 6391 | Py_RETURN_NONE; |
| 6392 | } |
| 6393 | #endif /* HAVE_KILLPG */ |
| 6394 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6395 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6396 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6397 | #ifdef HAVE_SYS_LOCK_H |
| 6398 | #include <sys/lock.h> |
| 6399 | #endif |
| 6400 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6401 | /*[clinic input] |
| 6402 | os.plock |
| 6403 | op: int |
| 6404 | / |
| 6405 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6406 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6407 | [clinic start generated code]*/ |
| 6408 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6409 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6410 | os_plock_impl(PyObject *module, int op) |
| 6411 | /*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6412 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6413 | if (plock(op) == -1) |
| 6414 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6415 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6416 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6417 | #endif /* HAVE_PLOCK */ |
| 6418 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6419 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6420 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6421 | /*[clinic input] |
| 6422 | os.setuid |
| 6423 | |
| 6424 | uid: uid_t |
| 6425 | / |
| 6426 | |
| 6427 | Set the current process's user id. |
| 6428 | [clinic start generated code]*/ |
| 6429 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6430 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6431 | os_setuid_impl(PyObject *module, uid_t uid) |
| 6432 | /*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6433 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6434 | if (setuid(uid) < 0) |
| 6435 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6436 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6437 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6438 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6439 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6440 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6441 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6442 | /*[clinic input] |
| 6443 | os.seteuid |
| 6444 | |
| 6445 | euid: uid_t |
| 6446 | / |
| 6447 | |
| 6448 | Set the current process's effective user id. |
| 6449 | [clinic start generated code]*/ |
| 6450 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6451 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6452 | os_seteuid_impl(PyObject *module, uid_t euid) |
| 6453 | /*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6454 | { |
| 6455 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6456 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6457 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6458 | } |
| 6459 | #endif /* HAVE_SETEUID */ |
| 6460 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6461 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6462 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6463 | /*[clinic input] |
| 6464 | os.setegid |
| 6465 | |
| 6466 | egid: gid_t |
| 6467 | / |
| 6468 | |
| 6469 | Set the current process's effective group id. |
| 6470 | [clinic start generated code]*/ |
| 6471 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6472 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6473 | os_setegid_impl(PyObject *module, gid_t egid) |
| 6474 | /*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6475 | { |
| 6476 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6477 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6478 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6479 | } |
| 6480 | #endif /* HAVE_SETEGID */ |
| 6481 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6482 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6483 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6484 | /*[clinic input] |
| 6485 | os.setreuid |
| 6486 | |
| 6487 | ruid: uid_t |
| 6488 | euid: uid_t |
| 6489 | / |
| 6490 | |
| 6491 | Set the current process's real and effective user ids. |
| 6492 | [clinic start generated code]*/ |
| 6493 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6494 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6495 | os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid) |
| 6496 | /*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6497 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6498 | if (setreuid(ruid, euid) < 0) { |
| 6499 | return posix_error(); |
| 6500 | } else { |
| 6501 | Py_INCREF(Py_None); |
| 6502 | return Py_None; |
| 6503 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6504 | } |
| 6505 | #endif /* HAVE_SETREUID */ |
| 6506 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6507 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6508 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6509 | /*[clinic input] |
| 6510 | os.setregid |
| 6511 | |
| 6512 | rgid: gid_t |
| 6513 | egid: gid_t |
| 6514 | / |
| 6515 | |
| 6516 | Set the current process's real and effective group ids. |
| 6517 | [clinic start generated code]*/ |
| 6518 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6519 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6520 | os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid) |
| 6521 | /*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6522 | { |
| 6523 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6524 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6525 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6526 | } |
| 6527 | #endif /* HAVE_SETREGID */ |
| 6528 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6529 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6530 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6531 | /*[clinic input] |
| 6532 | os.setgid |
| 6533 | gid: gid_t |
| 6534 | / |
| 6535 | |
| 6536 | Set the current process's group id. |
| 6537 | [clinic start generated code]*/ |
| 6538 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6539 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6540 | os_setgid_impl(PyObject *module, gid_t gid) |
| 6541 | /*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6542 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6543 | if (setgid(gid) < 0) |
| 6544 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6545 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6546 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6547 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6548 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6549 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6550 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6551 | /*[clinic input] |
| 6552 | os.setgroups |
| 6553 | |
| 6554 | groups: object |
| 6555 | / |
| 6556 | |
| 6557 | Set the groups of the current process to list. |
| 6558 | [clinic start generated code]*/ |
| 6559 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6560 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6561 | os_setgroups(PyObject *module, PyObject *groups) |
| 6562 | /*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6563 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6564 | int i, len; |
| 6565 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6566 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6567 | if (!PySequence_Check(groups)) { |
| 6568 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6569 | return NULL; |
| 6570 | } |
| 6571 | len = PySequence_Size(groups); |
| 6572 | if (len > MAX_GROUPS) { |
| 6573 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6574 | return NULL; |
| 6575 | } |
| 6576 | for(i = 0; i < len; i++) { |
| 6577 | PyObject *elem; |
| 6578 | elem = PySequence_GetItem(groups, i); |
| 6579 | if (!elem) |
| 6580 | return NULL; |
| 6581 | if (!PyLong_Check(elem)) { |
| 6582 | PyErr_SetString(PyExc_TypeError, |
| 6583 | "groups must be integers"); |
| 6584 | Py_DECREF(elem); |
| 6585 | return NULL; |
| 6586 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6587 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6588 | Py_DECREF(elem); |
| 6589 | return NULL; |
| 6590 | } |
| 6591 | } |
| 6592 | Py_DECREF(elem); |
| 6593 | } |
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 | if (setgroups(len, grouplist) < 0) |
| 6596 | return posix_error(); |
| 6597 | Py_INCREF(Py_None); |
| 6598 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6599 | } |
| 6600 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6601 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6602 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6603 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6604 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6605 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6606 | PyObject *result; |
| 6607 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6608 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6609 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6610 | if (pid == -1) |
| 6611 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6612 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6613 | if (struct_rusage == NULL) { |
| 6614 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6615 | if (m == NULL) |
| 6616 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6617 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6618 | Py_DECREF(m); |
| 6619 | if (struct_rusage == NULL) |
| 6620 | return NULL; |
| 6621 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6622 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6623 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6624 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6625 | if (!result) |
| 6626 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6627 | |
| 6628 | #ifndef doubletime |
| 6629 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6630 | #endif |
| 6631 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6632 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6633 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6634 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6635 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6636 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6637 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6638 | SET_INT(result, 2, ru->ru_maxrss); |
| 6639 | SET_INT(result, 3, ru->ru_ixrss); |
| 6640 | SET_INT(result, 4, ru->ru_idrss); |
| 6641 | SET_INT(result, 5, ru->ru_isrss); |
| 6642 | SET_INT(result, 6, ru->ru_minflt); |
| 6643 | SET_INT(result, 7, ru->ru_majflt); |
| 6644 | SET_INT(result, 8, ru->ru_nswap); |
| 6645 | SET_INT(result, 9, ru->ru_inblock); |
| 6646 | SET_INT(result, 10, ru->ru_oublock); |
| 6647 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6648 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6649 | SET_INT(result, 13, ru->ru_nsignals); |
| 6650 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6651 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6652 | #undef SET_INT |
| 6653 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6654 | if (PyErr_Occurred()) { |
| 6655 | Py_DECREF(result); |
| 6656 | return NULL; |
| 6657 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6658 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6659 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6660 | } |
| 6661 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6662 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6663 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6664 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6665 | /*[clinic input] |
| 6666 | os.wait3 |
| 6667 | |
| 6668 | options: int |
| 6669 | Wait for completion of a child process. |
| 6670 | |
| 6671 | Returns a tuple of information about the child process: |
| 6672 | (pid, status, rusage) |
| 6673 | [clinic start generated code]*/ |
| 6674 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6675 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6676 | os_wait3_impl(PyObject *module, int options) |
| 6677 | /*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6678 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6679 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6680 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6681 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6682 | WAIT_TYPE status; |
| 6683 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6684 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6685 | do { |
| 6686 | Py_BEGIN_ALLOW_THREADS |
| 6687 | pid = wait3(&status, options, &ru); |
| 6688 | Py_END_ALLOW_THREADS |
| 6689 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6690 | if (pid < 0) |
| 6691 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6692 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6693 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6694 | } |
| 6695 | #endif /* HAVE_WAIT3 */ |
| 6696 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6697 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6698 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6699 | /*[clinic input] |
| 6700 | |
| 6701 | os.wait4 |
| 6702 | |
| 6703 | pid: pid_t |
| 6704 | options: int |
| 6705 | |
| 6706 | Wait for completion of a specific child process. |
| 6707 | |
| 6708 | Returns a tuple of information about the child process: |
| 6709 | (pid, status, rusage) |
| 6710 | [clinic start generated code]*/ |
| 6711 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6712 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6713 | os_wait4_impl(PyObject *module, pid_t pid, int options) |
| 6714 | /*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6715 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6716 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6717 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6718 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6719 | WAIT_TYPE status; |
| 6720 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6721 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6722 | do { |
| 6723 | Py_BEGIN_ALLOW_THREADS |
| 6724 | res = wait4(pid, &status, options, &ru); |
| 6725 | Py_END_ALLOW_THREADS |
| 6726 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6727 | if (res < 0) |
| 6728 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6729 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6730 | return wait_helper(res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6731 | } |
| 6732 | #endif /* HAVE_WAIT4 */ |
| 6733 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6734 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6735 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6736 | /*[clinic input] |
| 6737 | os.waitid |
| 6738 | |
| 6739 | idtype: idtype_t |
| 6740 | Must be one of be P_PID, P_PGID or P_ALL. |
| 6741 | id: id_t |
| 6742 | The id to wait on. |
| 6743 | options: int |
| 6744 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 6745 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 6746 | / |
| 6747 | |
| 6748 | Returns the result of waiting for a process or processes. |
| 6749 | |
| 6750 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 6751 | no children in a waitable state. |
| 6752 | [clinic start generated code]*/ |
| 6753 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6754 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6755 | os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options) |
| 6756 | /*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6757 | { |
| 6758 | PyObject *result; |
| 6759 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6760 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6761 | siginfo_t si; |
| 6762 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6763 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6764 | do { |
| 6765 | Py_BEGIN_ALLOW_THREADS |
| 6766 | res = waitid(idtype, id, &si, options); |
| 6767 | Py_END_ALLOW_THREADS |
| 6768 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6769 | if (res < 0) |
| 6770 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6771 | |
| 6772 | if (si.si_pid == 0) |
| 6773 | Py_RETURN_NONE; |
| 6774 | |
| 6775 | result = PyStructSequence_New(&WaitidResultType); |
| 6776 | if (!result) |
| 6777 | return NULL; |
| 6778 | |
| 6779 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6780 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6781 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6782 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6783 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6784 | if (PyErr_Occurred()) { |
| 6785 | Py_DECREF(result); |
| 6786 | return NULL; |
| 6787 | } |
| 6788 | |
| 6789 | return result; |
| 6790 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6791 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6792 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6793 | |
| 6794 | #if defined(HAVE_WAITPID) |
| 6795 | /*[clinic input] |
| 6796 | os.waitpid |
| 6797 | pid: pid_t |
| 6798 | options: int |
| 6799 | / |
| 6800 | |
| 6801 | Wait for completion of a given child process. |
| 6802 | |
| 6803 | Returns a tuple of information regarding the child process: |
| 6804 | (pid, status) |
| 6805 | |
| 6806 | The options argument is ignored on Windows. |
| 6807 | [clinic start generated code]*/ |
| 6808 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6809 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6810 | os_waitpid_impl(PyObject *module, pid_t pid, int options) |
| 6811 | /*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6812 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6813 | pid_t res; |
| 6814 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6815 | WAIT_TYPE status; |
| 6816 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6817 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6818 | do { |
| 6819 | Py_BEGIN_ALLOW_THREADS |
| 6820 | res = waitpid(pid, &status, options); |
| 6821 | Py_END_ALLOW_THREADS |
| 6822 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6823 | if (res < 0) |
| 6824 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6825 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6826 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6827 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6828 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6829 | /* 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] | 6830 | /*[clinic input] |
| 6831 | os.waitpid |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 6832 | pid: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6833 | options: int |
| 6834 | / |
| 6835 | |
| 6836 | Wait for completion of a given process. |
| 6837 | |
| 6838 | Returns a tuple of information regarding the process: |
| 6839 | (pid, status << 8) |
| 6840 | |
| 6841 | The options argument is ignored on Windows. |
| 6842 | [clinic start generated code]*/ |
| 6843 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6844 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 6845 | os_waitpid_impl(PyObject *module, intptr_t pid, int options) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 6846 | /*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6847 | { |
| 6848 | int status; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 6849 | intptr_t res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6850 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6851 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6852 | do { |
| 6853 | Py_BEGIN_ALLOW_THREADS |
| 6854 | res = _cwait(&status, pid, options); |
| 6855 | Py_END_ALLOW_THREADS |
| 6856 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 6857 | if (res < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6858 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6859 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6860 | /* 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] | 6861 | return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6862 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6863 | #endif |
| 6864 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6865 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6866 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6867 | /*[clinic input] |
| 6868 | os.wait |
| 6869 | |
| 6870 | Wait for completion of a child process. |
| 6871 | |
| 6872 | Returns a tuple of information about the child process: |
| 6873 | (pid, status) |
| 6874 | [clinic start generated code]*/ |
| 6875 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6876 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6877 | os_wait_impl(PyObject *module) |
| 6878 | /*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6879 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6880 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6881 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6882 | WAIT_TYPE status; |
| 6883 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6884 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6885 | do { |
| 6886 | Py_BEGIN_ALLOW_THREADS |
| 6887 | pid = wait(&status); |
| 6888 | Py_END_ALLOW_THREADS |
| 6889 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6890 | if (pid < 0) |
| 6891 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6892 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6893 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6894 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6895 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6896 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6897 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6898 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
| 6899 | PyDoc_STRVAR(readlink__doc__, |
| 6900 | "readlink(path, *, dir_fd=None) -> path\n\n\ |
| 6901 | Return a string representing the path to which the symbolic link points.\n\ |
| 6902 | \n\ |
| 6903 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 6904 | and path should be relative; path will then be relative to that directory.\n\ |
| 6905 | dir_fd may not be implemented on your platform.\n\ |
| 6906 | If it is unavailable, using it will raise a NotImplementedError."); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6907 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6908 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6909 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6910 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6911 | /* AC 3.5: merge win32 and not together */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6912 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6913 | posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6914 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6915 | path_t path; |
| 6916 | int dir_fd = DEFAULT_DIR_FD; |
| 6917 | char buffer[MAXPATHLEN]; |
| 6918 | ssize_t length; |
| 6919 | PyObject *return_value = NULL; |
| 6920 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6921 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6922 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 6923 | path.function_name = "readlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6924 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords, |
| 6925 | path_converter, &path, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6926 | READLINKAT_DIR_FD_CONVERTER, &dir_fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6927 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6928 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6929 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6930 | #ifdef HAVE_READLINKAT |
| 6931 | if (dir_fd != DEFAULT_DIR_FD) |
| 6932 | length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer)); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 6933 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6934 | #endif |
| 6935 | length = readlink(path.narrow, buffer, sizeof(buffer)); |
| 6936 | Py_END_ALLOW_THREADS |
| 6937 | |
| 6938 | if (length < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 6939 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6940 | goto exit; |
| 6941 | } |
| 6942 | |
| 6943 | if (PyUnicode_Check(path.object)) |
| 6944 | return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 6945 | else |
| 6946 | return_value = PyBytes_FromStringAndSize(buffer, length); |
| 6947 | exit: |
| 6948 | path_cleanup(&path); |
| 6949 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6950 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6951 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6952 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6953 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6954 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 6955 | |
| 6956 | static PyObject * |
| 6957 | win_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6958 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 6959 | const wchar_t *path; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6960 | DWORD n_bytes_returned; |
| 6961 | DWORD io_result; |
| 6962 | PyObject *po, *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 6963 | int dir_fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6964 | HANDLE reparse_point_handle; |
| 6965 | |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 6966 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 6967 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 6968 | const wchar_t *print_name; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6969 | |
| 6970 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 6971 | |
| 6972 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords, |
| 6973 | &po, |
| 6974 | dir_fd_unavailable, &dir_fd |
| 6975 | )) |
| 6976 | return NULL; |
| 6977 | |
| 6978 | path = PyUnicode_AsUnicode(po); |
| 6979 | if (path == NULL) |
| 6980 | return NULL; |
| 6981 | |
| 6982 | /* First get a handle to the reparse point */ |
| 6983 | Py_BEGIN_ALLOW_THREADS |
| 6984 | reparse_point_handle = CreateFileW( |
| 6985 | path, |
| 6986 | 0, |
| 6987 | 0, |
| 6988 | 0, |
| 6989 | OPEN_EXISTING, |
| 6990 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 6991 | 0); |
| 6992 | Py_END_ALLOW_THREADS |
| 6993 | |
| 6994 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
| 6995 | return win32_error_object("readlink", po); |
| 6996 | |
| 6997 | Py_BEGIN_ALLOW_THREADS |
| 6998 | /* New call DeviceIoControl to read the reparse point */ |
| 6999 | io_result = DeviceIoControl( |
| 7000 | reparse_point_handle, |
| 7001 | FSCTL_GET_REPARSE_POINT, |
| 7002 | 0, 0, /* in buffer */ |
| 7003 | target_buffer, sizeof(target_buffer), |
| 7004 | &n_bytes_returned, |
| 7005 | 0 /* we're not using OVERLAPPED_IO */ |
| 7006 | ); |
| 7007 | CloseHandle(reparse_point_handle); |
| 7008 | Py_END_ALLOW_THREADS |
| 7009 | |
| 7010 | if (io_result==0) |
| 7011 | return win32_error_object("readlink", po); |
| 7012 | |
| 7013 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 7014 | { |
| 7015 | PyErr_SetString(PyExc_ValueError, |
| 7016 | "not a symbolic link"); |
| 7017 | return NULL; |
| 7018 | } |
| 7019 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7020 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 7021 | |
| 7022 | result = PyUnicode_FromWideChar(print_name, |
| 7023 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
| 7024 | return result; |
| 7025 | } |
| 7026 | |
| 7027 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 7028 | |
| 7029 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7030 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7031 | #ifdef HAVE_SYMLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7032 | |
| 7033 | #if defined(MS_WINDOWS) |
| 7034 | |
| 7035 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 7036 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPCWSTR, LPCWSTR, DWORD) = NULL; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7037 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7038 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7039 | check_CreateSymbolicLink(void) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7040 | { |
| 7041 | HINSTANCE hKernel32; |
| 7042 | /* only recheck */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7043 | if (Py_CreateSymbolicLinkW) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7044 | return 1; |
| 7045 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 7046 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 7047 | "CreateSymbolicLinkW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7048 | return Py_CreateSymbolicLinkW != NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7049 | } |
| 7050 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7051 | /* Remove the last portion of the path */ |
| 7052 | static void |
| 7053 | _dirnameW(WCHAR *path) |
| 7054 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7055 | WCHAR *ptr; |
| 7056 | |
| 7057 | /* walk the path from the end until a backslash is encountered */ |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7058 | for(ptr = path + wcslen(path); ptr != path; ptr--) { |
Victor Stinner | 072318b | 2013-06-05 02:07:46 +0200 | [diff] [blame] | 7059 | if (*ptr == L'\\' || *ptr == L'/') |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7060 | break; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7061 | } |
| 7062 | *ptr = 0; |
| 7063 | } |
| 7064 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7065 | /* Is this path absolute? */ |
| 7066 | static int |
| 7067 | _is_absW(const WCHAR *path) |
| 7068 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7069 | return path[0] == L'\\' || path[0] == L'/' || path[1] == L':'; |
| 7070 | |
| 7071 | } |
| 7072 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7073 | /* join root and rest with a backslash */ |
| 7074 | static void |
| 7075 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 7076 | { |
Victor Stinner | e7e7eba | 2013-06-05 00:35:54 +0200 | [diff] [blame] | 7077 | size_t root_len; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7078 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7079 | if (_is_absW(rest)) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7080 | wcscpy(dest_path, rest); |
| 7081 | return; |
| 7082 | } |
| 7083 | |
| 7084 | root_len = wcslen(root); |
| 7085 | |
| 7086 | wcscpy(dest_path, root); |
| 7087 | if(root_len) { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7088 | dest_path[root_len] = L'\\'; |
| 7089 | root_len++; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7090 | } |
| 7091 | wcscpy(dest_path+root_len, rest); |
| 7092 | } |
| 7093 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7094 | /* Return True if the path at src relative to dest is a directory */ |
| 7095 | static int |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 7096 | _check_dirW(LPCWSTR src, LPCWSTR dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7097 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7098 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7099 | WCHAR dest_parent[MAX_PATH]; |
| 7100 | WCHAR src_resolved[MAX_PATH] = L""; |
| 7101 | |
| 7102 | /* dest_parent = os.path.dirname(dest) */ |
| 7103 | wcscpy(dest_parent, dest); |
| 7104 | _dirnameW(dest_parent); |
| 7105 | /* src_resolved = os.path.join(dest_parent, src) */ |
| 7106 | _joinW(src_resolved, dest_parent, src); |
| 7107 | return ( |
| 7108 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 7109 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7110 | ); |
| 7111 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7112 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7113 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7114 | |
| 7115 | /*[clinic input] |
| 7116 | os.symlink |
| 7117 | src: path_t |
| 7118 | dst: path_t |
| 7119 | target_is_directory: bool = False |
| 7120 | * |
| 7121 | dir_fd: dir_fd(requires='symlinkat')=None |
| 7122 | |
| 7123 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 7124 | |
| 7125 | Create a symbolic link pointing to src named dst. |
| 7126 | |
| 7127 | target_is_directory is required on Windows if the target is to be |
| 7128 | interpreted as a directory. (On Windows, symlink requires |
| 7129 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 7130 | target_is_directory is ignored on non-Windows platforms. |
| 7131 | |
| 7132 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7133 | and path should be relative; path will then be relative to that directory. |
| 7134 | dir_fd may not be implemented on your platform. |
| 7135 | If it is unavailable, using it will raise a NotImplementedError. |
| 7136 | |
| 7137 | [clinic start generated code]*/ |
| 7138 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7139 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7140 | os_symlink_impl(PyObject *module, path_t *src, path_t *dst, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7141 | int target_is_directory, int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7142 | /*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7143 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7144 | #ifdef MS_WINDOWS |
| 7145 | DWORD result; |
| 7146 | #else |
| 7147 | int result; |
| 7148 | #endif |
| 7149 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7150 | #ifdef MS_WINDOWS |
| 7151 | if (!check_CreateSymbolicLink()) { |
| 7152 | PyErr_SetString(PyExc_NotImplementedError, |
| 7153 | "CreateSymbolicLink functions not found"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7154 | return NULL; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7155 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7156 | if (!win32_can_symlink) { |
| 7157 | PyErr_SetString(PyExc_OSError, "symbolic link privilege not held"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7158 | return NULL; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7159 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7160 | #endif |
| 7161 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7162 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7163 | PyErr_SetString(PyExc_ValueError, |
| 7164 | "symlink: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7165 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7166 | } |
| 7167 | |
| 7168 | #ifdef MS_WINDOWS |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7169 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7170 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7171 | /* if src is a directory, ensure target_is_directory==1 */ |
| 7172 | target_is_directory |= _check_dirW(src->wide, dst->wide); |
| 7173 | result = Py_CreateSymbolicLinkW(dst->wide, src->wide, |
| 7174 | target_is_directory); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7175 | Py_END_ALLOW_THREADS |
| 7176 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7177 | if (!result) |
| 7178 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7179 | |
| 7180 | #else |
| 7181 | |
| 7182 | Py_BEGIN_ALLOW_THREADS |
| 7183 | #if HAVE_SYMLINKAT |
| 7184 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7185 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7186 | else |
| 7187 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7188 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7189 | Py_END_ALLOW_THREADS |
| 7190 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7191 | if (result) |
| 7192 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7193 | #endif |
| 7194 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7195 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7196 | } |
| 7197 | #endif /* HAVE_SYMLINK */ |
| 7198 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7199 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7200 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7201 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7202 | static PyStructSequence_Field times_result_fields[] = { |
| 7203 | {"user", "user time"}, |
| 7204 | {"system", "system time"}, |
| 7205 | {"children_user", "user time of children"}, |
| 7206 | {"children_system", "system time of children"}, |
| 7207 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 7208 | {NULL} |
| 7209 | }; |
| 7210 | |
| 7211 | PyDoc_STRVAR(times_result__doc__, |
| 7212 | "times_result: Result from os.times().\n\n\ |
| 7213 | This object may be accessed either as a tuple of\n\ |
| 7214 | (user, system, children_user, children_system, elapsed),\n\ |
| 7215 | or via the attributes user, system, children_user, children_system,\n\ |
| 7216 | and elapsed.\n\ |
| 7217 | \n\ |
| 7218 | See os.times for more information."); |
| 7219 | |
| 7220 | static PyStructSequence_Desc times_result_desc = { |
| 7221 | "times_result", /* name */ |
| 7222 | times_result__doc__, /* doc */ |
| 7223 | times_result_fields, |
| 7224 | 5 |
| 7225 | }; |
| 7226 | |
| 7227 | static PyTypeObject TimesResultType; |
| 7228 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7229 | #ifdef MS_WINDOWS |
| 7230 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 7231 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7232 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7233 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7234 | |
| 7235 | static PyObject * |
| 7236 | build_times_result(double user, double system, |
| 7237 | double children_user, double children_system, |
| 7238 | double elapsed) |
| 7239 | { |
| 7240 | PyObject *value = PyStructSequence_New(&TimesResultType); |
| 7241 | if (value == NULL) |
| 7242 | return NULL; |
| 7243 | |
| 7244 | #define SET(i, field) \ |
| 7245 | { \ |
| 7246 | PyObject *o = PyFloat_FromDouble(field); \ |
| 7247 | if (!o) { \ |
| 7248 | Py_DECREF(value); \ |
| 7249 | return NULL; \ |
| 7250 | } \ |
| 7251 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 7252 | } \ |
| 7253 | |
| 7254 | SET(0, user); |
| 7255 | SET(1, system); |
| 7256 | SET(2, children_user); |
| 7257 | SET(3, children_system); |
| 7258 | SET(4, elapsed); |
| 7259 | |
| 7260 | #undef SET |
| 7261 | |
| 7262 | return value; |
| 7263 | } |
| 7264 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7265 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7266 | #ifndef MS_WINDOWS |
| 7267 | #define NEED_TICKS_PER_SECOND |
| 7268 | static long ticks_per_second = -1; |
| 7269 | #endif /* MS_WINDOWS */ |
| 7270 | |
| 7271 | /*[clinic input] |
| 7272 | os.times |
| 7273 | |
| 7274 | Return a collection containing process timing information. |
| 7275 | |
| 7276 | The object returned behaves like a named tuple with these fields: |
| 7277 | (utime, stime, cutime, cstime, elapsed_time) |
| 7278 | All fields are floating point numbers. |
| 7279 | [clinic start generated code]*/ |
| 7280 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7281 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7282 | os_times_impl(PyObject *module) |
| 7283 | /*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7284 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7285 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7286 | FILETIME create, exit, kernel, user; |
| 7287 | HANDLE hProc; |
| 7288 | hProc = GetCurrentProcess(); |
| 7289 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 7290 | /* The fields of a FILETIME structure are the hi and lo part |
| 7291 | of a 64-bit value expressed in 100 nanosecond units. |
| 7292 | 1e7 is one second in such units; 1e-7 the inverse. |
| 7293 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 7294 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7295 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7296 | (double)(user.dwHighDateTime*429.4967296 + |
| 7297 | user.dwLowDateTime*1e-7), |
| 7298 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 7299 | kernel.dwLowDateTime*1e-7), |
| 7300 | (double)0, |
| 7301 | (double)0, |
| 7302 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7303 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7304 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7305 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7306 | |
| 7307 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7308 | struct tms t; |
| 7309 | clock_t c; |
| 7310 | errno = 0; |
| 7311 | c = times(&t); |
| 7312 | if (c == (clock_t) -1) |
| 7313 | return posix_error(); |
| 7314 | return build_times_result( |
| 7315 | (double)t.tms_utime / ticks_per_second, |
| 7316 | (double)t.tms_stime / ticks_per_second, |
| 7317 | (double)t.tms_cutime / ticks_per_second, |
| 7318 | (double)t.tms_cstime / ticks_per_second, |
| 7319 | (double)c / ticks_per_second); |
| 7320 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7321 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7322 | #endif /* HAVE_TIMES */ |
| 7323 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7324 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7325 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7326 | /*[clinic input] |
| 7327 | os.getsid |
| 7328 | |
| 7329 | pid: pid_t |
| 7330 | / |
| 7331 | |
| 7332 | Call the system call getsid(pid) and return the result. |
| 7333 | [clinic start generated code]*/ |
| 7334 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7335 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7336 | os_getsid_impl(PyObject *module, pid_t pid) |
| 7337 | /*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7338 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7339 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7340 | sid = getsid(pid); |
| 7341 | if (sid < 0) |
| 7342 | return posix_error(); |
| 7343 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7344 | } |
| 7345 | #endif /* HAVE_GETSID */ |
| 7346 | |
| 7347 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7348 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7349 | /*[clinic input] |
| 7350 | os.setsid |
| 7351 | |
| 7352 | Call the system call setsid(). |
| 7353 | [clinic start generated code]*/ |
| 7354 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7355 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7356 | os_setsid_impl(PyObject *module) |
| 7357 | /*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7358 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7359 | if (setsid() < 0) |
| 7360 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7361 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7362 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7363 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7364 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7365 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7366 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7367 | /*[clinic input] |
| 7368 | os.setpgid |
| 7369 | |
| 7370 | pid: pid_t |
| 7371 | pgrp: pid_t |
| 7372 | / |
| 7373 | |
| 7374 | Call the system call setpgid(pid, pgrp). |
| 7375 | [clinic start generated code]*/ |
| 7376 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7377 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7378 | os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp) |
| 7379 | /*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7380 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7381 | if (setpgid(pid, pgrp) < 0) |
| 7382 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7383 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7384 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7385 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7386 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7387 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7388 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7389 | /*[clinic input] |
| 7390 | os.tcgetpgrp |
| 7391 | |
| 7392 | fd: int |
| 7393 | / |
| 7394 | |
| 7395 | Return the process group associated with the terminal specified by fd. |
| 7396 | [clinic start generated code]*/ |
| 7397 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7398 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7399 | os_tcgetpgrp_impl(PyObject *module, int fd) |
| 7400 | /*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7401 | { |
| 7402 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7403 | if (pgid < 0) |
| 7404 | return posix_error(); |
| 7405 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7406 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7407 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7408 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7409 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7410 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7411 | /*[clinic input] |
| 7412 | os.tcsetpgrp |
| 7413 | |
| 7414 | fd: int |
| 7415 | pgid: pid_t |
| 7416 | / |
| 7417 | |
| 7418 | Set the process group associated with the terminal specified by fd. |
| 7419 | [clinic start generated code]*/ |
| 7420 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7421 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7422 | os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid) |
| 7423 | /*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7424 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7425 | if (tcsetpgrp(fd, pgid) < 0) |
| 7426 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7427 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7428 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7429 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7430 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7431 | /* Functions acting on file descriptors */ |
| 7432 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7433 | #ifdef O_CLOEXEC |
| 7434 | extern int _Py_open_cloexec_works; |
| 7435 | #endif |
| 7436 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7437 | |
| 7438 | /*[clinic input] |
| 7439 | os.open -> int |
| 7440 | path: path_t |
| 7441 | flags: int |
| 7442 | mode: int = 0o777 |
| 7443 | * |
| 7444 | dir_fd: dir_fd(requires='openat') = None |
| 7445 | |
| 7446 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 7447 | |
| 7448 | Open a file for low level IO. Returns a file descriptor (integer). |
| 7449 | |
| 7450 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7451 | and path should be relative; path will then be relative to that directory. |
| 7452 | dir_fd may not be implemented on your platform. |
| 7453 | If it is unavailable, using it will raise a NotImplementedError. |
| 7454 | [clinic start generated code]*/ |
| 7455 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7456 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7457 | os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd) |
| 7458 | /*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7459 | { |
| 7460 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7461 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7462 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7463 | #ifdef O_CLOEXEC |
| 7464 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 7465 | #elif !defined(MS_WINDOWS) |
| 7466 | int *atomic_flag_works = NULL; |
| 7467 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7468 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7469 | #ifdef MS_WINDOWS |
| 7470 | flags |= O_NOINHERIT; |
| 7471 | #elif defined(O_CLOEXEC) |
| 7472 | flags |= O_CLOEXEC; |
| 7473 | #endif |
| 7474 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7475 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7476 | do { |
| 7477 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7478 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7479 | fd = _wopen(path->wide, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 7480 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7481 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7482 | if (dir_fd != DEFAULT_DIR_FD) |
| 7483 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 7484 | else |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 7485 | #endif /* HAVE_OPENAT */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7486 | fd = open(path->narrow, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 7487 | #endif /* !MS_WINDOWS */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7488 | Py_END_ALLOW_THREADS |
| 7489 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7490 | _Py_END_SUPPRESS_IPH |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7491 | |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 7492 | if (fd < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7493 | if (!async_err) |
| 7494 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7495 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7496 | } |
| 7497 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7498 | #ifndef MS_WINDOWS |
| 7499 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 7500 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7501 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7502 | } |
| 7503 | #endif |
| 7504 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7505 | return fd; |
| 7506 | } |
| 7507 | |
| 7508 | |
| 7509 | /*[clinic input] |
| 7510 | os.close |
| 7511 | |
| 7512 | fd: int |
| 7513 | |
| 7514 | Close a file descriptor. |
| 7515 | [clinic start generated code]*/ |
| 7516 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7517 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7518 | os_close_impl(PyObject *module, int fd) |
| 7519 | /*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7520 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7521 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7522 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 7523 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 7524 | * for more details. |
| 7525 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7526 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7527 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7528 | res = close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7529 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7530 | Py_END_ALLOW_THREADS |
| 7531 | if (res < 0) |
| 7532 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7533 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7534 | } |
| 7535 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7536 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7537 | /*[clinic input] |
| 7538 | os.closerange |
| 7539 | |
| 7540 | fd_low: int |
| 7541 | fd_high: int |
| 7542 | / |
| 7543 | |
| 7544 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 7545 | [clinic start generated code]*/ |
| 7546 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7547 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7548 | os_closerange_impl(PyObject *module, int fd_low, int fd_high) |
| 7549 | /*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7550 | { |
| 7551 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7552 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7553 | _Py_BEGIN_SUPPRESS_IPH |
Benjamin Peterson | 207116b | 2016-09-08 11:28:06 -0700 | [diff] [blame] | 7554 | for (i = Py_MAX(fd_low, 0); i < fd_high; i++) |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 7555 | close(i); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7556 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7557 | Py_END_ALLOW_THREADS |
| 7558 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7559 | } |
| 7560 | |
| 7561 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7562 | /*[clinic input] |
| 7563 | os.dup -> int |
| 7564 | |
| 7565 | fd: int |
| 7566 | / |
| 7567 | |
| 7568 | Return a duplicate of a file descriptor. |
| 7569 | [clinic start generated code]*/ |
| 7570 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7571 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7572 | os_dup_impl(PyObject *module, int fd) |
| 7573 | /*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7574 | { |
| 7575 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7576 | } |
| 7577 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7578 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7579 | /*[clinic input] |
| 7580 | os.dup2 |
| 7581 | fd: int |
| 7582 | fd2: int |
| 7583 | inheritable: bool=True |
| 7584 | |
| 7585 | Duplicate file descriptor. |
| 7586 | [clinic start generated code]*/ |
| 7587 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7588 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7589 | os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) |
| 7590 | /*[clinic end generated code: output=db832a2d872ccc5f input=76e96f511be0352f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7591 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7592 | int res; |
| 7593 | #if defined(HAVE_DUP3) && \ |
| 7594 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 7595 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
| 7596 | int dup3_works = -1; |
| 7597 | #endif |
| 7598 | |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 7599 | if (fd < 0 || fd2 < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7600 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7601 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7602 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 7603 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 7604 | * upon close(), and therefore below. |
| 7605 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7606 | #ifdef MS_WINDOWS |
| 7607 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7608 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7609 | res = dup2(fd, fd2); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7610 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7611 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7612 | if (res < 0) |
| 7613 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7614 | |
| 7615 | /* Character files like console cannot be make non-inheritable */ |
| 7616 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7617 | close(fd2); |
| 7618 | return NULL; |
| 7619 | } |
| 7620 | |
| 7621 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 7622 | Py_BEGIN_ALLOW_THREADS |
| 7623 | if (!inheritable) |
| 7624 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 7625 | else |
| 7626 | res = dup2(fd, fd2); |
| 7627 | Py_END_ALLOW_THREADS |
| 7628 | if (res < 0) |
| 7629 | return posix_error(); |
| 7630 | |
| 7631 | #else |
| 7632 | |
| 7633 | #ifdef HAVE_DUP3 |
| 7634 | if (!inheritable && dup3_works != 0) { |
| 7635 | Py_BEGIN_ALLOW_THREADS |
| 7636 | res = dup3(fd, fd2, O_CLOEXEC); |
| 7637 | Py_END_ALLOW_THREADS |
| 7638 | if (res < 0) { |
| 7639 | if (dup3_works == -1) |
| 7640 | dup3_works = (errno != ENOSYS); |
| 7641 | if (dup3_works) |
| 7642 | return posix_error(); |
| 7643 | } |
| 7644 | } |
| 7645 | |
| 7646 | if (inheritable || dup3_works == 0) |
| 7647 | { |
| 7648 | #endif |
| 7649 | Py_BEGIN_ALLOW_THREADS |
| 7650 | res = dup2(fd, fd2); |
| 7651 | Py_END_ALLOW_THREADS |
| 7652 | if (res < 0) |
| 7653 | return posix_error(); |
| 7654 | |
| 7655 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7656 | close(fd2); |
| 7657 | return NULL; |
| 7658 | } |
| 7659 | #ifdef HAVE_DUP3 |
| 7660 | } |
| 7661 | #endif |
| 7662 | |
| 7663 | #endif |
| 7664 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7665 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7666 | } |
| 7667 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7668 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7669 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7670 | /*[clinic input] |
| 7671 | os.lockf |
| 7672 | |
| 7673 | fd: int |
| 7674 | An open file descriptor. |
| 7675 | command: int |
| 7676 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 7677 | length: Py_off_t |
| 7678 | The number of bytes to lock, starting at the current position. |
| 7679 | / |
| 7680 | |
| 7681 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 7682 | |
| 7683 | [clinic start generated code]*/ |
| 7684 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7685 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7686 | os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length) |
| 7687 | /*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7688 | { |
| 7689 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7690 | |
| 7691 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7692 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7693 | Py_END_ALLOW_THREADS |
| 7694 | |
| 7695 | if (res < 0) |
| 7696 | return posix_error(); |
| 7697 | |
| 7698 | Py_RETURN_NONE; |
| 7699 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7700 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7701 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7702 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7703 | /*[clinic input] |
| 7704 | os.lseek -> Py_off_t |
| 7705 | |
| 7706 | fd: int |
| 7707 | position: Py_off_t |
| 7708 | how: int |
| 7709 | / |
| 7710 | |
| 7711 | Set the position of a file descriptor. Return the new position. |
| 7712 | |
| 7713 | Return the new cursor position in number of bytes |
| 7714 | relative to the beginning of the file. |
| 7715 | [clinic start generated code]*/ |
| 7716 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7717 | static Py_off_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7718 | os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how) |
| 7719 | /*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7720 | { |
| 7721 | Py_off_t result; |
| 7722 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7723 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7724 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 7725 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7726 | case 0: how = SEEK_SET; break; |
| 7727 | case 1: how = SEEK_CUR; break; |
| 7728 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7729 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7730 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7731 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7732 | if (PyErr_Occurred()) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7733 | return -1; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7734 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7735 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7736 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 7737 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7738 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7739 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7740 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7741 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7742 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7743 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7744 | if (result < 0) |
| 7745 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7746 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7747 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7748 | } |
| 7749 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7750 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7751 | /*[clinic input] |
| 7752 | os.read |
| 7753 | fd: int |
| 7754 | length: Py_ssize_t |
| 7755 | / |
| 7756 | |
| 7757 | Read from a file descriptor. Returns a bytes object. |
| 7758 | [clinic start generated code]*/ |
| 7759 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7760 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7761 | os_read_impl(PyObject *module, int fd, Py_ssize_t length) |
| 7762 | /*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7763 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7764 | Py_ssize_t n; |
| 7765 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7766 | |
| 7767 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7768 | errno = EINVAL; |
| 7769 | return posix_error(); |
| 7770 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7771 | |
| 7772 | #ifdef MS_WINDOWS |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 7773 | /* On Windows, the count parameter of read() is an int */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7774 | if (length > INT_MAX) |
| 7775 | length = INT_MAX; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7776 | #endif |
| 7777 | |
| 7778 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7779 | if (buffer == NULL) |
| 7780 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7781 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 7782 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 7783 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7784 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 7785 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7786 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7787 | |
| 7788 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7789 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7790 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7791 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7792 | } |
| 7793 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7794 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 7795 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7796 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7797 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 7798 | { |
| 7799 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7800 | Py_ssize_t blen, total = 0; |
| 7801 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7802 | *iov = PyMem_New(struct iovec, cnt); |
| 7803 | if (*iov == NULL) { |
| 7804 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 7805 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7806 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7807 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7808 | *buf = PyMem_New(Py_buffer, cnt); |
| 7809 | if (*buf == NULL) { |
| 7810 | PyMem_Del(*iov); |
| 7811 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 7812 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7813 | } |
| 7814 | |
| 7815 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7816 | PyObject *item = PySequence_GetItem(seq, i); |
| 7817 | if (item == NULL) |
| 7818 | goto fail; |
| 7819 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 7820 | Py_DECREF(item); |
| 7821 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7822 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7823 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7824 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7825 | blen = (*buf)[i].len; |
| 7826 | (*iov)[i].iov_len = blen; |
| 7827 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7828 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7829 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7830 | |
| 7831 | fail: |
| 7832 | PyMem_Del(*iov); |
| 7833 | for (j = 0; j < i; j++) { |
| 7834 | PyBuffer_Release(&(*buf)[j]); |
| 7835 | } |
| 7836 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 7837 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7838 | } |
| 7839 | |
| 7840 | static void |
| 7841 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 7842 | { |
| 7843 | int i; |
| 7844 | PyMem_Del(iov); |
| 7845 | for (i = 0; i < cnt; i++) { |
| 7846 | PyBuffer_Release(&buf[i]); |
| 7847 | } |
| 7848 | PyMem_Del(buf); |
| 7849 | } |
| 7850 | #endif |
| 7851 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7852 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7853 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7854 | /*[clinic input] |
| 7855 | os.readv -> Py_ssize_t |
| 7856 | |
| 7857 | fd: int |
| 7858 | buffers: object |
| 7859 | / |
| 7860 | |
| 7861 | Read from a file descriptor fd into an iterable of buffers. |
| 7862 | |
| 7863 | The buffers should be mutable buffers accepting bytes. |
| 7864 | readv will transfer data into each buffer until it is full |
| 7865 | and then move on to the next buffer in the sequence to hold |
| 7866 | the rest of the data. |
| 7867 | |
| 7868 | readv returns the total number of bytes read, |
| 7869 | which may be less than the total capacity of all the buffers. |
| 7870 | [clinic start generated code]*/ |
| 7871 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7872 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7873 | os_readv_impl(PyObject *module, int fd, PyObject *buffers) |
| 7874 | /*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7875 | { |
| 7876 | int cnt; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7877 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7878 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7879 | struct iovec *iov; |
| 7880 | Py_buffer *buf; |
| 7881 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7882 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7883 | PyErr_SetString(PyExc_TypeError, |
| 7884 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7885 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7886 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7887 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7888 | cnt = PySequence_Size(buffers); |
| 7889 | |
| 7890 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 7891 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7892 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7893 | do { |
| 7894 | Py_BEGIN_ALLOW_THREADS |
| 7895 | n = readv(fd, iov, cnt); |
| 7896 | Py_END_ALLOW_THREADS |
| 7897 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7898 | |
| 7899 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7900 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7901 | if (!async_err) |
| 7902 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7903 | return -1; |
| 7904 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 7905 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7906 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7907 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7908 | #endif /* HAVE_READV */ |
| 7909 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7910 | |
| 7911 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7912 | /*[clinic input] |
| 7913 | # TODO length should be size_t! but Python doesn't support parsing size_t yet. |
| 7914 | os.pread |
| 7915 | |
| 7916 | fd: int |
| 7917 | length: int |
| 7918 | offset: Py_off_t |
| 7919 | / |
| 7920 | |
| 7921 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 7922 | |
| 7923 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 7924 | the beginning of the file. The file offset remains unchanged. |
| 7925 | [clinic start generated code]*/ |
| 7926 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7927 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7928 | os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset) |
| 7929 | /*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7930 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7931 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7932 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7933 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7934 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7935 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7936 | errno = EINVAL; |
| 7937 | return posix_error(); |
| 7938 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7939 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7940 | if (buffer == NULL) |
| 7941 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7942 | |
| 7943 | do { |
| 7944 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7945 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7946 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7947 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7948 | Py_END_ALLOW_THREADS |
| 7949 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7950 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7951 | if (n < 0) { |
| 7952 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7953 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7954 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7955 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7956 | _PyBytes_Resize(&buffer, n); |
| 7957 | return buffer; |
| 7958 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7959 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7960 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7961 | |
| 7962 | /*[clinic input] |
| 7963 | os.write -> Py_ssize_t |
| 7964 | |
| 7965 | fd: int |
| 7966 | data: Py_buffer |
| 7967 | / |
| 7968 | |
| 7969 | Write a bytes object to a file descriptor. |
| 7970 | [clinic start generated code]*/ |
| 7971 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7972 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7973 | os_write_impl(PyObject *module, int fd, Py_buffer *data) |
| 7974 | /*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7975 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 7976 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7977 | } |
| 7978 | |
| 7979 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7980 | PyDoc_STRVAR(posix_sendfile__doc__, |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 7981 | "sendfile(out, in, offset, count) -> byteswritten\n\ |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 7982 | sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7983 | -> byteswritten\n\ |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 7984 | Copy count bytes from file descriptor in to file descriptor out."); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7985 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7986 | /* AC 3.5: don't bother converting, has optional group*/ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7987 | static PyObject * |
| 7988 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 7989 | { |
| 7990 | int in, out; |
| 7991 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7992 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7993 | off_t offset; |
| 7994 | |
| 7995 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 7996 | #ifndef __APPLE__ |
| 7997 | Py_ssize_t len; |
| 7998 | #endif |
| 7999 | PyObject *headers = NULL, *trailers = NULL; |
| 8000 | Py_buffer *hbuf, *tbuf; |
| 8001 | off_t sbytes; |
| 8002 | struct sf_hdtr sf; |
| 8003 | int flags = 0; |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8004 | /* Beware that "in" clashes with Python's own "in" operator keyword */ |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 8005 | static char *keywords[] = {"out", "in", |
| 8006 | "offset", "count", |
| 8007 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8008 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 8009 | sf.headers = NULL; |
| 8010 | sf.trailers = NULL; |
| 8011 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8012 | #ifdef __APPLE__ |
| 8013 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8014 | 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] | 8015 | #else |
| 8016 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8017 | keywords, &out, &in, Py_off_t_converter, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8018 | #endif |
| 8019 | &headers, &trailers, &flags)) |
| 8020 | return NULL; |
| 8021 | if (headers != NULL) { |
| 8022 | if (!PySequence_Check(headers)) { |
| 8023 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8024 | "sendfile() headers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8025 | return NULL; |
| 8026 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8027 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8028 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8029 | if (sf.hdr_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8030 | (i = iov_setup(&(sf.headers), &hbuf, |
| 8031 | headers, sf.hdr_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8032 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8033 | #ifdef __APPLE__ |
| 8034 | sbytes += i; |
| 8035 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8036 | } |
| 8037 | } |
| 8038 | if (trailers != NULL) { |
| 8039 | if (!PySequence_Check(trailers)) { |
| 8040 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8041 | "sendfile() trailers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8042 | return NULL; |
| 8043 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8044 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8045 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8046 | if (sf.trl_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8047 | (i = iov_setup(&(sf.trailers), &tbuf, |
| 8048 | trailers, sf.trl_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8049 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8050 | #ifdef __APPLE__ |
| 8051 | sbytes += i; |
| 8052 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8053 | } |
| 8054 | } |
| 8055 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8056 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8057 | do { |
| 8058 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8059 | #ifdef __APPLE__ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8060 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8061 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8062 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8063 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8064 | Py_END_ALLOW_THREADS |
| 8065 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8066 | _Py_END_SUPPRESS_IPH |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8067 | |
| 8068 | if (sf.headers != NULL) |
| 8069 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 8070 | if (sf.trailers != NULL) |
| 8071 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 8072 | |
| 8073 | if (ret < 0) { |
| 8074 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 8075 | if (sbytes != 0) { |
| 8076 | // some data has been sent |
| 8077 | goto done; |
| 8078 | } |
| 8079 | else { |
| 8080 | // no data has been sent; upper application is supposed |
| 8081 | // to retry on EAGAIN or EBUSY |
| 8082 | return posix_error(); |
| 8083 | } |
| 8084 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8085 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8086 | } |
| 8087 | goto done; |
| 8088 | |
| 8089 | done: |
| 8090 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 8091 | return Py_BuildValue("l", sbytes); |
| 8092 | #else |
| 8093 | return Py_BuildValue("L", sbytes); |
| 8094 | #endif |
| 8095 | |
| 8096 | #else |
| 8097 | Py_ssize_t count; |
| 8098 | PyObject *offobj; |
| 8099 | static char *keywords[] = {"out", "in", |
| 8100 | "offset", "count", NULL}; |
| 8101 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 8102 | keywords, &out, &in, &offobj, &count)) |
| 8103 | return NULL; |
Benjamin Peterson | 840ef8f | 2016-09-07 14:45:10 -0700 | [diff] [blame] | 8104 | #ifdef __linux__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8105 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8106 | do { |
| 8107 | Py_BEGIN_ALLOW_THREADS |
| 8108 | ret = sendfile(out, in, NULL, count); |
| 8109 | Py_END_ALLOW_THREADS |
| 8110 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8111 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8112 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 8113 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8114 | } |
| 8115 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8116 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 8117 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8118 | |
| 8119 | do { |
| 8120 | Py_BEGIN_ALLOW_THREADS |
| 8121 | ret = sendfile(out, in, &offset, count); |
| 8122 | Py_END_ALLOW_THREADS |
| 8123 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8124 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8125 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8126 | return Py_BuildValue("n", ret); |
| 8127 | #endif |
| 8128 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8129 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8130 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8131 | |
| 8132 | /*[clinic input] |
| 8133 | os.fstat |
| 8134 | |
| 8135 | fd : int |
| 8136 | |
| 8137 | Perform a stat system call on the given file descriptor. |
| 8138 | |
| 8139 | Like stat(), but for an open file descriptor. |
| 8140 | Equivalent to os.stat(fd). |
| 8141 | [clinic start generated code]*/ |
| 8142 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8143 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8144 | os_fstat_impl(PyObject *module, int fd) |
| 8145 | /*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8146 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8147 | STRUCT_STAT st; |
| 8148 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8149 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8150 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8151 | do { |
| 8152 | Py_BEGIN_ALLOW_THREADS |
| 8153 | res = FSTAT(fd, &st); |
| 8154 | Py_END_ALLOW_THREADS |
| 8155 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8156 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8157 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8158 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8159 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8160 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8161 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8162 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8163 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8164 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8165 | } |
| 8166 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8167 | |
| 8168 | /*[clinic input] |
| 8169 | os.isatty -> bool |
| 8170 | fd: int |
| 8171 | / |
| 8172 | |
| 8173 | Return True if the fd is connected to a terminal. |
| 8174 | |
| 8175 | Return True if the file descriptor is an open file descriptor |
| 8176 | connected to the slave end of a terminal. |
| 8177 | [clinic start generated code]*/ |
| 8178 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8179 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8180 | os_isatty_impl(PyObject *module, int fd) |
| 8181 | /*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8182 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8183 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8184 | _Py_BEGIN_SUPPRESS_IPH |
| 8185 | return_value = isatty(fd); |
| 8186 | _Py_END_SUPPRESS_IPH |
| 8187 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8188 | } |
| 8189 | |
| 8190 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8191 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8192 | /*[clinic input] |
| 8193 | os.pipe |
| 8194 | |
| 8195 | Create a pipe. |
| 8196 | |
| 8197 | Returns a tuple of two file descriptors: |
| 8198 | (read_fd, write_fd) |
| 8199 | [clinic start generated code]*/ |
| 8200 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8201 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8202 | os_pipe_impl(PyObject *module) |
| 8203 | /*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8204 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8205 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8206 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8207 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8208 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8209 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8210 | #else |
| 8211 | int res; |
| 8212 | #endif |
| 8213 | |
| 8214 | #ifdef MS_WINDOWS |
| 8215 | attr.nLength = sizeof(attr); |
| 8216 | attr.lpSecurityDescriptor = NULL; |
| 8217 | attr.bInheritHandle = FALSE; |
| 8218 | |
| 8219 | Py_BEGIN_ALLOW_THREADS |
| 8220 | ok = CreatePipe(&read, &write, &attr, 0); |
| 8221 | if (ok) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 8222 | fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY); |
| 8223 | fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8224 | if (fds[0] == -1 || fds[1] == -1) { |
| 8225 | CloseHandle(read); |
| 8226 | CloseHandle(write); |
| 8227 | ok = 0; |
| 8228 | } |
| 8229 | } |
| 8230 | Py_END_ALLOW_THREADS |
| 8231 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8232 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8233 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8234 | #else |
| 8235 | |
| 8236 | #ifdef HAVE_PIPE2 |
| 8237 | Py_BEGIN_ALLOW_THREADS |
| 8238 | res = pipe2(fds, O_CLOEXEC); |
| 8239 | Py_END_ALLOW_THREADS |
| 8240 | |
| 8241 | if (res != 0 && errno == ENOSYS) |
| 8242 | { |
| 8243 | #endif |
| 8244 | Py_BEGIN_ALLOW_THREADS |
| 8245 | res = pipe(fds); |
| 8246 | Py_END_ALLOW_THREADS |
| 8247 | |
| 8248 | if (res == 0) { |
| 8249 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 8250 | close(fds[0]); |
| 8251 | close(fds[1]); |
| 8252 | return NULL; |
| 8253 | } |
| 8254 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 8255 | close(fds[0]); |
| 8256 | close(fds[1]); |
| 8257 | return NULL; |
| 8258 | } |
| 8259 | } |
| 8260 | #ifdef HAVE_PIPE2 |
| 8261 | } |
| 8262 | #endif |
| 8263 | |
| 8264 | if (res != 0) |
| 8265 | return PyErr_SetFromErrno(PyExc_OSError); |
| 8266 | #endif /* !MS_WINDOWS */ |
| 8267 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8268 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8269 | #endif /* HAVE_PIPE */ |
| 8270 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8271 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8272 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8273 | /*[clinic input] |
| 8274 | os.pipe2 |
| 8275 | |
| 8276 | flags: int |
| 8277 | / |
| 8278 | |
| 8279 | Create a pipe with flags set atomically. |
| 8280 | |
| 8281 | Returns a tuple of two file descriptors: |
| 8282 | (read_fd, write_fd) |
| 8283 | |
| 8284 | flags can be constructed by ORing together one or more of these values: |
| 8285 | O_NONBLOCK, O_CLOEXEC. |
| 8286 | [clinic start generated code]*/ |
| 8287 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8288 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8289 | os_pipe2_impl(PyObject *module, int flags) |
| 8290 | /*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8291 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8292 | int fds[2]; |
| 8293 | int res; |
| 8294 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8295 | res = pipe2(fds, flags); |
| 8296 | if (res != 0) |
| 8297 | return posix_error(); |
| 8298 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 8299 | } |
| 8300 | #endif /* HAVE_PIPE2 */ |
| 8301 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8302 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8303 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8304 | /*[clinic input] |
| 8305 | os.writev -> Py_ssize_t |
| 8306 | fd: int |
| 8307 | buffers: object |
| 8308 | / |
| 8309 | |
| 8310 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 8311 | |
| 8312 | Returns the total number of bytes written. |
| 8313 | buffers must be a sequence of bytes-like objects. |
| 8314 | [clinic start generated code]*/ |
| 8315 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8316 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8317 | os_writev_impl(PyObject *module, int fd, PyObject *buffers) |
| 8318 | /*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8319 | { |
| 8320 | int cnt; |
| 8321 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8322 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8323 | struct iovec *iov; |
| 8324 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8325 | |
| 8326 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8327 | PyErr_SetString(PyExc_TypeError, |
| 8328 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8329 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8330 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8331 | cnt = PySequence_Size(buffers); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8332 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8333 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 8334 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8335 | } |
| 8336 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8337 | do { |
| 8338 | Py_BEGIN_ALLOW_THREADS |
| 8339 | result = writev(fd, iov, cnt); |
| 8340 | Py_END_ALLOW_THREADS |
| 8341 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8342 | |
| 8343 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8344 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8345 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8346 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8347 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8348 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8349 | #endif /* HAVE_WRITEV */ |
| 8350 | |
| 8351 | |
| 8352 | #ifdef HAVE_PWRITE |
| 8353 | /*[clinic input] |
| 8354 | os.pwrite -> Py_ssize_t |
| 8355 | |
| 8356 | fd: int |
| 8357 | buffer: Py_buffer |
| 8358 | offset: Py_off_t |
| 8359 | / |
| 8360 | |
| 8361 | Write bytes to a file descriptor starting at a particular offset. |
| 8362 | |
| 8363 | Write buffer to fd, starting at offset bytes from the beginning of |
| 8364 | the file. Returns the number of bytes writte. Does not change the |
| 8365 | current file offset. |
| 8366 | [clinic start generated code]*/ |
| 8367 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8368 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8369 | os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset) |
| 8370 | /*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8371 | { |
| 8372 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8373 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8374 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8375 | do { |
| 8376 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8377 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8378 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8379 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8380 | Py_END_ALLOW_THREADS |
| 8381 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8382 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8383 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8384 | posix_error(); |
| 8385 | return size; |
| 8386 | } |
| 8387 | #endif /* HAVE_PWRITE */ |
| 8388 | |
| 8389 | |
| 8390 | #ifdef HAVE_MKFIFO |
| 8391 | /*[clinic input] |
| 8392 | os.mkfifo |
| 8393 | |
| 8394 | path: path_t |
| 8395 | mode: int=0o666 |
| 8396 | * |
| 8397 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 8398 | |
| 8399 | Create a "fifo" (a POSIX named pipe). |
| 8400 | |
| 8401 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8402 | and path should be relative; path will then be relative to that directory. |
| 8403 | dir_fd may not be implemented on your platform. |
| 8404 | If it is unavailable, using it will raise a NotImplementedError. |
| 8405 | [clinic start generated code]*/ |
| 8406 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8407 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8408 | os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 8409 | /*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8410 | { |
| 8411 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8412 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8413 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8414 | do { |
| 8415 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8416 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8417 | if (dir_fd != DEFAULT_DIR_FD) |
| 8418 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 8419 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8420 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8421 | result = mkfifo(path->narrow, mode); |
| 8422 | Py_END_ALLOW_THREADS |
| 8423 | } while (result != 0 && errno == EINTR && |
| 8424 | !(async_err = PyErr_CheckSignals())); |
| 8425 | if (result != 0) |
| 8426 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8427 | |
| 8428 | Py_RETURN_NONE; |
| 8429 | } |
| 8430 | #endif /* HAVE_MKFIFO */ |
| 8431 | |
| 8432 | |
| 8433 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 8434 | /*[clinic input] |
| 8435 | os.mknod |
| 8436 | |
| 8437 | path: path_t |
| 8438 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8439 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8440 | * |
| 8441 | dir_fd: dir_fd(requires='mknodat')=None |
| 8442 | |
| 8443 | Create a node in the file system. |
| 8444 | |
| 8445 | Create a node in the file system (file, device special file or named pipe) |
| 8446 | at path. mode specifies both the permissions to use and the |
| 8447 | type of node to be created, being combined (bitwise OR) with one of |
| 8448 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 8449 | device defines the newly created device special file (probably using |
| 8450 | os.makedev()). Otherwise device is ignored. |
| 8451 | |
| 8452 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8453 | and path should be relative; path will then be relative to that directory. |
| 8454 | dir_fd may not be implemented on your platform. |
| 8455 | If it is unavailable, using it will raise a NotImplementedError. |
| 8456 | [clinic start generated code]*/ |
| 8457 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8458 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8459 | 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] | 8460 | int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8461 | /*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/ |
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_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8469 | if (dir_fd != DEFAULT_DIR_FD) |
| 8470 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 8471 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8472 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8473 | result = mknod(path->narrow, mode, device); |
| 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 /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 8483 | |
| 8484 | |
| 8485 | #ifdef HAVE_DEVICE_MACROS |
| 8486 | /*[clinic input] |
| 8487 | os.major -> unsigned_int |
| 8488 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8489 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8490 | / |
| 8491 | |
| 8492 | Extracts a device major number from a raw device number. |
| 8493 | [clinic start generated code]*/ |
| 8494 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8495 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8496 | os_major_impl(PyObject *module, dev_t device) |
| 8497 | /*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8498 | { |
| 8499 | return major(device); |
| 8500 | } |
| 8501 | |
| 8502 | |
| 8503 | /*[clinic input] |
| 8504 | os.minor -> unsigned_int |
| 8505 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8506 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8507 | / |
| 8508 | |
| 8509 | Extracts a device minor number from a raw device number. |
| 8510 | [clinic start generated code]*/ |
| 8511 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8512 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8513 | os_minor_impl(PyObject *module, dev_t device) |
| 8514 | /*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8515 | { |
| 8516 | return minor(device); |
| 8517 | } |
| 8518 | |
| 8519 | |
| 8520 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8521 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8522 | |
| 8523 | major: int |
| 8524 | minor: int |
| 8525 | / |
| 8526 | |
| 8527 | Composes a raw device number from the major and minor device numbers. |
| 8528 | [clinic start generated code]*/ |
| 8529 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8530 | static dev_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8531 | os_makedev_impl(PyObject *module, int major, int minor) |
| 8532 | /*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8533 | { |
| 8534 | return makedev(major, minor); |
| 8535 | } |
| 8536 | #endif /* HAVE_DEVICE_MACROS */ |
| 8537 | |
| 8538 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8539 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8540 | /*[clinic input] |
| 8541 | os.ftruncate |
| 8542 | |
| 8543 | fd: int |
| 8544 | length: Py_off_t |
| 8545 | / |
| 8546 | |
| 8547 | Truncate a file, specified by file descriptor, to a specific length. |
| 8548 | [clinic start generated code]*/ |
| 8549 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8550 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8551 | os_ftruncate_impl(PyObject *module, int fd, Py_off_t length) |
| 8552 | /*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8553 | { |
| 8554 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8555 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8556 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8557 | do { |
| 8558 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8559 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8560 | #ifdef MS_WINDOWS |
| 8561 | result = _chsize_s(fd, length); |
| 8562 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8563 | result = ftruncate(fd, length); |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8564 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8565 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8566 | Py_END_ALLOW_THREADS |
| 8567 | } while (result != 0 && errno == EINTR && |
| 8568 | !(async_err = PyErr_CheckSignals())); |
| 8569 | if (result != 0) |
| 8570 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8571 | Py_RETURN_NONE; |
| 8572 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8573 | #endif /* HAVE_FTRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8574 | |
| 8575 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8576 | #if defined HAVE_TRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8577 | /*[clinic input] |
| 8578 | os.truncate |
| 8579 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 8580 | length: Py_off_t |
| 8581 | |
| 8582 | Truncate a file, specified by path, to a specific length. |
| 8583 | |
| 8584 | On some platforms, path may also be specified as an open file descriptor. |
| 8585 | If this functionality is unavailable, using it raises an exception. |
| 8586 | [clinic start generated code]*/ |
| 8587 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8588 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8589 | os_truncate_impl(PyObject *module, path_t *path, Py_off_t length) |
| 8590 | /*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8591 | { |
| 8592 | int result; |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8593 | #ifdef MS_WINDOWS |
| 8594 | int fd; |
| 8595 | #endif |
| 8596 | |
| 8597 | if (path->fd != -1) |
| 8598 | return os_ftruncate_impl(module, path->fd, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8599 | |
| 8600 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8601 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8602 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 8603 | fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Victor Stinner | cc0bbbc | 2015-04-25 00:21:52 +0200 | [diff] [blame] | 8604 | if (fd < 0) |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8605 | result = -1; |
| 8606 | else { |
| 8607 | result = _chsize_s(fd, length); |
| 8608 | close(fd); |
| 8609 | if (result < 0) |
| 8610 | errno = result; |
| 8611 | } |
| 8612 | #else |
| 8613 | result = truncate(path->narrow, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8614 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8615 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8616 | Py_END_ALLOW_THREADS |
| 8617 | if (result < 0) |
| 8618 | return path_error(path); |
| 8619 | |
| 8620 | Py_RETURN_NONE; |
| 8621 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8622 | #endif /* HAVE_TRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8623 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8624 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 8625 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 8626 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 8627 | defined, which is the case in Python on AIX. AIX bug report: |
| 8628 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 8629 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 8630 | # define POSIX_FADVISE_AIX_BUG |
| 8631 | #endif |
| 8632 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 8633 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 8634 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8635 | /*[clinic input] |
| 8636 | os.posix_fallocate |
| 8637 | |
| 8638 | fd: int |
| 8639 | offset: Py_off_t |
| 8640 | length: Py_off_t |
| 8641 | / |
| 8642 | |
| 8643 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 8644 | |
| 8645 | Ensure that the file specified by fd encompasses a range of bytes |
| 8646 | starting at offset bytes from the beginning and continuing for length bytes. |
| 8647 | [clinic start generated code]*/ |
| 8648 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8649 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8650 | os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8651 | Py_off_t length) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8652 | /*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8653 | { |
| 8654 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8655 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8656 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8657 | do { |
| 8658 | Py_BEGIN_ALLOW_THREADS |
| 8659 | result = posix_fallocate(fd, offset, length); |
| 8660 | Py_END_ALLOW_THREADS |
| 8661 | } while (result != 0 && errno == EINTR && |
| 8662 | !(async_err = PyErr_CheckSignals())); |
| 8663 | if (result != 0) |
| 8664 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8665 | Py_RETURN_NONE; |
| 8666 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 8667 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8668 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8669 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 8670 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8671 | /*[clinic input] |
| 8672 | os.posix_fadvise |
| 8673 | |
| 8674 | fd: int |
| 8675 | offset: Py_off_t |
| 8676 | length: Py_off_t |
| 8677 | advice: int |
| 8678 | / |
| 8679 | |
| 8680 | Announce an intention to access data in a specific pattern. |
| 8681 | |
| 8682 | Announce an intention to access data in a specific pattern, thus allowing |
| 8683 | the kernel to make optimizations. |
| 8684 | The advice applies to the region of the file specified by fd starting at |
| 8685 | offset and continuing for length bytes. |
| 8686 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 8687 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 8688 | POSIX_FADV_DONTNEED. |
| 8689 | [clinic start generated code]*/ |
| 8690 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8691 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8692 | os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8693 | Py_off_t length, int advice) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8694 | /*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8695 | { |
| 8696 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8697 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8698 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8699 | do { |
| 8700 | Py_BEGIN_ALLOW_THREADS |
| 8701 | result = posix_fadvise(fd, offset, length, advice); |
| 8702 | Py_END_ALLOW_THREADS |
| 8703 | } while (result != 0 && errno == EINTR && |
| 8704 | !(async_err = PyErr_CheckSignals())); |
| 8705 | if (result != 0) |
| 8706 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8707 | Py_RETURN_NONE; |
| 8708 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 8709 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8710 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8711 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8712 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8713 | /* Save putenv() parameters as values here, so we can collect them when they |
| 8714 | * get re-set with another call for the same key. */ |
| 8715 | static PyObject *posix_putenv_garbage; |
| 8716 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8717 | static void |
| 8718 | posix_putenv_garbage_setitem(PyObject *name, PyObject *value) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8719 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8720 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 8721 | * this will cause previous value to be collected. This has to |
| 8722 | * happen after the real putenv() call because the old value |
| 8723 | * was still accessible until then. */ |
| 8724 | if (PyDict_SetItem(posix_putenv_garbage, name, value)) |
| 8725 | /* really not much we can do; just leak */ |
| 8726 | PyErr_Clear(); |
| 8727 | else |
| 8728 | Py_DECREF(value); |
| 8729 | } |
| 8730 | |
| 8731 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8732 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8733 | /*[clinic input] |
| 8734 | os.putenv |
| 8735 | |
| 8736 | name: unicode |
| 8737 | value: unicode |
| 8738 | / |
| 8739 | |
| 8740 | Change or add an environment variable. |
| 8741 | [clinic start generated code]*/ |
| 8742 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8743 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8744 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 8745 | /*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8746 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 8747 | const wchar_t *env; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8748 | |
| 8749 | PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 8750 | if (unicode == NULL) { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8751 | PyErr_NoMemory(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8752 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8753 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8754 | if (_MAX_ENV < PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8755 | PyErr_Format(PyExc_ValueError, |
| 8756 | "the environment variable is longer than %u characters", |
| 8757 | _MAX_ENV); |
| 8758 | goto error; |
| 8759 | } |
| 8760 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8761 | env = PyUnicode_AsUnicode(unicode); |
| 8762 | if (env == NULL) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 8763 | goto error; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8764 | if (_wputenv(env)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8765 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8766 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8767 | } |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8768 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8769 | posix_putenv_garbage_setitem(name, unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8770 | Py_RETURN_NONE; |
| 8771 | |
| 8772 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8773 | Py_DECREF(unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8774 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8775 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8776 | #else /* MS_WINDOWS */ |
| 8777 | /*[clinic input] |
| 8778 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8779 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8780 | name: FSConverter |
| 8781 | value: FSConverter |
| 8782 | / |
| 8783 | |
| 8784 | Change or add an environment variable. |
| 8785 | [clinic start generated code]*/ |
| 8786 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8787 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8788 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 8789 | /*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8790 | { |
| 8791 | PyObject *bytes = NULL; |
| 8792 | char *env; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 8793 | const char *name_string = PyBytes_AsString(name); |
| 8794 | const char *value_string = PyBytes_AsString(value); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8795 | |
| 8796 | bytes = PyBytes_FromFormat("%s=%s", name_string, value_string); |
| 8797 | if (bytes == NULL) { |
| 8798 | PyErr_NoMemory(); |
| 8799 | return NULL; |
| 8800 | } |
| 8801 | |
| 8802 | env = PyBytes_AS_STRING(bytes); |
| 8803 | if (putenv(env)) { |
| 8804 | Py_DECREF(bytes); |
| 8805 | return posix_error(); |
| 8806 | } |
| 8807 | |
| 8808 | posix_putenv_garbage_setitem(name, bytes); |
| 8809 | Py_RETURN_NONE; |
| 8810 | } |
| 8811 | #endif /* MS_WINDOWS */ |
| 8812 | #endif /* HAVE_PUTENV */ |
| 8813 | |
| 8814 | |
| 8815 | #ifdef HAVE_UNSETENV |
| 8816 | /*[clinic input] |
| 8817 | os.unsetenv |
| 8818 | name: FSConverter |
| 8819 | / |
| 8820 | |
| 8821 | Delete an environment variable. |
| 8822 | [clinic start generated code]*/ |
| 8823 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8824 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8825 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 8826 | /*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8827 | { |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8828 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8829 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8830 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8831 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8832 | #ifdef HAVE_BROKEN_UNSETENV |
| 8833 | unsetenv(PyBytes_AS_STRING(name)); |
| 8834 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8835 | err = unsetenv(PyBytes_AS_STRING(name)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8836 | if (err) |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8837 | return posix_error(); |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8838 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8839 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8840 | /* Remove the key from posix_putenv_garbage; |
| 8841 | * this will cause it to be collected. This has to |
| 8842 | * happen after the real unsetenv() call because the |
| 8843 | * old value was still accessible until then. |
| 8844 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8845 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8846 | /* really not much we can do; just leak */ |
| 8847 | PyErr_Clear(); |
| 8848 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8849 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8850 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8851 | #endif /* HAVE_UNSETENV */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8852 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8853 | |
| 8854 | /*[clinic input] |
| 8855 | os.strerror |
| 8856 | |
| 8857 | code: int |
| 8858 | / |
| 8859 | |
| 8860 | Translate an error code to a message string. |
| 8861 | [clinic start generated code]*/ |
| 8862 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8863 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8864 | os_strerror_impl(PyObject *module, int code) |
| 8865 | /*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8866 | { |
| 8867 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8868 | if (message == NULL) { |
| 8869 | PyErr_SetString(PyExc_ValueError, |
| 8870 | "strerror() argument out of range"); |
| 8871 | return NULL; |
| 8872 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 8873 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8874 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8875 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8876 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8877 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8878 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8879 | /*[clinic input] |
| 8880 | os.WCOREDUMP -> bool |
| 8881 | |
| 8882 | status: int |
| 8883 | / |
| 8884 | |
| 8885 | Return True if the process returning status was dumped to a core file. |
| 8886 | [clinic start generated code]*/ |
| 8887 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8888 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8889 | os_WCOREDUMP_impl(PyObject *module, int status) |
| 8890 | /*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8891 | { |
| 8892 | WAIT_TYPE wait_status; |
| 8893 | WAIT_STATUS_INT(wait_status) = status; |
| 8894 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8895 | } |
| 8896 | #endif /* WCOREDUMP */ |
| 8897 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8898 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8899 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8900 | /*[clinic input] |
| 8901 | os.WIFCONTINUED -> bool |
| 8902 | |
| 8903 | status: int |
| 8904 | |
| 8905 | Return True if a particular process was continued from a job control stop. |
| 8906 | |
| 8907 | Return True if the process returning status was continued from a |
| 8908 | job control stop. |
| 8909 | [clinic start generated code]*/ |
| 8910 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8911 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8912 | os_WIFCONTINUED_impl(PyObject *module, int status) |
| 8913 | /*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8914 | { |
| 8915 | WAIT_TYPE wait_status; |
| 8916 | WAIT_STATUS_INT(wait_status) = status; |
| 8917 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8918 | } |
| 8919 | #endif /* WIFCONTINUED */ |
| 8920 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8921 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8922 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8923 | /*[clinic input] |
| 8924 | os.WIFSTOPPED -> bool |
| 8925 | |
| 8926 | status: int |
| 8927 | |
| 8928 | Return True if the process returning status was stopped. |
| 8929 | [clinic start generated code]*/ |
| 8930 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8931 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8932 | os_WIFSTOPPED_impl(PyObject *module, int status) |
| 8933 | /*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8934 | { |
| 8935 | WAIT_TYPE wait_status; |
| 8936 | WAIT_STATUS_INT(wait_status) = status; |
| 8937 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8938 | } |
| 8939 | #endif /* WIFSTOPPED */ |
| 8940 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8941 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8942 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8943 | /*[clinic input] |
| 8944 | os.WIFSIGNALED -> bool |
| 8945 | |
| 8946 | status: int |
| 8947 | |
| 8948 | Return True if the process returning status was terminated by a signal. |
| 8949 | [clinic start generated code]*/ |
| 8950 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8951 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8952 | os_WIFSIGNALED_impl(PyObject *module, int status) |
| 8953 | /*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8954 | { |
| 8955 | WAIT_TYPE wait_status; |
| 8956 | WAIT_STATUS_INT(wait_status) = status; |
| 8957 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8958 | } |
| 8959 | #endif /* WIFSIGNALED */ |
| 8960 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8961 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8962 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8963 | /*[clinic input] |
| 8964 | os.WIFEXITED -> bool |
| 8965 | |
| 8966 | status: int |
| 8967 | |
| 8968 | Return True if the process returning status exited via the exit() system call. |
| 8969 | [clinic start generated code]*/ |
| 8970 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8971 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8972 | os_WIFEXITED_impl(PyObject *module, int status) |
| 8973 | /*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8974 | { |
| 8975 | WAIT_TYPE wait_status; |
| 8976 | WAIT_STATUS_INT(wait_status) = status; |
| 8977 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8978 | } |
| 8979 | #endif /* WIFEXITED */ |
| 8980 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8981 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 8982 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8983 | /*[clinic input] |
| 8984 | os.WEXITSTATUS -> int |
| 8985 | |
| 8986 | status: int |
| 8987 | |
| 8988 | Return the process return code from status. |
| 8989 | [clinic start generated code]*/ |
| 8990 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8991 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8992 | os_WEXITSTATUS_impl(PyObject *module, int status) |
| 8993 | /*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8994 | { |
| 8995 | WAIT_TYPE wait_status; |
| 8996 | WAIT_STATUS_INT(wait_status) = status; |
| 8997 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8998 | } |
| 8999 | #endif /* WEXITSTATUS */ |
| 9000 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9001 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9002 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9003 | /*[clinic input] |
| 9004 | os.WTERMSIG -> int |
| 9005 | |
| 9006 | status: int |
| 9007 | |
| 9008 | Return the signal that terminated the process that provided the status value. |
| 9009 | [clinic start generated code]*/ |
| 9010 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9011 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9012 | os_WTERMSIG_impl(PyObject *module, int status) |
| 9013 | /*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9014 | { |
| 9015 | WAIT_TYPE wait_status; |
| 9016 | WAIT_STATUS_INT(wait_status) = status; |
| 9017 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9018 | } |
| 9019 | #endif /* WTERMSIG */ |
| 9020 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9021 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9022 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9023 | /*[clinic input] |
| 9024 | os.WSTOPSIG -> int |
| 9025 | |
| 9026 | status: int |
| 9027 | |
| 9028 | Return the signal that stopped the process that provided the status value. |
| 9029 | [clinic start generated code]*/ |
| 9030 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9031 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9032 | os_WSTOPSIG_impl(PyObject *module, int status) |
| 9033 | /*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9034 | { |
| 9035 | WAIT_TYPE wait_status; |
| 9036 | WAIT_STATUS_INT(wait_status) = status; |
| 9037 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9038 | } |
| 9039 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9040 | #endif /* HAVE_SYS_WAIT_H */ |
| 9041 | |
| 9042 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9043 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 9044 | #ifdef _SCO_DS |
| 9045 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 9046 | needed definitions in sys/statvfs.h */ |
| 9047 | #define _SVID3 |
| 9048 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9049 | #include <sys/statvfs.h> |
| 9050 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9051 | static PyObject* |
| 9052 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9053 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 9054 | if (v == NULL) |
| 9055 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9056 | |
| 9057 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9058 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9059 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9060 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 9061 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 9062 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 9063 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 9064 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 9065 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 9066 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9067 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9068 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9069 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9070 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9071 | PyStructSequence_SET_ITEM(v, 2, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9072 | PyLong_FromLongLong((long long) st.f_blocks)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9073 | PyStructSequence_SET_ITEM(v, 3, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9074 | PyLong_FromLongLong((long long) st.f_bfree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9075 | PyStructSequence_SET_ITEM(v, 4, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9076 | PyLong_FromLongLong((long long) st.f_bavail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9077 | PyStructSequence_SET_ITEM(v, 5, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9078 | PyLong_FromLongLong((long long) st.f_files)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9079 | PyStructSequence_SET_ITEM(v, 6, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9080 | PyLong_FromLongLong((long long) st.f_ffree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9081 | PyStructSequence_SET_ITEM(v, 7, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9082 | PyLong_FromLongLong((long long) st.f_favail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9083 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9084 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9085 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 9086 | if (PyErr_Occurred()) { |
| 9087 | Py_DECREF(v); |
| 9088 | return NULL; |
| 9089 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9090 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9091 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9092 | } |
| 9093 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9094 | |
| 9095 | /*[clinic input] |
| 9096 | os.fstatvfs |
| 9097 | fd: int |
| 9098 | / |
| 9099 | |
| 9100 | Perform an fstatvfs system call on the given fd. |
| 9101 | |
| 9102 | Equivalent to statvfs(fd). |
| 9103 | [clinic start generated code]*/ |
| 9104 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9105 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9106 | os_fstatvfs_impl(PyObject *module, int fd) |
| 9107 | /*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9108 | { |
| 9109 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9110 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9111 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9112 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9113 | do { |
| 9114 | Py_BEGIN_ALLOW_THREADS |
| 9115 | result = fstatvfs(fd, &st); |
| 9116 | Py_END_ALLOW_THREADS |
| 9117 | } while (result != 0 && errno == EINTR && |
| 9118 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9119 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9120 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9121 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9122 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9123 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9124 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9125 | |
| 9126 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9127 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9128 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9129 | /*[clinic input] |
| 9130 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9131 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9132 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 9133 | |
| 9134 | Perform a statvfs system call on the given path. |
| 9135 | |
| 9136 | path may always be specified as a string. |
| 9137 | On some platforms, path may also be specified as an open file descriptor. |
| 9138 | If this functionality is unavailable, using it raises an exception. |
| 9139 | [clinic start generated code]*/ |
| 9140 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9141 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9142 | os_statvfs_impl(PyObject *module, path_t *path) |
| 9143 | /*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9144 | { |
| 9145 | int result; |
| 9146 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9147 | |
| 9148 | Py_BEGIN_ALLOW_THREADS |
| 9149 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9150 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9151 | #ifdef __APPLE__ |
| 9152 | /* handle weak-linking on Mac OS X 10.3 */ |
| 9153 | if (fstatvfs == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9154 | fd_specified("statvfs", path->fd); |
| 9155 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9156 | } |
| 9157 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9158 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9159 | } |
| 9160 | else |
| 9161 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9162 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9163 | Py_END_ALLOW_THREADS |
| 9164 | |
| 9165 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9166 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9167 | } |
| 9168 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9169 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9170 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9171 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 9172 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9173 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9174 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9175 | /*[clinic input] |
| 9176 | os._getdiskusage |
| 9177 | |
| 9178 | path: Py_UNICODE |
| 9179 | |
| 9180 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 9181 | [clinic start generated code]*/ |
| 9182 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9183 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9184 | os__getdiskusage_impl(PyObject *module, Py_UNICODE *path) |
| 9185 | /*[clinic end generated code: output=76d6adcd86b1db0b input=6458133aed893c78]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9186 | { |
| 9187 | BOOL retval; |
| 9188 | ULARGE_INTEGER _, total, free; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9189 | |
| 9190 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 9191 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9192 | Py_END_ALLOW_THREADS |
| 9193 | if (retval == 0) |
| 9194 | return PyErr_SetFromWindowsErr(0); |
| 9195 | |
| 9196 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 9197 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9198 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9199 | |
| 9200 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9201 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 9202 | * It maps strings representing configuration variable names to |
| 9203 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 9204 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9205 | * rarely-used constants. There are three separate tables that use |
| 9206 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9207 | * |
| 9208 | * This code is always included, even if none of the interfaces that |
| 9209 | * need it are included. The #if hackery needed to avoid it would be |
| 9210 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9211 | */ |
| 9212 | struct constdef { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 9213 | const char *name; |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 9214 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9215 | }; |
| 9216 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9217 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9218 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 9219 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9220 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9221 | if (PyLong_Check(arg)) { |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 9222 | int value = _PyLong_AsInt(arg); |
| 9223 | if (value == -1 && PyErr_Occurred()) |
| 9224 | return 0; |
| 9225 | *valuep = value; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9226 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9227 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 9228 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9229 | /* look up the value in the table using a binary search */ |
| 9230 | size_t lo = 0; |
| 9231 | size_t mid; |
| 9232 | size_t hi = tablesize; |
| 9233 | int cmp; |
| 9234 | const char *confname; |
| 9235 | if (!PyUnicode_Check(arg)) { |
| 9236 | PyErr_SetString(PyExc_TypeError, |
| 9237 | "configuration names must be strings or integers"); |
| 9238 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9239 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9240 | confname = _PyUnicode_AsString(arg); |
| 9241 | if (confname == NULL) |
| 9242 | return 0; |
| 9243 | while (lo < hi) { |
| 9244 | mid = (lo + hi) / 2; |
| 9245 | cmp = strcmp(confname, table[mid].name); |
| 9246 | if (cmp < 0) |
| 9247 | hi = mid; |
| 9248 | else if (cmp > 0) |
| 9249 | lo = mid + 1; |
| 9250 | else { |
| 9251 | *valuep = table[mid].value; |
| 9252 | return 1; |
| 9253 | } |
| 9254 | } |
| 9255 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 9256 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9257 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9258 | } |
| 9259 | |
| 9260 | |
| 9261 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 9262 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9263 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9264 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9265 | #endif |
| 9266 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9267 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9268 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9269 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9270 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9271 | #endif |
| 9272 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9273 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9274 | #endif |
| 9275 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9276 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9277 | #endif |
| 9278 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9279 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9280 | #endif |
| 9281 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9282 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9283 | #endif |
| 9284 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9285 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9286 | #endif |
| 9287 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9288 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9289 | #endif |
| 9290 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9291 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9292 | #endif |
| 9293 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9294 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9295 | #endif |
| 9296 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9297 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9298 | #endif |
| 9299 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9300 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9301 | #endif |
| 9302 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9303 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9304 | #endif |
| 9305 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9306 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9307 | #endif |
| 9308 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9309 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9310 | #endif |
| 9311 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9312 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9313 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 9314 | #ifdef _PC_ACL_ENABLED |
| 9315 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 9316 | #endif |
| 9317 | #ifdef _PC_MIN_HOLE_SIZE |
| 9318 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 9319 | #endif |
| 9320 | #ifdef _PC_ALLOC_SIZE_MIN |
| 9321 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 9322 | #endif |
| 9323 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 9324 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 9325 | #endif |
| 9326 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 9327 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 9328 | #endif |
| 9329 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 9330 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 9331 | #endif |
| 9332 | #ifdef _PC_REC_XFER_ALIGN |
| 9333 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 9334 | #endif |
| 9335 | #ifdef _PC_SYMLINK_MAX |
| 9336 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 9337 | #endif |
| 9338 | #ifdef _PC_XATTR_ENABLED |
| 9339 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 9340 | #endif |
| 9341 | #ifdef _PC_XATTR_EXISTS |
| 9342 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 9343 | #endif |
| 9344 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 9345 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 9346 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9347 | }; |
| 9348 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9349 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9350 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9351 | { |
| 9352 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 9353 | sizeof(posix_constants_pathconf) |
| 9354 | / sizeof(struct constdef)); |
| 9355 | } |
| 9356 | #endif |
| 9357 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9358 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9359 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9360 | /*[clinic input] |
| 9361 | os.fpathconf -> long |
| 9362 | |
| 9363 | fd: int |
| 9364 | name: path_confname |
| 9365 | / |
| 9366 | |
| 9367 | Return the configuration limit name for the file descriptor fd. |
| 9368 | |
| 9369 | If there is no limit, return -1. |
| 9370 | [clinic start generated code]*/ |
| 9371 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9372 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9373 | os_fpathconf_impl(PyObject *module, int fd, int name) |
| 9374 | /*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9375 | { |
| 9376 | long limit; |
| 9377 | |
| 9378 | errno = 0; |
| 9379 | limit = fpathconf(fd, name); |
| 9380 | if (limit == -1 && errno != 0) |
| 9381 | posix_error(); |
| 9382 | |
| 9383 | return limit; |
| 9384 | } |
| 9385 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9386 | |
| 9387 | |
| 9388 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9389 | /*[clinic input] |
| 9390 | os.pathconf -> long |
| 9391 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 9392 | name: path_confname |
| 9393 | |
| 9394 | Return the configuration limit name for the file or directory path. |
| 9395 | |
| 9396 | If there is no limit, return -1. |
| 9397 | On some platforms, path may also be specified as an open file descriptor. |
| 9398 | If this functionality is unavailable, using it raises an exception. |
| 9399 | [clinic start generated code]*/ |
| 9400 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9401 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9402 | os_pathconf_impl(PyObject *module, path_t *path, int name) |
| 9403 | /*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9404 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9405 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9406 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9407 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9408 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9409 | if (path->fd != -1) |
| 9410 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9411 | else |
| 9412 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9413 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9414 | if (limit == -1 && errno != 0) { |
| 9415 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 9416 | /* could be a path or name problem */ |
| 9417 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9418 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9419 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9420 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9421 | |
| 9422 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9423 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9424 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9425 | |
| 9426 | #ifdef HAVE_CONFSTR |
| 9427 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9428 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9429 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9430 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9431 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9432 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9433 | #endif |
| 9434 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9435 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9436 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9437 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9438 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9439 | #endif |
| 9440 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9441 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9442 | #endif |
| 9443 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9444 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9445 | #endif |
| 9446 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9447 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9448 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9449 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9450 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9451 | #endif |
| 9452 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9453 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9454 | #endif |
| 9455 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9456 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9457 | #endif |
| 9458 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9459 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9460 | #endif |
| 9461 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9462 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9463 | #endif |
| 9464 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9465 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9466 | #endif |
| 9467 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9468 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9469 | #endif |
| 9470 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9471 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9472 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9473 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9474 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9475 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9476 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9477 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9478 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9479 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9480 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9481 | #endif |
| 9482 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9483 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9484 | #endif |
| 9485 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9486 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9487 | #endif |
| 9488 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9489 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9490 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9491 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9492 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9493 | #endif |
| 9494 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9495 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9496 | #endif |
| 9497 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9498 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9499 | #endif |
| 9500 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9501 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9502 | #endif |
| 9503 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9504 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9505 | #endif |
| 9506 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9507 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9508 | #endif |
| 9509 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9510 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9511 | #endif |
| 9512 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9513 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9514 | #endif |
| 9515 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9516 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9517 | #endif |
| 9518 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9519 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9520 | #endif |
| 9521 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9522 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9523 | #endif |
| 9524 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9525 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9526 | #endif |
| 9527 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9528 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9529 | #endif |
| 9530 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9531 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9532 | #endif |
| 9533 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9534 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9535 | #endif |
| 9536 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9537 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9538 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9539 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9540 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9541 | #endif |
| 9542 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9543 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9544 | #endif |
| 9545 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9546 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9547 | #endif |
| 9548 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9549 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9550 | #endif |
| 9551 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9552 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9553 | #endif |
| 9554 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9555 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9556 | #endif |
| 9557 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9558 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9559 | #endif |
| 9560 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9561 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9562 | #endif |
| 9563 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9564 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9565 | #endif |
| 9566 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9567 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9568 | #endif |
| 9569 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9570 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9571 | #endif |
| 9572 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9573 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9574 | #endif |
| 9575 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9576 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9577 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9578 | }; |
| 9579 | |
| 9580 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9581 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9582 | { |
| 9583 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 9584 | sizeof(posix_constants_confstr) |
| 9585 | / sizeof(struct constdef)); |
| 9586 | } |
| 9587 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9588 | |
| 9589 | /*[clinic input] |
| 9590 | os.confstr |
| 9591 | |
| 9592 | name: confstr_confname |
| 9593 | / |
| 9594 | |
| 9595 | Return a string-valued system configuration variable. |
| 9596 | [clinic start generated code]*/ |
| 9597 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9598 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9599 | os_confstr_impl(PyObject *module, int name) |
| 9600 | /*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9601 | { |
| 9602 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9603 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9604 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9605 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9606 | errno = 0; |
| 9607 | len = confstr(name, buffer, sizeof(buffer)); |
| 9608 | if (len == 0) { |
| 9609 | if (errno) { |
| 9610 | posix_error(); |
| 9611 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9612 | } |
| 9613 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9614 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9615 | } |
| 9616 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9617 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9618 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 9619 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9620 | char *buf = PyMem_Malloc(len); |
| 9621 | if (buf == NULL) |
| 9622 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 9623 | len2 = confstr(name, buf, len); |
| 9624 | assert(len == len2); |
Christian Heimes | 8714cfd | 2015-04-21 10:57:41 +0200 | [diff] [blame] | 9625 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9626 | PyMem_Free(buf); |
| 9627 | } |
| 9628 | else |
| 9629 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9630 | return result; |
| 9631 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9632 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9633 | |
| 9634 | |
| 9635 | #ifdef HAVE_SYSCONF |
| 9636 | static struct constdef posix_constants_sysconf[] = { |
| 9637 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9638 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9639 | #endif |
| 9640 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9641 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9642 | #endif |
| 9643 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9644 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9645 | #endif |
| 9646 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9647 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9648 | #endif |
| 9649 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9650 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9651 | #endif |
| 9652 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9653 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9654 | #endif |
| 9655 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9656 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9657 | #endif |
| 9658 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9659 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9660 | #endif |
| 9661 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9662 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9663 | #endif |
| 9664 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9665 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9666 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9667 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9668 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9669 | #endif |
| 9670 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9671 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9672 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9673 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9674 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9675 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9676 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9677 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9678 | #endif |
| 9679 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9680 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9681 | #endif |
| 9682 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9683 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9684 | #endif |
| 9685 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9686 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9687 | #endif |
| 9688 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9689 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9690 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9691 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9692 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9693 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9694 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9695 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9696 | #endif |
| 9697 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9698 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9699 | #endif |
| 9700 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9701 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9702 | #endif |
| 9703 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9704 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9705 | #endif |
| 9706 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9707 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9708 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9709 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9710 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9711 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9712 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9713 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9714 | #endif |
| 9715 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9716 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9717 | #endif |
| 9718 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9719 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9720 | #endif |
| 9721 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9722 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9723 | #endif |
| 9724 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9725 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9726 | #endif |
| 9727 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9728 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9729 | #endif |
| 9730 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9731 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9732 | #endif |
| 9733 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9734 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9735 | #endif |
| 9736 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9737 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9738 | #endif |
| 9739 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9740 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9741 | #endif |
| 9742 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9743 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9744 | #endif |
| 9745 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9746 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9747 | #endif |
| 9748 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9749 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9750 | #endif |
| 9751 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9752 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9753 | #endif |
| 9754 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9755 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9756 | #endif |
| 9757 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9758 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9759 | #endif |
| 9760 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9761 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9762 | #endif |
| 9763 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9764 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9765 | #endif |
| 9766 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9767 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9768 | #endif |
| 9769 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9770 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9771 | #endif |
| 9772 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9773 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9774 | #endif |
| 9775 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9776 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9777 | #endif |
| 9778 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9779 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9780 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9781 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9782 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9783 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9784 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9785 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9786 | #endif |
| 9787 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9788 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9789 | #endif |
| 9790 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9791 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9792 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9793 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9794 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9795 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9796 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9797 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9798 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9799 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9800 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9801 | #endif |
| 9802 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9803 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9804 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9805 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9806 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9807 | #endif |
| 9808 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9809 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9810 | #endif |
| 9811 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9812 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9813 | #endif |
| 9814 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9815 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9816 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9817 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9818 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9819 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9820 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9821 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9822 | #endif |
| 9823 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9824 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9825 | #endif |
| 9826 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9827 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9828 | #endif |
| 9829 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9830 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9831 | #endif |
| 9832 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9833 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9834 | #endif |
| 9835 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9836 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9837 | #endif |
| 9838 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9839 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9840 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9841 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9842 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9843 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9844 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9845 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9846 | #endif |
| 9847 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9848 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9849 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9850 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9851 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9852 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9853 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9854 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9855 | #endif |
| 9856 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9857 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9858 | #endif |
| 9859 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9860 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9861 | #endif |
| 9862 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9863 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9864 | #endif |
| 9865 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9866 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9867 | #endif |
| 9868 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9869 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9870 | #endif |
| 9871 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9872 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9873 | #endif |
| 9874 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9875 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9876 | #endif |
| 9877 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9878 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9879 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9880 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9881 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9882 | #endif |
| 9883 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9884 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9885 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9886 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9887 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9888 | #endif |
| 9889 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9890 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9891 | #endif |
| 9892 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9893 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9894 | #endif |
| 9895 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9896 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9897 | #endif |
| 9898 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9899 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9900 | #endif |
| 9901 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9902 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9903 | #endif |
| 9904 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9905 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9906 | #endif |
| 9907 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9908 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9909 | #endif |
| 9910 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9911 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9912 | #endif |
| 9913 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9914 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9915 | #endif |
| 9916 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9917 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9918 | #endif |
| 9919 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9920 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9921 | #endif |
| 9922 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9923 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9924 | #endif |
| 9925 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9926 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9927 | #endif |
| 9928 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9929 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9930 | #endif |
| 9931 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9932 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9933 | #endif |
| 9934 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9935 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9936 | #endif |
| 9937 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9938 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9939 | #endif |
| 9940 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9941 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9942 | #endif |
| 9943 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9944 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9945 | #endif |
| 9946 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9947 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9948 | #endif |
| 9949 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9950 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9951 | #endif |
| 9952 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9953 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9954 | #endif |
| 9955 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9956 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9957 | #endif |
| 9958 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9959 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9960 | #endif |
| 9961 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9962 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9963 | #endif |
| 9964 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9965 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9966 | #endif |
| 9967 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9968 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9969 | #endif |
| 9970 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9971 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9972 | #endif |
| 9973 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9974 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9975 | #endif |
| 9976 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9977 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9978 | #endif |
| 9979 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9980 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9981 | #endif |
| 9982 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9983 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9984 | #endif |
| 9985 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9986 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9987 | #endif |
| 9988 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9989 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9990 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9991 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9992 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9993 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9994 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9995 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9996 | #endif |
| 9997 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9998 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9999 | #endif |
| 10000 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10001 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10002 | #endif |
| 10003 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10004 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10005 | #endif |
| 10006 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10007 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10008 | #endif |
| 10009 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10010 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10011 | #endif |
| 10012 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10013 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10014 | #endif |
| 10015 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10016 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10017 | #endif |
| 10018 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10019 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10020 | #endif |
| 10021 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10022 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10023 | #endif |
| 10024 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10025 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10026 | #endif |
| 10027 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10028 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10029 | #endif |
| 10030 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10031 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10032 | #endif |
| 10033 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10034 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10035 | #endif |
| 10036 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10037 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10038 | #endif |
| 10039 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10040 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10041 | #endif |
| 10042 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10043 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10044 | #endif |
| 10045 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10046 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10047 | #endif |
| 10048 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10049 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10050 | #endif |
| 10051 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10052 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10053 | #endif |
| 10054 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10055 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10056 | #endif |
| 10057 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10058 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10059 | #endif |
| 10060 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10061 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10062 | #endif |
| 10063 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10064 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10065 | #endif |
| 10066 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10067 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10068 | #endif |
| 10069 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10070 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10071 | #endif |
| 10072 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10073 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10074 | #endif |
| 10075 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10076 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10077 | #endif |
| 10078 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10079 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10080 | #endif |
| 10081 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10082 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10083 | #endif |
| 10084 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10085 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10086 | #endif |
| 10087 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10088 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10089 | #endif |
| 10090 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10091 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10092 | #endif |
| 10093 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10094 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10095 | #endif |
| 10096 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10097 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10098 | #endif |
| 10099 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10100 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10101 | #endif |
| 10102 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10103 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10104 | #endif |
| 10105 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10106 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10107 | #endif |
| 10108 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10109 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10110 | #endif |
| 10111 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10112 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10113 | #endif |
| 10114 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10115 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10116 | #endif |
| 10117 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10118 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10119 | #endif |
| 10120 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10121 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10122 | #endif |
| 10123 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10124 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10125 | #endif |
| 10126 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10127 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10128 | #endif |
| 10129 | }; |
| 10130 | |
| 10131 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10132 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10133 | { |
| 10134 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 10135 | sizeof(posix_constants_sysconf) |
| 10136 | / sizeof(struct constdef)); |
| 10137 | } |
| 10138 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10139 | |
| 10140 | /*[clinic input] |
| 10141 | os.sysconf -> long |
| 10142 | name: sysconf_confname |
| 10143 | / |
| 10144 | |
| 10145 | Return an integer-valued system configuration variable. |
| 10146 | [clinic start generated code]*/ |
| 10147 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10148 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10149 | os_sysconf_impl(PyObject *module, int name) |
| 10150 | /*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10151 | { |
| 10152 | long value; |
| 10153 | |
| 10154 | errno = 0; |
| 10155 | value = sysconf(name); |
| 10156 | if (value == -1 && errno != 0) |
| 10157 | posix_error(); |
| 10158 | return value; |
| 10159 | } |
| 10160 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10161 | |
| 10162 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10163 | /* 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] | 10164 | * 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] | 10165 | * the exported dictionaries that are used to publish information about the |
| 10166 | * names available on the host platform. |
| 10167 | * |
| 10168 | * Sorting the table at runtime ensures that the table is properly ordered |
| 10169 | * when used, even for platforms we're not able to test on. It also makes |
| 10170 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10171 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10172 | |
| 10173 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10174 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10175 | { |
| 10176 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10177 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10178 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10179 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10180 | |
| 10181 | return strcmp(c1->name, c2->name); |
| 10182 | } |
| 10183 | |
| 10184 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10185 | setup_confname_table(struct constdef *table, size_t tablesize, |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 10186 | const char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10187 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10188 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10189 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10190 | |
| 10191 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 10192 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10193 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10194 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10195 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10196 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10197 | PyObject *o = PyLong_FromLong(table[i].value); |
| 10198 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 10199 | Py_XDECREF(o); |
| 10200 | Py_DECREF(d); |
| 10201 | return -1; |
| 10202 | } |
| 10203 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10204 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10205 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10206 | } |
| 10207 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10208 | /* Return -1 on failure, 0 on success. */ |
| 10209 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10210 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10211 | { |
| 10212 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10213 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10214 | sizeof(posix_constants_pathconf) |
| 10215 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10216 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10217 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10218 | #endif |
| 10219 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10220 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10221 | sizeof(posix_constants_confstr) |
| 10222 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10223 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10224 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10225 | #endif |
| 10226 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10227 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10228 | sizeof(posix_constants_sysconf) |
| 10229 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10230 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10231 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10232 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10233 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10234 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10235 | |
| 10236 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10237 | /*[clinic input] |
| 10238 | os.abort |
| 10239 | |
| 10240 | Abort the interpreter immediately. |
| 10241 | |
| 10242 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 10243 | on the hosting operating system. This function never returns. |
| 10244 | [clinic start generated code]*/ |
| 10245 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10246 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10247 | os_abort_impl(PyObject *module) |
| 10248 | /*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10249 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10250 | abort(); |
| 10251 | /*NOTREACHED*/ |
| 10252 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 10253 | return NULL; |
| 10254 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10255 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10256 | #ifdef MS_WINDOWS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10257 | /* Grab ShellExecute dynamically from shell32 */ |
| 10258 | static int has_ShellExecute = -1; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10259 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 10260 | LPCWSTR, INT); |
| 10261 | static int |
| 10262 | check_ShellExecute() |
| 10263 | { |
| 10264 | HINSTANCE hShell32; |
| 10265 | |
| 10266 | /* only recheck */ |
| 10267 | if (-1 == has_ShellExecute) { |
| 10268 | Py_BEGIN_ALLOW_THREADS |
| 10269 | hShell32 = LoadLibraryW(L"SHELL32"); |
| 10270 | Py_END_ALLOW_THREADS |
| 10271 | if (hShell32) { |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10272 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 10273 | "ShellExecuteW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10274 | has_ShellExecute = Py_ShellExecuteW != NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10275 | } else { |
| 10276 | has_ShellExecute = 0; |
| 10277 | } |
| 10278 | } |
| 10279 | return has_ShellExecute; |
| 10280 | } |
| 10281 | |
| 10282 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10283 | /*[clinic input] |
| 10284 | os.startfile |
| 10285 | filepath: path_t |
| 10286 | operation: Py_UNICODE = NULL |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 10287 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10288 | startfile(filepath [, operation]) |
| 10289 | |
| 10290 | Start a file with its associated application. |
| 10291 | |
| 10292 | When "operation" is not specified or "open", this acts like |
| 10293 | double-clicking the file in Explorer, or giving the file name as an |
| 10294 | argument to the DOS "start" command: the file is opened with whatever |
| 10295 | application (if any) its extension is associated. |
| 10296 | When another "operation" is given, it specifies what should be done with |
| 10297 | the file. A typical operation is "print". |
| 10298 | |
| 10299 | startfile returns as soon as the associated application is launched. |
| 10300 | There is no option to wait for the application to close, and no way |
| 10301 | to retrieve the application's exit status. |
| 10302 | |
| 10303 | The filepath is relative to the current directory. If you want to use |
| 10304 | an absolute path, make sure the first character is not a slash ("/"); |
| 10305 | the underlying Win32 ShellExecute function doesn't work if it is. |
| 10306 | [clinic start generated code]*/ |
| 10307 | |
| 10308 | static PyObject * |
| 10309 | os_startfile_impl(PyObject *module, path_t *filepath, Py_UNICODE *operation) |
| 10310 | /*[clinic end generated code: output=912ceba79acfa1c9 input=63950bf2986380d0]*/ |
| 10311 | { |
| 10312 | HINSTANCE rc; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10313 | |
| 10314 | if(!check_ShellExecute()) { |
| 10315 | /* If the OS doesn't have ShellExecute, return a |
| 10316 | NotImplementedError. */ |
| 10317 | return PyErr_Format(PyExc_NotImplementedError, |
| 10318 | "startfile not available on this platform"); |
| 10319 | } |
| 10320 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10321 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10322 | rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide, |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10323 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10324 | Py_END_ALLOW_THREADS |
| 10325 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10326 | if (rc <= (HINSTANCE)32) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10327 | win32_error_object("startfile", filepath->object); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10328 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10329 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10330 | Py_RETURN_NONE; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10331 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10332 | #endif /* MS_WINDOWS */ |
| 10333 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10334 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10335 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10336 | /*[clinic input] |
| 10337 | os.getloadavg |
| 10338 | |
| 10339 | Return average recent system load information. |
| 10340 | |
| 10341 | Return the number of processes in the system run queue averaged over |
| 10342 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 10343 | Raises OSError if the load average was unobtainable. |
| 10344 | [clinic start generated code]*/ |
| 10345 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10346 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10347 | os_getloadavg_impl(PyObject *module) |
| 10348 | /*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10349 | { |
| 10350 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10351 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10352 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 10353 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10354 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10355 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10356 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10357 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10358 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10359 | |
| 10360 | /*[clinic input] |
| 10361 | os.device_encoding |
| 10362 | fd: int |
| 10363 | |
| 10364 | Return a string describing the encoding of a terminal's file descriptor. |
| 10365 | |
| 10366 | The file descriptor must be attached to a terminal. |
| 10367 | If the device is not a terminal, return None. |
| 10368 | [clinic start generated code]*/ |
| 10369 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10370 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10371 | os_device_encoding_impl(PyObject *module, int fd) |
| 10372 | /*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10373 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10374 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 10375 | } |
| 10376 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10377 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10378 | #ifdef HAVE_SETRESUID |
| 10379 | /*[clinic input] |
| 10380 | os.setresuid |
| 10381 | |
| 10382 | ruid: uid_t |
| 10383 | euid: uid_t |
| 10384 | suid: uid_t |
| 10385 | / |
| 10386 | |
| 10387 | Set the current process's real, effective, and saved user ids. |
| 10388 | [clinic start generated code]*/ |
| 10389 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10390 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10391 | os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid) |
| 10392 | /*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10393 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10394 | if (setresuid(ruid, euid, suid) < 0) |
| 10395 | return posix_error(); |
| 10396 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10397 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10398 | #endif /* HAVE_SETRESUID */ |
| 10399 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10400 | |
| 10401 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10402 | /*[clinic input] |
| 10403 | os.setresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10404 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10405 | rgid: gid_t |
| 10406 | egid: gid_t |
| 10407 | sgid: gid_t |
| 10408 | / |
| 10409 | |
| 10410 | Set the current process's real, effective, and saved group ids. |
| 10411 | [clinic start generated code]*/ |
| 10412 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10413 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10414 | os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid) |
| 10415 | /*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10416 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10417 | if (setresgid(rgid, egid, sgid) < 0) |
| 10418 | return posix_error(); |
| 10419 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10420 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10421 | #endif /* HAVE_SETRESGID */ |
| 10422 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10423 | |
| 10424 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10425 | /*[clinic input] |
| 10426 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10427 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10428 | Return a tuple of the current process's real, effective, and saved user ids. |
| 10429 | [clinic start generated code]*/ |
| 10430 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10431 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10432 | os_getresuid_impl(PyObject *module) |
| 10433 | /*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10434 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10435 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10436 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 10437 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10438 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 10439 | _PyLong_FromUid(euid), |
| 10440 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10441 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10442 | #endif /* HAVE_GETRESUID */ |
| 10443 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10444 | |
| 10445 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10446 | /*[clinic input] |
| 10447 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10448 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10449 | Return a tuple of the current process's real, effective, and saved group ids. |
| 10450 | [clinic start generated code]*/ |
| 10451 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10452 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10453 | os_getresgid_impl(PyObject *module) |
| 10454 | /*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10455 | { |
| 10456 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10457 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 10458 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10459 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 10460 | _PyLong_FromGid(egid), |
| 10461 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10462 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10463 | #endif /* HAVE_GETRESGID */ |
| 10464 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10465 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10466 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10467 | /*[clinic input] |
| 10468 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10469 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10470 | path: path_t(allow_fd=True) |
| 10471 | attribute: path_t |
| 10472 | * |
| 10473 | follow_symlinks: bool = True |
| 10474 | |
| 10475 | Return the value of extended attribute attribute on path. |
| 10476 | |
| 10477 | path may be either a string or an open file descriptor. |
| 10478 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10479 | link, getxattr will examine the symbolic link itself instead of the file |
| 10480 | the link points to. |
| 10481 | |
| 10482 | [clinic start generated code]*/ |
| 10483 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10484 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10485 | os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10486 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10487 | /*[clinic end generated code: output=5f2f44200a43cff2 input=8c8ea3bab78d89c2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10488 | { |
| 10489 | Py_ssize_t i; |
| 10490 | PyObject *buffer = NULL; |
| 10491 | |
| 10492 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 10493 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10494 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10495 | for (i = 0; ; i++) { |
| 10496 | void *ptr; |
| 10497 | ssize_t result; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 10498 | static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10499 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10500 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10501 | path_error(path); |
| 10502 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10503 | } |
| 10504 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 10505 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10506 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10507 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10508 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10509 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10510 | if (path->fd >= 0) |
| 10511 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10512 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10513 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10514 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10515 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10516 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10517 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10518 | if (result < 0) { |
| 10519 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10520 | if (errno == ERANGE) |
| 10521 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10522 | path_error(path); |
| 10523 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10524 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10525 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10526 | if (result != buffer_size) { |
| 10527 | /* Can only shrink. */ |
| 10528 | _PyBytes_Resize(&buffer, result); |
| 10529 | } |
| 10530 | break; |
| 10531 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10532 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10533 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10534 | } |
| 10535 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10536 | |
| 10537 | /*[clinic input] |
| 10538 | os.setxattr |
| 10539 | |
| 10540 | path: path_t(allow_fd=True) |
| 10541 | attribute: path_t |
| 10542 | value: Py_buffer |
| 10543 | flags: int = 0 |
| 10544 | * |
| 10545 | follow_symlinks: bool = True |
| 10546 | |
| 10547 | Set extended attribute attribute on path to value. |
| 10548 | |
| 10549 | path may be either a string or an open file descriptor. |
| 10550 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10551 | link, setxattr will modify the symbolic link itself instead of the file |
| 10552 | the link points to. |
| 10553 | |
| 10554 | [clinic start generated code]*/ |
| 10555 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10556 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10557 | os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10558 | Py_buffer *value, int flags, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10559 | /*[clinic end generated code: output=98b83f63fdde26bb input=f0d26833992015c2]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10560 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10561 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10562 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10563 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10564 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10565 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10566 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10567 | if (path->fd > -1) |
| 10568 | result = fsetxattr(path->fd, attribute->narrow, |
| 10569 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10570 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10571 | result = setxattr(path->narrow, attribute->narrow, |
| 10572 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10573 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10574 | result = lsetxattr(path->narrow, attribute->narrow, |
| 10575 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10576 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10577 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10578 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10579 | path_error(path); |
| 10580 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10581 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10582 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10583 | Py_RETURN_NONE; |
| 10584 | } |
| 10585 | |
| 10586 | |
| 10587 | /*[clinic input] |
| 10588 | os.removexattr |
| 10589 | |
| 10590 | path: path_t(allow_fd=True) |
| 10591 | attribute: path_t |
| 10592 | * |
| 10593 | follow_symlinks: bool = True |
| 10594 | |
| 10595 | Remove extended attribute attribute on path. |
| 10596 | |
| 10597 | path may be either a string or an open file descriptor. |
| 10598 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10599 | link, removexattr will modify the symbolic link itself instead of the file |
| 10600 | the link points to. |
| 10601 | |
| 10602 | [clinic start generated code]*/ |
| 10603 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10604 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10605 | os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10606 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10607 | /*[clinic end generated code: output=521a51817980cda6 input=cdb54834161e3329]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10608 | { |
| 10609 | ssize_t result; |
| 10610 | |
| 10611 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 10612 | return NULL; |
| 10613 | |
| 10614 | Py_BEGIN_ALLOW_THREADS; |
| 10615 | if (path->fd > -1) |
| 10616 | result = fremovexattr(path->fd, attribute->narrow); |
| 10617 | else if (follow_symlinks) |
| 10618 | result = removexattr(path->narrow, attribute->narrow); |
| 10619 | else |
| 10620 | result = lremovexattr(path->narrow, attribute->narrow); |
| 10621 | Py_END_ALLOW_THREADS; |
| 10622 | |
| 10623 | if (result) { |
| 10624 | return path_error(path); |
| 10625 | } |
| 10626 | |
| 10627 | Py_RETURN_NONE; |
| 10628 | } |
| 10629 | |
| 10630 | |
| 10631 | /*[clinic input] |
| 10632 | os.listxattr |
| 10633 | |
| 10634 | path: path_t(allow_fd=True, nullable=True) = None |
| 10635 | * |
| 10636 | follow_symlinks: bool = True |
| 10637 | |
| 10638 | Return a list of extended attributes on path. |
| 10639 | |
| 10640 | path may be either None, a string, or an open file descriptor. |
| 10641 | if path is None, listxattr will examine the current directory. |
| 10642 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10643 | link, listxattr will examine the symbolic link itself instead of the file |
| 10644 | the link points to. |
| 10645 | [clinic start generated code]*/ |
| 10646 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10647 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10648 | os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks) |
| 10649 | /*[clinic end generated code: output=bebdb4e2ad0ce435 input=08cca53ac0b07c13]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10650 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10651 | Py_ssize_t i; |
| 10652 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10653 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10654 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10655 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10656 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10657 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10658 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10659 | name = path->narrow ? path->narrow : "."; |
| 10660 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10661 | for (i = 0; ; i++) { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 10662 | const char *start, *trace, *end; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10663 | ssize_t length; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 10664 | static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10665 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10666 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 10667 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10668 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10669 | break; |
| 10670 | } |
| 10671 | buffer = PyMem_MALLOC(buffer_size); |
| 10672 | if (!buffer) { |
| 10673 | PyErr_NoMemory(); |
| 10674 | break; |
| 10675 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10676 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10677 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10678 | if (path->fd > -1) |
| 10679 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10680 | else if (follow_symlinks) |
| 10681 | length = listxattr(name, buffer, buffer_size); |
| 10682 | else |
| 10683 | length = llistxattr(name, buffer, buffer_size); |
| 10684 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10685 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10686 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 10687 | if (errno == ERANGE) { |
| 10688 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 10689 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10690 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 10691 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10692 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10693 | break; |
| 10694 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10695 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10696 | result = PyList_New(0); |
| 10697 | if (!result) { |
| 10698 | goto exit; |
| 10699 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10700 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10701 | end = buffer + length; |
| 10702 | for (trace = start = buffer; trace != end; trace++) { |
| 10703 | if (!*trace) { |
| 10704 | int error; |
| 10705 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 10706 | trace - start); |
| 10707 | if (!attribute) { |
| 10708 | Py_DECREF(result); |
| 10709 | result = NULL; |
| 10710 | goto exit; |
| 10711 | } |
| 10712 | error = PyList_Append(result, attribute); |
| 10713 | Py_DECREF(attribute); |
| 10714 | if (error) { |
| 10715 | Py_DECREF(result); |
| 10716 | result = NULL; |
| 10717 | goto exit; |
| 10718 | } |
| 10719 | start = trace + 1; |
| 10720 | } |
| 10721 | } |
| 10722 | break; |
| 10723 | } |
| 10724 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10725 | if (buffer) |
| 10726 | PyMem_FREE(buffer); |
| 10727 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10728 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10729 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10730 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10731 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10732 | /*[clinic input] |
| 10733 | os.urandom |
| 10734 | |
| 10735 | size: Py_ssize_t |
| 10736 | / |
| 10737 | |
| 10738 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 10739 | [clinic start generated code]*/ |
| 10740 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10741 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10742 | os_urandom_impl(PyObject *module, Py_ssize_t size) |
| 10743 | /*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10744 | { |
| 10745 | PyObject *bytes; |
| 10746 | int result; |
| 10747 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10748 | if (size < 0) |
| 10749 | return PyErr_Format(PyExc_ValueError, |
| 10750 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10751 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 10752 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10753 | return NULL; |
| 10754 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 10755 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10756 | if (result == -1) { |
| 10757 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10758 | return NULL; |
| 10759 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10760 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10761 | } |
| 10762 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10763 | /* Terminal size querying */ |
| 10764 | |
| 10765 | static PyTypeObject TerminalSizeType; |
| 10766 | |
| 10767 | PyDoc_STRVAR(TerminalSize_docstring, |
| 10768 | "A tuple of (columns, lines) for holding terminal window size"); |
| 10769 | |
| 10770 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 10771 | {"columns", "width of the terminal window in characters"}, |
| 10772 | {"lines", "height of the terminal window in characters"}, |
| 10773 | {NULL, NULL} |
| 10774 | }; |
| 10775 | |
| 10776 | static PyStructSequence_Desc TerminalSize_desc = { |
| 10777 | "os.terminal_size", |
| 10778 | TerminalSize_docstring, |
| 10779 | TerminalSize_fields, |
| 10780 | 2, |
| 10781 | }; |
| 10782 | |
| 10783 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10784 | /* AC 3.5: fd should accept None */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10785 | PyDoc_STRVAR(termsize__doc__, |
| 10786 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 10787 | "\n" \ |
| 10788 | "The optional argument fd (default standard output) specifies\n" \ |
| 10789 | "which file descriptor should be queried.\n" \ |
| 10790 | "\n" \ |
| 10791 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 10792 | "is thrown.\n" \ |
| 10793 | "\n" \ |
| 10794 | "This function will only be defined if an implementation is\n" \ |
| 10795 | "available for this system.\n" \ |
| 10796 | "\n" \ |
| 10797 | "shutil.get_terminal_size is the high-level function which should \n" \ |
| 10798 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 10799 | |
| 10800 | static PyObject* |
| 10801 | get_terminal_size(PyObject *self, PyObject *args) |
| 10802 | { |
| 10803 | int columns, lines; |
| 10804 | PyObject *termsize; |
| 10805 | |
| 10806 | int fd = fileno(stdout); |
| 10807 | /* Under some conditions stdout may not be connected and |
| 10808 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 10809 | * GUI apps don't have valid standard streams by default. |
| 10810 | * |
| 10811 | * If this happens, and the optional fd argument is not present, |
| 10812 | * the ioctl below will fail returning EBADF. This is what we want. |
| 10813 | */ |
| 10814 | |
| 10815 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 10816 | return NULL; |
| 10817 | |
| 10818 | #ifdef TERMSIZE_USE_IOCTL |
| 10819 | { |
| 10820 | struct winsize w; |
| 10821 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 10822 | return PyErr_SetFromErrno(PyExc_OSError); |
| 10823 | columns = w.ws_col; |
| 10824 | lines = w.ws_row; |
| 10825 | } |
| 10826 | #endif /* TERMSIZE_USE_IOCTL */ |
| 10827 | |
| 10828 | #ifdef TERMSIZE_USE_CONIO |
| 10829 | { |
| 10830 | DWORD nhandle; |
| 10831 | HANDLE handle; |
| 10832 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 10833 | switch (fd) { |
| 10834 | case 0: nhandle = STD_INPUT_HANDLE; |
| 10835 | break; |
| 10836 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 10837 | break; |
| 10838 | case 2: nhandle = STD_ERROR_HANDLE; |
| 10839 | break; |
| 10840 | default: |
| 10841 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 10842 | } |
| 10843 | handle = GetStdHandle(nhandle); |
| 10844 | if (handle == NULL) |
| 10845 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 10846 | if (handle == INVALID_HANDLE_VALUE) |
| 10847 | return PyErr_SetFromWindowsErr(0); |
| 10848 | |
| 10849 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 10850 | return PyErr_SetFromWindowsErr(0); |
| 10851 | |
| 10852 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 10853 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 10854 | } |
| 10855 | #endif /* TERMSIZE_USE_CONIO */ |
| 10856 | |
| 10857 | termsize = PyStructSequence_New(&TerminalSizeType); |
| 10858 | if (termsize == NULL) |
| 10859 | return NULL; |
| 10860 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 10861 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 10862 | if (PyErr_Occurred()) { |
| 10863 | Py_DECREF(termsize); |
| 10864 | return NULL; |
| 10865 | } |
| 10866 | return termsize; |
| 10867 | } |
| 10868 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 10869 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10870 | |
| 10871 | /*[clinic input] |
| 10872 | os.cpu_count |
| 10873 | |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 10874 | Return the number of CPUs in the system; return None if indeterminable. |
| 10875 | |
| 10876 | This number is not equivalent to the number of CPUs the current process can |
| 10877 | use. The number of usable CPUs can be obtained with |
| 10878 | ``len(os.sched_getaffinity(0))`` |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10879 | [clinic start generated code]*/ |
| 10880 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10881 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10882 | os_cpu_count_impl(PyObject *module) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 10883 | /*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10884 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 10885 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10886 | #ifdef MS_WINDOWS |
| 10887 | SYSTEM_INFO sysinfo; |
| 10888 | GetSystemInfo(&sysinfo); |
| 10889 | ncpu = sysinfo.dwNumberOfProcessors; |
| 10890 | #elif defined(__hpux) |
| 10891 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 10892 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 10893 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10894 | #elif defined(__DragonFly__) || \ |
| 10895 | defined(__OpenBSD__) || \ |
| 10896 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 10897 | defined(__NetBSD__) || \ |
| 10898 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 10899 | int mib[2]; |
| 10900 | size_t len = sizeof(ncpu); |
| 10901 | mib[0] = CTL_HW; |
| 10902 | mib[1] = HW_NCPU; |
| 10903 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 10904 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10905 | #endif |
| 10906 | if (ncpu >= 1) |
| 10907 | return PyLong_FromLong(ncpu); |
| 10908 | else |
| 10909 | Py_RETURN_NONE; |
| 10910 | } |
| 10911 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10912 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10913 | /*[clinic input] |
| 10914 | os.get_inheritable -> bool |
| 10915 | |
| 10916 | fd: int |
| 10917 | / |
| 10918 | |
| 10919 | Get the close-on-exe flag of the specified file descriptor. |
| 10920 | [clinic start generated code]*/ |
| 10921 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10922 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10923 | os_get_inheritable_impl(PyObject *module, int fd) |
| 10924 | /*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10925 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 10926 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 10927 | _Py_BEGIN_SUPPRESS_IPH |
| 10928 | return_value = _Py_get_inheritable(fd); |
| 10929 | _Py_END_SUPPRESS_IPH |
| 10930 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10931 | } |
| 10932 | |
| 10933 | |
| 10934 | /*[clinic input] |
| 10935 | os.set_inheritable |
| 10936 | fd: int |
| 10937 | inheritable: int |
| 10938 | / |
| 10939 | |
| 10940 | Set the inheritable flag of the specified file descriptor. |
| 10941 | [clinic start generated code]*/ |
| 10942 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10943 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10944 | os_set_inheritable_impl(PyObject *module, int fd, int inheritable) |
| 10945 | /*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10946 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 10947 | int result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10948 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 10949 | _Py_BEGIN_SUPPRESS_IPH |
| 10950 | result = _Py_set_inheritable(fd, inheritable, NULL); |
| 10951 | _Py_END_SUPPRESS_IPH |
| 10952 | if (result < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10953 | return NULL; |
| 10954 | Py_RETURN_NONE; |
| 10955 | } |
| 10956 | |
| 10957 | |
| 10958 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10959 | /*[clinic input] |
| 10960 | os.get_handle_inheritable -> bool |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 10961 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10962 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10963 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10964 | Get the close-on-exe flag of the specified file descriptor. |
| 10965 | [clinic start generated code]*/ |
| 10966 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10967 | static int |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 10968 | os_get_handle_inheritable_impl(PyObject *module, intptr_t handle) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 10969 | /*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10970 | { |
| 10971 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10972 | |
| 10973 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 10974 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10975 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10976 | } |
| 10977 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10978 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10979 | } |
| 10980 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10981 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10982 | /*[clinic input] |
| 10983 | os.set_handle_inheritable |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 10984 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10985 | inheritable: bool |
| 10986 | / |
| 10987 | |
| 10988 | Set the inheritable flag of the specified handle. |
| 10989 | [clinic start generated code]*/ |
| 10990 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10991 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 10992 | os_set_handle_inheritable_impl(PyObject *module, intptr_t handle, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10993 | int inheritable) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 10994 | /*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10995 | { |
| 10996 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10997 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 10998 | PyErr_SetFromWindowsErr(0); |
| 10999 | return NULL; |
| 11000 | } |
| 11001 | Py_RETURN_NONE; |
| 11002 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11003 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11004 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11005 | #ifndef MS_WINDOWS |
| 11006 | PyDoc_STRVAR(get_blocking__doc__, |
| 11007 | "get_blocking(fd) -> bool\n" \ |
| 11008 | "\n" \ |
| 11009 | "Get the blocking mode of the file descriptor:\n" \ |
| 11010 | "False if the O_NONBLOCK flag is set, True if the flag is cleared."); |
| 11011 | |
| 11012 | static PyObject* |
| 11013 | posix_get_blocking(PyObject *self, PyObject *args) |
| 11014 | { |
| 11015 | int fd; |
| 11016 | int blocking; |
| 11017 | |
| 11018 | if (!PyArg_ParseTuple(args, "i:get_blocking", &fd)) |
| 11019 | return NULL; |
| 11020 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11021 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11022 | blocking = _Py_get_blocking(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11023 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11024 | if (blocking < 0) |
| 11025 | return NULL; |
| 11026 | return PyBool_FromLong(blocking); |
| 11027 | } |
| 11028 | |
| 11029 | PyDoc_STRVAR(set_blocking__doc__, |
| 11030 | "set_blocking(fd, blocking)\n" \ |
| 11031 | "\n" \ |
| 11032 | "Set the blocking mode of the specified file descriptor.\n" \ |
| 11033 | "Set the O_NONBLOCK flag if blocking is False,\n" \ |
| 11034 | "clear the O_NONBLOCK flag otherwise."); |
| 11035 | |
| 11036 | static PyObject* |
| 11037 | posix_set_blocking(PyObject *self, PyObject *args) |
| 11038 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11039 | int fd, blocking, result; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11040 | |
| 11041 | if (!PyArg_ParseTuple(args, "ii:set_blocking", &fd, &blocking)) |
| 11042 | return NULL; |
| 11043 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11044 | _Py_BEGIN_SUPPRESS_IPH |
| 11045 | result = _Py_set_blocking(fd, blocking); |
| 11046 | _Py_END_SUPPRESS_IPH |
| 11047 | if (result < 0) |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11048 | return NULL; |
| 11049 | Py_RETURN_NONE; |
| 11050 | } |
| 11051 | #endif /* !MS_WINDOWS */ |
| 11052 | |
| 11053 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11054 | PyDoc_STRVAR(posix_scandir__doc__, |
| 11055 | "scandir(path='.') -> iterator of DirEntry objects for given path"); |
| 11056 | |
| 11057 | static char *follow_symlinks_keywords[] = {"follow_symlinks", NULL}; |
| 11058 | |
| 11059 | typedef struct { |
| 11060 | PyObject_HEAD |
| 11061 | PyObject *name; |
| 11062 | PyObject *path; |
| 11063 | PyObject *stat; |
| 11064 | PyObject *lstat; |
| 11065 | #ifdef MS_WINDOWS |
| 11066 | struct _Py_stat_struct win32_lstat; |
| 11067 | __int64 win32_file_index; |
| 11068 | int got_file_index; |
| 11069 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11070 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11071 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11072 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11073 | ino_t d_ino; |
| 11074 | #endif |
| 11075 | } DirEntry; |
| 11076 | |
| 11077 | static void |
| 11078 | DirEntry_dealloc(DirEntry *entry) |
| 11079 | { |
| 11080 | Py_XDECREF(entry->name); |
| 11081 | Py_XDECREF(entry->path); |
| 11082 | Py_XDECREF(entry->stat); |
| 11083 | Py_XDECREF(entry->lstat); |
| 11084 | Py_TYPE(entry)->tp_free((PyObject *)entry); |
| 11085 | } |
| 11086 | |
| 11087 | /* Forward reference */ |
| 11088 | static int |
| 11089 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits); |
| 11090 | |
| 11091 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 11092 | static int |
| 11093 | DirEntry_is_symlink(DirEntry *self) |
| 11094 | { |
| 11095 | #ifdef MS_WINDOWS |
| 11096 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11097 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 11098 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11099 | if (self->d_type != DT_UNKNOWN) |
| 11100 | return self->d_type == DT_LNK; |
| 11101 | else |
| 11102 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11103 | #else |
| 11104 | /* POSIX without d_type */ |
| 11105 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11106 | #endif |
| 11107 | } |
| 11108 | |
| 11109 | static PyObject * |
| 11110 | DirEntry_py_is_symlink(DirEntry *self) |
| 11111 | { |
| 11112 | int result; |
| 11113 | |
| 11114 | result = DirEntry_is_symlink(self); |
| 11115 | if (result == -1) |
| 11116 | return NULL; |
| 11117 | return PyBool_FromLong(result); |
| 11118 | } |
| 11119 | |
| 11120 | static PyObject * |
| 11121 | DirEntry_fetch_stat(DirEntry *self, int follow_symlinks) |
| 11122 | { |
| 11123 | int result; |
| 11124 | struct _Py_stat_struct st; |
| 11125 | |
| 11126 | #ifdef MS_WINDOWS |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11127 | const wchar_t *path; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11128 | |
| 11129 | path = PyUnicode_AsUnicode(self->path); |
| 11130 | if (!path) |
| 11131 | return NULL; |
| 11132 | |
| 11133 | if (follow_symlinks) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11134 | result = win32_stat(path, &st); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11135 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11136 | result = win32_lstat(path, &st); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11137 | |
| 11138 | if (result != 0) { |
| 11139 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 11140 | 0, self->path); |
| 11141 | } |
| 11142 | #else /* POSIX */ |
| 11143 | PyObject *bytes; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11144 | const char *path; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11145 | |
| 11146 | if (!PyUnicode_FSConverter(self->path, &bytes)) |
| 11147 | return NULL; |
| 11148 | path = PyBytes_AS_STRING(bytes); |
| 11149 | |
| 11150 | if (follow_symlinks) |
| 11151 | result = STAT(path, &st); |
| 11152 | else |
| 11153 | result = LSTAT(path, &st); |
| 11154 | Py_DECREF(bytes); |
| 11155 | |
| 11156 | if (result != 0) |
| 11157 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, self->path); |
| 11158 | #endif |
| 11159 | |
| 11160 | return _pystat_fromstructstat(&st); |
| 11161 | } |
| 11162 | |
| 11163 | static PyObject * |
| 11164 | DirEntry_get_lstat(DirEntry *self) |
| 11165 | { |
| 11166 | if (!self->lstat) { |
| 11167 | #ifdef MS_WINDOWS |
| 11168 | self->lstat = _pystat_fromstructstat(&self->win32_lstat); |
| 11169 | #else /* POSIX */ |
| 11170 | self->lstat = DirEntry_fetch_stat(self, 0); |
| 11171 | #endif |
| 11172 | } |
| 11173 | Py_XINCREF(self->lstat); |
| 11174 | return self->lstat; |
| 11175 | } |
| 11176 | |
| 11177 | static PyObject * |
| 11178 | DirEntry_get_stat(DirEntry *self, int follow_symlinks) |
| 11179 | { |
| 11180 | if (!follow_symlinks) |
| 11181 | return DirEntry_get_lstat(self); |
| 11182 | |
| 11183 | if (!self->stat) { |
| 11184 | int result = DirEntry_is_symlink(self); |
| 11185 | if (result == -1) |
| 11186 | return NULL; |
| 11187 | else if (result) |
| 11188 | self->stat = DirEntry_fetch_stat(self, 1); |
| 11189 | else |
| 11190 | self->stat = DirEntry_get_lstat(self); |
| 11191 | } |
| 11192 | |
| 11193 | Py_XINCREF(self->stat); |
| 11194 | return self->stat; |
| 11195 | } |
| 11196 | |
| 11197 | static PyObject * |
| 11198 | DirEntry_stat(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 11199 | { |
| 11200 | int follow_symlinks = 1; |
| 11201 | |
| 11202 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.stat", |
| 11203 | follow_symlinks_keywords, &follow_symlinks)) |
| 11204 | return NULL; |
| 11205 | |
| 11206 | return DirEntry_get_stat(self, follow_symlinks); |
| 11207 | } |
| 11208 | |
| 11209 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 11210 | static int |
| 11211 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 11212 | { |
| 11213 | PyObject *stat = NULL; |
| 11214 | PyObject *st_mode = NULL; |
| 11215 | long mode; |
| 11216 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11217 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11218 | int is_symlink; |
| 11219 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11220 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11221 | #ifdef MS_WINDOWS |
| 11222 | unsigned long dir_bits; |
| 11223 | #endif |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11224 | _Py_IDENTIFIER(st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11225 | |
| 11226 | #ifdef MS_WINDOWS |
| 11227 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 11228 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11229 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11230 | is_symlink = self->d_type == DT_LNK; |
| 11231 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 11232 | #endif |
| 11233 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11234 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11235 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11236 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11237 | stat = DirEntry_get_stat(self, follow_symlinks); |
| 11238 | if (!stat) { |
| 11239 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 11240 | /* If file doesn't exist (anymore), then return False |
| 11241 | (i.e., say it's not a file/directory) */ |
| 11242 | PyErr_Clear(); |
| 11243 | return 0; |
| 11244 | } |
| 11245 | goto error; |
| 11246 | } |
| 11247 | st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode); |
| 11248 | if (!st_mode) |
| 11249 | goto error; |
| 11250 | |
| 11251 | mode = PyLong_AsLong(st_mode); |
| 11252 | if (mode == -1 && PyErr_Occurred()) |
| 11253 | goto error; |
| 11254 | Py_CLEAR(st_mode); |
| 11255 | Py_CLEAR(stat); |
| 11256 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11257 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11258 | } |
| 11259 | else if (is_symlink) { |
| 11260 | assert(mode_bits != S_IFLNK); |
| 11261 | result = 0; |
| 11262 | } |
| 11263 | else { |
| 11264 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 11265 | #ifdef MS_WINDOWS |
| 11266 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 11267 | if (mode_bits == S_IFDIR) |
| 11268 | result = dir_bits != 0; |
| 11269 | else |
| 11270 | result = dir_bits == 0; |
| 11271 | #else /* POSIX */ |
| 11272 | if (mode_bits == S_IFDIR) |
| 11273 | result = self->d_type == DT_DIR; |
| 11274 | else |
| 11275 | result = self->d_type == DT_REG; |
| 11276 | #endif |
| 11277 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11278 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11279 | |
| 11280 | return result; |
| 11281 | |
| 11282 | error: |
| 11283 | Py_XDECREF(st_mode); |
| 11284 | Py_XDECREF(stat); |
| 11285 | return -1; |
| 11286 | } |
| 11287 | |
| 11288 | static PyObject * |
| 11289 | DirEntry_py_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 11290 | { |
| 11291 | int result; |
| 11292 | |
| 11293 | result = DirEntry_test_mode(self, follow_symlinks, mode_bits); |
| 11294 | if (result == -1) |
| 11295 | return NULL; |
| 11296 | return PyBool_FromLong(result); |
| 11297 | } |
| 11298 | |
| 11299 | static PyObject * |
| 11300 | DirEntry_is_dir(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 11301 | { |
| 11302 | int follow_symlinks = 1; |
| 11303 | |
| 11304 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_dir", |
| 11305 | follow_symlinks_keywords, &follow_symlinks)) |
| 11306 | return NULL; |
| 11307 | |
| 11308 | return DirEntry_py_test_mode(self, follow_symlinks, S_IFDIR); |
| 11309 | } |
| 11310 | |
| 11311 | static PyObject * |
| 11312 | DirEntry_is_file(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 11313 | { |
| 11314 | int follow_symlinks = 1; |
| 11315 | |
| 11316 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_file", |
| 11317 | follow_symlinks_keywords, &follow_symlinks)) |
| 11318 | return NULL; |
| 11319 | |
| 11320 | return DirEntry_py_test_mode(self, follow_symlinks, S_IFREG); |
| 11321 | } |
| 11322 | |
| 11323 | static PyObject * |
| 11324 | DirEntry_inode(DirEntry *self) |
| 11325 | { |
| 11326 | #ifdef MS_WINDOWS |
| 11327 | if (!self->got_file_index) { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11328 | const wchar_t *path; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11329 | struct _Py_stat_struct stat; |
| 11330 | |
| 11331 | path = PyUnicode_AsUnicode(self->path); |
| 11332 | if (!path) |
| 11333 | return NULL; |
| 11334 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11335 | if (win32_lstat(path, &stat) != 0) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11336 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 11337 | 0, self->path); |
| 11338 | } |
| 11339 | |
| 11340 | self->win32_file_index = stat.st_ino; |
| 11341 | self->got_file_index = 1; |
| 11342 | } |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 11343 | return PyLong_FromLongLong((long long)self->win32_file_index); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11344 | #else /* POSIX */ |
| 11345 | #ifdef HAVE_LARGEFILE_SUPPORT |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 11346 | return PyLong_FromLongLong((long long)self->d_ino); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11347 | #else |
| 11348 | return PyLong_FromLong((long)self->d_ino); |
| 11349 | #endif |
| 11350 | #endif |
| 11351 | } |
| 11352 | |
| 11353 | static PyObject * |
| 11354 | DirEntry_repr(DirEntry *self) |
| 11355 | { |
| 11356 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 11357 | } |
| 11358 | |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 11359 | static PyObject * |
| 11360 | DirEntry_fspath(DirEntry *self) |
| 11361 | { |
| 11362 | Py_INCREF(self->path); |
| 11363 | return self->path; |
| 11364 | } |
| 11365 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11366 | static PyMemberDef DirEntry_members[] = { |
| 11367 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 11368 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 11369 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 11370 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 11371 | {NULL} |
| 11372 | }; |
| 11373 | |
| 11374 | static PyMethodDef DirEntry_methods[] = { |
| 11375 | {"is_dir", (PyCFunction)DirEntry_is_dir, METH_VARARGS | METH_KEYWORDS, |
| 11376 | "return True if the entry is a directory; cached per entry" |
| 11377 | }, |
| 11378 | {"is_file", (PyCFunction)DirEntry_is_file, METH_VARARGS | METH_KEYWORDS, |
| 11379 | "return True if the entry is a file; cached per entry" |
| 11380 | }, |
| 11381 | {"is_symlink", (PyCFunction)DirEntry_py_is_symlink, METH_NOARGS, |
| 11382 | "return True if the entry is a symbolic link; cached per entry" |
| 11383 | }, |
| 11384 | {"stat", (PyCFunction)DirEntry_stat, METH_VARARGS | METH_KEYWORDS, |
| 11385 | "return stat_result object for the entry; cached per entry" |
| 11386 | }, |
| 11387 | {"inode", (PyCFunction)DirEntry_inode, METH_NOARGS, |
| 11388 | "return inode of the entry; cached per entry", |
| 11389 | }, |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 11390 | {"__fspath__", (PyCFunction)DirEntry_fspath, METH_NOARGS, |
| 11391 | "returns the path for the entry", |
| 11392 | }, |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11393 | {NULL} |
| 11394 | }; |
| 11395 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 11396 | static PyTypeObject DirEntryType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11397 | PyVarObject_HEAD_INIT(NULL, 0) |
| 11398 | MODNAME ".DirEntry", /* tp_name */ |
| 11399 | sizeof(DirEntry), /* tp_basicsize */ |
| 11400 | 0, /* tp_itemsize */ |
| 11401 | /* methods */ |
| 11402 | (destructor)DirEntry_dealloc, /* tp_dealloc */ |
| 11403 | 0, /* tp_print */ |
| 11404 | 0, /* tp_getattr */ |
| 11405 | 0, /* tp_setattr */ |
| 11406 | 0, /* tp_compare */ |
| 11407 | (reprfunc)DirEntry_repr, /* tp_repr */ |
| 11408 | 0, /* tp_as_number */ |
| 11409 | 0, /* tp_as_sequence */ |
| 11410 | 0, /* tp_as_mapping */ |
| 11411 | 0, /* tp_hash */ |
| 11412 | 0, /* tp_call */ |
| 11413 | 0, /* tp_str */ |
| 11414 | 0, /* tp_getattro */ |
| 11415 | 0, /* tp_setattro */ |
| 11416 | 0, /* tp_as_buffer */ |
| 11417 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 11418 | 0, /* tp_doc */ |
| 11419 | 0, /* tp_traverse */ |
| 11420 | 0, /* tp_clear */ |
| 11421 | 0, /* tp_richcompare */ |
| 11422 | 0, /* tp_weaklistoffset */ |
| 11423 | 0, /* tp_iter */ |
| 11424 | 0, /* tp_iternext */ |
| 11425 | DirEntry_methods, /* tp_methods */ |
| 11426 | DirEntry_members, /* tp_members */ |
| 11427 | }; |
| 11428 | |
| 11429 | #ifdef MS_WINDOWS |
| 11430 | |
| 11431 | static wchar_t * |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11432 | join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11433 | { |
| 11434 | Py_ssize_t path_len; |
| 11435 | Py_ssize_t size; |
| 11436 | wchar_t *result; |
| 11437 | wchar_t ch; |
| 11438 | |
| 11439 | if (!path_wide) { /* Default arg: "." */ |
| 11440 | path_wide = L"."; |
| 11441 | path_len = 1; |
| 11442 | } |
| 11443 | else { |
| 11444 | path_len = wcslen(path_wide); |
| 11445 | } |
| 11446 | |
| 11447 | /* The +1's are for the path separator and the NUL */ |
| 11448 | size = path_len + 1 + wcslen(filename) + 1; |
| 11449 | result = PyMem_New(wchar_t, size); |
| 11450 | if (!result) { |
| 11451 | PyErr_NoMemory(); |
| 11452 | return NULL; |
| 11453 | } |
| 11454 | wcscpy(result, path_wide); |
| 11455 | if (path_len > 0) { |
| 11456 | ch = result[path_len - 1]; |
| 11457 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 11458 | result[path_len++] = SEP; |
| 11459 | wcscpy(result + path_len, filename); |
| 11460 | } |
| 11461 | return result; |
| 11462 | } |
| 11463 | |
| 11464 | static PyObject * |
| 11465 | DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW) |
| 11466 | { |
| 11467 | DirEntry *entry; |
| 11468 | BY_HANDLE_FILE_INFORMATION file_info; |
| 11469 | ULONG reparse_tag; |
| 11470 | wchar_t *joined_path; |
| 11471 | |
| 11472 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 11473 | if (!entry) |
| 11474 | return NULL; |
| 11475 | entry->name = NULL; |
| 11476 | entry->path = NULL; |
| 11477 | entry->stat = NULL; |
| 11478 | entry->lstat = NULL; |
| 11479 | entry->got_file_index = 0; |
| 11480 | |
| 11481 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 11482 | if (!entry->name) |
| 11483 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11484 | if (path->narrow) { |
| 11485 | Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name)); |
| 11486 | if (!entry->name) |
| 11487 | goto error; |
| 11488 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11489 | |
| 11490 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 11491 | if (!joined_path) |
| 11492 | goto error; |
| 11493 | |
| 11494 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 11495 | PyMem_Free(joined_path); |
| 11496 | if (!entry->path) |
| 11497 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11498 | if (path->narrow) { |
| 11499 | Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path)); |
| 11500 | if (!entry->path) |
| 11501 | goto error; |
| 11502 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11503 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11504 | find_data_to_file_info(dataW, &file_info, &reparse_tag); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11505 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 11506 | |
| 11507 | return (PyObject *)entry; |
| 11508 | |
| 11509 | error: |
| 11510 | Py_DECREF(entry); |
| 11511 | return NULL; |
| 11512 | } |
| 11513 | |
| 11514 | #else /* POSIX */ |
| 11515 | |
| 11516 | static char * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 11517 | 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] | 11518 | { |
| 11519 | Py_ssize_t path_len; |
| 11520 | Py_ssize_t size; |
| 11521 | char *result; |
| 11522 | |
| 11523 | if (!path_narrow) { /* Default arg: "." */ |
| 11524 | path_narrow = "."; |
| 11525 | path_len = 1; |
| 11526 | } |
| 11527 | else { |
| 11528 | path_len = strlen(path_narrow); |
| 11529 | } |
| 11530 | |
| 11531 | if (filename_len == -1) |
| 11532 | filename_len = strlen(filename); |
| 11533 | |
| 11534 | /* The +1's are for the path separator and the NUL */ |
| 11535 | size = path_len + 1 + filename_len + 1; |
| 11536 | result = PyMem_New(char, size); |
| 11537 | if (!result) { |
| 11538 | PyErr_NoMemory(); |
| 11539 | return NULL; |
| 11540 | } |
| 11541 | strcpy(result, path_narrow); |
| 11542 | if (path_len > 0 && result[path_len - 1] != '/') |
| 11543 | result[path_len++] = '/'; |
| 11544 | strcpy(result + path_len, filename); |
| 11545 | return result; |
| 11546 | } |
| 11547 | |
| 11548 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 11549 | 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] | 11550 | ino_t d_ino |
| 11551 | #ifdef HAVE_DIRENT_D_TYPE |
| 11552 | , unsigned char d_type |
| 11553 | #endif |
| 11554 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11555 | { |
| 11556 | DirEntry *entry; |
| 11557 | char *joined_path; |
| 11558 | |
| 11559 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 11560 | if (!entry) |
| 11561 | return NULL; |
| 11562 | entry->name = NULL; |
| 11563 | entry->path = NULL; |
| 11564 | entry->stat = NULL; |
| 11565 | entry->lstat = NULL; |
| 11566 | |
| 11567 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 11568 | if (!joined_path) |
| 11569 | goto error; |
| 11570 | |
| 11571 | if (!path->narrow || !PyBytes_Check(path->object)) { |
| 11572 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
| 11573 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
| 11574 | } |
| 11575 | else { |
| 11576 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
| 11577 | entry->path = PyBytes_FromString(joined_path); |
| 11578 | } |
| 11579 | PyMem_Free(joined_path); |
| 11580 | if (!entry->name || !entry->path) |
| 11581 | goto error; |
| 11582 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11583 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11584 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11585 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11586 | entry->d_ino = d_ino; |
| 11587 | |
| 11588 | return (PyObject *)entry; |
| 11589 | |
| 11590 | error: |
| 11591 | Py_XDECREF(entry); |
| 11592 | return NULL; |
| 11593 | } |
| 11594 | |
| 11595 | #endif |
| 11596 | |
| 11597 | |
| 11598 | typedef struct { |
| 11599 | PyObject_HEAD |
| 11600 | path_t path; |
| 11601 | #ifdef MS_WINDOWS |
| 11602 | HANDLE handle; |
| 11603 | WIN32_FIND_DATAW file_data; |
| 11604 | int first_time; |
| 11605 | #else /* POSIX */ |
| 11606 | DIR *dirp; |
| 11607 | #endif |
| 11608 | } ScandirIterator; |
| 11609 | |
| 11610 | #ifdef MS_WINDOWS |
| 11611 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11612 | static int |
| 11613 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 11614 | { |
| 11615 | return iterator->handle == INVALID_HANDLE_VALUE; |
| 11616 | } |
| 11617 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11618 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11619 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11620 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 11621 | HANDLE handle = iterator->handle; |
| 11622 | |
| 11623 | if (handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11624 | return; |
| 11625 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11626 | iterator->handle = INVALID_HANDLE_VALUE; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 11627 | Py_BEGIN_ALLOW_THREADS |
| 11628 | FindClose(handle); |
| 11629 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11630 | } |
| 11631 | |
| 11632 | static PyObject * |
| 11633 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 11634 | { |
| 11635 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 11636 | BOOL success; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11637 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11638 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11639 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11640 | if (iterator->handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11641 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11642 | |
| 11643 | while (1) { |
| 11644 | if (!iterator->first_time) { |
| 11645 | Py_BEGIN_ALLOW_THREADS |
| 11646 | success = FindNextFileW(iterator->handle, file_data); |
| 11647 | Py_END_ALLOW_THREADS |
| 11648 | if (!success) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11649 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11650 | if (GetLastError() != ERROR_NO_MORE_FILES) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11651 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11652 | break; |
| 11653 | } |
| 11654 | } |
| 11655 | iterator->first_time = 0; |
| 11656 | |
| 11657 | /* Skip over . and .. */ |
| 11658 | if (wcscmp(file_data->cFileName, L".") != 0 && |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11659 | wcscmp(file_data->cFileName, L"..") != 0) { |
| 11660 | entry = DirEntry_from_find_data(&iterator->path, file_data); |
| 11661 | if (!entry) |
| 11662 | break; |
| 11663 | return entry; |
| 11664 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11665 | |
| 11666 | /* Loop till we get a non-dot directory or finish iterating */ |
| 11667 | } |
| 11668 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11669 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11670 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11671 | return NULL; |
| 11672 | } |
| 11673 | |
| 11674 | #else /* POSIX */ |
| 11675 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11676 | static int |
| 11677 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 11678 | { |
| 11679 | return !iterator->dirp; |
| 11680 | } |
| 11681 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11682 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11683 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11684 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 11685 | DIR *dirp = iterator->dirp; |
| 11686 | |
| 11687 | if (!dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11688 | return; |
| 11689 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11690 | iterator->dirp = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 11691 | Py_BEGIN_ALLOW_THREADS |
| 11692 | closedir(dirp); |
| 11693 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11694 | return; |
| 11695 | } |
| 11696 | |
| 11697 | static PyObject * |
| 11698 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 11699 | { |
| 11700 | struct dirent *direntp; |
| 11701 | Py_ssize_t name_len; |
| 11702 | int is_dot; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11703 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11704 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11705 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11706 | if (!iterator->dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11707 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11708 | |
| 11709 | while (1) { |
| 11710 | errno = 0; |
| 11711 | Py_BEGIN_ALLOW_THREADS |
| 11712 | direntp = readdir(iterator->dirp); |
| 11713 | Py_END_ALLOW_THREADS |
| 11714 | |
| 11715 | if (!direntp) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11716 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11717 | if (errno != 0) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11718 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11719 | break; |
| 11720 | } |
| 11721 | |
| 11722 | /* Skip over . and .. */ |
| 11723 | name_len = NAMLEN(direntp); |
| 11724 | is_dot = direntp->d_name[0] == '.' && |
| 11725 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 11726 | if (!is_dot) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11727 | entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11728 | name_len, direntp->d_ino |
| 11729 | #ifdef HAVE_DIRENT_D_TYPE |
| 11730 | , direntp->d_type |
| 11731 | #endif |
| 11732 | ); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11733 | if (!entry) |
| 11734 | break; |
| 11735 | return entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11736 | } |
| 11737 | |
| 11738 | /* Loop till we get a non-dot directory or finish iterating */ |
| 11739 | } |
| 11740 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11741 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11742 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11743 | return NULL; |
| 11744 | } |
| 11745 | |
| 11746 | #endif |
| 11747 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11748 | static PyObject * |
| 11749 | ScandirIterator_close(ScandirIterator *self, PyObject *args) |
| 11750 | { |
| 11751 | ScandirIterator_closedir(self); |
| 11752 | Py_RETURN_NONE; |
| 11753 | } |
| 11754 | |
| 11755 | static PyObject * |
| 11756 | ScandirIterator_enter(PyObject *self, PyObject *args) |
| 11757 | { |
| 11758 | Py_INCREF(self); |
| 11759 | return self; |
| 11760 | } |
| 11761 | |
| 11762 | static PyObject * |
| 11763 | ScandirIterator_exit(ScandirIterator *self, PyObject *args) |
| 11764 | { |
| 11765 | ScandirIterator_closedir(self); |
| 11766 | Py_RETURN_NONE; |
| 11767 | } |
| 11768 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11769 | static void |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 11770 | ScandirIterator_finalize(ScandirIterator *iterator) |
| 11771 | { |
| 11772 | PyObject *error_type, *error_value, *error_traceback; |
| 11773 | |
| 11774 | /* Save the current exception, if any. */ |
| 11775 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 11776 | |
| 11777 | if (!ScandirIterator_is_closed(iterator)) { |
| 11778 | ScandirIterator_closedir(iterator); |
| 11779 | |
| 11780 | if (PyErr_ResourceWarning((PyObject *)iterator, 1, |
| 11781 | "unclosed scandir iterator %R", iterator)) { |
| 11782 | /* Spurious errors can appear at shutdown */ |
| 11783 | if (PyErr_ExceptionMatches(PyExc_Warning)) { |
| 11784 | PyErr_WriteUnraisable((PyObject *) iterator); |
| 11785 | } |
| 11786 | } |
| 11787 | } |
| 11788 | |
| 11789 | Py_CLEAR(iterator->path.object); |
| 11790 | path_cleanup(&iterator->path); |
| 11791 | |
| 11792 | /* Restore the saved exception. */ |
| 11793 | PyErr_Restore(error_type, error_value, error_traceback); |
| 11794 | } |
| 11795 | |
| 11796 | static void |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11797 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 11798 | { |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 11799 | if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0) |
| 11800 | return; |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11801 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11802 | Py_TYPE(iterator)->tp_free((PyObject *)iterator); |
| 11803 | } |
| 11804 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11805 | static PyMethodDef ScandirIterator_methods[] = { |
| 11806 | {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS}, |
| 11807 | {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS}, |
| 11808 | {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS}, |
| 11809 | {NULL} |
| 11810 | }; |
| 11811 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 11812 | static PyTypeObject ScandirIteratorType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11813 | PyVarObject_HEAD_INIT(NULL, 0) |
| 11814 | MODNAME ".ScandirIterator", /* tp_name */ |
| 11815 | sizeof(ScandirIterator), /* tp_basicsize */ |
| 11816 | 0, /* tp_itemsize */ |
| 11817 | /* methods */ |
| 11818 | (destructor)ScandirIterator_dealloc, /* tp_dealloc */ |
| 11819 | 0, /* tp_print */ |
| 11820 | 0, /* tp_getattr */ |
| 11821 | 0, /* tp_setattr */ |
| 11822 | 0, /* tp_compare */ |
| 11823 | 0, /* tp_repr */ |
| 11824 | 0, /* tp_as_number */ |
| 11825 | 0, /* tp_as_sequence */ |
| 11826 | 0, /* tp_as_mapping */ |
| 11827 | 0, /* tp_hash */ |
| 11828 | 0, /* tp_call */ |
| 11829 | 0, /* tp_str */ |
| 11830 | 0, /* tp_getattro */ |
| 11831 | 0, /* tp_setattro */ |
| 11832 | 0, /* tp_as_buffer */ |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 11833 | Py_TPFLAGS_DEFAULT |
| 11834 | | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11835 | 0, /* tp_doc */ |
| 11836 | 0, /* tp_traverse */ |
| 11837 | 0, /* tp_clear */ |
| 11838 | 0, /* tp_richcompare */ |
| 11839 | 0, /* tp_weaklistoffset */ |
| 11840 | PyObject_SelfIter, /* tp_iter */ |
| 11841 | (iternextfunc)ScandirIterator_iternext, /* tp_iternext */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11842 | ScandirIterator_methods, /* tp_methods */ |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 11843 | 0, /* tp_members */ |
| 11844 | 0, /* tp_getset */ |
| 11845 | 0, /* tp_base */ |
| 11846 | 0, /* tp_dict */ |
| 11847 | 0, /* tp_descr_get */ |
| 11848 | 0, /* tp_descr_set */ |
| 11849 | 0, /* tp_dictoffset */ |
| 11850 | 0, /* tp_init */ |
| 11851 | 0, /* tp_alloc */ |
| 11852 | 0, /* tp_new */ |
| 11853 | 0, /* tp_free */ |
| 11854 | 0, /* tp_is_gc */ |
| 11855 | 0, /* tp_bases */ |
| 11856 | 0, /* tp_mro */ |
| 11857 | 0, /* tp_cache */ |
| 11858 | 0, /* tp_subclasses */ |
| 11859 | 0, /* tp_weaklist */ |
| 11860 | 0, /* tp_del */ |
| 11861 | 0, /* tp_version_tag */ |
| 11862 | (destructor)ScandirIterator_finalize, /* tp_finalize */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11863 | }; |
| 11864 | |
| 11865 | static PyObject * |
| 11866 | posix_scandir(PyObject *self, PyObject *args, PyObject *kwargs) |
| 11867 | { |
| 11868 | ScandirIterator *iterator; |
| 11869 | static char *keywords[] = {"path", NULL}; |
| 11870 | #ifdef MS_WINDOWS |
| 11871 | wchar_t *path_strW; |
| 11872 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11873 | const char *path; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11874 | #endif |
| 11875 | |
| 11876 | iterator = PyObject_New(ScandirIterator, &ScandirIteratorType); |
| 11877 | if (!iterator) |
| 11878 | return NULL; |
| 11879 | memset(&iterator->path, 0, sizeof(path_t)); |
| 11880 | iterator->path.function_name = "scandir"; |
| 11881 | iterator->path.nullable = 1; |
| 11882 | |
| 11883 | #ifdef MS_WINDOWS |
| 11884 | iterator->handle = INVALID_HANDLE_VALUE; |
| 11885 | #else |
| 11886 | iterator->dirp = NULL; |
| 11887 | #endif |
| 11888 | |
| 11889 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:scandir", keywords, |
| 11890 | path_converter, &iterator->path)) |
| 11891 | goto error; |
| 11892 | |
| 11893 | /* path_converter doesn't keep path.object around, so do it |
| 11894 | manually for the lifetime of the iterator here (the refcount |
| 11895 | is decremented in ScandirIterator_dealloc) |
| 11896 | */ |
| 11897 | Py_XINCREF(iterator->path.object); |
| 11898 | |
| 11899 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11900 | iterator->first_time = 1; |
| 11901 | |
| 11902 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 11903 | if (!path_strW) |
| 11904 | goto error; |
| 11905 | |
| 11906 | Py_BEGIN_ALLOW_THREADS |
| 11907 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 11908 | Py_END_ALLOW_THREADS |
| 11909 | |
| 11910 | PyMem_Free(path_strW); |
| 11911 | |
| 11912 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 11913 | path_error(&iterator->path); |
| 11914 | goto error; |
| 11915 | } |
| 11916 | #else /* POSIX */ |
| 11917 | if (iterator->path.narrow) |
| 11918 | path = iterator->path.narrow; |
| 11919 | else |
| 11920 | path = "."; |
| 11921 | |
| 11922 | errno = 0; |
| 11923 | Py_BEGIN_ALLOW_THREADS |
| 11924 | iterator->dirp = opendir(path); |
| 11925 | Py_END_ALLOW_THREADS |
| 11926 | |
| 11927 | if (!iterator->dirp) { |
| 11928 | path_error(&iterator->path); |
| 11929 | goto error; |
| 11930 | } |
| 11931 | #endif |
| 11932 | |
| 11933 | return (PyObject *)iterator; |
| 11934 | |
| 11935 | error: |
| 11936 | Py_DECREF(iterator); |
| 11937 | return NULL; |
| 11938 | } |
| 11939 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 11940 | /* |
| 11941 | Return the file system path representation of the object. |
| 11942 | |
| 11943 | If the object is str or bytes, then allow it to pass through with |
| 11944 | an incremented refcount. If the object defines __fspath__(), then |
| 11945 | return the result of that method. All other types raise a TypeError. |
| 11946 | */ |
| 11947 | PyObject * |
| 11948 | PyOS_FSPath(PyObject *path) |
| 11949 | { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 11950 | /* For error message reasons, this function is manually inlined in |
| 11951 | path_converter(). */ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 11952 | _Py_IDENTIFIER(__fspath__); |
| 11953 | PyObject *func = NULL; |
| 11954 | PyObject *path_repr = NULL; |
| 11955 | |
| 11956 | if (PyUnicode_Check(path) || PyBytes_Check(path)) { |
| 11957 | Py_INCREF(path); |
| 11958 | return path; |
| 11959 | } |
| 11960 | |
| 11961 | func = _PyObject_LookupSpecial(path, &PyId___fspath__); |
| 11962 | if (NULL == func) { |
| 11963 | return PyErr_Format(PyExc_TypeError, |
| 11964 | "expected str, bytes or os.PathLike object, " |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 11965 | "not %.200s", |
| 11966 | Py_TYPE(path)->tp_name); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 11967 | } |
| 11968 | |
| 11969 | path_repr = PyObject_CallFunctionObjArgs(func, NULL); |
| 11970 | Py_DECREF(func); |
Brett Cannon | 044283a | 2016-07-15 10:41:49 -0700 | [diff] [blame] | 11971 | if (NULL == path_repr) { |
| 11972 | return NULL; |
| 11973 | } |
| 11974 | |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 11975 | if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) { |
| 11976 | PyErr_Format(PyExc_TypeError, |
| 11977 | "expected %.200s.__fspath__() to return str or bytes, " |
| 11978 | "not %.200s", Py_TYPE(path)->tp_name, |
| 11979 | Py_TYPE(path_repr)->tp_name); |
| 11980 | Py_DECREF(path_repr); |
| 11981 | return NULL; |
| 11982 | } |
| 11983 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 11984 | return path_repr; |
| 11985 | } |
| 11986 | |
| 11987 | /*[clinic input] |
| 11988 | os.fspath |
| 11989 | |
| 11990 | path: object |
| 11991 | |
| 11992 | Return the file system path representation of the object. |
| 11993 | |
Brett Cannon | b4f43e9 | 2016-06-09 14:32:08 -0700 | [diff] [blame] | 11994 | If the object is str or bytes, then allow it to pass through as-is. If the |
| 11995 | object defines __fspath__(), then return the result of that method. All other |
| 11996 | types raise a TypeError. |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 11997 | [clinic start generated code]*/ |
| 11998 | |
| 11999 | static PyObject * |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 12000 | os_fspath_impl(PyObject *module, PyObject *path) |
| 12001 | /*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12002 | { |
| 12003 | return PyOS_FSPath(path); |
| 12004 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12005 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12006 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 12007 | /*[clinic input] |
| 12008 | os.getrandom |
| 12009 | |
| 12010 | size: Py_ssize_t |
| 12011 | flags: int=0 |
| 12012 | |
| 12013 | Obtain a series of random bytes. |
| 12014 | [clinic start generated code]*/ |
| 12015 | |
| 12016 | static PyObject * |
| 12017 | os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags) |
| 12018 | /*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/ |
| 12019 | { |
| 12020 | char *buffer; |
| 12021 | Py_ssize_t n; |
| 12022 | PyObject *bytes; |
| 12023 | |
| 12024 | if (size < 0) { |
| 12025 | errno = EINVAL; |
| 12026 | return posix_error(); |
| 12027 | } |
| 12028 | |
| 12029 | buffer = PyMem_Malloc(size); |
| 12030 | if (buffer == NULL) { |
| 12031 | PyErr_NoMemory(); |
| 12032 | return NULL; |
| 12033 | } |
| 12034 | |
| 12035 | while (1) { |
| 12036 | n = syscall(SYS_getrandom, buffer, size, flags); |
| 12037 | if (n < 0 && errno == EINTR) { |
| 12038 | if (PyErr_CheckSignals() < 0) { |
| 12039 | return NULL; |
| 12040 | } |
| 12041 | continue; |
| 12042 | } |
| 12043 | break; |
| 12044 | } |
| 12045 | |
| 12046 | if (n < 0) { |
| 12047 | PyMem_Free(buffer); |
| 12048 | PyErr_SetFromErrno(PyExc_OSError); |
| 12049 | return NULL; |
| 12050 | } |
| 12051 | |
| 12052 | bytes = PyBytes_FromStringAndSize(buffer, n); |
| 12053 | PyMem_Free(buffer); |
| 12054 | |
| 12055 | return bytes; |
| 12056 | } |
| 12057 | #endif /* HAVE_GETRANDOM_SYSCALL */ |
| 12058 | |
Serhiy Storchaka | a4c6bad | 2015-04-04 23:35:52 +0300 | [diff] [blame] | 12059 | #include "clinic/posixmodule.c.h" |
| 12060 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 12061 | /*[clinic input] |
| 12062 | dump buffer |
| 12063 | [clinic start generated code]*/ |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 12064 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/ |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 12065 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12066 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12067 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12068 | |
| 12069 | OS_STAT_METHODDEF |
| 12070 | OS_ACCESS_METHODDEF |
| 12071 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12072 | OS_CHDIR_METHODDEF |
| 12073 | OS_CHFLAGS_METHODDEF |
| 12074 | OS_CHMOD_METHODDEF |
| 12075 | OS_FCHMOD_METHODDEF |
| 12076 | OS_LCHMOD_METHODDEF |
| 12077 | OS_CHOWN_METHODDEF |
| 12078 | OS_FCHOWN_METHODDEF |
| 12079 | OS_LCHOWN_METHODDEF |
| 12080 | OS_LCHFLAGS_METHODDEF |
| 12081 | OS_CHROOT_METHODDEF |
| 12082 | OS_CTERMID_METHODDEF |
| 12083 | OS_GETCWD_METHODDEF |
| 12084 | OS_GETCWDB_METHODDEF |
| 12085 | OS_LINK_METHODDEF |
| 12086 | OS_LISTDIR_METHODDEF |
| 12087 | OS_LSTAT_METHODDEF |
| 12088 | OS_MKDIR_METHODDEF |
| 12089 | OS_NICE_METHODDEF |
| 12090 | OS_GETPRIORITY_METHODDEF |
| 12091 | OS_SETPRIORITY_METHODDEF |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12092 | #ifdef HAVE_READLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12093 | {"readlink", (PyCFunction)posix_readlink, |
| 12094 | METH_VARARGS | METH_KEYWORDS, |
| 12095 | readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 12096 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 12097 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12098 | {"readlink", (PyCFunction)win_readlink, |
| 12099 | METH_VARARGS | METH_KEYWORDS, |
| 12100 | readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 12101 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12102 | OS_RENAME_METHODDEF |
| 12103 | OS_REPLACE_METHODDEF |
| 12104 | OS_RMDIR_METHODDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12105 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12106 | OS_SYMLINK_METHODDEF |
| 12107 | OS_SYSTEM_METHODDEF |
| 12108 | OS_UMASK_METHODDEF |
| 12109 | OS_UNAME_METHODDEF |
| 12110 | OS_UNLINK_METHODDEF |
| 12111 | OS_REMOVE_METHODDEF |
| 12112 | OS_UTIME_METHODDEF |
| 12113 | OS_TIMES_METHODDEF |
| 12114 | OS__EXIT_METHODDEF |
| 12115 | OS_EXECV_METHODDEF |
| 12116 | OS_EXECVE_METHODDEF |
| 12117 | OS_SPAWNV_METHODDEF |
| 12118 | OS_SPAWNVE_METHODDEF |
| 12119 | OS_FORK1_METHODDEF |
| 12120 | OS_FORK_METHODDEF |
| 12121 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 12122 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 12123 | OS_SCHED_GETPARAM_METHODDEF |
| 12124 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 12125 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 12126 | OS_SCHED_SETPARAM_METHODDEF |
| 12127 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 12128 | OS_SCHED_YIELD_METHODDEF |
| 12129 | OS_SCHED_SETAFFINITY_METHODDEF |
| 12130 | OS_SCHED_GETAFFINITY_METHODDEF |
| 12131 | OS_OPENPTY_METHODDEF |
| 12132 | OS_FORKPTY_METHODDEF |
| 12133 | OS_GETEGID_METHODDEF |
| 12134 | OS_GETEUID_METHODDEF |
| 12135 | OS_GETGID_METHODDEF |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 12136 | #ifdef HAVE_GETGROUPLIST |
| 12137 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 12138 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12139 | OS_GETGROUPS_METHODDEF |
| 12140 | OS_GETPID_METHODDEF |
| 12141 | OS_GETPGRP_METHODDEF |
| 12142 | OS_GETPPID_METHODDEF |
| 12143 | OS_GETUID_METHODDEF |
| 12144 | OS_GETLOGIN_METHODDEF |
| 12145 | OS_KILL_METHODDEF |
| 12146 | OS_KILLPG_METHODDEF |
| 12147 | OS_PLOCK_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 12148 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12149 | OS_STARTFILE_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 12150 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12151 | OS_SETUID_METHODDEF |
| 12152 | OS_SETEUID_METHODDEF |
| 12153 | OS_SETREUID_METHODDEF |
| 12154 | OS_SETGID_METHODDEF |
| 12155 | OS_SETEGID_METHODDEF |
| 12156 | OS_SETREGID_METHODDEF |
| 12157 | OS_SETGROUPS_METHODDEF |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 12158 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12159 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 12160 | #endif /* HAVE_INITGROUPS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12161 | OS_GETPGID_METHODDEF |
| 12162 | OS_SETPGRP_METHODDEF |
| 12163 | OS_WAIT_METHODDEF |
| 12164 | OS_WAIT3_METHODDEF |
| 12165 | OS_WAIT4_METHODDEF |
| 12166 | OS_WAITID_METHODDEF |
| 12167 | OS_WAITPID_METHODDEF |
| 12168 | OS_GETSID_METHODDEF |
| 12169 | OS_SETSID_METHODDEF |
| 12170 | OS_SETPGID_METHODDEF |
| 12171 | OS_TCGETPGRP_METHODDEF |
| 12172 | OS_TCSETPGRP_METHODDEF |
| 12173 | OS_OPEN_METHODDEF |
| 12174 | OS_CLOSE_METHODDEF |
| 12175 | OS_CLOSERANGE_METHODDEF |
| 12176 | OS_DEVICE_ENCODING_METHODDEF |
| 12177 | OS_DUP_METHODDEF |
| 12178 | OS_DUP2_METHODDEF |
| 12179 | OS_LOCKF_METHODDEF |
| 12180 | OS_LSEEK_METHODDEF |
| 12181 | OS_READ_METHODDEF |
| 12182 | OS_READV_METHODDEF |
| 12183 | OS_PREAD_METHODDEF |
| 12184 | OS_WRITE_METHODDEF |
| 12185 | OS_WRITEV_METHODDEF |
| 12186 | OS_PWRITE_METHODDEF |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12187 | #ifdef HAVE_SENDFILE |
| 12188 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 12189 | posix_sendfile__doc__}, |
| 12190 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12191 | OS_FSTAT_METHODDEF |
| 12192 | OS_ISATTY_METHODDEF |
| 12193 | OS_PIPE_METHODDEF |
| 12194 | OS_PIPE2_METHODDEF |
| 12195 | OS_MKFIFO_METHODDEF |
| 12196 | OS_MKNOD_METHODDEF |
| 12197 | OS_MAJOR_METHODDEF |
| 12198 | OS_MINOR_METHODDEF |
| 12199 | OS_MAKEDEV_METHODDEF |
| 12200 | OS_FTRUNCATE_METHODDEF |
| 12201 | OS_TRUNCATE_METHODDEF |
| 12202 | OS_POSIX_FALLOCATE_METHODDEF |
| 12203 | OS_POSIX_FADVISE_METHODDEF |
| 12204 | OS_PUTENV_METHODDEF |
| 12205 | OS_UNSETENV_METHODDEF |
| 12206 | OS_STRERROR_METHODDEF |
| 12207 | OS_FCHDIR_METHODDEF |
| 12208 | OS_FSYNC_METHODDEF |
| 12209 | OS_SYNC_METHODDEF |
| 12210 | OS_FDATASYNC_METHODDEF |
| 12211 | OS_WCOREDUMP_METHODDEF |
| 12212 | OS_WIFCONTINUED_METHODDEF |
| 12213 | OS_WIFSTOPPED_METHODDEF |
| 12214 | OS_WIFSIGNALED_METHODDEF |
| 12215 | OS_WIFEXITED_METHODDEF |
| 12216 | OS_WEXITSTATUS_METHODDEF |
| 12217 | OS_WTERMSIG_METHODDEF |
| 12218 | OS_WSTOPSIG_METHODDEF |
| 12219 | OS_FSTATVFS_METHODDEF |
| 12220 | OS_STATVFS_METHODDEF |
| 12221 | OS_CONFSTR_METHODDEF |
| 12222 | OS_SYSCONF_METHODDEF |
| 12223 | OS_FPATHCONF_METHODDEF |
| 12224 | OS_PATHCONF_METHODDEF |
| 12225 | OS_ABORT_METHODDEF |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 12226 | OS__GETFULLPATHNAME_METHODDEF |
| 12227 | OS__ISDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12228 | OS__GETDISKUSAGE_METHODDEF |
| 12229 | OS__GETFINALPATHNAME_METHODDEF |
| 12230 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 12231 | OS_GETLOADAVG_METHODDEF |
| 12232 | OS_URANDOM_METHODDEF |
| 12233 | OS_SETRESUID_METHODDEF |
| 12234 | OS_SETRESGID_METHODDEF |
| 12235 | OS_GETRESUID_METHODDEF |
| 12236 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12237 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12238 | OS_GETXATTR_METHODDEF |
| 12239 | OS_SETXATTR_METHODDEF |
| 12240 | OS_REMOVEXATTR_METHODDEF |
| 12241 | OS_LISTXATTR_METHODDEF |
| 12242 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12243 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 12244 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 12245 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12246 | OS_CPU_COUNT_METHODDEF |
| 12247 | OS_GET_INHERITABLE_METHODDEF |
| 12248 | OS_SET_INHERITABLE_METHODDEF |
| 12249 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 12250 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12251 | #ifndef MS_WINDOWS |
| 12252 | {"get_blocking", posix_get_blocking, METH_VARARGS, get_blocking__doc__}, |
| 12253 | {"set_blocking", posix_set_blocking, METH_VARARGS, set_blocking__doc__}, |
| 12254 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12255 | {"scandir", (PyCFunction)posix_scandir, |
| 12256 | METH_VARARGS | METH_KEYWORDS, |
| 12257 | posix_scandir__doc__}, |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12258 | OS_FSPATH_METHODDEF |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12259 | OS_GETRANDOM_METHODDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12260 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 12261 | }; |
| 12262 | |
| 12263 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12264 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12265 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12266 | enable_symlink() |
| 12267 | { |
| 12268 | HANDLE tok; |
| 12269 | TOKEN_PRIVILEGES tok_priv; |
| 12270 | LUID luid; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12271 | |
| 12272 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12273 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12274 | |
| 12275 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12276 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12277 | |
| 12278 | tok_priv.PrivilegeCount = 1; |
| 12279 | tok_priv.Privileges[0].Luid = luid; |
| 12280 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 12281 | |
| 12282 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 12283 | sizeof(TOKEN_PRIVILEGES), |
| 12284 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12285 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12286 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12287 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 12288 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12289 | } |
| 12290 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 12291 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12292 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12293 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12294 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12295 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12296 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12297 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12298 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12299 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12300 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12301 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12302 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12303 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12304 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12305 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12306 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12307 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12308 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12309 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12310 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12311 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12312 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12313 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12314 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12315 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12316 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12317 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12318 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12319 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12320 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12321 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12322 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12323 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12324 | #endif |
| 12325 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12326 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12327 | #endif |
| 12328 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12329 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12330 | #endif |
| 12331 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12332 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12333 | #endif |
| 12334 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12335 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12336 | #endif |
| 12337 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12338 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12339 | #endif |
| 12340 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12341 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12342 | #endif |
| 12343 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12344 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12345 | #endif |
| 12346 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12347 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12348 | #endif |
| 12349 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12350 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12351 | #endif |
| 12352 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12353 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12354 | #endif |
| 12355 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12356 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12357 | #endif |
| 12358 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12359 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12360 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 12361 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12362 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 12363 | #endif |
| 12364 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12365 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 12366 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12367 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12368 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12369 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12370 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12371 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12372 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 12373 | #ifndef __GNU__ |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 12374 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12375 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 12376 | #endif |
| 12377 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12378 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 12379 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 12380 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12381 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12382 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12383 | #endif |
| 12384 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12385 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12386 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 12387 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12388 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 12389 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12390 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12391 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12392 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 12393 | #ifdef O_TMPFILE |
| 12394 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 12395 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12396 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12397 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12398 | #endif |
| 12399 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12400 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12401 | #endif |
| 12402 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12403 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12404 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 12405 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12406 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 12407 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12408 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12409 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12410 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12411 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12412 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 12413 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12414 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 12415 | #endif |
| 12416 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12417 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 12418 | #endif |
| 12419 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12420 | /* MS Windows */ |
| 12421 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12422 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12423 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12424 | #endif |
| 12425 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12426 | /* Optimize for short life (keep in memory). */ |
| 12427 | /* 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] | 12428 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12429 | #endif |
| 12430 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12431 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12432 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12433 | #endif |
| 12434 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12435 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12436 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12437 | #endif |
| 12438 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12439 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12440 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12441 | #endif |
| 12442 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12443 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 12444 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12445 | /* Send a SIGIO signal whenever input or output |
| 12446 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12447 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 12448 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12449 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12450 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12451 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12452 | #endif |
| 12453 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12454 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12455 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12456 | #endif |
| 12457 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12458 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12459 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12460 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12461 | #ifdef O_NOLINKS |
| 12462 | /* 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] | 12463 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12464 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 12465 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12466 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12467 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 12468 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 12469 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12470 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12471 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12472 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12473 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12474 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12475 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12476 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12477 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12478 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12479 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12480 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12481 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12482 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12483 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12484 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12485 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12486 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12487 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12488 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12489 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12490 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12491 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12492 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12493 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12494 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12495 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12496 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12497 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12498 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12499 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12500 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12501 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12502 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12503 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12504 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12505 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12506 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12507 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12508 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12509 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12510 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12511 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12512 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12513 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12514 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12515 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12516 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12517 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12518 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12519 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12520 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12521 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12522 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 12523 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 12524 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12525 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 12526 | #endif /* ST_RDONLY */ |
| 12527 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12528 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 12529 | #endif /* ST_NOSUID */ |
| 12530 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 12531 | /* GNU extensions */ |
| 12532 | #ifdef ST_NODEV |
| 12533 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 12534 | #endif /* ST_NODEV */ |
| 12535 | #ifdef ST_NOEXEC |
| 12536 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 12537 | #endif /* ST_NOEXEC */ |
| 12538 | #ifdef ST_SYNCHRONOUS |
| 12539 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 12540 | #endif /* ST_SYNCHRONOUS */ |
| 12541 | #ifdef ST_MANDLOCK |
| 12542 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 12543 | #endif /* ST_MANDLOCK */ |
| 12544 | #ifdef ST_WRITE |
| 12545 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 12546 | #endif /* ST_WRITE */ |
| 12547 | #ifdef ST_APPEND |
| 12548 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 12549 | #endif /* ST_APPEND */ |
| 12550 | #ifdef ST_NOATIME |
| 12551 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 12552 | #endif /* ST_NOATIME */ |
| 12553 | #ifdef ST_NODIRATIME |
| 12554 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 12555 | #endif /* ST_NODIRATIME */ |
| 12556 | #ifdef ST_RELATIME |
| 12557 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 12558 | #endif /* ST_RELATIME */ |
| 12559 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12560 | /* FreeBSD sendfile() constants */ |
| 12561 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12562 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12563 | #endif |
| 12564 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12565 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12566 | #endif |
| 12567 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12568 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12569 | #endif |
| 12570 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12571 | /* constants for posix_fadvise */ |
| 12572 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12573 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12574 | #endif |
| 12575 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12576 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12577 | #endif |
| 12578 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12579 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12580 | #endif |
| 12581 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12582 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12583 | #endif |
| 12584 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12585 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12586 | #endif |
| 12587 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12588 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12589 | #endif |
| 12590 | |
| 12591 | /* constants for waitid */ |
| 12592 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12593 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 12594 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 12595 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12596 | #endif |
| 12597 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12598 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12599 | #endif |
| 12600 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12601 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12602 | #endif |
| 12603 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12604 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12605 | #endif |
| 12606 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12607 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12608 | #endif |
| 12609 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12610 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12611 | #endif |
| 12612 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12613 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12614 | #endif |
| 12615 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12616 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12617 | #endif |
| 12618 | |
| 12619 | /* constants for lockf */ |
| 12620 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12621 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12622 | #endif |
| 12623 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12624 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12625 | #endif |
| 12626 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12627 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12628 | #endif |
| 12629 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12630 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12631 | #endif |
| 12632 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 12633 | #ifdef HAVE_SPAWNV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12634 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 12635 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
| 12636 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
| 12637 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
| 12638 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 12639 | #endif |
| 12640 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12641 | #ifdef HAVE_SCHED_H |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 12642 | #ifdef SCHED_OTHER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12643 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 12644 | #endif |
| 12645 | #ifdef SCHED_FIFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12646 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 12647 | #endif |
| 12648 | #ifdef SCHED_RR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12649 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 12650 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12651 | #ifdef SCHED_SPORADIC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12652 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12653 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12654 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12655 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12656 | #endif |
| 12657 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12658 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12659 | #endif |
| 12660 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12661 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12662 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12663 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12664 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12665 | #endif |
| 12666 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12667 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12668 | #endif |
| 12669 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12670 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12671 | #endif |
| 12672 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12673 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12674 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12675 | #endif |
| 12676 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12677 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12678 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 12679 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 12680 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12681 | #endif |
| 12682 | |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 12683 | #if HAVE_DECL_RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12684 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12685 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 12686 | #if HAVE_DECL_RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12687 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12688 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 12689 | #if HAVE_DECL_RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12690 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12691 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 12692 | #if HAVE_DECL_RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12693 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12694 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 12695 | #if HAVE_DECL_RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12696 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12697 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 12698 | #if HAVE_DECL_RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12699 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12700 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 12701 | #if HAVE_DECL_RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12702 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12703 | #endif |
| 12704 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12705 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 12706 | if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1; |
| 12707 | if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1; |
| 12708 | #endif |
| 12709 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12710 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12711 | } |
| 12712 | |
| 12713 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12714 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12715 | PyModuleDef_HEAD_INIT, |
| 12716 | MODNAME, |
| 12717 | posix__doc__, |
| 12718 | -1, |
| 12719 | posix_methods, |
| 12720 | NULL, |
| 12721 | NULL, |
| 12722 | NULL, |
| 12723 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12724 | }; |
| 12725 | |
| 12726 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12727 | static const char * const have_functions[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12728 | |
| 12729 | #ifdef HAVE_FACCESSAT |
| 12730 | "HAVE_FACCESSAT", |
| 12731 | #endif |
| 12732 | |
| 12733 | #ifdef HAVE_FCHDIR |
| 12734 | "HAVE_FCHDIR", |
| 12735 | #endif |
| 12736 | |
| 12737 | #ifdef HAVE_FCHMOD |
| 12738 | "HAVE_FCHMOD", |
| 12739 | #endif |
| 12740 | |
| 12741 | #ifdef HAVE_FCHMODAT |
| 12742 | "HAVE_FCHMODAT", |
| 12743 | #endif |
| 12744 | |
| 12745 | #ifdef HAVE_FCHOWN |
| 12746 | "HAVE_FCHOWN", |
| 12747 | #endif |
| 12748 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 12749 | #ifdef HAVE_FCHOWNAT |
| 12750 | "HAVE_FCHOWNAT", |
| 12751 | #endif |
| 12752 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12753 | #ifdef HAVE_FEXECVE |
| 12754 | "HAVE_FEXECVE", |
| 12755 | #endif |
| 12756 | |
| 12757 | #ifdef HAVE_FDOPENDIR |
| 12758 | "HAVE_FDOPENDIR", |
| 12759 | #endif |
| 12760 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12761 | #ifdef HAVE_FPATHCONF |
| 12762 | "HAVE_FPATHCONF", |
| 12763 | #endif |
| 12764 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12765 | #ifdef HAVE_FSTATAT |
| 12766 | "HAVE_FSTATAT", |
| 12767 | #endif |
| 12768 | |
| 12769 | #ifdef HAVE_FSTATVFS |
| 12770 | "HAVE_FSTATVFS", |
| 12771 | #endif |
| 12772 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 12773 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12774 | "HAVE_FTRUNCATE", |
| 12775 | #endif |
| 12776 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12777 | #ifdef HAVE_FUTIMENS |
| 12778 | "HAVE_FUTIMENS", |
| 12779 | #endif |
| 12780 | |
| 12781 | #ifdef HAVE_FUTIMES |
| 12782 | "HAVE_FUTIMES", |
| 12783 | #endif |
| 12784 | |
| 12785 | #ifdef HAVE_FUTIMESAT |
| 12786 | "HAVE_FUTIMESAT", |
| 12787 | #endif |
| 12788 | |
| 12789 | #ifdef HAVE_LINKAT |
| 12790 | "HAVE_LINKAT", |
| 12791 | #endif |
| 12792 | |
| 12793 | #ifdef HAVE_LCHFLAGS |
| 12794 | "HAVE_LCHFLAGS", |
| 12795 | #endif |
| 12796 | |
| 12797 | #ifdef HAVE_LCHMOD |
| 12798 | "HAVE_LCHMOD", |
| 12799 | #endif |
| 12800 | |
| 12801 | #ifdef HAVE_LCHOWN |
| 12802 | "HAVE_LCHOWN", |
| 12803 | #endif |
| 12804 | |
| 12805 | #ifdef HAVE_LSTAT |
| 12806 | "HAVE_LSTAT", |
| 12807 | #endif |
| 12808 | |
| 12809 | #ifdef HAVE_LUTIMES |
| 12810 | "HAVE_LUTIMES", |
| 12811 | #endif |
| 12812 | |
| 12813 | #ifdef HAVE_MKDIRAT |
| 12814 | "HAVE_MKDIRAT", |
| 12815 | #endif |
| 12816 | |
| 12817 | #ifdef HAVE_MKFIFOAT |
| 12818 | "HAVE_MKFIFOAT", |
| 12819 | #endif |
| 12820 | |
| 12821 | #ifdef HAVE_MKNODAT |
| 12822 | "HAVE_MKNODAT", |
| 12823 | #endif |
| 12824 | |
| 12825 | #ifdef HAVE_OPENAT |
| 12826 | "HAVE_OPENAT", |
| 12827 | #endif |
| 12828 | |
| 12829 | #ifdef HAVE_READLINKAT |
| 12830 | "HAVE_READLINKAT", |
| 12831 | #endif |
| 12832 | |
| 12833 | #ifdef HAVE_RENAMEAT |
| 12834 | "HAVE_RENAMEAT", |
| 12835 | #endif |
| 12836 | |
| 12837 | #ifdef HAVE_SYMLINKAT |
| 12838 | "HAVE_SYMLINKAT", |
| 12839 | #endif |
| 12840 | |
| 12841 | #ifdef HAVE_UNLINKAT |
| 12842 | "HAVE_UNLINKAT", |
| 12843 | #endif |
| 12844 | |
| 12845 | #ifdef HAVE_UTIMENSAT |
| 12846 | "HAVE_UTIMENSAT", |
| 12847 | #endif |
| 12848 | |
| 12849 | #ifdef MS_WINDOWS |
| 12850 | "MS_WINDOWS", |
| 12851 | #endif |
| 12852 | |
| 12853 | NULL |
| 12854 | }; |
| 12855 | |
| 12856 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 12857 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 12858 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12859 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12860 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12861 | PyObject *list; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12862 | const char * const *trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12863 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12864 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12865 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12866 | #endif |
| 12867 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12868 | m = PyModule_Create(&posixmodule); |
| 12869 | if (m == NULL) |
| 12870 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12871 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12872 | /* Initialize environ dictionary */ |
| 12873 | v = convertenviron(); |
| 12874 | Py_XINCREF(v); |
| 12875 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 12876 | return NULL; |
| 12877 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12878 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12879 | if (all_ins(m)) |
| 12880 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12881 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12882 | if (setup_confname_tables(m)) |
| 12883 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12884 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12885 | Py_INCREF(PyExc_OSError); |
| 12886 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 12887 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12888 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12889 | if (posix_putenv_garbage == NULL) |
| 12890 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12891 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 12892 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12893 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12894 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 12895 | waitid_result_desc.name = MODNAME ".waitid_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12896 | if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0) |
| 12897 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12898 | #endif |
| 12899 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 12900 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12901 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 12902 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 12903 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12904 | if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0) |
| 12905 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12906 | structseq_new = StatResultType.tp_new; |
| 12907 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12908 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 12909 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12910 | if (PyStructSequence_InitType2(&StatVFSResultType, |
| 12911 | &statvfs_result_desc) < 0) |
| 12912 | return NULL; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12913 | #ifdef NEED_TICKS_PER_SECOND |
| 12914 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12915 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12916 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12917 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12918 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12919 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12920 | # endif |
| 12921 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12922 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 12923 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12924 | sched_param_desc.name = MODNAME ".sched_param"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12925 | if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0) |
| 12926 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12927 | SchedParamType.tp_new = os_sched_param; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12928 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12929 | |
| 12930 | /* initialize TerminalSize_info */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12931 | if (PyStructSequence_InitType2(&TerminalSizeType, |
| 12932 | &TerminalSize_desc) < 0) |
| 12933 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12934 | |
| 12935 | /* initialize scandir types */ |
| 12936 | if (PyType_Ready(&ScandirIteratorType) < 0) |
| 12937 | return NULL; |
| 12938 | if (PyType_Ready(&DirEntryType) < 0) |
| 12939 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12940 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12941 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 12942 | Py_INCREF((PyObject*) &WaitidResultType); |
| 12943 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 12944 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12945 | Py_INCREF((PyObject*) &StatResultType); |
| 12946 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 12947 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 12948 | PyModule_AddObject(m, "statvfs_result", |
| 12949 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 12950 | |
| 12951 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12952 | Py_INCREF(&SchedParamType); |
| 12953 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 12954 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12955 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 12956 | times_result_desc.name = MODNAME ".times_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12957 | if (PyStructSequence_InitType2(&TimesResultType, ×_result_desc) < 0) |
| 12958 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 12959 | PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType); |
| 12960 | |
| 12961 | uname_result_desc.name = MODNAME ".uname_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12962 | if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0) |
| 12963 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 12964 | PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType); |
| 12965 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12966 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12967 | /* |
| 12968 | * Step 2 of weak-linking support on Mac OS X. |
| 12969 | * |
| 12970 | * The code below removes functions that are not available on the |
| 12971 | * currently active platform. |
| 12972 | * |
| 12973 | * 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] | 12974 | * 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] | 12975 | * OSX 10.4. |
| 12976 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12977 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12978 | if (fstatvfs == NULL) { |
| 12979 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 12980 | return NULL; |
| 12981 | } |
| 12982 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12983 | #endif /* HAVE_FSTATVFS */ |
| 12984 | |
| 12985 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12986 | if (statvfs == NULL) { |
| 12987 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 12988 | return NULL; |
| 12989 | } |
| 12990 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12991 | #endif /* HAVE_STATVFS */ |
| 12992 | |
| 12993 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12994 | if (lchown == NULL) { |
| 12995 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 12996 | return NULL; |
| 12997 | } |
| 12998 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12999 | #endif /* HAVE_LCHOWN */ |
| 13000 | |
| 13001 | |
| 13002 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13003 | |
Antoine Pitrou | 9d20e0e | 2012-09-12 18:01:36 +0200 | [diff] [blame] | 13004 | Py_INCREF(&TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13005 | PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); |
| 13006 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 13007 | billion = PyLong_FromLong(1000000000); |
| 13008 | if (!billion) |
| 13009 | return NULL; |
| 13010 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13011 | /* suppress "function not used" warnings */ |
| 13012 | { |
| 13013 | int ignored; |
| 13014 | fd_specified("", -1); |
| 13015 | follow_symlinks_specified("", 1); |
| 13016 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 13017 | dir_fd_converter(Py_None, &ignored); |
| 13018 | dir_fd_unavailable(Py_None, &ignored); |
| 13019 | } |
| 13020 | |
| 13021 | /* |
| 13022 | * provide list of locally available functions |
| 13023 | * so os.py can populate support_* lists |
| 13024 | */ |
| 13025 | list = PyList_New(0); |
| 13026 | if (!list) |
| 13027 | return NULL; |
| 13028 | for (trace = have_functions; *trace; trace++) { |
| 13029 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 13030 | if (!unicode) |
| 13031 | return NULL; |
| 13032 | if (PyList_Append(list, unicode)) |
| 13033 | return NULL; |
| 13034 | Py_DECREF(unicode); |
| 13035 | } |
| 13036 | PyModule_AddObject(m, "_have_functions", list); |
Ned Deily | eb3be66 | 2016-08-15 14:40:38 -0400 | [diff] [blame] | 13037 | |
| 13038 | Py_INCREF((PyObject *) &DirEntryType); |
Brett Cannon | a32c4d0 | 2016-06-24 14:14:44 -0700 | [diff] [blame] | 13039 | PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13040 | |
| 13041 | initialized = 1; |
| 13042 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13043 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 13044 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13045 | |
| 13046 | #ifdef __cplusplus |
| 13047 | } |
| 13048 | #endif |