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 | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5033 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5034 | #ifdef HAVE_WSPAWNV |
| 5035 | spawnval = _wspawnv(mode, path->wide, argvlist); |
| 5036 | #else |
| 5037 | spawnval = _spawnv(mode, path->narrow, argvlist); |
| 5038 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5039 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5040 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5041 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5042 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5043 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5044 | if (spawnval == -1) |
| 5045 | return posix_error(); |
| 5046 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5047 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5048 | } |
| 5049 | |
| 5050 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5051 | /*[clinic input] |
| 5052 | os.spawnve |
| 5053 | |
| 5054 | mode: int |
| 5055 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5056 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5057 | Path of executable file. |
| 5058 | argv: object |
| 5059 | Tuple or list of strings. |
| 5060 | env: object |
| 5061 | Dictionary of strings mapping to strings. |
| 5062 | / |
| 5063 | |
| 5064 | Execute the program specified by path in a new process. |
| 5065 | [clinic start generated code]*/ |
| 5066 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5067 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5068 | os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv, |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5069 | PyObject *env) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5070 | /*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5071 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5072 | EXECV_CHAR **argvlist; |
| 5073 | EXECV_CHAR **envlist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5074 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5075 | Py_ssize_t argc, i, envc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5076 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5077 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 5078 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5079 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5080 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5081 | argv is a list or tuple of strings and env is a dictionary |
| 5082 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5083 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5084 | if (PyList_Check(argv)) { |
| 5085 | argc = PyList_Size(argv); |
| 5086 | getitem = PyList_GetItem; |
| 5087 | } |
| 5088 | else if (PyTuple_Check(argv)) { |
| 5089 | argc = PyTuple_Size(argv); |
| 5090 | getitem = PyTuple_GetItem; |
| 5091 | } |
| 5092 | else { |
| 5093 | PyErr_SetString(PyExc_TypeError, |
| 5094 | "spawnve() arg 2 must be a tuple or list"); |
| 5095 | goto fail_0; |
| 5096 | } |
| 5097 | if (!PyMapping_Check(env)) { |
| 5098 | PyErr_SetString(PyExc_TypeError, |
| 5099 | "spawnve() arg 3 must be a mapping object"); |
| 5100 | goto fail_0; |
| 5101 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5102 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5103 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5104 | if (argvlist == NULL) { |
| 5105 | PyErr_NoMemory(); |
| 5106 | goto fail_0; |
| 5107 | } |
| 5108 | for (i = 0; i < argc; i++) { |
| 5109 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5110 | &argvlist[i])) |
| 5111 | { |
| 5112 | lastarg = i; |
| 5113 | goto fail_1; |
| 5114 | } |
| 5115 | } |
| 5116 | lastarg = argc; |
| 5117 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5118 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5119 | envlist = parse_envlist(env, &envc); |
| 5120 | if (envlist == NULL) |
| 5121 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5122 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5123 | if (mode == _OLD_P_OVERLAY) |
| 5124 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5125 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5126 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5127 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5128 | #ifdef HAVE_WSPAWNV |
| 5129 | spawnval = _wspawnve(mode, path->wide, argvlist, envlist); |
| 5130 | #else |
| 5131 | spawnval = _spawnve(mode, path->narrow, argvlist, envlist); |
| 5132 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5133 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5134 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5135 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5136 | if (spawnval == -1) |
| 5137 | (void) posix_error(); |
| 5138 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5139 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5140 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5141 | while (--envc >= 0) |
| 5142 | PyMem_DEL(envlist[envc]); |
| 5143 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5144 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5145 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5146 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5147 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5148 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5149 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5150 | #endif /* HAVE_SPAWNV */ |
| 5151 | |
| 5152 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5153 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5154 | /*[clinic input] |
| 5155 | os.fork1 |
| 5156 | |
| 5157 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 5158 | |
| 5159 | Return 0 to child process and PID of child to parent process. |
| 5160 | [clinic start generated code]*/ |
| 5161 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5162 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5163 | os_fork1_impl(PyObject *module) |
| 5164 | /*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5165 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5166 | pid_t pid; |
| 5167 | int result = 0; |
| 5168 | _PyImport_AcquireLock(); |
| 5169 | pid = fork1(); |
| 5170 | if (pid == 0) { |
| 5171 | /* child: this clobbers and resets the import lock. */ |
| 5172 | PyOS_AfterFork(); |
| 5173 | } else { |
| 5174 | /* parent: release the import lock. */ |
| 5175 | result = _PyImport_ReleaseLock(); |
| 5176 | } |
| 5177 | if (pid == -1) |
| 5178 | return posix_error(); |
| 5179 | if (result < 0) { |
| 5180 | /* Don't clobber the OSError if the fork failed. */ |
| 5181 | PyErr_SetString(PyExc_RuntimeError, |
| 5182 | "not holding the import lock"); |
| 5183 | return NULL; |
| 5184 | } |
| 5185 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5186 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5187 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5188 | |
| 5189 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5190 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5191 | /*[clinic input] |
| 5192 | os.fork |
| 5193 | |
| 5194 | Fork a child process. |
| 5195 | |
| 5196 | Return 0 to child process and PID of child to parent process. |
| 5197 | [clinic start generated code]*/ |
| 5198 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5199 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5200 | os_fork_impl(PyObject *module) |
| 5201 | /*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5202 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5203 | pid_t pid; |
| 5204 | int result = 0; |
| 5205 | _PyImport_AcquireLock(); |
| 5206 | pid = fork(); |
| 5207 | if (pid == 0) { |
| 5208 | /* child: this clobbers and resets the import lock. */ |
| 5209 | PyOS_AfterFork(); |
| 5210 | } else { |
| 5211 | /* parent: release the import lock. */ |
| 5212 | result = _PyImport_ReleaseLock(); |
| 5213 | } |
| 5214 | if (pid == -1) |
| 5215 | return posix_error(); |
| 5216 | if (result < 0) { |
| 5217 | /* Don't clobber the OSError if the fork failed. */ |
| 5218 | PyErr_SetString(PyExc_RuntimeError, |
| 5219 | "not holding the import lock"); |
| 5220 | return NULL; |
| 5221 | } |
| 5222 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5223 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5224 | #endif /* HAVE_FORK */ |
| 5225 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5226 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5227 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5228 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5229 | /*[clinic input] |
| 5230 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5231 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5232 | policy: int |
| 5233 | |
| 5234 | Get the maximum scheduling priority for policy. |
| 5235 | [clinic start generated code]*/ |
| 5236 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5237 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5238 | os_sched_get_priority_max_impl(PyObject *module, int policy) |
| 5239 | /*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5240 | { |
| 5241 | int max; |
| 5242 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5243 | max = sched_get_priority_max(policy); |
| 5244 | if (max < 0) |
| 5245 | return posix_error(); |
| 5246 | return PyLong_FromLong(max); |
| 5247 | } |
| 5248 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5249 | |
| 5250 | /*[clinic input] |
| 5251 | os.sched_get_priority_min |
| 5252 | |
| 5253 | policy: int |
| 5254 | |
| 5255 | Get the minimum scheduling priority for policy. |
| 5256 | [clinic start generated code]*/ |
| 5257 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5258 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5259 | os_sched_get_priority_min_impl(PyObject *module, int policy) |
| 5260 | /*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5261 | { |
| 5262 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5263 | if (min < 0) |
| 5264 | return posix_error(); |
| 5265 | return PyLong_FromLong(min); |
| 5266 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5267 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 5268 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5269 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5270 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5271 | /*[clinic input] |
| 5272 | os.sched_getscheduler |
| 5273 | pid: pid_t |
| 5274 | / |
| 5275 | |
| 5276 | Get the scheduling policy for the process identifiedy by pid. |
| 5277 | |
| 5278 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 5279 | [clinic start generated code]*/ |
| 5280 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5281 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5282 | os_sched_getscheduler_impl(PyObject *module, pid_t pid) |
| 5283 | /*[clinic end generated code: output=dce4c0bd3f1b34c8 input=5f14cfd1f189e1a0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5284 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5285 | int policy; |
| 5286 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5287 | policy = sched_getscheduler(pid); |
| 5288 | if (policy < 0) |
| 5289 | return posix_error(); |
| 5290 | return PyLong_FromLong(policy); |
| 5291 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5292 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5293 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5294 | |
| 5295 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5296 | /*[clinic input] |
| 5297 | class os.sched_param "PyObject *" "&SchedParamType" |
| 5298 | |
| 5299 | @classmethod |
| 5300 | os.sched_param.__new__ |
| 5301 | |
| 5302 | sched_priority: object |
| 5303 | A scheduling parameter. |
| 5304 | |
| 5305 | Current has only one field: sched_priority"); |
| 5306 | [clinic start generated code]*/ |
| 5307 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5308 | static PyObject * |
| 5309 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5310 | /*[clinic end generated code: output=48f4067d60f48c13 input=73a4c22f7071fc62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5311 | { |
| 5312 | PyObject *res; |
| 5313 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5314 | res = PyStructSequence_New(type); |
| 5315 | if (!res) |
| 5316 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5317 | Py_INCREF(sched_priority); |
| 5318 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5319 | return res; |
| 5320 | } |
| 5321 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5322 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5323 | PyDoc_VAR(os_sched_param__doc__); |
| 5324 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5325 | static PyStructSequence_Field sched_param_fields[] = { |
| 5326 | {"sched_priority", "the scheduling priority"}, |
| 5327 | {0} |
| 5328 | }; |
| 5329 | |
| 5330 | static PyStructSequence_Desc sched_param_desc = { |
| 5331 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5332 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5333 | sched_param_fields, |
| 5334 | 1 |
| 5335 | }; |
| 5336 | |
| 5337 | static int |
| 5338 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 5339 | { |
| 5340 | long priority; |
| 5341 | |
| 5342 | if (Py_TYPE(param) != &SchedParamType) { |
| 5343 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 5344 | return 0; |
| 5345 | } |
| 5346 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 5347 | if (priority == -1 && PyErr_Occurred()) |
| 5348 | return 0; |
| 5349 | if (priority > INT_MAX || priority < INT_MIN) { |
| 5350 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 5351 | return 0; |
| 5352 | } |
| 5353 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 5354 | return 1; |
| 5355 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5356 | #endif /* defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5357 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5358 | |
| 5359 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5360 | /*[clinic input] |
| 5361 | os.sched_setscheduler |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5362 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5363 | pid: pid_t |
| 5364 | policy: int |
| 5365 | param: sched_param |
| 5366 | / |
| 5367 | |
| 5368 | Set the scheduling policy for the process identified by pid. |
| 5369 | |
| 5370 | If pid is 0, the calling process is changed. |
| 5371 | param is an instance of sched_param. |
| 5372 | [clinic start generated code]*/ |
| 5373 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5374 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5375 | os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 5376 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5377 | /*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5378 | { |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5379 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 5380 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 5381 | ** scheduling policy under Solaris/Illumos, and others. |
| 5382 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5383 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5384 | if (sched_setscheduler(pid, policy, param) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5385 | return posix_error(); |
| 5386 | Py_RETURN_NONE; |
| 5387 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5388 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5389 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5390 | |
| 5391 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5392 | /*[clinic input] |
| 5393 | os.sched_getparam |
| 5394 | pid: pid_t |
| 5395 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5396 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5397 | Returns scheduling parameters for the process identified by pid. |
| 5398 | |
| 5399 | If pid is 0, returns parameters for the calling process. |
| 5400 | Return value is an instance of sched_param. |
| 5401 | [clinic start generated code]*/ |
| 5402 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5403 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5404 | os_sched_getparam_impl(PyObject *module, pid_t pid) |
| 5405 | /*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5406 | { |
| 5407 | struct sched_param param; |
| 5408 | PyObject *result; |
| 5409 | PyObject *priority; |
| 5410 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5411 | if (sched_getparam(pid, ¶m)) |
| 5412 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5413 | result = PyStructSequence_New(&SchedParamType); |
| 5414 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5415 | return NULL; |
| 5416 | priority = PyLong_FromLong(param.sched_priority); |
| 5417 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5418 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5419 | return NULL; |
| 5420 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5421 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 5422 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5423 | } |
| 5424 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5425 | |
| 5426 | /*[clinic input] |
| 5427 | os.sched_setparam |
| 5428 | pid: pid_t |
| 5429 | param: sched_param |
| 5430 | / |
| 5431 | |
| 5432 | Set scheduling parameters for the process identified by pid. |
| 5433 | |
| 5434 | If pid is 0, sets parameters for the calling process. |
| 5435 | param should be an instance of sched_param. |
| 5436 | [clinic start generated code]*/ |
| 5437 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5438 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5439 | os_sched_setparam_impl(PyObject *module, pid_t pid, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 5440 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5441 | /*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5442 | { |
| 5443 | if (sched_setparam(pid, param)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5444 | return posix_error(); |
| 5445 | Py_RETURN_NONE; |
| 5446 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5447 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5448 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5449 | |
| 5450 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5451 | /*[clinic input] |
| 5452 | os.sched_rr_get_interval -> double |
| 5453 | pid: pid_t |
| 5454 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5455 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5456 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 5457 | |
| 5458 | Value returned is a float. |
| 5459 | [clinic start generated code]*/ |
| 5460 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5461 | static double |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5462 | os_sched_rr_get_interval_impl(PyObject *module, pid_t pid) |
| 5463 | /*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5464 | { |
| 5465 | struct timespec interval; |
| 5466 | if (sched_rr_get_interval(pid, &interval)) { |
| 5467 | posix_error(); |
| 5468 | return -1.0; |
| 5469 | } |
| 5470 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 5471 | } |
| 5472 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5473 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5474 | |
| 5475 | /*[clinic input] |
| 5476 | os.sched_yield |
| 5477 | |
| 5478 | Voluntarily relinquish the CPU. |
| 5479 | [clinic start generated code]*/ |
| 5480 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5481 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5482 | os_sched_yield_impl(PyObject *module) |
| 5483 | /*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5484 | { |
| 5485 | if (sched_yield()) |
| 5486 | return posix_error(); |
| 5487 | Py_RETURN_NONE; |
| 5488 | } |
| 5489 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5490 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5491 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 5492 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5493 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5494 | /*[clinic input] |
| 5495 | os.sched_setaffinity |
| 5496 | pid: pid_t |
| 5497 | mask : object |
| 5498 | / |
| 5499 | |
| 5500 | Set the CPU affinity of the process identified by pid to mask. |
| 5501 | |
| 5502 | mask should be an iterable of integers identifying CPUs. |
| 5503 | [clinic start generated code]*/ |
| 5504 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5505 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5506 | os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask) |
| 5507 | /*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5508 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5509 | int ncpus; |
| 5510 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5511 | cpu_set_t *cpu_set = NULL; |
| 5512 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5513 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5514 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5515 | if (iterator == NULL) |
| 5516 | return NULL; |
| 5517 | |
| 5518 | ncpus = NCPUS_START; |
| 5519 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5520 | cpu_set = CPU_ALLOC(ncpus); |
| 5521 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5522 | PyErr_NoMemory(); |
| 5523 | goto error; |
| 5524 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5525 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5526 | |
| 5527 | while ((item = PyIter_Next(iterator))) { |
| 5528 | long cpu; |
| 5529 | if (!PyLong_Check(item)) { |
| 5530 | PyErr_Format(PyExc_TypeError, |
| 5531 | "expected an iterator of ints, " |
| 5532 | "but iterator yielded %R", |
| 5533 | Py_TYPE(item)); |
| 5534 | Py_DECREF(item); |
| 5535 | goto error; |
| 5536 | } |
| 5537 | cpu = PyLong_AsLong(item); |
| 5538 | Py_DECREF(item); |
| 5539 | if (cpu < 0) { |
| 5540 | if (!PyErr_Occurred()) |
| 5541 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 5542 | goto error; |
| 5543 | } |
| 5544 | if (cpu > INT_MAX - 1) { |
| 5545 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 5546 | goto error; |
| 5547 | } |
| 5548 | if (cpu >= ncpus) { |
| 5549 | /* Grow CPU mask to fit the CPU number */ |
| 5550 | int newncpus = ncpus; |
| 5551 | cpu_set_t *newmask; |
| 5552 | size_t newsetsize; |
| 5553 | while (newncpus <= cpu) { |
| 5554 | if (newncpus > INT_MAX / 2) |
| 5555 | newncpus = cpu + 1; |
| 5556 | else |
| 5557 | newncpus = newncpus * 2; |
| 5558 | } |
| 5559 | newmask = CPU_ALLOC(newncpus); |
| 5560 | if (newmask == NULL) { |
| 5561 | PyErr_NoMemory(); |
| 5562 | goto error; |
| 5563 | } |
| 5564 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 5565 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5566 | memcpy(newmask, cpu_set, setsize); |
| 5567 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5568 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5569 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5570 | ncpus = newncpus; |
| 5571 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5572 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5573 | } |
| 5574 | Py_CLEAR(iterator); |
| 5575 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5576 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5577 | posix_error(); |
| 5578 | goto error; |
| 5579 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5580 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5581 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5582 | |
| 5583 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5584 | if (cpu_set) |
| 5585 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5586 | Py_XDECREF(iterator); |
| 5587 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5588 | } |
| 5589 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5590 | |
| 5591 | /*[clinic input] |
| 5592 | os.sched_getaffinity |
| 5593 | pid: pid_t |
| 5594 | / |
| 5595 | |
Charles-François Natali | dc87e4b | 2015-07-13 21:01:39 +0100 | [diff] [blame] | 5596 | 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] | 5597 | |
| 5598 | The affinity is returned as a set of CPU identifiers. |
| 5599 | [clinic start generated code]*/ |
| 5600 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5601 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5602 | os_sched_getaffinity_impl(PyObject *module, pid_t pid) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 5603 | /*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5604 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5605 | int cpu, ncpus, count; |
| 5606 | size_t setsize; |
| 5607 | cpu_set_t *mask = NULL; |
| 5608 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5609 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5610 | ncpus = NCPUS_START; |
| 5611 | while (1) { |
| 5612 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5613 | mask = CPU_ALLOC(ncpus); |
| 5614 | if (mask == NULL) |
| 5615 | return PyErr_NoMemory(); |
| 5616 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 5617 | break; |
| 5618 | CPU_FREE(mask); |
| 5619 | if (errno != EINVAL) |
| 5620 | return posix_error(); |
| 5621 | if (ncpus > INT_MAX / 2) { |
| 5622 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 5623 | "a large enough CPU set"); |
| 5624 | return NULL; |
| 5625 | } |
| 5626 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5627 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5628 | |
| 5629 | res = PySet_New(NULL); |
| 5630 | if (res == NULL) |
| 5631 | goto error; |
| 5632 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 5633 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 5634 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 5635 | --count; |
| 5636 | if (cpu_num == NULL) |
| 5637 | goto error; |
| 5638 | if (PySet_Add(res, cpu_num)) { |
| 5639 | Py_DECREF(cpu_num); |
| 5640 | goto error; |
| 5641 | } |
| 5642 | Py_DECREF(cpu_num); |
| 5643 | } |
| 5644 | } |
| 5645 | CPU_FREE(mask); |
| 5646 | return res; |
| 5647 | |
| 5648 | error: |
| 5649 | if (mask) |
| 5650 | CPU_FREE(mask); |
| 5651 | Py_XDECREF(res); |
| 5652 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5653 | } |
| 5654 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5655 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5656 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5657 | #endif /* HAVE_SCHED_H */ |
| 5658 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5659 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5660 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5661 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5662 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5663 | #define DEV_PTY_FILE "/dev/ptc" |
| 5664 | #define HAVE_DEV_PTMX |
| 5665 | #else |
| 5666 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5667 | #endif |
| 5668 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5669 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5670 | #ifdef HAVE_PTY_H |
| 5671 | #include <pty.h> |
| 5672 | #else |
| 5673 | #ifdef HAVE_LIBUTIL_H |
| 5674 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5675 | #else |
| 5676 | #ifdef HAVE_UTIL_H |
| 5677 | #include <util.h> |
| 5678 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5679 | #endif /* HAVE_LIBUTIL_H */ |
| 5680 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5681 | #ifdef HAVE_STROPTS_H |
| 5682 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5683 | #endif |
| 5684 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5685 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5686 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5687 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5688 | /*[clinic input] |
| 5689 | os.openpty |
| 5690 | |
| 5691 | Open a pseudo-terminal. |
| 5692 | |
| 5693 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 5694 | for both the master and slave ends. |
| 5695 | [clinic start generated code]*/ |
| 5696 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5697 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5698 | os_openpty_impl(PyObject *module) |
| 5699 | /*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5700 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5701 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5702 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5703 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5704 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5705 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5706 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5707 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5708 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5709 | #endif |
| 5710 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5711 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5712 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5713 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5714 | goto posix_error; |
| 5715 | |
| 5716 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5717 | goto error; |
| 5718 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 5719 | goto error; |
| 5720 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5721 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5722 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5723 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5724 | goto posix_error; |
| 5725 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5726 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5727 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5728 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5729 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 5730 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5731 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5732 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 5733 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5734 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5735 | goto posix_error; |
| 5736 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5737 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5738 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5739 | /* change permission of slave */ |
| 5740 | if (grantpt(master_fd) < 0) { |
| 5741 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5742 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5743 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5744 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5745 | /* unlock slave */ |
| 5746 | if (unlockpt(master_fd) < 0) { |
| 5747 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5748 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5749 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5750 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5751 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5752 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5753 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 5754 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5755 | goto posix_error; |
| 5756 | |
| 5757 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 5758 | if (slave_fd == -1) |
| 5759 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 5760 | |
| 5761 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5762 | goto posix_error; |
| 5763 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 5764 | #if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5765 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 5766 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5767 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5768 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5769 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5770 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 5771 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5772 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5773 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5774 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5775 | posix_error: |
| 5776 | posix_error(); |
| 5777 | error: |
| 5778 | if (master_fd != -1) |
| 5779 | close(master_fd); |
| 5780 | if (slave_fd != -1) |
| 5781 | close(slave_fd); |
| 5782 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5783 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5784 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5785 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5786 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5787 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5788 | /*[clinic input] |
| 5789 | os.forkpty |
| 5790 | |
| 5791 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 5792 | |
| 5793 | Returns a tuple of (pid, master_fd). |
| 5794 | Like fork(), return pid of 0 to the child process, |
| 5795 | and pid of child to the parent process. |
| 5796 | To both, return fd of newly opened pseudo-terminal. |
| 5797 | [clinic start generated code]*/ |
| 5798 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5799 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5800 | os_forkpty_impl(PyObject *module) |
| 5801 | /*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5802 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5803 | int master_fd = -1, result = 0; |
| 5804 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5805 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5806 | _PyImport_AcquireLock(); |
| 5807 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 5808 | if (pid == 0) { |
| 5809 | /* child: this clobbers and resets the import lock. */ |
| 5810 | PyOS_AfterFork(); |
| 5811 | } else { |
| 5812 | /* parent: release the import lock. */ |
| 5813 | result = _PyImport_ReleaseLock(); |
| 5814 | } |
| 5815 | if (pid == -1) |
| 5816 | return posix_error(); |
| 5817 | if (result < 0) { |
| 5818 | /* Don't clobber the OSError if the fork failed. */ |
| 5819 | PyErr_SetString(PyExc_RuntimeError, |
| 5820 | "not holding the import lock"); |
| 5821 | return NULL; |
| 5822 | } |
| 5823 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5824 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5825 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5826 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5827 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5828 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5829 | /*[clinic input] |
| 5830 | os.getegid |
| 5831 | |
| 5832 | Return the current process's effective group id. |
| 5833 | [clinic start generated code]*/ |
| 5834 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5835 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5836 | os_getegid_impl(PyObject *module) |
| 5837 | /*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5838 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5839 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5840 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5841 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5842 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5843 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5844 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5845 | /*[clinic input] |
| 5846 | os.geteuid |
| 5847 | |
| 5848 | Return the current process's effective user id. |
| 5849 | [clinic start generated code]*/ |
| 5850 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5851 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5852 | os_geteuid_impl(PyObject *module) |
| 5853 | /*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5854 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5855 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5856 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5857 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5858 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5859 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5860 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5861 | /*[clinic input] |
| 5862 | os.getgid |
| 5863 | |
| 5864 | Return the current process's group id. |
| 5865 | [clinic start generated code]*/ |
| 5866 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5867 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5868 | os_getgid_impl(PyObject *module) |
| 5869 | /*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5870 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5871 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5872 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5873 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5874 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5875 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5876 | /*[clinic input] |
| 5877 | os.getpid |
| 5878 | |
| 5879 | Return the current process id. |
| 5880 | [clinic start generated code]*/ |
| 5881 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5882 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5883 | os_getpid_impl(PyObject *module) |
| 5884 | /*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5885 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5886 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5887 | } |
| 5888 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5889 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5890 | |
| 5891 | /* AC 3.5: funny apple logic below */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5892 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 5893 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 5894 | Returns a list of groups to which a user belongs.\n\n\ |
| 5895 | user: username to lookup\n\ |
| 5896 | group: base group id of the user"); |
| 5897 | |
| 5898 | static PyObject * |
| 5899 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 5900 | { |
| 5901 | #ifdef NGROUPS_MAX |
| 5902 | #define MAX_GROUPS NGROUPS_MAX |
| 5903 | #else |
| 5904 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 5905 | #define MAX_GROUPS 64 |
| 5906 | #endif |
| 5907 | |
| 5908 | const char *user; |
| 5909 | int i, ngroups; |
| 5910 | PyObject *list; |
| 5911 | #ifdef __APPLE__ |
| 5912 | int *groups, basegid; |
| 5913 | #else |
| 5914 | gid_t *groups, basegid; |
| 5915 | #endif |
| 5916 | ngroups = MAX_GROUPS; |
| 5917 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5918 | #ifdef __APPLE__ |
| 5919 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5920 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5921 | #else |
| 5922 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 5923 | _Py_Gid_Converter, &basegid)) |
| 5924 | return NULL; |
| 5925 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5926 | |
| 5927 | #ifdef __APPLE__ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 5928 | groups = PyMem_New(int, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5929 | #else |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 5930 | groups = PyMem_New(gid_t, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5931 | #endif |
| 5932 | if (groups == NULL) |
| 5933 | return PyErr_NoMemory(); |
| 5934 | |
| 5935 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 5936 | PyMem_Del(groups); |
| 5937 | return posix_error(); |
| 5938 | } |
| 5939 | |
| 5940 | list = PyList_New(ngroups); |
| 5941 | if (list == NULL) { |
| 5942 | PyMem_Del(groups); |
| 5943 | return NULL; |
| 5944 | } |
| 5945 | |
| 5946 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5947 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5948 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5949 | #else |
| 5950 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 5951 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5952 | if (o == NULL) { |
| 5953 | Py_DECREF(list); |
| 5954 | PyMem_Del(groups); |
| 5955 | return NULL; |
| 5956 | } |
| 5957 | PyList_SET_ITEM(list, i, o); |
| 5958 | } |
| 5959 | |
| 5960 | PyMem_Del(groups); |
| 5961 | |
| 5962 | return list; |
| 5963 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5964 | #endif /* HAVE_GETGROUPLIST */ |
| 5965 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5966 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5967 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5968 | /*[clinic input] |
| 5969 | os.getgroups |
| 5970 | |
| 5971 | Return list of supplemental group IDs for the process. |
| 5972 | [clinic start generated code]*/ |
| 5973 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5974 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5975 | os_getgroups_impl(PyObject *module) |
| 5976 | /*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5977 | { |
| 5978 | PyObject *result = NULL; |
| 5979 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5980 | #ifdef NGROUPS_MAX |
| 5981 | #define MAX_GROUPS NGROUPS_MAX |
| 5982 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5983 | /* 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] | 5984 | #define MAX_GROUPS 64 |
| 5985 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5986 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5987 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5988 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5989 | * This is a helper variable to store the intermediate result when |
| 5990 | * that happens. |
| 5991 | * |
| 5992 | * To keep the code readable the OSX behaviour is unconditional, |
| 5993 | * according to the POSIX spec this should be safe on all unix-y |
| 5994 | * systems. |
| 5995 | */ |
| 5996 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5997 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5998 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 5999 | #ifdef __APPLE__ |
| 6000 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 6001 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 6002 | * always first call getgroups with length 0 to get the actual number |
| 6003 | * of groups. |
| 6004 | */ |
| 6005 | n = getgroups(0, NULL); |
| 6006 | if (n < 0) { |
| 6007 | return posix_error(); |
| 6008 | } else if (n <= MAX_GROUPS) { |
| 6009 | /* groups will fit in existing array */ |
| 6010 | alt_grouplist = grouplist; |
| 6011 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6012 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6013 | if (alt_grouplist == NULL) { |
| 6014 | errno = EINVAL; |
| 6015 | return posix_error(); |
| 6016 | } |
| 6017 | } |
| 6018 | |
| 6019 | n = getgroups(n, alt_grouplist); |
| 6020 | if (n == -1) { |
| 6021 | if (alt_grouplist != grouplist) { |
| 6022 | PyMem_Free(alt_grouplist); |
| 6023 | } |
| 6024 | return posix_error(); |
| 6025 | } |
| 6026 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6027 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6028 | if (n < 0) { |
| 6029 | if (errno == EINVAL) { |
| 6030 | n = getgroups(0, NULL); |
| 6031 | if (n == -1) { |
| 6032 | return posix_error(); |
| 6033 | } |
| 6034 | if (n == 0) { |
| 6035 | /* Avoid malloc(0) */ |
| 6036 | alt_grouplist = grouplist; |
| 6037 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6038 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6039 | if (alt_grouplist == NULL) { |
| 6040 | errno = EINVAL; |
| 6041 | return posix_error(); |
| 6042 | } |
| 6043 | n = getgroups(n, alt_grouplist); |
| 6044 | if (n == -1) { |
| 6045 | PyMem_Free(alt_grouplist); |
| 6046 | return posix_error(); |
| 6047 | } |
| 6048 | } |
| 6049 | } else { |
| 6050 | return posix_error(); |
| 6051 | } |
| 6052 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6053 | #endif |
| 6054 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6055 | result = PyList_New(n); |
| 6056 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6057 | int i; |
| 6058 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6059 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6060 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6061 | Py_DECREF(result); |
| 6062 | result = NULL; |
| 6063 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6064 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6065 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6066 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6067 | } |
| 6068 | |
| 6069 | if (alt_grouplist != grouplist) { |
| 6070 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6071 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6072 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6073 | return result; |
| 6074 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6075 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6076 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6077 | #ifdef HAVE_INITGROUPS |
| 6078 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 6079 | "initgroups(username, gid) -> None\n\n\ |
| 6080 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 6081 | the groups of which the specified username is a member, plus the specified\n\ |
| 6082 | group id."); |
| 6083 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6084 | /* AC 3.5: funny apple logic */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6085 | static PyObject * |
| 6086 | posix_initgroups(PyObject *self, PyObject *args) |
| 6087 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6088 | PyObject *oname; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 6089 | const char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6090 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6091 | #ifdef __APPLE__ |
| 6092 | int gid; |
| 6093 | #else |
| 6094 | gid_t gid; |
| 6095 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6096 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6097 | #ifdef __APPLE__ |
| 6098 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 6099 | PyUnicode_FSConverter, &oname, |
| 6100 | &gid)) |
| 6101 | #else |
| 6102 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 6103 | PyUnicode_FSConverter, &oname, |
| 6104 | _Py_Gid_Converter, &gid)) |
| 6105 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6106 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6107 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6108 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6109 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6110 | Py_DECREF(oname); |
| 6111 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6112 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6113 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6114 | Py_INCREF(Py_None); |
| 6115 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6116 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6117 | #endif /* HAVE_INITGROUPS */ |
| 6118 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6119 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6120 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6121 | /*[clinic input] |
| 6122 | os.getpgid |
| 6123 | |
| 6124 | pid: pid_t |
| 6125 | |
| 6126 | Call the system call getpgid(), and return the result. |
| 6127 | [clinic start generated code]*/ |
| 6128 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6129 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6130 | os_getpgid_impl(PyObject *module, pid_t pid) |
| 6131 | /*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6132 | { |
| 6133 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6134 | if (pgid < 0) |
| 6135 | return posix_error(); |
| 6136 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6137 | } |
| 6138 | #endif /* HAVE_GETPGID */ |
| 6139 | |
| 6140 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6141 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6142 | /*[clinic input] |
| 6143 | os.getpgrp |
| 6144 | |
| 6145 | Return the current process group id. |
| 6146 | [clinic start generated code]*/ |
| 6147 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6148 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6149 | os_getpgrp_impl(PyObject *module) |
| 6150 | /*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6151 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6152 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6153 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6154 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6155 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6156 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6157 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6158 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6159 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6160 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6161 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6162 | /*[clinic input] |
| 6163 | os.setpgrp |
| 6164 | |
| 6165 | Make the current process the leader of its process group. |
| 6166 | [clinic start generated code]*/ |
| 6167 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6168 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6169 | os_setpgrp_impl(PyObject *module) |
| 6170 | /*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6171 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6172 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6173 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6174 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6175 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6176 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6177 | return posix_error(); |
| 6178 | Py_INCREF(Py_None); |
| 6179 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6180 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6181 | #endif /* HAVE_SETPGRP */ |
| 6182 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6183 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6184 | |
| 6185 | #ifdef MS_WINDOWS |
| 6186 | #include <tlhelp32.h> |
| 6187 | |
| 6188 | static PyObject* |
| 6189 | win32_getppid() |
| 6190 | { |
| 6191 | HANDLE snapshot; |
| 6192 | pid_t mypid; |
| 6193 | PyObject* result = NULL; |
| 6194 | BOOL have_record; |
| 6195 | PROCESSENTRY32 pe; |
| 6196 | |
| 6197 | mypid = getpid(); /* This function never fails */ |
| 6198 | |
| 6199 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 6200 | if (snapshot == INVALID_HANDLE_VALUE) |
| 6201 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 6202 | |
| 6203 | pe.dwSize = sizeof(pe); |
| 6204 | have_record = Process32First(snapshot, &pe); |
| 6205 | while (have_record) { |
| 6206 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 6207 | /* We could cache the ulong value in a static variable. */ |
| 6208 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 6209 | break; |
| 6210 | } |
| 6211 | |
| 6212 | have_record = Process32Next(snapshot, &pe); |
| 6213 | } |
| 6214 | |
| 6215 | /* If our loop exits and our pid was not found (result will be NULL) |
| 6216 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 6217 | * error anyway, so let's raise it. */ |
| 6218 | if (!result) |
| 6219 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6220 | |
| 6221 | CloseHandle(snapshot); |
| 6222 | |
| 6223 | return result; |
| 6224 | } |
| 6225 | #endif /*MS_WINDOWS*/ |
| 6226 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6227 | |
| 6228 | /*[clinic input] |
| 6229 | os.getppid |
| 6230 | |
| 6231 | Return the parent's process id. |
| 6232 | |
| 6233 | If the parent process has already exited, Windows machines will still |
| 6234 | return its id; others systems will return the id of the 'init' process (1). |
| 6235 | [clinic start generated code]*/ |
| 6236 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6237 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6238 | os_getppid_impl(PyObject *module) |
| 6239 | /*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6240 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6241 | #ifdef MS_WINDOWS |
| 6242 | return win32_getppid(); |
| 6243 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6244 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6245 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6246 | } |
| 6247 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6248 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6249 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6250 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6251 | /*[clinic input] |
| 6252 | os.getlogin |
| 6253 | |
| 6254 | Return the actual login name. |
| 6255 | [clinic start generated code]*/ |
| 6256 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6257 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6258 | os_getlogin_impl(PyObject *module) |
| 6259 | /*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6260 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6261 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6262 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6263 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6264 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6265 | |
| 6266 | if (GetUserNameW(user_name, &num_chars)) { |
| 6267 | /* num_chars is the number of unicode chars plus null terminator */ |
| 6268 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6269 | } |
| 6270 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6271 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6272 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6273 | char *name; |
| 6274 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6275 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6276 | errno = 0; |
| 6277 | name = getlogin(); |
| 6278 | if (name == NULL) { |
| 6279 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6280 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6281 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6282 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6283 | } |
| 6284 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6285 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6286 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6287 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6288 | return result; |
| 6289 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6290 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6291 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6292 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6293 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6294 | /*[clinic input] |
| 6295 | os.getuid |
| 6296 | |
| 6297 | Return the current process's user id. |
| 6298 | [clinic start generated code]*/ |
| 6299 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6300 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6301 | os_getuid_impl(PyObject *module) |
| 6302 | /*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6303 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6304 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6305 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6306 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6307 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6308 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6309 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6310 | #define HAVE_KILL |
| 6311 | #endif /* MS_WINDOWS */ |
| 6312 | |
| 6313 | #ifdef HAVE_KILL |
| 6314 | /*[clinic input] |
| 6315 | os.kill |
| 6316 | |
| 6317 | pid: pid_t |
| 6318 | signal: Py_ssize_t |
| 6319 | / |
| 6320 | |
| 6321 | Kill a process with a signal. |
| 6322 | [clinic start generated code]*/ |
| 6323 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6324 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6325 | os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal) |
| 6326 | /*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6327 | #ifndef MS_WINDOWS |
| 6328 | { |
| 6329 | if (kill(pid, (int)signal) == -1) |
| 6330 | return posix_error(); |
| 6331 | Py_RETURN_NONE; |
| 6332 | } |
| 6333 | #else /* !MS_WINDOWS */ |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6334 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 6335 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6336 | DWORD sig = (DWORD)signal; |
| 6337 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6338 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6339 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6340 | /* Console processes which share a common console can be sent CTRL+C or |
| 6341 | CTRL+BREAK events, provided they handle said events. */ |
| 6342 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6343 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6344 | err = GetLastError(); |
| 6345 | PyErr_SetFromWindowsErr(err); |
| 6346 | } |
| 6347 | else |
| 6348 | Py_RETURN_NONE; |
| 6349 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6350 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6351 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 6352 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6353 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6354 | if (handle == NULL) { |
| 6355 | err = GetLastError(); |
| 6356 | return PyErr_SetFromWindowsErr(err); |
| 6357 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6358 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6359 | if (TerminateProcess(handle, sig) == 0) { |
| 6360 | err = GetLastError(); |
| 6361 | result = PyErr_SetFromWindowsErr(err); |
| 6362 | } else { |
| 6363 | Py_INCREF(Py_None); |
| 6364 | result = Py_None; |
| 6365 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6366 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6367 | CloseHandle(handle); |
| 6368 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6369 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6370 | #endif /* !MS_WINDOWS */ |
| 6371 | #endif /* HAVE_KILL */ |
| 6372 | |
| 6373 | |
| 6374 | #ifdef HAVE_KILLPG |
| 6375 | /*[clinic input] |
| 6376 | os.killpg |
| 6377 | |
| 6378 | pgid: pid_t |
| 6379 | signal: int |
| 6380 | / |
| 6381 | |
| 6382 | Kill a process group with a signal. |
| 6383 | [clinic start generated code]*/ |
| 6384 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6385 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6386 | os_killpg_impl(PyObject *module, pid_t pgid, int signal) |
| 6387 | /*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6388 | { |
| 6389 | /* XXX some man pages make the `pgid` parameter an int, others |
| 6390 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 6391 | take the same type. Moreover, pid_t is always at least as wide as |
| 6392 | int (else compilation of this module fails), which is safe. */ |
| 6393 | if (killpg(pgid, signal) == -1) |
| 6394 | return posix_error(); |
| 6395 | Py_RETURN_NONE; |
| 6396 | } |
| 6397 | #endif /* HAVE_KILLPG */ |
| 6398 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6399 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6400 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6401 | #ifdef HAVE_SYS_LOCK_H |
| 6402 | #include <sys/lock.h> |
| 6403 | #endif |
| 6404 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6405 | /*[clinic input] |
| 6406 | os.plock |
| 6407 | op: int |
| 6408 | / |
| 6409 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6410 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6411 | [clinic start generated code]*/ |
| 6412 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6413 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6414 | os_plock_impl(PyObject *module, int op) |
| 6415 | /*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6416 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6417 | if (plock(op) == -1) |
| 6418 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6419 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6420 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6421 | #endif /* HAVE_PLOCK */ |
| 6422 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6423 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6424 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6425 | /*[clinic input] |
| 6426 | os.setuid |
| 6427 | |
| 6428 | uid: uid_t |
| 6429 | / |
| 6430 | |
| 6431 | Set the current process's user id. |
| 6432 | [clinic start generated code]*/ |
| 6433 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6434 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6435 | os_setuid_impl(PyObject *module, uid_t uid) |
| 6436 | /*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6437 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6438 | if (setuid(uid) < 0) |
| 6439 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6440 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6441 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6442 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6443 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6444 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6445 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6446 | /*[clinic input] |
| 6447 | os.seteuid |
| 6448 | |
| 6449 | euid: uid_t |
| 6450 | / |
| 6451 | |
| 6452 | Set the current process's effective user id. |
| 6453 | [clinic start generated code]*/ |
| 6454 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6455 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6456 | os_seteuid_impl(PyObject *module, uid_t euid) |
| 6457 | /*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6458 | { |
| 6459 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6460 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6461 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6462 | } |
| 6463 | #endif /* HAVE_SETEUID */ |
| 6464 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6465 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6466 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6467 | /*[clinic input] |
| 6468 | os.setegid |
| 6469 | |
| 6470 | egid: gid_t |
| 6471 | / |
| 6472 | |
| 6473 | Set the current process's effective group id. |
| 6474 | [clinic start generated code]*/ |
| 6475 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6476 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6477 | os_setegid_impl(PyObject *module, gid_t egid) |
| 6478 | /*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6479 | { |
| 6480 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6481 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6482 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6483 | } |
| 6484 | #endif /* HAVE_SETEGID */ |
| 6485 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6486 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6487 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6488 | /*[clinic input] |
| 6489 | os.setreuid |
| 6490 | |
| 6491 | ruid: uid_t |
| 6492 | euid: uid_t |
| 6493 | / |
| 6494 | |
| 6495 | Set the current process's real and effective user ids. |
| 6496 | [clinic start generated code]*/ |
| 6497 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6498 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6499 | os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid) |
| 6500 | /*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6501 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6502 | if (setreuid(ruid, euid) < 0) { |
| 6503 | return posix_error(); |
| 6504 | } else { |
| 6505 | Py_INCREF(Py_None); |
| 6506 | return Py_None; |
| 6507 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6508 | } |
| 6509 | #endif /* HAVE_SETREUID */ |
| 6510 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6511 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6512 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6513 | /*[clinic input] |
| 6514 | os.setregid |
| 6515 | |
| 6516 | rgid: gid_t |
| 6517 | egid: gid_t |
| 6518 | / |
| 6519 | |
| 6520 | Set the current process's real and effective group ids. |
| 6521 | [clinic start generated code]*/ |
| 6522 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6523 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6524 | os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid) |
| 6525 | /*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6526 | { |
| 6527 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6528 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6529 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6530 | } |
| 6531 | #endif /* HAVE_SETREGID */ |
| 6532 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6533 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6534 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6535 | /*[clinic input] |
| 6536 | os.setgid |
| 6537 | gid: gid_t |
| 6538 | / |
| 6539 | |
| 6540 | Set the current process's group id. |
| 6541 | [clinic start generated code]*/ |
| 6542 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6543 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6544 | os_setgid_impl(PyObject *module, gid_t gid) |
| 6545 | /*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6546 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6547 | if (setgid(gid) < 0) |
| 6548 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6549 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6550 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6551 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6552 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6553 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6554 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6555 | /*[clinic input] |
| 6556 | os.setgroups |
| 6557 | |
| 6558 | groups: object |
| 6559 | / |
| 6560 | |
| 6561 | Set the groups of the current process to list. |
| 6562 | [clinic start generated code]*/ |
| 6563 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6564 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6565 | os_setgroups(PyObject *module, PyObject *groups) |
| 6566 | /*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6567 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6568 | int i, len; |
| 6569 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6570 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6571 | if (!PySequence_Check(groups)) { |
| 6572 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6573 | return NULL; |
| 6574 | } |
| 6575 | len = PySequence_Size(groups); |
| 6576 | if (len > MAX_GROUPS) { |
| 6577 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6578 | return NULL; |
| 6579 | } |
| 6580 | for(i = 0; i < len; i++) { |
| 6581 | PyObject *elem; |
| 6582 | elem = PySequence_GetItem(groups, i); |
| 6583 | if (!elem) |
| 6584 | return NULL; |
| 6585 | if (!PyLong_Check(elem)) { |
| 6586 | PyErr_SetString(PyExc_TypeError, |
| 6587 | "groups must be integers"); |
| 6588 | Py_DECREF(elem); |
| 6589 | return NULL; |
| 6590 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6591 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6592 | Py_DECREF(elem); |
| 6593 | return NULL; |
| 6594 | } |
| 6595 | } |
| 6596 | Py_DECREF(elem); |
| 6597 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6598 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6599 | if (setgroups(len, grouplist) < 0) |
| 6600 | return posix_error(); |
| 6601 | Py_INCREF(Py_None); |
| 6602 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6603 | } |
| 6604 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6605 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6606 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6607 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6608 | wait_helper(pid_t pid, int status, struct rusage *ru) |
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 | PyObject *result; |
| 6611 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6612 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6613 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6614 | if (pid == -1) |
| 6615 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6616 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6617 | if (struct_rusage == NULL) { |
| 6618 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6619 | if (m == NULL) |
| 6620 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6621 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6622 | Py_DECREF(m); |
| 6623 | if (struct_rusage == NULL) |
| 6624 | return NULL; |
| 6625 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6626 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6627 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6628 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6629 | if (!result) |
| 6630 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6631 | |
| 6632 | #ifndef doubletime |
| 6633 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6634 | #endif |
| 6635 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6636 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6637 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6638 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6639 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6640 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6641 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6642 | SET_INT(result, 2, ru->ru_maxrss); |
| 6643 | SET_INT(result, 3, ru->ru_ixrss); |
| 6644 | SET_INT(result, 4, ru->ru_idrss); |
| 6645 | SET_INT(result, 5, ru->ru_isrss); |
| 6646 | SET_INT(result, 6, ru->ru_minflt); |
| 6647 | SET_INT(result, 7, ru->ru_majflt); |
| 6648 | SET_INT(result, 8, ru->ru_nswap); |
| 6649 | SET_INT(result, 9, ru->ru_inblock); |
| 6650 | SET_INT(result, 10, ru->ru_oublock); |
| 6651 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6652 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6653 | SET_INT(result, 13, ru->ru_nsignals); |
| 6654 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6655 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6656 | #undef SET_INT |
| 6657 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6658 | if (PyErr_Occurred()) { |
| 6659 | Py_DECREF(result); |
| 6660 | return NULL; |
| 6661 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6662 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6663 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6664 | } |
| 6665 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6666 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6667 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6668 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6669 | /*[clinic input] |
| 6670 | os.wait3 |
| 6671 | |
| 6672 | options: int |
| 6673 | Wait for completion of a child process. |
| 6674 | |
| 6675 | Returns a tuple of information about the child process: |
| 6676 | (pid, status, rusage) |
| 6677 | [clinic start generated code]*/ |
| 6678 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6679 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6680 | os_wait3_impl(PyObject *module, int options) |
| 6681 | /*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6682 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6683 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6684 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6685 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6686 | WAIT_TYPE status; |
| 6687 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6688 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6689 | do { |
| 6690 | Py_BEGIN_ALLOW_THREADS |
| 6691 | pid = wait3(&status, options, &ru); |
| 6692 | Py_END_ALLOW_THREADS |
| 6693 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6694 | if (pid < 0) |
| 6695 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6696 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6697 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6698 | } |
| 6699 | #endif /* HAVE_WAIT3 */ |
| 6700 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6701 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6702 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6703 | /*[clinic input] |
| 6704 | |
| 6705 | os.wait4 |
| 6706 | |
| 6707 | pid: pid_t |
| 6708 | options: int |
| 6709 | |
| 6710 | Wait for completion of a specific child process. |
| 6711 | |
| 6712 | Returns a tuple of information about the child process: |
| 6713 | (pid, status, rusage) |
| 6714 | [clinic start generated code]*/ |
| 6715 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6716 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6717 | os_wait4_impl(PyObject *module, pid_t pid, int options) |
| 6718 | /*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6719 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6720 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6721 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6722 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6723 | WAIT_TYPE status; |
| 6724 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6725 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6726 | do { |
| 6727 | Py_BEGIN_ALLOW_THREADS |
| 6728 | res = wait4(pid, &status, options, &ru); |
| 6729 | Py_END_ALLOW_THREADS |
| 6730 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6731 | if (res < 0) |
| 6732 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6733 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6734 | return wait_helper(res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6735 | } |
| 6736 | #endif /* HAVE_WAIT4 */ |
| 6737 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6738 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6739 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6740 | /*[clinic input] |
| 6741 | os.waitid |
| 6742 | |
| 6743 | idtype: idtype_t |
| 6744 | Must be one of be P_PID, P_PGID or P_ALL. |
| 6745 | id: id_t |
| 6746 | The id to wait on. |
| 6747 | options: int |
| 6748 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 6749 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 6750 | / |
| 6751 | |
| 6752 | Returns the result of waiting for a process or processes. |
| 6753 | |
| 6754 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 6755 | no children in a waitable state. |
| 6756 | [clinic start generated code]*/ |
| 6757 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6758 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6759 | os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options) |
| 6760 | /*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6761 | { |
| 6762 | PyObject *result; |
| 6763 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6764 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6765 | siginfo_t si; |
| 6766 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6767 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6768 | do { |
| 6769 | Py_BEGIN_ALLOW_THREADS |
| 6770 | res = waitid(idtype, id, &si, options); |
| 6771 | Py_END_ALLOW_THREADS |
| 6772 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6773 | if (res < 0) |
| 6774 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6775 | |
| 6776 | if (si.si_pid == 0) |
| 6777 | Py_RETURN_NONE; |
| 6778 | |
| 6779 | result = PyStructSequence_New(&WaitidResultType); |
| 6780 | if (!result) |
| 6781 | return NULL; |
| 6782 | |
| 6783 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6784 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6785 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6786 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6787 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6788 | if (PyErr_Occurred()) { |
| 6789 | Py_DECREF(result); |
| 6790 | return NULL; |
| 6791 | } |
| 6792 | |
| 6793 | return result; |
| 6794 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6795 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6796 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6797 | |
| 6798 | #if defined(HAVE_WAITPID) |
| 6799 | /*[clinic input] |
| 6800 | os.waitpid |
| 6801 | pid: pid_t |
| 6802 | options: int |
| 6803 | / |
| 6804 | |
| 6805 | Wait for completion of a given child process. |
| 6806 | |
| 6807 | Returns a tuple of information regarding the child process: |
| 6808 | (pid, status) |
| 6809 | |
| 6810 | The options argument is ignored on Windows. |
| 6811 | [clinic start generated code]*/ |
| 6812 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6813 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6814 | os_waitpid_impl(PyObject *module, pid_t pid, int options) |
| 6815 | /*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6816 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6817 | pid_t res; |
| 6818 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6819 | WAIT_TYPE status; |
| 6820 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6821 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6822 | do { |
| 6823 | Py_BEGIN_ALLOW_THREADS |
| 6824 | res = waitpid(pid, &status, options); |
| 6825 | Py_END_ALLOW_THREADS |
| 6826 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6827 | if (res < 0) |
| 6828 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6829 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6830 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6831 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6832 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6833 | /* 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] | 6834 | /*[clinic input] |
| 6835 | os.waitpid |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 6836 | pid: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6837 | options: int |
| 6838 | / |
| 6839 | |
| 6840 | Wait for completion of a given process. |
| 6841 | |
| 6842 | Returns a tuple of information regarding the process: |
| 6843 | (pid, status << 8) |
| 6844 | |
| 6845 | The options argument is ignored on Windows. |
| 6846 | [clinic start generated code]*/ |
| 6847 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6848 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 6849 | os_waitpid_impl(PyObject *module, intptr_t pid, int options) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 6850 | /*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6851 | { |
| 6852 | int status; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 6853 | intptr_t res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6854 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6855 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6856 | do { |
| 6857 | Py_BEGIN_ALLOW_THREADS |
| 6858 | res = _cwait(&status, pid, options); |
| 6859 | Py_END_ALLOW_THREADS |
| 6860 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 6861 | if (res < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6862 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6863 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6864 | /* 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] | 6865 | return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6866 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6867 | #endif |
| 6868 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6869 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6870 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6871 | /*[clinic input] |
| 6872 | os.wait |
| 6873 | |
| 6874 | Wait for completion of a child process. |
| 6875 | |
| 6876 | Returns a tuple of information about the child process: |
| 6877 | (pid, status) |
| 6878 | [clinic start generated code]*/ |
| 6879 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6880 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6881 | os_wait_impl(PyObject *module) |
| 6882 | /*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6883 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6884 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6885 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6886 | WAIT_TYPE status; |
| 6887 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6888 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6889 | do { |
| 6890 | Py_BEGIN_ALLOW_THREADS |
| 6891 | pid = wait(&status); |
| 6892 | Py_END_ALLOW_THREADS |
| 6893 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6894 | if (pid < 0) |
| 6895 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6896 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6897 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6898 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6899 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6900 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6901 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6902 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
| 6903 | PyDoc_STRVAR(readlink__doc__, |
| 6904 | "readlink(path, *, dir_fd=None) -> path\n\n\ |
| 6905 | Return a string representing the path to which the symbolic link points.\n\ |
| 6906 | \n\ |
| 6907 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 6908 | and path should be relative; path will then be relative to that directory.\n\ |
| 6909 | dir_fd may not be implemented on your platform.\n\ |
| 6910 | If it is unavailable, using it will raise a NotImplementedError."); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6911 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6912 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6913 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6914 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6915 | /* AC 3.5: merge win32 and not together */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6916 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6917 | posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6918 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6919 | path_t path; |
| 6920 | int dir_fd = DEFAULT_DIR_FD; |
| 6921 | char buffer[MAXPATHLEN]; |
| 6922 | ssize_t length; |
| 6923 | PyObject *return_value = NULL; |
| 6924 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6925 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6926 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 6927 | path.function_name = "readlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6928 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords, |
| 6929 | path_converter, &path, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6930 | READLINKAT_DIR_FD_CONVERTER, &dir_fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6931 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6932 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6933 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6934 | #ifdef HAVE_READLINKAT |
| 6935 | if (dir_fd != DEFAULT_DIR_FD) |
| 6936 | length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer)); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 6937 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6938 | #endif |
| 6939 | length = readlink(path.narrow, buffer, sizeof(buffer)); |
| 6940 | Py_END_ALLOW_THREADS |
| 6941 | |
| 6942 | if (length < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 6943 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6944 | goto exit; |
| 6945 | } |
| 6946 | |
| 6947 | if (PyUnicode_Check(path.object)) |
| 6948 | return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 6949 | else |
| 6950 | return_value = PyBytes_FromStringAndSize(buffer, length); |
| 6951 | exit: |
| 6952 | path_cleanup(&path); |
| 6953 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6954 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6955 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6956 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6957 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6958 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 6959 | |
| 6960 | static PyObject * |
| 6961 | win_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6962 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 6963 | const wchar_t *path; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6964 | DWORD n_bytes_returned; |
| 6965 | DWORD io_result; |
| 6966 | PyObject *po, *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 6967 | int dir_fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6968 | HANDLE reparse_point_handle; |
| 6969 | |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 6970 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 6971 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 6972 | const wchar_t *print_name; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6973 | |
| 6974 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 6975 | |
| 6976 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords, |
| 6977 | &po, |
| 6978 | dir_fd_unavailable, &dir_fd |
| 6979 | )) |
| 6980 | return NULL; |
| 6981 | |
| 6982 | path = PyUnicode_AsUnicode(po); |
| 6983 | if (path == NULL) |
| 6984 | return NULL; |
| 6985 | |
| 6986 | /* First get a handle to the reparse point */ |
| 6987 | Py_BEGIN_ALLOW_THREADS |
| 6988 | reparse_point_handle = CreateFileW( |
| 6989 | path, |
| 6990 | 0, |
| 6991 | 0, |
| 6992 | 0, |
| 6993 | OPEN_EXISTING, |
| 6994 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 6995 | 0); |
| 6996 | Py_END_ALLOW_THREADS |
| 6997 | |
| 6998 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
| 6999 | return win32_error_object("readlink", po); |
| 7000 | |
| 7001 | Py_BEGIN_ALLOW_THREADS |
| 7002 | /* New call DeviceIoControl to read the reparse point */ |
| 7003 | io_result = DeviceIoControl( |
| 7004 | reparse_point_handle, |
| 7005 | FSCTL_GET_REPARSE_POINT, |
| 7006 | 0, 0, /* in buffer */ |
| 7007 | target_buffer, sizeof(target_buffer), |
| 7008 | &n_bytes_returned, |
| 7009 | 0 /* we're not using OVERLAPPED_IO */ |
| 7010 | ); |
| 7011 | CloseHandle(reparse_point_handle); |
| 7012 | Py_END_ALLOW_THREADS |
| 7013 | |
| 7014 | if (io_result==0) |
| 7015 | return win32_error_object("readlink", po); |
| 7016 | |
| 7017 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 7018 | { |
| 7019 | PyErr_SetString(PyExc_ValueError, |
| 7020 | "not a symbolic link"); |
| 7021 | return NULL; |
| 7022 | } |
| 7023 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7024 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 7025 | |
| 7026 | result = PyUnicode_FromWideChar(print_name, |
| 7027 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
| 7028 | return result; |
| 7029 | } |
| 7030 | |
| 7031 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 7032 | |
| 7033 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7034 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7035 | #ifdef HAVE_SYMLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7036 | |
| 7037 | #if defined(MS_WINDOWS) |
| 7038 | |
| 7039 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 7040 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPCWSTR, LPCWSTR, DWORD) = NULL; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7041 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7042 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7043 | check_CreateSymbolicLink(void) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7044 | { |
| 7045 | HINSTANCE hKernel32; |
| 7046 | /* only recheck */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7047 | if (Py_CreateSymbolicLinkW) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7048 | return 1; |
| 7049 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 7050 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 7051 | "CreateSymbolicLinkW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7052 | return Py_CreateSymbolicLinkW != NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7053 | } |
| 7054 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7055 | /* Remove the last portion of the path */ |
| 7056 | static void |
| 7057 | _dirnameW(WCHAR *path) |
| 7058 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7059 | WCHAR *ptr; |
| 7060 | |
| 7061 | /* walk the path from the end until a backslash is encountered */ |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7062 | for(ptr = path + wcslen(path); ptr != path; ptr--) { |
Victor Stinner | 072318b | 2013-06-05 02:07:46 +0200 | [diff] [blame] | 7063 | if (*ptr == L'\\' || *ptr == L'/') |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7064 | break; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7065 | } |
| 7066 | *ptr = 0; |
| 7067 | } |
| 7068 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7069 | /* Is this path absolute? */ |
| 7070 | static int |
| 7071 | _is_absW(const WCHAR *path) |
| 7072 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7073 | return path[0] == L'\\' || path[0] == L'/' || path[1] == L':'; |
| 7074 | |
| 7075 | } |
| 7076 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7077 | /* join root and rest with a backslash */ |
| 7078 | static void |
| 7079 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 7080 | { |
Victor Stinner | e7e7eba | 2013-06-05 00:35:54 +0200 | [diff] [blame] | 7081 | size_t root_len; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7082 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7083 | if (_is_absW(rest)) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7084 | wcscpy(dest_path, rest); |
| 7085 | return; |
| 7086 | } |
| 7087 | |
| 7088 | root_len = wcslen(root); |
| 7089 | |
| 7090 | wcscpy(dest_path, root); |
| 7091 | if(root_len) { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7092 | dest_path[root_len] = L'\\'; |
| 7093 | root_len++; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7094 | } |
| 7095 | wcscpy(dest_path+root_len, rest); |
| 7096 | } |
| 7097 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7098 | /* Return True if the path at src relative to dest is a directory */ |
| 7099 | static int |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 7100 | _check_dirW(LPCWSTR src, LPCWSTR dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7101 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7102 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7103 | WCHAR dest_parent[MAX_PATH]; |
| 7104 | WCHAR src_resolved[MAX_PATH] = L""; |
| 7105 | |
| 7106 | /* dest_parent = os.path.dirname(dest) */ |
| 7107 | wcscpy(dest_parent, dest); |
| 7108 | _dirnameW(dest_parent); |
| 7109 | /* src_resolved = os.path.join(dest_parent, src) */ |
| 7110 | _joinW(src_resolved, dest_parent, src); |
| 7111 | return ( |
| 7112 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 7113 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7114 | ); |
| 7115 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7116 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7117 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7118 | |
| 7119 | /*[clinic input] |
| 7120 | os.symlink |
| 7121 | src: path_t |
| 7122 | dst: path_t |
| 7123 | target_is_directory: bool = False |
| 7124 | * |
| 7125 | dir_fd: dir_fd(requires='symlinkat')=None |
| 7126 | |
| 7127 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 7128 | |
| 7129 | Create a symbolic link pointing to src named dst. |
| 7130 | |
| 7131 | target_is_directory is required on Windows if the target is to be |
| 7132 | interpreted as a directory. (On Windows, symlink requires |
| 7133 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 7134 | target_is_directory is ignored on non-Windows platforms. |
| 7135 | |
| 7136 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7137 | and path should be relative; path will then be relative to that directory. |
| 7138 | dir_fd may not be implemented on your platform. |
| 7139 | If it is unavailable, using it will raise a NotImplementedError. |
| 7140 | |
| 7141 | [clinic start generated code]*/ |
| 7142 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7143 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7144 | os_symlink_impl(PyObject *module, path_t *src, path_t *dst, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7145 | int target_is_directory, int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7146 | /*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7147 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7148 | #ifdef MS_WINDOWS |
| 7149 | DWORD result; |
| 7150 | #else |
| 7151 | int result; |
| 7152 | #endif |
| 7153 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7154 | #ifdef MS_WINDOWS |
| 7155 | if (!check_CreateSymbolicLink()) { |
| 7156 | PyErr_SetString(PyExc_NotImplementedError, |
| 7157 | "CreateSymbolicLink functions not found"); |
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 | if (!win32_can_symlink) { |
| 7161 | PyErr_SetString(PyExc_OSError, "symbolic link privilege not held"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7162 | return NULL; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7163 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7164 | #endif |
| 7165 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7166 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7167 | PyErr_SetString(PyExc_ValueError, |
| 7168 | "symlink: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7169 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7170 | } |
| 7171 | |
| 7172 | #ifdef MS_WINDOWS |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7173 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7174 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7175 | /* if src is a directory, ensure target_is_directory==1 */ |
| 7176 | target_is_directory |= _check_dirW(src->wide, dst->wide); |
| 7177 | result = Py_CreateSymbolicLinkW(dst->wide, src->wide, |
| 7178 | target_is_directory); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7179 | Py_END_ALLOW_THREADS |
| 7180 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7181 | if (!result) |
| 7182 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7183 | |
| 7184 | #else |
| 7185 | |
| 7186 | Py_BEGIN_ALLOW_THREADS |
| 7187 | #if HAVE_SYMLINKAT |
| 7188 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7189 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7190 | else |
| 7191 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7192 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7193 | Py_END_ALLOW_THREADS |
| 7194 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7195 | if (result) |
| 7196 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7197 | #endif |
| 7198 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7199 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7200 | } |
| 7201 | #endif /* HAVE_SYMLINK */ |
| 7202 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7203 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7204 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7205 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7206 | static PyStructSequence_Field times_result_fields[] = { |
| 7207 | {"user", "user time"}, |
| 7208 | {"system", "system time"}, |
| 7209 | {"children_user", "user time of children"}, |
| 7210 | {"children_system", "system time of children"}, |
| 7211 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 7212 | {NULL} |
| 7213 | }; |
| 7214 | |
| 7215 | PyDoc_STRVAR(times_result__doc__, |
| 7216 | "times_result: Result from os.times().\n\n\ |
| 7217 | This object may be accessed either as a tuple of\n\ |
| 7218 | (user, system, children_user, children_system, elapsed),\n\ |
| 7219 | or via the attributes user, system, children_user, children_system,\n\ |
| 7220 | and elapsed.\n\ |
| 7221 | \n\ |
| 7222 | See os.times for more information."); |
| 7223 | |
| 7224 | static PyStructSequence_Desc times_result_desc = { |
| 7225 | "times_result", /* name */ |
| 7226 | times_result__doc__, /* doc */ |
| 7227 | times_result_fields, |
| 7228 | 5 |
| 7229 | }; |
| 7230 | |
| 7231 | static PyTypeObject TimesResultType; |
| 7232 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7233 | #ifdef MS_WINDOWS |
| 7234 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 7235 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7236 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7237 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7238 | |
| 7239 | static PyObject * |
| 7240 | build_times_result(double user, double system, |
| 7241 | double children_user, double children_system, |
| 7242 | double elapsed) |
| 7243 | { |
| 7244 | PyObject *value = PyStructSequence_New(&TimesResultType); |
| 7245 | if (value == NULL) |
| 7246 | return NULL; |
| 7247 | |
| 7248 | #define SET(i, field) \ |
| 7249 | { \ |
| 7250 | PyObject *o = PyFloat_FromDouble(field); \ |
| 7251 | if (!o) { \ |
| 7252 | Py_DECREF(value); \ |
| 7253 | return NULL; \ |
| 7254 | } \ |
| 7255 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 7256 | } \ |
| 7257 | |
| 7258 | SET(0, user); |
| 7259 | SET(1, system); |
| 7260 | SET(2, children_user); |
| 7261 | SET(3, children_system); |
| 7262 | SET(4, elapsed); |
| 7263 | |
| 7264 | #undef SET |
| 7265 | |
| 7266 | return value; |
| 7267 | } |
| 7268 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7269 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7270 | #ifndef MS_WINDOWS |
| 7271 | #define NEED_TICKS_PER_SECOND |
| 7272 | static long ticks_per_second = -1; |
| 7273 | #endif /* MS_WINDOWS */ |
| 7274 | |
| 7275 | /*[clinic input] |
| 7276 | os.times |
| 7277 | |
| 7278 | Return a collection containing process timing information. |
| 7279 | |
| 7280 | The object returned behaves like a named tuple with these fields: |
| 7281 | (utime, stime, cutime, cstime, elapsed_time) |
| 7282 | All fields are floating point numbers. |
| 7283 | [clinic start generated code]*/ |
| 7284 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7285 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7286 | os_times_impl(PyObject *module) |
| 7287 | /*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7288 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7289 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7290 | FILETIME create, exit, kernel, user; |
| 7291 | HANDLE hProc; |
| 7292 | hProc = GetCurrentProcess(); |
| 7293 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 7294 | /* The fields of a FILETIME structure are the hi and lo part |
| 7295 | of a 64-bit value expressed in 100 nanosecond units. |
| 7296 | 1e7 is one second in such units; 1e-7 the inverse. |
| 7297 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 7298 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7299 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7300 | (double)(user.dwHighDateTime*429.4967296 + |
| 7301 | user.dwLowDateTime*1e-7), |
| 7302 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 7303 | kernel.dwLowDateTime*1e-7), |
| 7304 | (double)0, |
| 7305 | (double)0, |
| 7306 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7307 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7308 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7309 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7310 | |
| 7311 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7312 | struct tms t; |
| 7313 | clock_t c; |
| 7314 | errno = 0; |
| 7315 | c = times(&t); |
| 7316 | if (c == (clock_t) -1) |
| 7317 | return posix_error(); |
| 7318 | return build_times_result( |
| 7319 | (double)t.tms_utime / ticks_per_second, |
| 7320 | (double)t.tms_stime / ticks_per_second, |
| 7321 | (double)t.tms_cutime / ticks_per_second, |
| 7322 | (double)t.tms_cstime / ticks_per_second, |
| 7323 | (double)c / ticks_per_second); |
| 7324 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7325 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7326 | #endif /* HAVE_TIMES */ |
| 7327 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7328 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7329 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7330 | /*[clinic input] |
| 7331 | os.getsid |
| 7332 | |
| 7333 | pid: pid_t |
| 7334 | / |
| 7335 | |
| 7336 | Call the system call getsid(pid) and return the result. |
| 7337 | [clinic start generated code]*/ |
| 7338 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7339 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7340 | os_getsid_impl(PyObject *module, pid_t pid) |
| 7341 | /*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7342 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7343 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7344 | sid = getsid(pid); |
| 7345 | if (sid < 0) |
| 7346 | return posix_error(); |
| 7347 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7348 | } |
| 7349 | #endif /* HAVE_GETSID */ |
| 7350 | |
| 7351 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7352 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7353 | /*[clinic input] |
| 7354 | os.setsid |
| 7355 | |
| 7356 | Call the system call setsid(). |
| 7357 | [clinic start generated code]*/ |
| 7358 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7359 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7360 | os_setsid_impl(PyObject *module) |
| 7361 | /*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7362 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7363 | if (setsid() < 0) |
| 7364 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7365 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7366 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7367 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7368 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7369 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7370 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7371 | /*[clinic input] |
| 7372 | os.setpgid |
| 7373 | |
| 7374 | pid: pid_t |
| 7375 | pgrp: pid_t |
| 7376 | / |
| 7377 | |
| 7378 | Call the system call setpgid(pid, pgrp). |
| 7379 | [clinic start generated code]*/ |
| 7380 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7381 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7382 | os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp) |
| 7383 | /*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7384 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7385 | if (setpgid(pid, pgrp) < 0) |
| 7386 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7387 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7388 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7389 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7390 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7391 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7392 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7393 | /*[clinic input] |
| 7394 | os.tcgetpgrp |
| 7395 | |
| 7396 | fd: int |
| 7397 | / |
| 7398 | |
| 7399 | Return the process group associated with the terminal specified by fd. |
| 7400 | [clinic start generated code]*/ |
| 7401 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7402 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7403 | os_tcgetpgrp_impl(PyObject *module, int fd) |
| 7404 | /*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7405 | { |
| 7406 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7407 | if (pgid < 0) |
| 7408 | return posix_error(); |
| 7409 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7410 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7411 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7412 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7413 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7414 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7415 | /*[clinic input] |
| 7416 | os.tcsetpgrp |
| 7417 | |
| 7418 | fd: int |
| 7419 | pgid: pid_t |
| 7420 | / |
| 7421 | |
| 7422 | Set the process group associated with the terminal specified by fd. |
| 7423 | [clinic start generated code]*/ |
| 7424 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7425 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7426 | os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid) |
| 7427 | /*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7428 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7429 | if (tcsetpgrp(fd, pgid) < 0) |
| 7430 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7431 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7432 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7433 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7434 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7435 | /* Functions acting on file descriptors */ |
| 7436 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7437 | #ifdef O_CLOEXEC |
| 7438 | extern int _Py_open_cloexec_works; |
| 7439 | #endif |
| 7440 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7441 | |
| 7442 | /*[clinic input] |
| 7443 | os.open -> int |
| 7444 | path: path_t |
| 7445 | flags: int |
| 7446 | mode: int = 0o777 |
| 7447 | * |
| 7448 | dir_fd: dir_fd(requires='openat') = None |
| 7449 | |
| 7450 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 7451 | |
| 7452 | Open a file for low level IO. Returns a file descriptor (integer). |
| 7453 | |
| 7454 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7455 | and path should be relative; path will then be relative to that directory. |
| 7456 | dir_fd may not be implemented on your platform. |
| 7457 | If it is unavailable, using it will raise a NotImplementedError. |
| 7458 | [clinic start generated code]*/ |
| 7459 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7460 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7461 | os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd) |
| 7462 | /*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7463 | { |
| 7464 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7465 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7466 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7467 | #ifdef O_CLOEXEC |
| 7468 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 7469 | #elif !defined(MS_WINDOWS) |
| 7470 | int *atomic_flag_works = NULL; |
| 7471 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7472 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7473 | #ifdef MS_WINDOWS |
| 7474 | flags |= O_NOINHERIT; |
| 7475 | #elif defined(O_CLOEXEC) |
| 7476 | flags |= O_CLOEXEC; |
| 7477 | #endif |
| 7478 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7479 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7480 | do { |
| 7481 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7482 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 7483 | fd = _wopen(path->wide, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 7484 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7485 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7486 | if (dir_fd != DEFAULT_DIR_FD) |
| 7487 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 7488 | else |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 7489 | #endif /* HAVE_OPENAT */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7490 | fd = open(path->narrow, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 7491 | #endif /* !MS_WINDOWS */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7492 | Py_END_ALLOW_THREADS |
| 7493 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7494 | _Py_END_SUPPRESS_IPH |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7495 | |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 7496 | if (fd < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7497 | if (!async_err) |
| 7498 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7499 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7500 | } |
| 7501 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7502 | #ifndef MS_WINDOWS |
| 7503 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 7504 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7505 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7506 | } |
| 7507 | #endif |
| 7508 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7509 | return fd; |
| 7510 | } |
| 7511 | |
| 7512 | |
| 7513 | /*[clinic input] |
| 7514 | os.close |
| 7515 | |
| 7516 | fd: int |
| 7517 | |
| 7518 | Close a file descriptor. |
| 7519 | [clinic start generated code]*/ |
| 7520 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7521 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7522 | os_close_impl(PyObject *module, int fd) |
| 7523 | /*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7524 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7525 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7526 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 7527 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 7528 | * for more details. |
| 7529 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7530 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7531 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7532 | res = close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7533 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7534 | Py_END_ALLOW_THREADS |
| 7535 | if (res < 0) |
| 7536 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7537 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7538 | } |
| 7539 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7540 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7541 | /*[clinic input] |
| 7542 | os.closerange |
| 7543 | |
| 7544 | fd_low: int |
| 7545 | fd_high: int |
| 7546 | / |
| 7547 | |
| 7548 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 7549 | [clinic start generated code]*/ |
| 7550 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7551 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7552 | os_closerange_impl(PyObject *module, int fd_low, int fd_high) |
| 7553 | /*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7554 | { |
| 7555 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7556 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7557 | _Py_BEGIN_SUPPRESS_IPH |
Benjamin Peterson | 207116b | 2016-09-08 11:28:06 -0700 | [diff] [blame] | 7558 | for (i = Py_MAX(fd_low, 0); i < fd_high; i++) |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 7559 | close(i); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7560 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7561 | Py_END_ALLOW_THREADS |
| 7562 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7563 | } |
| 7564 | |
| 7565 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7566 | /*[clinic input] |
| 7567 | os.dup -> int |
| 7568 | |
| 7569 | fd: int |
| 7570 | / |
| 7571 | |
| 7572 | Return a duplicate of a file descriptor. |
| 7573 | [clinic start generated code]*/ |
| 7574 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7575 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7576 | os_dup_impl(PyObject *module, int fd) |
| 7577 | /*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7578 | { |
| 7579 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7580 | } |
| 7581 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7582 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7583 | /*[clinic input] |
| 7584 | os.dup2 |
| 7585 | fd: int |
| 7586 | fd2: int |
| 7587 | inheritable: bool=True |
| 7588 | |
| 7589 | Duplicate file descriptor. |
| 7590 | [clinic start generated code]*/ |
| 7591 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7592 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7593 | os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) |
| 7594 | /*[clinic end generated code: output=db832a2d872ccc5f input=76e96f511be0352f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7595 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7596 | int res; |
| 7597 | #if defined(HAVE_DUP3) && \ |
| 7598 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 7599 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
| 7600 | int dup3_works = -1; |
| 7601 | #endif |
| 7602 | |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 7603 | if (fd < 0 || fd2 < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7604 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7605 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7606 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 7607 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 7608 | * upon close(), and therefore below. |
| 7609 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7610 | #ifdef MS_WINDOWS |
| 7611 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7612 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7613 | res = dup2(fd, fd2); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7614 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7615 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7616 | if (res < 0) |
| 7617 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7618 | |
| 7619 | /* Character files like console cannot be make non-inheritable */ |
| 7620 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7621 | close(fd2); |
| 7622 | return NULL; |
| 7623 | } |
| 7624 | |
| 7625 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 7626 | Py_BEGIN_ALLOW_THREADS |
| 7627 | if (!inheritable) |
| 7628 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 7629 | else |
| 7630 | res = dup2(fd, fd2); |
| 7631 | Py_END_ALLOW_THREADS |
| 7632 | if (res < 0) |
| 7633 | return posix_error(); |
| 7634 | |
| 7635 | #else |
| 7636 | |
| 7637 | #ifdef HAVE_DUP3 |
| 7638 | if (!inheritable && dup3_works != 0) { |
| 7639 | Py_BEGIN_ALLOW_THREADS |
| 7640 | res = dup3(fd, fd2, O_CLOEXEC); |
| 7641 | Py_END_ALLOW_THREADS |
| 7642 | if (res < 0) { |
| 7643 | if (dup3_works == -1) |
| 7644 | dup3_works = (errno != ENOSYS); |
| 7645 | if (dup3_works) |
| 7646 | return posix_error(); |
| 7647 | } |
| 7648 | } |
| 7649 | |
| 7650 | if (inheritable || dup3_works == 0) |
| 7651 | { |
| 7652 | #endif |
| 7653 | Py_BEGIN_ALLOW_THREADS |
| 7654 | res = dup2(fd, fd2); |
| 7655 | Py_END_ALLOW_THREADS |
| 7656 | if (res < 0) |
| 7657 | return posix_error(); |
| 7658 | |
| 7659 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7660 | close(fd2); |
| 7661 | return NULL; |
| 7662 | } |
| 7663 | #ifdef HAVE_DUP3 |
| 7664 | } |
| 7665 | #endif |
| 7666 | |
| 7667 | #endif |
| 7668 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7669 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7670 | } |
| 7671 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7672 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7673 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7674 | /*[clinic input] |
| 7675 | os.lockf |
| 7676 | |
| 7677 | fd: int |
| 7678 | An open file descriptor. |
| 7679 | command: int |
| 7680 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 7681 | length: Py_off_t |
| 7682 | The number of bytes to lock, starting at the current position. |
| 7683 | / |
| 7684 | |
| 7685 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 7686 | |
| 7687 | [clinic start generated code]*/ |
| 7688 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7689 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7690 | os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length) |
| 7691 | /*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7692 | { |
| 7693 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7694 | |
| 7695 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7696 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7697 | Py_END_ALLOW_THREADS |
| 7698 | |
| 7699 | if (res < 0) |
| 7700 | return posix_error(); |
| 7701 | |
| 7702 | Py_RETURN_NONE; |
| 7703 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7704 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7705 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7706 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7707 | /*[clinic input] |
| 7708 | os.lseek -> Py_off_t |
| 7709 | |
| 7710 | fd: int |
| 7711 | position: Py_off_t |
| 7712 | how: int |
| 7713 | / |
| 7714 | |
| 7715 | Set the position of a file descriptor. Return the new position. |
| 7716 | |
| 7717 | Return the new cursor position in number of bytes |
| 7718 | relative to the beginning of the file. |
| 7719 | [clinic start generated code]*/ |
| 7720 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7721 | static Py_off_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7722 | os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how) |
| 7723 | /*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7724 | { |
| 7725 | Py_off_t result; |
| 7726 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7727 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7728 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 7729 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7730 | case 0: how = SEEK_SET; break; |
| 7731 | case 1: how = SEEK_CUR; break; |
| 7732 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7733 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7734 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7735 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7736 | if (PyErr_Occurred()) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7737 | return -1; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7738 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7739 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7740 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 7741 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7742 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7743 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7744 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7745 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7746 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7747 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7748 | if (result < 0) |
| 7749 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7750 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7751 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7752 | } |
| 7753 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7754 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7755 | /*[clinic input] |
| 7756 | os.read |
| 7757 | fd: int |
| 7758 | length: Py_ssize_t |
| 7759 | / |
| 7760 | |
| 7761 | Read from a file descriptor. Returns a bytes object. |
| 7762 | [clinic start generated code]*/ |
| 7763 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7764 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7765 | os_read_impl(PyObject *module, int fd, Py_ssize_t length) |
| 7766 | /*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7767 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7768 | Py_ssize_t n; |
| 7769 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7770 | |
| 7771 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7772 | errno = EINVAL; |
| 7773 | return posix_error(); |
| 7774 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7775 | |
| 7776 | #ifdef MS_WINDOWS |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 7777 | /* On Windows, the count parameter of read() is an int */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7778 | if (length > INT_MAX) |
| 7779 | length = INT_MAX; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7780 | #endif |
| 7781 | |
| 7782 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7783 | if (buffer == NULL) |
| 7784 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7785 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 7786 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 7787 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7788 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 7789 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7790 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7791 | |
| 7792 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7793 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7794 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7795 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7796 | } |
| 7797 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7798 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 7799 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7800 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7801 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 7802 | { |
| 7803 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7804 | Py_ssize_t blen, total = 0; |
| 7805 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7806 | *iov = PyMem_New(struct iovec, cnt); |
| 7807 | if (*iov == NULL) { |
| 7808 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 7809 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7810 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7811 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7812 | *buf = PyMem_New(Py_buffer, cnt); |
| 7813 | if (*buf == NULL) { |
| 7814 | PyMem_Del(*iov); |
| 7815 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 7816 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7817 | } |
| 7818 | |
| 7819 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7820 | PyObject *item = PySequence_GetItem(seq, i); |
| 7821 | if (item == NULL) |
| 7822 | goto fail; |
| 7823 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 7824 | Py_DECREF(item); |
| 7825 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7826 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7827 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7828 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7829 | blen = (*buf)[i].len; |
| 7830 | (*iov)[i].iov_len = blen; |
| 7831 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7832 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7833 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7834 | |
| 7835 | fail: |
| 7836 | PyMem_Del(*iov); |
| 7837 | for (j = 0; j < i; j++) { |
| 7838 | PyBuffer_Release(&(*buf)[j]); |
| 7839 | } |
| 7840 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 7841 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7842 | } |
| 7843 | |
| 7844 | static void |
| 7845 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 7846 | { |
| 7847 | int i; |
| 7848 | PyMem_Del(iov); |
| 7849 | for (i = 0; i < cnt; i++) { |
| 7850 | PyBuffer_Release(&buf[i]); |
| 7851 | } |
| 7852 | PyMem_Del(buf); |
| 7853 | } |
| 7854 | #endif |
| 7855 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7856 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7857 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7858 | /*[clinic input] |
| 7859 | os.readv -> Py_ssize_t |
| 7860 | |
| 7861 | fd: int |
| 7862 | buffers: object |
| 7863 | / |
| 7864 | |
| 7865 | Read from a file descriptor fd into an iterable of buffers. |
| 7866 | |
| 7867 | The buffers should be mutable buffers accepting bytes. |
| 7868 | readv will transfer data into each buffer until it is full |
| 7869 | and then move on to the next buffer in the sequence to hold |
| 7870 | the rest of the data. |
| 7871 | |
| 7872 | readv returns the total number of bytes read, |
| 7873 | which may be less than the total capacity of all the buffers. |
| 7874 | [clinic start generated code]*/ |
| 7875 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7876 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7877 | os_readv_impl(PyObject *module, int fd, PyObject *buffers) |
| 7878 | /*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7879 | { |
| 7880 | int cnt; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7881 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7882 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7883 | struct iovec *iov; |
| 7884 | Py_buffer *buf; |
| 7885 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7886 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7887 | PyErr_SetString(PyExc_TypeError, |
| 7888 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7889 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7890 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7891 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7892 | cnt = PySequence_Size(buffers); |
| 7893 | |
| 7894 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 7895 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7896 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7897 | do { |
| 7898 | Py_BEGIN_ALLOW_THREADS |
| 7899 | n = readv(fd, iov, cnt); |
| 7900 | Py_END_ALLOW_THREADS |
| 7901 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7902 | |
| 7903 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7904 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7905 | if (!async_err) |
| 7906 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7907 | return -1; |
| 7908 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 7909 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7910 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7911 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7912 | #endif /* HAVE_READV */ |
| 7913 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7914 | |
| 7915 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7916 | /*[clinic input] |
| 7917 | # TODO length should be size_t! but Python doesn't support parsing size_t yet. |
| 7918 | os.pread |
| 7919 | |
| 7920 | fd: int |
| 7921 | length: int |
| 7922 | offset: Py_off_t |
| 7923 | / |
| 7924 | |
| 7925 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 7926 | |
| 7927 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 7928 | the beginning of the file. The file offset remains unchanged. |
| 7929 | [clinic start generated code]*/ |
| 7930 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7931 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7932 | os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset) |
| 7933 | /*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7934 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7935 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7936 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7937 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7938 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7939 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7940 | errno = EINVAL; |
| 7941 | return posix_error(); |
| 7942 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7943 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7944 | if (buffer == NULL) |
| 7945 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7946 | |
| 7947 | do { |
| 7948 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7949 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7950 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7951 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7952 | Py_END_ALLOW_THREADS |
| 7953 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7954 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7955 | if (n < 0) { |
| 7956 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7957 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7958 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7959 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7960 | _PyBytes_Resize(&buffer, n); |
| 7961 | return buffer; |
| 7962 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7963 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7964 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7965 | |
| 7966 | /*[clinic input] |
| 7967 | os.write -> Py_ssize_t |
| 7968 | |
| 7969 | fd: int |
| 7970 | data: Py_buffer |
| 7971 | / |
| 7972 | |
| 7973 | Write a bytes object to a file descriptor. |
| 7974 | [clinic start generated code]*/ |
| 7975 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7976 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7977 | os_write_impl(PyObject *module, int fd, Py_buffer *data) |
| 7978 | /*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7979 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 7980 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7981 | } |
| 7982 | |
| 7983 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7984 | PyDoc_STRVAR(posix_sendfile__doc__, |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 7985 | "sendfile(out, in, offset, count) -> byteswritten\n\ |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 7986 | sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7987 | -> byteswritten\n\ |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 7988 | Copy count bytes from file descriptor in to file descriptor out."); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7989 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7990 | /* AC 3.5: don't bother converting, has optional group*/ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7991 | static PyObject * |
| 7992 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 7993 | { |
| 7994 | int in, out; |
| 7995 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7996 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7997 | off_t offset; |
| 7998 | |
| 7999 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 8000 | #ifndef __APPLE__ |
| 8001 | Py_ssize_t len; |
| 8002 | #endif |
| 8003 | PyObject *headers = NULL, *trailers = NULL; |
| 8004 | Py_buffer *hbuf, *tbuf; |
| 8005 | off_t sbytes; |
| 8006 | struct sf_hdtr sf; |
| 8007 | int flags = 0; |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8008 | /* Beware that "in" clashes with Python's own "in" operator keyword */ |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 8009 | static char *keywords[] = {"out", "in", |
| 8010 | "offset", "count", |
| 8011 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8012 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 8013 | sf.headers = NULL; |
| 8014 | sf.trailers = NULL; |
| 8015 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8016 | #ifdef __APPLE__ |
| 8017 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8018 | 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] | 8019 | #else |
| 8020 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8021 | keywords, &out, &in, Py_off_t_converter, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8022 | #endif |
| 8023 | &headers, &trailers, &flags)) |
| 8024 | return NULL; |
| 8025 | if (headers != NULL) { |
| 8026 | if (!PySequence_Check(headers)) { |
| 8027 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8028 | "sendfile() headers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8029 | return NULL; |
| 8030 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8031 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8032 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8033 | if (sf.hdr_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8034 | (i = iov_setup(&(sf.headers), &hbuf, |
| 8035 | headers, sf.hdr_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8036 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8037 | #ifdef __APPLE__ |
| 8038 | sbytes += i; |
| 8039 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8040 | } |
| 8041 | } |
| 8042 | if (trailers != NULL) { |
| 8043 | if (!PySequence_Check(trailers)) { |
| 8044 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8045 | "sendfile() trailers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8046 | return NULL; |
| 8047 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8048 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8049 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8050 | if (sf.trl_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8051 | (i = iov_setup(&(sf.trailers), &tbuf, |
| 8052 | trailers, sf.trl_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8053 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8054 | #ifdef __APPLE__ |
| 8055 | sbytes += i; |
| 8056 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8057 | } |
| 8058 | } |
| 8059 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8060 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8061 | do { |
| 8062 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8063 | #ifdef __APPLE__ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8064 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8065 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8066 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8067 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8068 | Py_END_ALLOW_THREADS |
| 8069 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8070 | _Py_END_SUPPRESS_IPH |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8071 | |
| 8072 | if (sf.headers != NULL) |
| 8073 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 8074 | if (sf.trailers != NULL) |
| 8075 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 8076 | |
| 8077 | if (ret < 0) { |
| 8078 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 8079 | if (sbytes != 0) { |
| 8080 | // some data has been sent |
| 8081 | goto done; |
| 8082 | } |
| 8083 | else { |
| 8084 | // no data has been sent; upper application is supposed |
| 8085 | // to retry on EAGAIN or EBUSY |
| 8086 | return posix_error(); |
| 8087 | } |
| 8088 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8089 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8090 | } |
| 8091 | goto done; |
| 8092 | |
| 8093 | done: |
| 8094 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 8095 | return Py_BuildValue("l", sbytes); |
| 8096 | #else |
| 8097 | return Py_BuildValue("L", sbytes); |
| 8098 | #endif |
| 8099 | |
| 8100 | #else |
| 8101 | Py_ssize_t count; |
| 8102 | PyObject *offobj; |
| 8103 | static char *keywords[] = {"out", "in", |
| 8104 | "offset", "count", NULL}; |
| 8105 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 8106 | keywords, &out, &in, &offobj, &count)) |
| 8107 | return NULL; |
Benjamin Peterson | 840ef8f | 2016-09-07 14:45:10 -0700 | [diff] [blame] | 8108 | #ifdef __linux__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8109 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8110 | do { |
| 8111 | Py_BEGIN_ALLOW_THREADS |
| 8112 | ret = sendfile(out, in, NULL, count); |
| 8113 | Py_END_ALLOW_THREADS |
| 8114 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8115 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8116 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 8117 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8118 | } |
| 8119 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8120 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 8121 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8122 | |
| 8123 | do { |
| 8124 | Py_BEGIN_ALLOW_THREADS |
| 8125 | ret = sendfile(out, in, &offset, count); |
| 8126 | Py_END_ALLOW_THREADS |
| 8127 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8128 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8129 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8130 | return Py_BuildValue("n", ret); |
| 8131 | #endif |
| 8132 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8133 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8134 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8135 | |
| 8136 | /*[clinic input] |
| 8137 | os.fstat |
| 8138 | |
| 8139 | fd : int |
| 8140 | |
| 8141 | Perform a stat system call on the given file descriptor. |
| 8142 | |
| 8143 | Like stat(), but for an open file descriptor. |
| 8144 | Equivalent to os.stat(fd). |
| 8145 | [clinic start generated code]*/ |
| 8146 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8147 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8148 | os_fstat_impl(PyObject *module, int fd) |
| 8149 | /*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8150 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8151 | STRUCT_STAT st; |
| 8152 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8153 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8154 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8155 | do { |
| 8156 | Py_BEGIN_ALLOW_THREADS |
| 8157 | res = FSTAT(fd, &st); |
| 8158 | Py_END_ALLOW_THREADS |
| 8159 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8160 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8161 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8162 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8163 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8164 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8165 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8166 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8167 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8168 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8169 | } |
| 8170 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8171 | |
| 8172 | /*[clinic input] |
| 8173 | os.isatty -> bool |
| 8174 | fd: int |
| 8175 | / |
| 8176 | |
| 8177 | Return True if the fd is connected to a terminal. |
| 8178 | |
| 8179 | Return True if the file descriptor is an open file descriptor |
| 8180 | connected to the slave end of a terminal. |
| 8181 | [clinic start generated code]*/ |
| 8182 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8183 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8184 | os_isatty_impl(PyObject *module, int fd) |
| 8185 | /*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8186 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8187 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8188 | _Py_BEGIN_SUPPRESS_IPH |
| 8189 | return_value = isatty(fd); |
| 8190 | _Py_END_SUPPRESS_IPH |
| 8191 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8192 | } |
| 8193 | |
| 8194 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8195 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8196 | /*[clinic input] |
| 8197 | os.pipe |
| 8198 | |
| 8199 | Create a pipe. |
| 8200 | |
| 8201 | Returns a tuple of two file descriptors: |
| 8202 | (read_fd, write_fd) |
| 8203 | [clinic start generated code]*/ |
| 8204 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8205 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8206 | os_pipe_impl(PyObject *module) |
| 8207 | /*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8208 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8209 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8210 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8211 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8212 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8213 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8214 | #else |
| 8215 | int res; |
| 8216 | #endif |
| 8217 | |
| 8218 | #ifdef MS_WINDOWS |
| 8219 | attr.nLength = sizeof(attr); |
| 8220 | attr.lpSecurityDescriptor = NULL; |
| 8221 | attr.bInheritHandle = FALSE; |
| 8222 | |
| 8223 | Py_BEGIN_ALLOW_THREADS |
| 8224 | ok = CreatePipe(&read, &write, &attr, 0); |
| 8225 | if (ok) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 8226 | fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY); |
| 8227 | fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8228 | if (fds[0] == -1 || fds[1] == -1) { |
| 8229 | CloseHandle(read); |
| 8230 | CloseHandle(write); |
| 8231 | ok = 0; |
| 8232 | } |
| 8233 | } |
| 8234 | Py_END_ALLOW_THREADS |
| 8235 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8236 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8237 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8238 | #else |
| 8239 | |
| 8240 | #ifdef HAVE_PIPE2 |
| 8241 | Py_BEGIN_ALLOW_THREADS |
| 8242 | res = pipe2(fds, O_CLOEXEC); |
| 8243 | Py_END_ALLOW_THREADS |
| 8244 | |
| 8245 | if (res != 0 && errno == ENOSYS) |
| 8246 | { |
| 8247 | #endif |
| 8248 | Py_BEGIN_ALLOW_THREADS |
| 8249 | res = pipe(fds); |
| 8250 | Py_END_ALLOW_THREADS |
| 8251 | |
| 8252 | if (res == 0) { |
| 8253 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 8254 | close(fds[0]); |
| 8255 | close(fds[1]); |
| 8256 | return NULL; |
| 8257 | } |
| 8258 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 8259 | close(fds[0]); |
| 8260 | close(fds[1]); |
| 8261 | return NULL; |
| 8262 | } |
| 8263 | } |
| 8264 | #ifdef HAVE_PIPE2 |
| 8265 | } |
| 8266 | #endif |
| 8267 | |
| 8268 | if (res != 0) |
| 8269 | return PyErr_SetFromErrno(PyExc_OSError); |
| 8270 | #endif /* !MS_WINDOWS */ |
| 8271 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8272 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8273 | #endif /* HAVE_PIPE */ |
| 8274 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8275 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8276 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8277 | /*[clinic input] |
| 8278 | os.pipe2 |
| 8279 | |
| 8280 | flags: int |
| 8281 | / |
| 8282 | |
| 8283 | Create a pipe with flags set atomically. |
| 8284 | |
| 8285 | Returns a tuple of two file descriptors: |
| 8286 | (read_fd, write_fd) |
| 8287 | |
| 8288 | flags can be constructed by ORing together one or more of these values: |
| 8289 | O_NONBLOCK, O_CLOEXEC. |
| 8290 | [clinic start generated code]*/ |
| 8291 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8292 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8293 | os_pipe2_impl(PyObject *module, int flags) |
| 8294 | /*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8295 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8296 | int fds[2]; |
| 8297 | int res; |
| 8298 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8299 | res = pipe2(fds, flags); |
| 8300 | if (res != 0) |
| 8301 | return posix_error(); |
| 8302 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 8303 | } |
| 8304 | #endif /* HAVE_PIPE2 */ |
| 8305 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8306 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8307 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8308 | /*[clinic input] |
| 8309 | os.writev -> Py_ssize_t |
| 8310 | fd: int |
| 8311 | buffers: object |
| 8312 | / |
| 8313 | |
| 8314 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 8315 | |
| 8316 | Returns the total number of bytes written. |
| 8317 | buffers must be a sequence of bytes-like objects. |
| 8318 | [clinic start generated code]*/ |
| 8319 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8320 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8321 | os_writev_impl(PyObject *module, int fd, PyObject *buffers) |
| 8322 | /*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8323 | { |
| 8324 | int cnt; |
| 8325 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8326 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8327 | struct iovec *iov; |
| 8328 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8329 | |
| 8330 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8331 | PyErr_SetString(PyExc_TypeError, |
| 8332 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8333 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8334 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8335 | cnt = PySequence_Size(buffers); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8336 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8337 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 8338 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8339 | } |
| 8340 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8341 | do { |
| 8342 | Py_BEGIN_ALLOW_THREADS |
| 8343 | result = writev(fd, iov, cnt); |
| 8344 | Py_END_ALLOW_THREADS |
| 8345 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8346 | |
| 8347 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8348 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8349 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8350 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8351 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8352 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8353 | #endif /* HAVE_WRITEV */ |
| 8354 | |
| 8355 | |
| 8356 | #ifdef HAVE_PWRITE |
| 8357 | /*[clinic input] |
| 8358 | os.pwrite -> Py_ssize_t |
| 8359 | |
| 8360 | fd: int |
| 8361 | buffer: Py_buffer |
| 8362 | offset: Py_off_t |
| 8363 | / |
| 8364 | |
| 8365 | Write bytes to a file descriptor starting at a particular offset. |
| 8366 | |
| 8367 | Write buffer to fd, starting at offset bytes from the beginning of |
| 8368 | the file. Returns the number of bytes writte. Does not change the |
| 8369 | current file offset. |
| 8370 | [clinic start generated code]*/ |
| 8371 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8372 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8373 | os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset) |
| 8374 | /*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8375 | { |
| 8376 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8377 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8378 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8379 | do { |
| 8380 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8381 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8382 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8383 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8384 | Py_END_ALLOW_THREADS |
| 8385 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8386 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8387 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8388 | posix_error(); |
| 8389 | return size; |
| 8390 | } |
| 8391 | #endif /* HAVE_PWRITE */ |
| 8392 | |
| 8393 | |
| 8394 | #ifdef HAVE_MKFIFO |
| 8395 | /*[clinic input] |
| 8396 | os.mkfifo |
| 8397 | |
| 8398 | path: path_t |
| 8399 | mode: int=0o666 |
| 8400 | * |
| 8401 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 8402 | |
| 8403 | Create a "fifo" (a POSIX named pipe). |
| 8404 | |
| 8405 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8406 | and path should be relative; path will then be relative to that directory. |
| 8407 | dir_fd may not be implemented on your platform. |
| 8408 | If it is unavailable, using it will raise a NotImplementedError. |
| 8409 | [clinic start generated code]*/ |
| 8410 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8411 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8412 | os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 8413 | /*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8414 | { |
| 8415 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8416 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8417 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8418 | do { |
| 8419 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8420 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8421 | if (dir_fd != DEFAULT_DIR_FD) |
| 8422 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 8423 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8424 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8425 | result = mkfifo(path->narrow, mode); |
| 8426 | Py_END_ALLOW_THREADS |
| 8427 | } while (result != 0 && errno == EINTR && |
| 8428 | !(async_err = PyErr_CheckSignals())); |
| 8429 | if (result != 0) |
| 8430 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8431 | |
| 8432 | Py_RETURN_NONE; |
| 8433 | } |
| 8434 | #endif /* HAVE_MKFIFO */ |
| 8435 | |
| 8436 | |
| 8437 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 8438 | /*[clinic input] |
| 8439 | os.mknod |
| 8440 | |
| 8441 | path: path_t |
| 8442 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8443 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8444 | * |
| 8445 | dir_fd: dir_fd(requires='mknodat')=None |
| 8446 | |
| 8447 | Create a node in the file system. |
| 8448 | |
| 8449 | Create a node in the file system (file, device special file or named pipe) |
| 8450 | at path. mode specifies both the permissions to use and the |
| 8451 | type of node to be created, being combined (bitwise OR) with one of |
| 8452 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 8453 | device defines the newly created device special file (probably using |
| 8454 | os.makedev()). Otherwise device is ignored. |
| 8455 | |
| 8456 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8457 | and path should be relative; path will then be relative to that directory. |
| 8458 | dir_fd may not be implemented on your platform. |
| 8459 | If it is unavailable, using it will raise a NotImplementedError. |
| 8460 | [clinic start generated code]*/ |
| 8461 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8462 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8463 | 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] | 8464 | int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8465 | /*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8466 | { |
| 8467 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8468 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8469 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8470 | do { |
| 8471 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8472 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8473 | if (dir_fd != DEFAULT_DIR_FD) |
| 8474 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 8475 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8476 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8477 | result = mknod(path->narrow, mode, device); |
| 8478 | Py_END_ALLOW_THREADS |
| 8479 | } while (result != 0 && errno == EINTR && |
| 8480 | !(async_err = PyErr_CheckSignals())); |
| 8481 | if (result != 0) |
| 8482 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8483 | |
| 8484 | Py_RETURN_NONE; |
| 8485 | } |
| 8486 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 8487 | |
| 8488 | |
| 8489 | #ifdef HAVE_DEVICE_MACROS |
| 8490 | /*[clinic input] |
| 8491 | os.major -> unsigned_int |
| 8492 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8493 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8494 | / |
| 8495 | |
| 8496 | Extracts a device major number from a raw device number. |
| 8497 | [clinic start generated code]*/ |
| 8498 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8499 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8500 | os_major_impl(PyObject *module, dev_t device) |
| 8501 | /*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8502 | { |
| 8503 | return major(device); |
| 8504 | } |
| 8505 | |
| 8506 | |
| 8507 | /*[clinic input] |
| 8508 | os.minor -> unsigned_int |
| 8509 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8510 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8511 | / |
| 8512 | |
| 8513 | Extracts a device minor number from a raw device number. |
| 8514 | [clinic start generated code]*/ |
| 8515 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8516 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8517 | os_minor_impl(PyObject *module, dev_t device) |
| 8518 | /*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8519 | { |
| 8520 | return minor(device); |
| 8521 | } |
| 8522 | |
| 8523 | |
| 8524 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8525 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8526 | |
| 8527 | major: int |
| 8528 | minor: int |
| 8529 | / |
| 8530 | |
| 8531 | Composes a raw device number from the major and minor device numbers. |
| 8532 | [clinic start generated code]*/ |
| 8533 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8534 | static dev_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8535 | os_makedev_impl(PyObject *module, int major, int minor) |
| 8536 | /*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8537 | { |
| 8538 | return makedev(major, minor); |
| 8539 | } |
| 8540 | #endif /* HAVE_DEVICE_MACROS */ |
| 8541 | |
| 8542 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8543 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8544 | /*[clinic input] |
| 8545 | os.ftruncate |
| 8546 | |
| 8547 | fd: int |
| 8548 | length: Py_off_t |
| 8549 | / |
| 8550 | |
| 8551 | Truncate a file, specified by file descriptor, to a specific length. |
| 8552 | [clinic start generated code]*/ |
| 8553 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8554 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8555 | os_ftruncate_impl(PyObject *module, int fd, Py_off_t length) |
| 8556 | /*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8557 | { |
| 8558 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8559 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8560 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8561 | do { |
| 8562 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8563 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8564 | #ifdef MS_WINDOWS |
| 8565 | result = _chsize_s(fd, length); |
| 8566 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8567 | result = ftruncate(fd, length); |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8568 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8569 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8570 | Py_END_ALLOW_THREADS |
| 8571 | } while (result != 0 && errno == EINTR && |
| 8572 | !(async_err = PyErr_CheckSignals())); |
| 8573 | if (result != 0) |
| 8574 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8575 | Py_RETURN_NONE; |
| 8576 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8577 | #endif /* HAVE_FTRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8578 | |
| 8579 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8580 | #if defined HAVE_TRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8581 | /*[clinic input] |
| 8582 | os.truncate |
| 8583 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 8584 | length: Py_off_t |
| 8585 | |
| 8586 | Truncate a file, specified by path, to a specific length. |
| 8587 | |
| 8588 | On some platforms, path may also be specified as an open file descriptor. |
| 8589 | If this functionality is unavailable, using it raises an exception. |
| 8590 | [clinic start generated code]*/ |
| 8591 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8592 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8593 | os_truncate_impl(PyObject *module, path_t *path, Py_off_t length) |
| 8594 | /*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8595 | { |
| 8596 | int result; |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8597 | #ifdef MS_WINDOWS |
| 8598 | int fd; |
| 8599 | #endif |
| 8600 | |
| 8601 | if (path->fd != -1) |
| 8602 | return os_ftruncate_impl(module, path->fd, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8603 | |
| 8604 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8605 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8606 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 8607 | fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Victor Stinner | cc0bbbc | 2015-04-25 00:21:52 +0200 | [diff] [blame] | 8608 | if (fd < 0) |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8609 | result = -1; |
| 8610 | else { |
| 8611 | result = _chsize_s(fd, length); |
| 8612 | close(fd); |
| 8613 | if (result < 0) |
| 8614 | errno = result; |
| 8615 | } |
| 8616 | #else |
| 8617 | result = truncate(path->narrow, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8618 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8619 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8620 | Py_END_ALLOW_THREADS |
| 8621 | if (result < 0) |
| 8622 | return path_error(path); |
| 8623 | |
| 8624 | Py_RETURN_NONE; |
| 8625 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8626 | #endif /* HAVE_TRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8627 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8628 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 8629 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 8630 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 8631 | defined, which is the case in Python on AIX. AIX bug report: |
| 8632 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 8633 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 8634 | # define POSIX_FADVISE_AIX_BUG |
| 8635 | #endif |
| 8636 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 8637 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 8638 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8639 | /*[clinic input] |
| 8640 | os.posix_fallocate |
| 8641 | |
| 8642 | fd: int |
| 8643 | offset: Py_off_t |
| 8644 | length: Py_off_t |
| 8645 | / |
| 8646 | |
| 8647 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 8648 | |
| 8649 | Ensure that the file specified by fd encompasses a range of bytes |
| 8650 | starting at offset bytes from the beginning and continuing for length bytes. |
| 8651 | [clinic start generated code]*/ |
| 8652 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8653 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8654 | os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8655 | Py_off_t length) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8656 | /*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8657 | { |
| 8658 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8659 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8660 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8661 | do { |
| 8662 | Py_BEGIN_ALLOW_THREADS |
| 8663 | result = posix_fallocate(fd, offset, length); |
| 8664 | Py_END_ALLOW_THREADS |
| 8665 | } while (result != 0 && errno == EINTR && |
| 8666 | !(async_err = PyErr_CheckSignals())); |
| 8667 | if (result != 0) |
| 8668 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8669 | Py_RETURN_NONE; |
| 8670 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 8671 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8672 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8673 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 8674 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8675 | /*[clinic input] |
| 8676 | os.posix_fadvise |
| 8677 | |
| 8678 | fd: int |
| 8679 | offset: Py_off_t |
| 8680 | length: Py_off_t |
| 8681 | advice: int |
| 8682 | / |
| 8683 | |
| 8684 | Announce an intention to access data in a specific pattern. |
| 8685 | |
| 8686 | Announce an intention to access data in a specific pattern, thus allowing |
| 8687 | the kernel to make optimizations. |
| 8688 | The advice applies to the region of the file specified by fd starting at |
| 8689 | offset and continuing for length bytes. |
| 8690 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 8691 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 8692 | POSIX_FADV_DONTNEED. |
| 8693 | [clinic start generated code]*/ |
| 8694 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8695 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8696 | os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8697 | Py_off_t length, int advice) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8698 | /*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8699 | { |
| 8700 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8701 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8702 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8703 | do { |
| 8704 | Py_BEGIN_ALLOW_THREADS |
| 8705 | result = posix_fadvise(fd, offset, length, advice); |
| 8706 | Py_END_ALLOW_THREADS |
| 8707 | } while (result != 0 && errno == EINTR && |
| 8708 | !(async_err = PyErr_CheckSignals())); |
| 8709 | if (result != 0) |
| 8710 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8711 | Py_RETURN_NONE; |
| 8712 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 8713 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8714 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8715 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8716 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8717 | /* Save putenv() parameters as values here, so we can collect them when they |
| 8718 | * get re-set with another call for the same key. */ |
| 8719 | static PyObject *posix_putenv_garbage; |
| 8720 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8721 | static void |
| 8722 | posix_putenv_garbage_setitem(PyObject *name, PyObject *value) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8723 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8724 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 8725 | * this will cause previous value to be collected. This has to |
| 8726 | * happen after the real putenv() call because the old value |
| 8727 | * was still accessible until then. */ |
| 8728 | if (PyDict_SetItem(posix_putenv_garbage, name, value)) |
| 8729 | /* really not much we can do; just leak */ |
| 8730 | PyErr_Clear(); |
| 8731 | else |
| 8732 | Py_DECREF(value); |
| 8733 | } |
| 8734 | |
| 8735 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8736 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8737 | /*[clinic input] |
| 8738 | os.putenv |
| 8739 | |
| 8740 | name: unicode |
| 8741 | value: unicode |
| 8742 | / |
| 8743 | |
| 8744 | Change or add an environment variable. |
| 8745 | [clinic start generated code]*/ |
| 8746 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8747 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8748 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 8749 | /*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8750 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 8751 | const wchar_t *env; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8752 | |
| 8753 | PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 8754 | if (unicode == NULL) { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8755 | PyErr_NoMemory(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8756 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8757 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8758 | if (_MAX_ENV < PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8759 | PyErr_Format(PyExc_ValueError, |
| 8760 | "the environment variable is longer than %u characters", |
| 8761 | _MAX_ENV); |
| 8762 | goto error; |
| 8763 | } |
| 8764 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8765 | env = PyUnicode_AsUnicode(unicode); |
| 8766 | if (env == NULL) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 8767 | goto error; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8768 | if (_wputenv(env)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8769 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8770 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8771 | } |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8772 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8773 | posix_putenv_garbage_setitem(name, unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8774 | Py_RETURN_NONE; |
| 8775 | |
| 8776 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8777 | Py_DECREF(unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8778 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8779 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8780 | #else /* MS_WINDOWS */ |
| 8781 | /*[clinic input] |
| 8782 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8783 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8784 | name: FSConverter |
| 8785 | value: FSConverter |
| 8786 | / |
| 8787 | |
| 8788 | Change or add an environment variable. |
| 8789 | [clinic start generated code]*/ |
| 8790 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8791 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8792 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 8793 | /*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8794 | { |
| 8795 | PyObject *bytes = NULL; |
| 8796 | char *env; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 8797 | const char *name_string = PyBytes_AsString(name); |
| 8798 | const char *value_string = PyBytes_AsString(value); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8799 | |
| 8800 | bytes = PyBytes_FromFormat("%s=%s", name_string, value_string); |
| 8801 | if (bytes == NULL) { |
| 8802 | PyErr_NoMemory(); |
| 8803 | return NULL; |
| 8804 | } |
| 8805 | |
| 8806 | env = PyBytes_AS_STRING(bytes); |
| 8807 | if (putenv(env)) { |
| 8808 | Py_DECREF(bytes); |
| 8809 | return posix_error(); |
| 8810 | } |
| 8811 | |
| 8812 | posix_putenv_garbage_setitem(name, bytes); |
| 8813 | Py_RETURN_NONE; |
| 8814 | } |
| 8815 | #endif /* MS_WINDOWS */ |
| 8816 | #endif /* HAVE_PUTENV */ |
| 8817 | |
| 8818 | |
| 8819 | #ifdef HAVE_UNSETENV |
| 8820 | /*[clinic input] |
| 8821 | os.unsetenv |
| 8822 | name: FSConverter |
| 8823 | / |
| 8824 | |
| 8825 | Delete an environment variable. |
| 8826 | [clinic start generated code]*/ |
| 8827 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8828 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8829 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 8830 | /*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8831 | { |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8832 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8833 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8834 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8835 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8836 | #ifdef HAVE_BROKEN_UNSETENV |
| 8837 | unsetenv(PyBytes_AS_STRING(name)); |
| 8838 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8839 | err = unsetenv(PyBytes_AS_STRING(name)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8840 | if (err) |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8841 | return posix_error(); |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8842 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8843 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8844 | /* Remove the key from posix_putenv_garbage; |
| 8845 | * this will cause it to be collected. This has to |
| 8846 | * happen after the real unsetenv() call because the |
| 8847 | * old value was still accessible until then. |
| 8848 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8849 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8850 | /* really not much we can do; just leak */ |
| 8851 | PyErr_Clear(); |
| 8852 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8853 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8854 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8855 | #endif /* HAVE_UNSETENV */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8856 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8857 | |
| 8858 | /*[clinic input] |
| 8859 | os.strerror |
| 8860 | |
| 8861 | code: int |
| 8862 | / |
| 8863 | |
| 8864 | Translate an error code to a message string. |
| 8865 | [clinic start generated code]*/ |
| 8866 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8867 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8868 | os_strerror_impl(PyObject *module, int code) |
| 8869 | /*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8870 | { |
| 8871 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8872 | if (message == NULL) { |
| 8873 | PyErr_SetString(PyExc_ValueError, |
| 8874 | "strerror() argument out of range"); |
| 8875 | return NULL; |
| 8876 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 8877 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8878 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8879 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8880 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8881 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8882 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8883 | /*[clinic input] |
| 8884 | os.WCOREDUMP -> bool |
| 8885 | |
| 8886 | status: int |
| 8887 | / |
| 8888 | |
| 8889 | Return True if the process returning status was dumped to a core file. |
| 8890 | [clinic start generated code]*/ |
| 8891 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8892 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8893 | os_WCOREDUMP_impl(PyObject *module, int status) |
| 8894 | /*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8895 | { |
| 8896 | WAIT_TYPE wait_status; |
| 8897 | WAIT_STATUS_INT(wait_status) = status; |
| 8898 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8899 | } |
| 8900 | #endif /* WCOREDUMP */ |
| 8901 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8902 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8903 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8904 | /*[clinic input] |
| 8905 | os.WIFCONTINUED -> bool |
| 8906 | |
| 8907 | status: int |
| 8908 | |
| 8909 | Return True if a particular process was continued from a job control stop. |
| 8910 | |
| 8911 | Return True if the process returning status was continued from a |
| 8912 | job control stop. |
| 8913 | [clinic start generated code]*/ |
| 8914 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8915 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8916 | os_WIFCONTINUED_impl(PyObject *module, int status) |
| 8917 | /*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8918 | { |
| 8919 | WAIT_TYPE wait_status; |
| 8920 | WAIT_STATUS_INT(wait_status) = status; |
| 8921 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8922 | } |
| 8923 | #endif /* WIFCONTINUED */ |
| 8924 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8925 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8926 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8927 | /*[clinic input] |
| 8928 | os.WIFSTOPPED -> bool |
| 8929 | |
| 8930 | status: int |
| 8931 | |
| 8932 | Return True if the process returning status was stopped. |
| 8933 | [clinic start generated code]*/ |
| 8934 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8935 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8936 | os_WIFSTOPPED_impl(PyObject *module, int status) |
| 8937 | /*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8938 | { |
| 8939 | WAIT_TYPE wait_status; |
| 8940 | WAIT_STATUS_INT(wait_status) = status; |
| 8941 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8942 | } |
| 8943 | #endif /* WIFSTOPPED */ |
| 8944 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8945 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8946 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8947 | /*[clinic input] |
| 8948 | os.WIFSIGNALED -> bool |
| 8949 | |
| 8950 | status: int |
| 8951 | |
| 8952 | Return True if the process returning status was terminated by a signal. |
| 8953 | [clinic start generated code]*/ |
| 8954 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8955 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8956 | os_WIFSIGNALED_impl(PyObject *module, int status) |
| 8957 | /*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8958 | { |
| 8959 | WAIT_TYPE wait_status; |
| 8960 | WAIT_STATUS_INT(wait_status) = status; |
| 8961 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8962 | } |
| 8963 | #endif /* WIFSIGNALED */ |
| 8964 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8965 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8966 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8967 | /*[clinic input] |
| 8968 | os.WIFEXITED -> bool |
| 8969 | |
| 8970 | status: int |
| 8971 | |
| 8972 | Return True if the process returning status exited via the exit() system call. |
| 8973 | [clinic start generated code]*/ |
| 8974 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8975 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8976 | os_WIFEXITED_impl(PyObject *module, int status) |
| 8977 | /*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8978 | { |
| 8979 | WAIT_TYPE wait_status; |
| 8980 | WAIT_STATUS_INT(wait_status) = status; |
| 8981 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8982 | } |
| 8983 | #endif /* WIFEXITED */ |
| 8984 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8985 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 8986 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8987 | /*[clinic input] |
| 8988 | os.WEXITSTATUS -> int |
| 8989 | |
| 8990 | status: int |
| 8991 | |
| 8992 | Return the process return code from status. |
| 8993 | [clinic start generated code]*/ |
| 8994 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8995 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8996 | os_WEXITSTATUS_impl(PyObject *module, int status) |
| 8997 | /*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8998 | { |
| 8999 | WAIT_TYPE wait_status; |
| 9000 | WAIT_STATUS_INT(wait_status) = status; |
| 9001 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9002 | } |
| 9003 | #endif /* WEXITSTATUS */ |
| 9004 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9005 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9006 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9007 | /*[clinic input] |
| 9008 | os.WTERMSIG -> int |
| 9009 | |
| 9010 | status: int |
| 9011 | |
| 9012 | Return the signal that terminated the process that provided the status value. |
| 9013 | [clinic start generated code]*/ |
| 9014 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9015 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9016 | os_WTERMSIG_impl(PyObject *module, int status) |
| 9017 | /*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9018 | { |
| 9019 | WAIT_TYPE wait_status; |
| 9020 | WAIT_STATUS_INT(wait_status) = status; |
| 9021 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9022 | } |
| 9023 | #endif /* WTERMSIG */ |
| 9024 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9025 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9026 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9027 | /*[clinic input] |
| 9028 | os.WSTOPSIG -> int |
| 9029 | |
| 9030 | status: int |
| 9031 | |
| 9032 | Return the signal that stopped the process that provided the status value. |
| 9033 | [clinic start generated code]*/ |
| 9034 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9035 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9036 | os_WSTOPSIG_impl(PyObject *module, int status) |
| 9037 | /*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9038 | { |
| 9039 | WAIT_TYPE wait_status; |
| 9040 | WAIT_STATUS_INT(wait_status) = status; |
| 9041 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9042 | } |
| 9043 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9044 | #endif /* HAVE_SYS_WAIT_H */ |
| 9045 | |
| 9046 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9047 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 9048 | #ifdef _SCO_DS |
| 9049 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 9050 | needed definitions in sys/statvfs.h */ |
| 9051 | #define _SVID3 |
| 9052 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9053 | #include <sys/statvfs.h> |
| 9054 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9055 | static PyObject* |
| 9056 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9057 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 9058 | if (v == NULL) |
| 9059 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9060 | |
| 9061 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9062 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9063 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9064 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 9065 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 9066 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 9067 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 9068 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 9069 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 9070 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9071 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9072 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9073 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9074 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9075 | PyStructSequence_SET_ITEM(v, 2, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9076 | PyLong_FromLongLong((long long) st.f_blocks)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9077 | PyStructSequence_SET_ITEM(v, 3, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9078 | PyLong_FromLongLong((long long) st.f_bfree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9079 | PyStructSequence_SET_ITEM(v, 4, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9080 | PyLong_FromLongLong((long long) st.f_bavail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9081 | PyStructSequence_SET_ITEM(v, 5, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9082 | PyLong_FromLongLong((long long) st.f_files)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9083 | PyStructSequence_SET_ITEM(v, 6, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9084 | PyLong_FromLongLong((long long) st.f_ffree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9085 | PyStructSequence_SET_ITEM(v, 7, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 9086 | PyLong_FromLongLong((long long) st.f_favail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9087 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9088 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9089 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 9090 | if (PyErr_Occurred()) { |
| 9091 | Py_DECREF(v); |
| 9092 | return NULL; |
| 9093 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9094 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9095 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9096 | } |
| 9097 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9098 | |
| 9099 | /*[clinic input] |
| 9100 | os.fstatvfs |
| 9101 | fd: int |
| 9102 | / |
| 9103 | |
| 9104 | Perform an fstatvfs system call on the given fd. |
| 9105 | |
| 9106 | Equivalent to statvfs(fd). |
| 9107 | [clinic start generated code]*/ |
| 9108 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9109 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9110 | os_fstatvfs_impl(PyObject *module, int fd) |
| 9111 | /*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9112 | { |
| 9113 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9114 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9115 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9116 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9117 | do { |
| 9118 | Py_BEGIN_ALLOW_THREADS |
| 9119 | result = fstatvfs(fd, &st); |
| 9120 | Py_END_ALLOW_THREADS |
| 9121 | } while (result != 0 && errno == EINTR && |
| 9122 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9123 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9124 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9125 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9126 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9127 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9128 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9129 | |
| 9130 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9131 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9132 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9133 | /*[clinic input] |
| 9134 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9135 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9136 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 9137 | |
| 9138 | Perform a statvfs system call on the given path. |
| 9139 | |
| 9140 | path may always be specified as a string. |
| 9141 | On some platforms, path may also be specified as an open file descriptor. |
| 9142 | If this functionality is unavailable, using it raises an exception. |
| 9143 | [clinic start generated code]*/ |
| 9144 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9145 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9146 | os_statvfs_impl(PyObject *module, path_t *path) |
| 9147 | /*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9148 | { |
| 9149 | int result; |
| 9150 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9151 | |
| 9152 | Py_BEGIN_ALLOW_THREADS |
| 9153 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9154 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9155 | #ifdef __APPLE__ |
| 9156 | /* handle weak-linking on Mac OS X 10.3 */ |
| 9157 | if (fstatvfs == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9158 | fd_specified("statvfs", path->fd); |
| 9159 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9160 | } |
| 9161 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9162 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9163 | } |
| 9164 | else |
| 9165 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9166 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9167 | Py_END_ALLOW_THREADS |
| 9168 | |
| 9169 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9170 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9171 | } |
| 9172 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9173 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9174 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9175 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 9176 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9177 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9178 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9179 | /*[clinic input] |
| 9180 | os._getdiskusage |
| 9181 | |
| 9182 | path: Py_UNICODE |
| 9183 | |
| 9184 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 9185 | [clinic start generated code]*/ |
| 9186 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9187 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9188 | os__getdiskusage_impl(PyObject *module, Py_UNICODE *path) |
| 9189 | /*[clinic end generated code: output=76d6adcd86b1db0b input=6458133aed893c78]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9190 | { |
| 9191 | BOOL retval; |
| 9192 | ULARGE_INTEGER _, total, free; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9193 | |
| 9194 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 9195 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9196 | Py_END_ALLOW_THREADS |
| 9197 | if (retval == 0) |
| 9198 | return PyErr_SetFromWindowsErr(0); |
| 9199 | |
| 9200 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 9201 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9202 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9203 | |
| 9204 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9205 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 9206 | * It maps strings representing configuration variable names to |
| 9207 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 9208 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9209 | * rarely-used constants. There are three separate tables that use |
| 9210 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9211 | * |
| 9212 | * This code is always included, even if none of the interfaces that |
| 9213 | * need it are included. The #if hackery needed to avoid it would be |
| 9214 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9215 | */ |
| 9216 | struct constdef { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 9217 | const char *name; |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 9218 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9219 | }; |
| 9220 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9221 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9222 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 9223 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9224 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9225 | if (PyLong_Check(arg)) { |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 9226 | int value = _PyLong_AsInt(arg); |
| 9227 | if (value == -1 && PyErr_Occurred()) |
| 9228 | return 0; |
| 9229 | *valuep = value; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9230 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9231 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 9232 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9233 | /* look up the value in the table using a binary search */ |
| 9234 | size_t lo = 0; |
| 9235 | size_t mid; |
| 9236 | size_t hi = tablesize; |
| 9237 | int cmp; |
| 9238 | const char *confname; |
| 9239 | if (!PyUnicode_Check(arg)) { |
| 9240 | PyErr_SetString(PyExc_TypeError, |
| 9241 | "configuration names must be strings or integers"); |
| 9242 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9243 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9244 | confname = _PyUnicode_AsString(arg); |
| 9245 | if (confname == NULL) |
| 9246 | return 0; |
| 9247 | while (lo < hi) { |
| 9248 | mid = (lo + hi) / 2; |
| 9249 | cmp = strcmp(confname, table[mid].name); |
| 9250 | if (cmp < 0) |
| 9251 | hi = mid; |
| 9252 | else if (cmp > 0) |
| 9253 | lo = mid + 1; |
| 9254 | else { |
| 9255 | *valuep = table[mid].value; |
| 9256 | return 1; |
| 9257 | } |
| 9258 | } |
| 9259 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 9260 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9261 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9262 | } |
| 9263 | |
| 9264 | |
| 9265 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 9266 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9267 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9268 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9269 | #endif |
| 9270 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9271 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9272 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9273 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9274 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9275 | #endif |
| 9276 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9277 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9278 | #endif |
| 9279 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9280 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9281 | #endif |
| 9282 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9283 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9284 | #endif |
| 9285 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9286 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9287 | #endif |
| 9288 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9289 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9290 | #endif |
| 9291 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9292 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9293 | #endif |
| 9294 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9295 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9296 | #endif |
| 9297 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9298 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9299 | #endif |
| 9300 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9301 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9302 | #endif |
| 9303 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9304 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9305 | #endif |
| 9306 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9307 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9308 | #endif |
| 9309 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9310 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9311 | #endif |
| 9312 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9313 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9314 | #endif |
| 9315 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9316 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9317 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 9318 | #ifdef _PC_ACL_ENABLED |
| 9319 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 9320 | #endif |
| 9321 | #ifdef _PC_MIN_HOLE_SIZE |
| 9322 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 9323 | #endif |
| 9324 | #ifdef _PC_ALLOC_SIZE_MIN |
| 9325 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 9326 | #endif |
| 9327 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 9328 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 9329 | #endif |
| 9330 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 9331 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 9332 | #endif |
| 9333 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 9334 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 9335 | #endif |
| 9336 | #ifdef _PC_REC_XFER_ALIGN |
| 9337 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 9338 | #endif |
| 9339 | #ifdef _PC_SYMLINK_MAX |
| 9340 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 9341 | #endif |
| 9342 | #ifdef _PC_XATTR_ENABLED |
| 9343 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 9344 | #endif |
| 9345 | #ifdef _PC_XATTR_EXISTS |
| 9346 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 9347 | #endif |
| 9348 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 9349 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 9350 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9351 | }; |
| 9352 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9353 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9354 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9355 | { |
| 9356 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 9357 | sizeof(posix_constants_pathconf) |
| 9358 | / sizeof(struct constdef)); |
| 9359 | } |
| 9360 | #endif |
| 9361 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9362 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9363 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9364 | /*[clinic input] |
| 9365 | os.fpathconf -> long |
| 9366 | |
| 9367 | fd: int |
| 9368 | name: path_confname |
| 9369 | / |
| 9370 | |
| 9371 | Return the configuration limit name for the file descriptor fd. |
| 9372 | |
| 9373 | If there is no limit, return -1. |
| 9374 | [clinic start generated code]*/ |
| 9375 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9376 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9377 | os_fpathconf_impl(PyObject *module, int fd, int name) |
| 9378 | /*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9379 | { |
| 9380 | long limit; |
| 9381 | |
| 9382 | errno = 0; |
| 9383 | limit = fpathconf(fd, name); |
| 9384 | if (limit == -1 && errno != 0) |
| 9385 | posix_error(); |
| 9386 | |
| 9387 | return limit; |
| 9388 | } |
| 9389 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9390 | |
| 9391 | |
| 9392 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9393 | /*[clinic input] |
| 9394 | os.pathconf -> long |
| 9395 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 9396 | name: path_confname |
| 9397 | |
| 9398 | Return the configuration limit name for the file or directory path. |
| 9399 | |
| 9400 | If there is no limit, return -1. |
| 9401 | On some platforms, path may also be specified as an open file descriptor. |
| 9402 | If this functionality is unavailable, using it raises an exception. |
| 9403 | [clinic start generated code]*/ |
| 9404 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9405 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9406 | os_pathconf_impl(PyObject *module, path_t *path, int name) |
| 9407 | /*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9408 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9409 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9410 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9411 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9412 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9413 | if (path->fd != -1) |
| 9414 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9415 | else |
| 9416 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9417 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9418 | if (limit == -1 && errno != 0) { |
| 9419 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 9420 | /* could be a path or name problem */ |
| 9421 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9422 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9423 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9424 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9425 | |
| 9426 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9427 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9428 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9429 | |
| 9430 | #ifdef HAVE_CONFSTR |
| 9431 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9432 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9433 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9434 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9435 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9436 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9437 | #endif |
| 9438 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9439 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9440 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9441 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9442 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9443 | #endif |
| 9444 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9445 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9446 | #endif |
| 9447 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9448 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9449 | #endif |
| 9450 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9451 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9452 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9453 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9454 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9455 | #endif |
| 9456 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9457 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9458 | #endif |
| 9459 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9460 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9461 | #endif |
| 9462 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9463 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9464 | #endif |
| 9465 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9466 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9467 | #endif |
| 9468 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9469 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9470 | #endif |
| 9471 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9472 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9473 | #endif |
| 9474 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9475 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9476 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9477 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9478 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9479 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9480 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9481 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9482 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9483 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9484 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9485 | #endif |
| 9486 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9487 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9488 | #endif |
| 9489 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9490 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9491 | #endif |
| 9492 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9493 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9494 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9495 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9496 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9497 | #endif |
| 9498 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9499 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9500 | #endif |
| 9501 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9502 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9503 | #endif |
| 9504 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9505 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9506 | #endif |
| 9507 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9508 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9509 | #endif |
| 9510 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9511 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9512 | #endif |
| 9513 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9514 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9515 | #endif |
| 9516 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9517 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9518 | #endif |
| 9519 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9520 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9521 | #endif |
| 9522 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9523 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9524 | #endif |
| 9525 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9526 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9527 | #endif |
| 9528 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9529 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9530 | #endif |
| 9531 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9532 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9533 | #endif |
| 9534 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9535 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9536 | #endif |
| 9537 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9538 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9539 | #endif |
| 9540 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9541 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9542 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9543 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9544 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9545 | #endif |
| 9546 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9547 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9548 | #endif |
| 9549 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9550 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9551 | #endif |
| 9552 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9553 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9554 | #endif |
| 9555 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9556 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9557 | #endif |
| 9558 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9559 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9560 | #endif |
| 9561 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9562 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9563 | #endif |
| 9564 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9565 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9566 | #endif |
| 9567 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9568 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9569 | #endif |
| 9570 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9571 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9572 | #endif |
| 9573 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9574 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9575 | #endif |
| 9576 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9577 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9578 | #endif |
| 9579 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9580 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9581 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9582 | }; |
| 9583 | |
| 9584 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9585 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9586 | { |
| 9587 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 9588 | sizeof(posix_constants_confstr) |
| 9589 | / sizeof(struct constdef)); |
| 9590 | } |
| 9591 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9592 | |
| 9593 | /*[clinic input] |
| 9594 | os.confstr |
| 9595 | |
| 9596 | name: confstr_confname |
| 9597 | / |
| 9598 | |
| 9599 | Return a string-valued system configuration variable. |
| 9600 | [clinic start generated code]*/ |
| 9601 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9602 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9603 | os_confstr_impl(PyObject *module, int name) |
| 9604 | /*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9605 | { |
| 9606 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9607 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9608 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9609 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9610 | errno = 0; |
| 9611 | len = confstr(name, buffer, sizeof(buffer)); |
| 9612 | if (len == 0) { |
| 9613 | if (errno) { |
| 9614 | posix_error(); |
| 9615 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9616 | } |
| 9617 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9618 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9619 | } |
| 9620 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9621 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9622 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 9623 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9624 | char *buf = PyMem_Malloc(len); |
| 9625 | if (buf == NULL) |
| 9626 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 9627 | len2 = confstr(name, buf, len); |
| 9628 | assert(len == len2); |
Christian Heimes | 8714cfd | 2015-04-21 10:57:41 +0200 | [diff] [blame] | 9629 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9630 | PyMem_Free(buf); |
| 9631 | } |
| 9632 | else |
| 9633 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9634 | return result; |
| 9635 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9636 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9637 | |
| 9638 | |
| 9639 | #ifdef HAVE_SYSCONF |
| 9640 | static struct constdef posix_constants_sysconf[] = { |
| 9641 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9642 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9643 | #endif |
| 9644 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9645 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9646 | #endif |
| 9647 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9648 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9649 | #endif |
| 9650 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9651 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9652 | #endif |
| 9653 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9654 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9655 | #endif |
| 9656 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9657 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9658 | #endif |
| 9659 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9660 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9661 | #endif |
| 9662 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9663 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9664 | #endif |
| 9665 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9666 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9667 | #endif |
| 9668 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9669 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9670 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9671 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9672 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9673 | #endif |
| 9674 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9675 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9676 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9677 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9678 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9679 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9680 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9681 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9682 | #endif |
| 9683 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9684 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9685 | #endif |
| 9686 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9687 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9688 | #endif |
| 9689 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9690 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9691 | #endif |
| 9692 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9693 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9694 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9695 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9696 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9697 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9698 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9699 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9700 | #endif |
| 9701 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9702 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9703 | #endif |
| 9704 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9705 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9706 | #endif |
| 9707 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9708 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9709 | #endif |
| 9710 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9711 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9712 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9713 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9714 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9715 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9716 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9717 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9718 | #endif |
| 9719 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9720 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9721 | #endif |
| 9722 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9723 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9724 | #endif |
| 9725 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9726 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9727 | #endif |
| 9728 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9729 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9730 | #endif |
| 9731 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9732 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9733 | #endif |
| 9734 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9735 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9736 | #endif |
| 9737 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9738 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9739 | #endif |
| 9740 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9741 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9742 | #endif |
| 9743 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9744 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9745 | #endif |
| 9746 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9747 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9748 | #endif |
| 9749 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9750 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9751 | #endif |
| 9752 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9753 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9754 | #endif |
| 9755 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9756 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9757 | #endif |
| 9758 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9759 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9760 | #endif |
| 9761 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9762 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9763 | #endif |
| 9764 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9765 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9766 | #endif |
| 9767 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9768 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9769 | #endif |
| 9770 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9771 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9772 | #endif |
| 9773 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9774 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9775 | #endif |
| 9776 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9777 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9778 | #endif |
| 9779 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9780 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9781 | #endif |
| 9782 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9783 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9784 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9785 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9786 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9787 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9788 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9789 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9790 | #endif |
| 9791 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9792 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9793 | #endif |
| 9794 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9795 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9796 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9797 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9798 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9799 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9800 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9801 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9802 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9803 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9804 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9805 | #endif |
| 9806 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9807 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9808 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9809 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9810 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9811 | #endif |
| 9812 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9813 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9814 | #endif |
| 9815 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9816 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9817 | #endif |
| 9818 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9819 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9820 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9821 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9822 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9823 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9824 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9825 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9826 | #endif |
| 9827 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9828 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9829 | #endif |
| 9830 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9831 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9832 | #endif |
| 9833 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9834 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9835 | #endif |
| 9836 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9837 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9838 | #endif |
| 9839 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9840 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9841 | #endif |
| 9842 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9843 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9844 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9845 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9846 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9847 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9848 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9849 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9850 | #endif |
| 9851 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9852 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9853 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9854 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9855 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9856 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9857 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9858 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9859 | #endif |
| 9860 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9861 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9862 | #endif |
| 9863 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9864 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9865 | #endif |
| 9866 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9867 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9868 | #endif |
| 9869 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9870 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9871 | #endif |
| 9872 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9873 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9874 | #endif |
| 9875 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9876 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9877 | #endif |
| 9878 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9879 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9880 | #endif |
| 9881 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9882 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9883 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9884 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9885 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9886 | #endif |
| 9887 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9888 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9889 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9890 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9891 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9892 | #endif |
| 9893 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9894 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9895 | #endif |
| 9896 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9897 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9898 | #endif |
| 9899 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9900 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9901 | #endif |
| 9902 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9903 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9904 | #endif |
| 9905 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9906 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9907 | #endif |
| 9908 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9909 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9910 | #endif |
| 9911 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9912 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9913 | #endif |
| 9914 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9915 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9916 | #endif |
| 9917 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9918 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9919 | #endif |
| 9920 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9921 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9922 | #endif |
| 9923 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9924 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9925 | #endif |
| 9926 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9927 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9928 | #endif |
| 9929 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9930 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9931 | #endif |
| 9932 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9933 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9934 | #endif |
| 9935 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9936 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9937 | #endif |
| 9938 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9939 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9940 | #endif |
| 9941 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9942 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9943 | #endif |
| 9944 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9945 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9946 | #endif |
| 9947 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9948 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9949 | #endif |
| 9950 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9951 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9952 | #endif |
| 9953 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9954 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9955 | #endif |
| 9956 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9957 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9958 | #endif |
| 9959 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9960 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9961 | #endif |
| 9962 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9963 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9964 | #endif |
| 9965 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9966 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9967 | #endif |
| 9968 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9969 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9970 | #endif |
| 9971 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9972 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9973 | #endif |
| 9974 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9975 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9976 | #endif |
| 9977 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9978 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9979 | #endif |
| 9980 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9981 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9982 | #endif |
| 9983 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9984 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9985 | #endif |
| 9986 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9987 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9988 | #endif |
| 9989 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9990 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9991 | #endif |
| 9992 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9993 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9994 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9995 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9996 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9997 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9998 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9999 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10000 | #endif |
| 10001 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10002 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10003 | #endif |
| 10004 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10005 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10006 | #endif |
| 10007 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10008 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10009 | #endif |
| 10010 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10011 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10012 | #endif |
| 10013 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10014 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10015 | #endif |
| 10016 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10017 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10018 | #endif |
| 10019 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10020 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10021 | #endif |
| 10022 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10023 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10024 | #endif |
| 10025 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10026 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10027 | #endif |
| 10028 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10029 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10030 | #endif |
| 10031 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10032 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10033 | #endif |
| 10034 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10035 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10036 | #endif |
| 10037 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10038 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10039 | #endif |
| 10040 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10041 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10042 | #endif |
| 10043 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10044 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10045 | #endif |
| 10046 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10047 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10048 | #endif |
| 10049 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10050 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10051 | #endif |
| 10052 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10053 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10054 | #endif |
| 10055 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10056 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10057 | #endif |
| 10058 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10059 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10060 | #endif |
| 10061 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10062 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10063 | #endif |
| 10064 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10065 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10066 | #endif |
| 10067 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10068 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10069 | #endif |
| 10070 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10071 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10072 | #endif |
| 10073 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10074 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10075 | #endif |
| 10076 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10077 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10078 | #endif |
| 10079 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10080 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10081 | #endif |
| 10082 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10083 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10084 | #endif |
| 10085 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10086 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10087 | #endif |
| 10088 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10089 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10090 | #endif |
| 10091 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10092 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10093 | #endif |
| 10094 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10095 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10096 | #endif |
| 10097 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10098 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10099 | #endif |
| 10100 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10101 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10102 | #endif |
| 10103 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10104 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10105 | #endif |
| 10106 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10107 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10108 | #endif |
| 10109 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10110 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10111 | #endif |
| 10112 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10113 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10114 | #endif |
| 10115 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10116 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10117 | #endif |
| 10118 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10119 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10120 | #endif |
| 10121 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10122 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10123 | #endif |
| 10124 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10125 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10126 | #endif |
| 10127 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10128 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10129 | #endif |
| 10130 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10131 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10132 | #endif |
| 10133 | }; |
| 10134 | |
| 10135 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10136 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10137 | { |
| 10138 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 10139 | sizeof(posix_constants_sysconf) |
| 10140 | / sizeof(struct constdef)); |
| 10141 | } |
| 10142 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10143 | |
| 10144 | /*[clinic input] |
| 10145 | os.sysconf -> long |
| 10146 | name: sysconf_confname |
| 10147 | / |
| 10148 | |
| 10149 | Return an integer-valued system configuration variable. |
| 10150 | [clinic start generated code]*/ |
| 10151 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10152 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10153 | os_sysconf_impl(PyObject *module, int name) |
| 10154 | /*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10155 | { |
| 10156 | long value; |
| 10157 | |
| 10158 | errno = 0; |
| 10159 | value = sysconf(name); |
| 10160 | if (value == -1 && errno != 0) |
| 10161 | posix_error(); |
| 10162 | return value; |
| 10163 | } |
| 10164 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10165 | |
| 10166 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10167 | /* 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] | 10168 | * 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] | 10169 | * the exported dictionaries that are used to publish information about the |
| 10170 | * names available on the host platform. |
| 10171 | * |
| 10172 | * Sorting the table at runtime ensures that the table is properly ordered |
| 10173 | * when used, even for platforms we're not able to test on. It also makes |
| 10174 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10175 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10176 | |
| 10177 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10178 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10179 | { |
| 10180 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10181 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10182 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10183 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10184 | |
| 10185 | return strcmp(c1->name, c2->name); |
| 10186 | } |
| 10187 | |
| 10188 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10189 | setup_confname_table(struct constdef *table, size_t tablesize, |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 10190 | const char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10191 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10192 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10193 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10194 | |
| 10195 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 10196 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10197 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10198 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10199 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10200 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10201 | PyObject *o = PyLong_FromLong(table[i].value); |
| 10202 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 10203 | Py_XDECREF(o); |
| 10204 | Py_DECREF(d); |
| 10205 | return -1; |
| 10206 | } |
| 10207 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10208 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10209 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10210 | } |
| 10211 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10212 | /* Return -1 on failure, 0 on success. */ |
| 10213 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10214 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10215 | { |
| 10216 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10217 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10218 | sizeof(posix_constants_pathconf) |
| 10219 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10220 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10221 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10222 | #endif |
| 10223 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10224 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10225 | sizeof(posix_constants_confstr) |
| 10226 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10227 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10228 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10229 | #endif |
| 10230 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10231 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10232 | sizeof(posix_constants_sysconf) |
| 10233 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10234 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10235 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10236 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10237 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10238 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10239 | |
| 10240 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10241 | /*[clinic input] |
| 10242 | os.abort |
| 10243 | |
| 10244 | Abort the interpreter immediately. |
| 10245 | |
| 10246 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 10247 | on the hosting operating system. This function never returns. |
| 10248 | [clinic start generated code]*/ |
| 10249 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10250 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10251 | os_abort_impl(PyObject *module) |
| 10252 | /*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10253 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10254 | abort(); |
| 10255 | /*NOTREACHED*/ |
| 10256 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 10257 | return NULL; |
| 10258 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10259 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10260 | #ifdef MS_WINDOWS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10261 | /* Grab ShellExecute dynamically from shell32 */ |
| 10262 | static int has_ShellExecute = -1; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10263 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 10264 | LPCWSTR, INT); |
| 10265 | static int |
| 10266 | check_ShellExecute() |
| 10267 | { |
| 10268 | HINSTANCE hShell32; |
| 10269 | |
| 10270 | /* only recheck */ |
| 10271 | if (-1 == has_ShellExecute) { |
| 10272 | Py_BEGIN_ALLOW_THREADS |
| 10273 | hShell32 = LoadLibraryW(L"SHELL32"); |
| 10274 | Py_END_ALLOW_THREADS |
| 10275 | if (hShell32) { |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10276 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 10277 | "ShellExecuteW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10278 | has_ShellExecute = Py_ShellExecuteW != NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10279 | } else { |
| 10280 | has_ShellExecute = 0; |
| 10281 | } |
| 10282 | } |
| 10283 | return has_ShellExecute; |
| 10284 | } |
| 10285 | |
| 10286 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10287 | /*[clinic input] |
| 10288 | os.startfile |
| 10289 | filepath: path_t |
| 10290 | operation: Py_UNICODE = NULL |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 10291 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10292 | startfile(filepath [, operation]) |
| 10293 | |
| 10294 | Start a file with its associated application. |
| 10295 | |
| 10296 | When "operation" is not specified or "open", this acts like |
| 10297 | double-clicking the file in Explorer, or giving the file name as an |
| 10298 | argument to the DOS "start" command: the file is opened with whatever |
| 10299 | application (if any) its extension is associated. |
| 10300 | When another "operation" is given, it specifies what should be done with |
| 10301 | the file. A typical operation is "print". |
| 10302 | |
| 10303 | startfile returns as soon as the associated application is launched. |
| 10304 | There is no option to wait for the application to close, and no way |
| 10305 | to retrieve the application's exit status. |
| 10306 | |
| 10307 | The filepath is relative to the current directory. If you want to use |
| 10308 | an absolute path, make sure the first character is not a slash ("/"); |
| 10309 | the underlying Win32 ShellExecute function doesn't work if it is. |
| 10310 | [clinic start generated code]*/ |
| 10311 | |
| 10312 | static PyObject * |
| 10313 | os_startfile_impl(PyObject *module, path_t *filepath, Py_UNICODE *operation) |
| 10314 | /*[clinic end generated code: output=912ceba79acfa1c9 input=63950bf2986380d0]*/ |
| 10315 | { |
| 10316 | HINSTANCE rc; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10317 | |
| 10318 | if(!check_ShellExecute()) { |
| 10319 | /* If the OS doesn't have ShellExecute, return a |
| 10320 | NotImplementedError. */ |
| 10321 | return PyErr_Format(PyExc_NotImplementedError, |
| 10322 | "startfile not available on this platform"); |
| 10323 | } |
| 10324 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10325 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10326 | rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide, |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10327 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10328 | Py_END_ALLOW_THREADS |
| 10329 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10330 | if (rc <= (HINSTANCE)32) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10331 | win32_error_object("startfile", filepath->object); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10332 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10333 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10334 | Py_RETURN_NONE; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10335 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10336 | #endif /* MS_WINDOWS */ |
| 10337 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10338 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10339 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10340 | /*[clinic input] |
| 10341 | os.getloadavg |
| 10342 | |
| 10343 | Return average recent system load information. |
| 10344 | |
| 10345 | Return the number of processes in the system run queue averaged over |
| 10346 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 10347 | Raises OSError if the load average was unobtainable. |
| 10348 | [clinic start generated code]*/ |
| 10349 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10350 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10351 | os_getloadavg_impl(PyObject *module) |
| 10352 | /*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10353 | { |
| 10354 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10355 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10356 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 10357 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10358 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10359 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10360 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10361 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10362 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10363 | |
| 10364 | /*[clinic input] |
| 10365 | os.device_encoding |
| 10366 | fd: int |
| 10367 | |
| 10368 | Return a string describing the encoding of a terminal's file descriptor. |
| 10369 | |
| 10370 | The file descriptor must be attached to a terminal. |
| 10371 | If the device is not a terminal, return None. |
| 10372 | [clinic start generated code]*/ |
| 10373 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10374 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10375 | os_device_encoding_impl(PyObject *module, int fd) |
| 10376 | /*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10377 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10378 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 10379 | } |
| 10380 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10381 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10382 | #ifdef HAVE_SETRESUID |
| 10383 | /*[clinic input] |
| 10384 | os.setresuid |
| 10385 | |
| 10386 | ruid: uid_t |
| 10387 | euid: uid_t |
| 10388 | suid: uid_t |
| 10389 | / |
| 10390 | |
| 10391 | Set the current process's real, effective, and saved user ids. |
| 10392 | [clinic start generated code]*/ |
| 10393 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10394 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10395 | os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid) |
| 10396 | /*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10397 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10398 | if (setresuid(ruid, euid, suid) < 0) |
| 10399 | return posix_error(); |
| 10400 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10401 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10402 | #endif /* HAVE_SETRESUID */ |
| 10403 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10404 | |
| 10405 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10406 | /*[clinic input] |
| 10407 | os.setresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10408 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10409 | rgid: gid_t |
| 10410 | egid: gid_t |
| 10411 | sgid: gid_t |
| 10412 | / |
| 10413 | |
| 10414 | Set the current process's real, effective, and saved group ids. |
| 10415 | [clinic start generated code]*/ |
| 10416 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10417 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10418 | os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid) |
| 10419 | /*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10420 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10421 | if (setresgid(rgid, egid, sgid) < 0) |
| 10422 | return posix_error(); |
| 10423 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10424 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10425 | #endif /* HAVE_SETRESGID */ |
| 10426 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10427 | |
| 10428 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10429 | /*[clinic input] |
| 10430 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10431 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10432 | Return a tuple of the current process's real, effective, and saved user ids. |
| 10433 | [clinic start generated code]*/ |
| 10434 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10435 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10436 | os_getresuid_impl(PyObject *module) |
| 10437 | /*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10438 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10439 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10440 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 10441 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10442 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 10443 | _PyLong_FromUid(euid), |
| 10444 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10445 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10446 | #endif /* HAVE_GETRESUID */ |
| 10447 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10448 | |
| 10449 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10450 | /*[clinic input] |
| 10451 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10452 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10453 | Return a tuple of the current process's real, effective, and saved group ids. |
| 10454 | [clinic start generated code]*/ |
| 10455 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10456 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10457 | os_getresgid_impl(PyObject *module) |
| 10458 | /*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10459 | { |
| 10460 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10461 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 10462 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10463 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 10464 | _PyLong_FromGid(egid), |
| 10465 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10466 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10467 | #endif /* HAVE_GETRESGID */ |
| 10468 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10469 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10470 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10471 | /*[clinic input] |
| 10472 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10473 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10474 | path: path_t(allow_fd=True) |
| 10475 | attribute: path_t |
| 10476 | * |
| 10477 | follow_symlinks: bool = True |
| 10478 | |
| 10479 | Return the value of extended attribute attribute on path. |
| 10480 | |
| 10481 | path may be either a string or an open file descriptor. |
| 10482 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10483 | link, getxattr will examine the symbolic link itself instead of the file |
| 10484 | the link points to. |
| 10485 | |
| 10486 | [clinic start generated code]*/ |
| 10487 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10488 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10489 | os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10490 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10491 | /*[clinic end generated code: output=5f2f44200a43cff2 input=8c8ea3bab78d89c2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10492 | { |
| 10493 | Py_ssize_t i; |
| 10494 | PyObject *buffer = NULL; |
| 10495 | |
| 10496 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 10497 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10498 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10499 | for (i = 0; ; i++) { |
| 10500 | void *ptr; |
| 10501 | ssize_t result; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 10502 | static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10503 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10504 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10505 | path_error(path); |
| 10506 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10507 | } |
| 10508 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 10509 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10510 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10511 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10512 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10513 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10514 | if (path->fd >= 0) |
| 10515 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10516 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10517 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10518 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10519 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10520 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10521 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10522 | if (result < 0) { |
| 10523 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10524 | if (errno == ERANGE) |
| 10525 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10526 | path_error(path); |
| 10527 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10528 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10529 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10530 | if (result != buffer_size) { |
| 10531 | /* Can only shrink. */ |
| 10532 | _PyBytes_Resize(&buffer, result); |
| 10533 | } |
| 10534 | break; |
| 10535 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10536 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10537 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10538 | } |
| 10539 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10540 | |
| 10541 | /*[clinic input] |
| 10542 | os.setxattr |
| 10543 | |
| 10544 | path: path_t(allow_fd=True) |
| 10545 | attribute: path_t |
| 10546 | value: Py_buffer |
| 10547 | flags: int = 0 |
| 10548 | * |
| 10549 | follow_symlinks: bool = True |
| 10550 | |
| 10551 | Set extended attribute attribute on path to value. |
| 10552 | |
| 10553 | path may be either a string or an open file descriptor. |
| 10554 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10555 | link, setxattr will modify the symbolic link itself instead of the file |
| 10556 | the link points to. |
| 10557 | |
| 10558 | [clinic start generated code]*/ |
| 10559 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10560 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10561 | os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10562 | Py_buffer *value, int flags, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10563 | /*[clinic end generated code: output=98b83f63fdde26bb input=f0d26833992015c2]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10564 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10565 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10566 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10567 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10568 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10569 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10570 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10571 | if (path->fd > -1) |
| 10572 | result = fsetxattr(path->fd, attribute->narrow, |
| 10573 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10574 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10575 | result = setxattr(path->narrow, attribute->narrow, |
| 10576 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10577 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10578 | result = lsetxattr(path->narrow, attribute->narrow, |
| 10579 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10580 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10581 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10582 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10583 | path_error(path); |
| 10584 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10585 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10586 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10587 | Py_RETURN_NONE; |
| 10588 | } |
| 10589 | |
| 10590 | |
| 10591 | /*[clinic input] |
| 10592 | os.removexattr |
| 10593 | |
| 10594 | path: path_t(allow_fd=True) |
| 10595 | attribute: path_t |
| 10596 | * |
| 10597 | follow_symlinks: bool = True |
| 10598 | |
| 10599 | Remove extended attribute attribute on path. |
| 10600 | |
| 10601 | path may be either a string or an open file descriptor. |
| 10602 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10603 | link, removexattr will modify the symbolic link itself instead of the file |
| 10604 | the link points to. |
| 10605 | |
| 10606 | [clinic start generated code]*/ |
| 10607 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10608 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10609 | os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10610 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10611 | /*[clinic end generated code: output=521a51817980cda6 input=cdb54834161e3329]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10612 | { |
| 10613 | ssize_t result; |
| 10614 | |
| 10615 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 10616 | return NULL; |
| 10617 | |
| 10618 | Py_BEGIN_ALLOW_THREADS; |
| 10619 | if (path->fd > -1) |
| 10620 | result = fremovexattr(path->fd, attribute->narrow); |
| 10621 | else if (follow_symlinks) |
| 10622 | result = removexattr(path->narrow, attribute->narrow); |
| 10623 | else |
| 10624 | result = lremovexattr(path->narrow, attribute->narrow); |
| 10625 | Py_END_ALLOW_THREADS; |
| 10626 | |
| 10627 | if (result) { |
| 10628 | return path_error(path); |
| 10629 | } |
| 10630 | |
| 10631 | Py_RETURN_NONE; |
| 10632 | } |
| 10633 | |
| 10634 | |
| 10635 | /*[clinic input] |
| 10636 | os.listxattr |
| 10637 | |
| 10638 | path: path_t(allow_fd=True, nullable=True) = None |
| 10639 | * |
| 10640 | follow_symlinks: bool = True |
| 10641 | |
| 10642 | Return a list of extended attributes on path. |
| 10643 | |
| 10644 | path may be either None, a string, or an open file descriptor. |
| 10645 | if path is None, listxattr will examine the current directory. |
| 10646 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10647 | link, listxattr will examine the symbolic link itself instead of the file |
| 10648 | the link points to. |
| 10649 | [clinic start generated code]*/ |
| 10650 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10651 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10652 | os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks) |
| 10653 | /*[clinic end generated code: output=bebdb4e2ad0ce435 input=08cca53ac0b07c13]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10654 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10655 | Py_ssize_t i; |
| 10656 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10657 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10658 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10659 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10660 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10661 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10662 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10663 | name = path->narrow ? path->narrow : "."; |
| 10664 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10665 | for (i = 0; ; i++) { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 10666 | const char *start, *trace, *end; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10667 | ssize_t length; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 10668 | static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10669 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10670 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 10671 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10672 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10673 | break; |
| 10674 | } |
| 10675 | buffer = PyMem_MALLOC(buffer_size); |
| 10676 | if (!buffer) { |
| 10677 | PyErr_NoMemory(); |
| 10678 | break; |
| 10679 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10680 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10681 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10682 | if (path->fd > -1) |
| 10683 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10684 | else if (follow_symlinks) |
| 10685 | length = listxattr(name, buffer, buffer_size); |
| 10686 | else |
| 10687 | length = llistxattr(name, buffer, buffer_size); |
| 10688 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10689 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10690 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 10691 | if (errno == ERANGE) { |
| 10692 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 10693 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10694 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 10695 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10696 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10697 | break; |
| 10698 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10699 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10700 | result = PyList_New(0); |
| 10701 | if (!result) { |
| 10702 | goto exit; |
| 10703 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10704 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10705 | end = buffer + length; |
| 10706 | for (trace = start = buffer; trace != end; trace++) { |
| 10707 | if (!*trace) { |
| 10708 | int error; |
| 10709 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 10710 | trace - start); |
| 10711 | if (!attribute) { |
| 10712 | Py_DECREF(result); |
| 10713 | result = NULL; |
| 10714 | goto exit; |
| 10715 | } |
| 10716 | error = PyList_Append(result, attribute); |
| 10717 | Py_DECREF(attribute); |
| 10718 | if (error) { |
| 10719 | Py_DECREF(result); |
| 10720 | result = NULL; |
| 10721 | goto exit; |
| 10722 | } |
| 10723 | start = trace + 1; |
| 10724 | } |
| 10725 | } |
| 10726 | break; |
| 10727 | } |
| 10728 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10729 | if (buffer) |
| 10730 | PyMem_FREE(buffer); |
| 10731 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10732 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10733 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10734 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10735 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10736 | /*[clinic input] |
| 10737 | os.urandom |
| 10738 | |
| 10739 | size: Py_ssize_t |
| 10740 | / |
| 10741 | |
| 10742 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 10743 | [clinic start generated code]*/ |
| 10744 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10745 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10746 | os_urandom_impl(PyObject *module, Py_ssize_t size) |
| 10747 | /*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10748 | { |
| 10749 | PyObject *bytes; |
| 10750 | int result; |
| 10751 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10752 | if (size < 0) |
| 10753 | return PyErr_Format(PyExc_ValueError, |
| 10754 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10755 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 10756 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10757 | return NULL; |
| 10758 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 10759 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10760 | if (result == -1) { |
| 10761 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10762 | return NULL; |
| 10763 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10764 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10765 | } |
| 10766 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10767 | /* Terminal size querying */ |
| 10768 | |
| 10769 | static PyTypeObject TerminalSizeType; |
| 10770 | |
| 10771 | PyDoc_STRVAR(TerminalSize_docstring, |
| 10772 | "A tuple of (columns, lines) for holding terminal window size"); |
| 10773 | |
| 10774 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 10775 | {"columns", "width of the terminal window in characters"}, |
| 10776 | {"lines", "height of the terminal window in characters"}, |
| 10777 | {NULL, NULL} |
| 10778 | }; |
| 10779 | |
| 10780 | static PyStructSequence_Desc TerminalSize_desc = { |
| 10781 | "os.terminal_size", |
| 10782 | TerminalSize_docstring, |
| 10783 | TerminalSize_fields, |
| 10784 | 2, |
| 10785 | }; |
| 10786 | |
| 10787 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10788 | /* AC 3.5: fd should accept None */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10789 | PyDoc_STRVAR(termsize__doc__, |
| 10790 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 10791 | "\n" \ |
| 10792 | "The optional argument fd (default standard output) specifies\n" \ |
| 10793 | "which file descriptor should be queried.\n" \ |
| 10794 | "\n" \ |
| 10795 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 10796 | "is thrown.\n" \ |
| 10797 | "\n" \ |
| 10798 | "This function will only be defined if an implementation is\n" \ |
| 10799 | "available for this system.\n" \ |
| 10800 | "\n" \ |
| 10801 | "shutil.get_terminal_size is the high-level function which should \n" \ |
| 10802 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 10803 | |
| 10804 | static PyObject* |
| 10805 | get_terminal_size(PyObject *self, PyObject *args) |
| 10806 | { |
| 10807 | int columns, lines; |
| 10808 | PyObject *termsize; |
| 10809 | |
| 10810 | int fd = fileno(stdout); |
| 10811 | /* Under some conditions stdout may not be connected and |
| 10812 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 10813 | * GUI apps don't have valid standard streams by default. |
| 10814 | * |
| 10815 | * If this happens, and the optional fd argument is not present, |
| 10816 | * the ioctl below will fail returning EBADF. This is what we want. |
| 10817 | */ |
| 10818 | |
| 10819 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 10820 | return NULL; |
| 10821 | |
| 10822 | #ifdef TERMSIZE_USE_IOCTL |
| 10823 | { |
| 10824 | struct winsize w; |
| 10825 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 10826 | return PyErr_SetFromErrno(PyExc_OSError); |
| 10827 | columns = w.ws_col; |
| 10828 | lines = w.ws_row; |
| 10829 | } |
| 10830 | #endif /* TERMSIZE_USE_IOCTL */ |
| 10831 | |
| 10832 | #ifdef TERMSIZE_USE_CONIO |
| 10833 | { |
| 10834 | DWORD nhandle; |
| 10835 | HANDLE handle; |
| 10836 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 10837 | switch (fd) { |
| 10838 | case 0: nhandle = STD_INPUT_HANDLE; |
| 10839 | break; |
| 10840 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 10841 | break; |
| 10842 | case 2: nhandle = STD_ERROR_HANDLE; |
| 10843 | break; |
| 10844 | default: |
| 10845 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 10846 | } |
| 10847 | handle = GetStdHandle(nhandle); |
| 10848 | if (handle == NULL) |
| 10849 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 10850 | if (handle == INVALID_HANDLE_VALUE) |
| 10851 | return PyErr_SetFromWindowsErr(0); |
| 10852 | |
| 10853 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 10854 | return PyErr_SetFromWindowsErr(0); |
| 10855 | |
| 10856 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 10857 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 10858 | } |
| 10859 | #endif /* TERMSIZE_USE_CONIO */ |
| 10860 | |
| 10861 | termsize = PyStructSequence_New(&TerminalSizeType); |
| 10862 | if (termsize == NULL) |
| 10863 | return NULL; |
| 10864 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 10865 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 10866 | if (PyErr_Occurred()) { |
| 10867 | Py_DECREF(termsize); |
| 10868 | return NULL; |
| 10869 | } |
| 10870 | return termsize; |
| 10871 | } |
| 10872 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 10873 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10874 | |
| 10875 | /*[clinic input] |
| 10876 | os.cpu_count |
| 10877 | |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 10878 | Return the number of CPUs in the system; return None if indeterminable. |
| 10879 | |
| 10880 | This number is not equivalent to the number of CPUs the current process can |
| 10881 | use. The number of usable CPUs can be obtained with |
| 10882 | ``len(os.sched_getaffinity(0))`` |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10883 | [clinic start generated code]*/ |
| 10884 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10885 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10886 | os_cpu_count_impl(PyObject *module) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 10887 | /*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10888 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 10889 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10890 | #ifdef MS_WINDOWS |
| 10891 | SYSTEM_INFO sysinfo; |
| 10892 | GetSystemInfo(&sysinfo); |
| 10893 | ncpu = sysinfo.dwNumberOfProcessors; |
| 10894 | #elif defined(__hpux) |
| 10895 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 10896 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 10897 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10898 | #elif defined(__DragonFly__) || \ |
| 10899 | defined(__OpenBSD__) || \ |
| 10900 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 10901 | defined(__NetBSD__) || \ |
| 10902 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 10903 | int mib[2]; |
| 10904 | size_t len = sizeof(ncpu); |
| 10905 | mib[0] = CTL_HW; |
| 10906 | mib[1] = HW_NCPU; |
| 10907 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 10908 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10909 | #endif |
| 10910 | if (ncpu >= 1) |
| 10911 | return PyLong_FromLong(ncpu); |
| 10912 | else |
| 10913 | Py_RETURN_NONE; |
| 10914 | } |
| 10915 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10916 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10917 | /*[clinic input] |
| 10918 | os.get_inheritable -> bool |
| 10919 | |
| 10920 | fd: int |
| 10921 | / |
| 10922 | |
| 10923 | Get the close-on-exe flag of the specified file descriptor. |
| 10924 | [clinic start generated code]*/ |
| 10925 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10926 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10927 | os_get_inheritable_impl(PyObject *module, int fd) |
| 10928 | /*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10929 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 10930 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 10931 | _Py_BEGIN_SUPPRESS_IPH |
| 10932 | return_value = _Py_get_inheritable(fd); |
| 10933 | _Py_END_SUPPRESS_IPH |
| 10934 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10935 | } |
| 10936 | |
| 10937 | |
| 10938 | /*[clinic input] |
| 10939 | os.set_inheritable |
| 10940 | fd: int |
| 10941 | inheritable: int |
| 10942 | / |
| 10943 | |
| 10944 | Set the inheritable flag of the specified file descriptor. |
| 10945 | [clinic start generated code]*/ |
| 10946 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10947 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10948 | os_set_inheritable_impl(PyObject *module, int fd, int inheritable) |
| 10949 | /*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10950 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 10951 | int result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10952 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 10953 | _Py_BEGIN_SUPPRESS_IPH |
| 10954 | result = _Py_set_inheritable(fd, inheritable, NULL); |
| 10955 | _Py_END_SUPPRESS_IPH |
| 10956 | if (result < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10957 | return NULL; |
| 10958 | Py_RETURN_NONE; |
| 10959 | } |
| 10960 | |
| 10961 | |
| 10962 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10963 | /*[clinic input] |
| 10964 | os.get_handle_inheritable -> bool |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 10965 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10966 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10967 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10968 | Get the close-on-exe flag of the specified file descriptor. |
| 10969 | [clinic start generated code]*/ |
| 10970 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10971 | static int |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 10972 | os_get_handle_inheritable_impl(PyObject *module, intptr_t handle) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 10973 | /*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10974 | { |
| 10975 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10976 | |
| 10977 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 10978 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10979 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10980 | } |
| 10981 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10982 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10983 | } |
| 10984 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10985 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10986 | /*[clinic input] |
| 10987 | os.set_handle_inheritable |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 10988 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10989 | inheritable: bool |
| 10990 | / |
| 10991 | |
| 10992 | Set the inheritable flag of the specified handle. |
| 10993 | [clinic start generated code]*/ |
| 10994 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10995 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 10996 | os_set_handle_inheritable_impl(PyObject *module, intptr_t handle, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10997 | int inheritable) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 10998 | /*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10999 | { |
| 11000 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11001 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 11002 | PyErr_SetFromWindowsErr(0); |
| 11003 | return NULL; |
| 11004 | } |
| 11005 | Py_RETURN_NONE; |
| 11006 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11007 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11008 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11009 | #ifndef MS_WINDOWS |
| 11010 | PyDoc_STRVAR(get_blocking__doc__, |
| 11011 | "get_blocking(fd) -> bool\n" \ |
| 11012 | "\n" \ |
| 11013 | "Get the blocking mode of the file descriptor:\n" \ |
| 11014 | "False if the O_NONBLOCK flag is set, True if the flag is cleared."); |
| 11015 | |
| 11016 | static PyObject* |
| 11017 | posix_get_blocking(PyObject *self, PyObject *args) |
| 11018 | { |
| 11019 | int fd; |
| 11020 | int blocking; |
| 11021 | |
| 11022 | if (!PyArg_ParseTuple(args, "i:get_blocking", &fd)) |
| 11023 | return NULL; |
| 11024 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11025 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11026 | blocking = _Py_get_blocking(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11027 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11028 | if (blocking < 0) |
| 11029 | return NULL; |
| 11030 | return PyBool_FromLong(blocking); |
| 11031 | } |
| 11032 | |
| 11033 | PyDoc_STRVAR(set_blocking__doc__, |
| 11034 | "set_blocking(fd, blocking)\n" \ |
| 11035 | "\n" \ |
| 11036 | "Set the blocking mode of the specified file descriptor.\n" \ |
| 11037 | "Set the O_NONBLOCK flag if blocking is False,\n" \ |
| 11038 | "clear the O_NONBLOCK flag otherwise."); |
| 11039 | |
| 11040 | static PyObject* |
| 11041 | posix_set_blocking(PyObject *self, PyObject *args) |
| 11042 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11043 | int fd, blocking, result; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11044 | |
| 11045 | if (!PyArg_ParseTuple(args, "ii:set_blocking", &fd, &blocking)) |
| 11046 | return NULL; |
| 11047 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11048 | _Py_BEGIN_SUPPRESS_IPH |
| 11049 | result = _Py_set_blocking(fd, blocking); |
| 11050 | _Py_END_SUPPRESS_IPH |
| 11051 | if (result < 0) |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11052 | return NULL; |
| 11053 | Py_RETURN_NONE; |
| 11054 | } |
| 11055 | #endif /* !MS_WINDOWS */ |
| 11056 | |
| 11057 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11058 | PyDoc_STRVAR(posix_scandir__doc__, |
| 11059 | "scandir(path='.') -> iterator of DirEntry objects for given path"); |
| 11060 | |
| 11061 | static char *follow_symlinks_keywords[] = {"follow_symlinks", NULL}; |
| 11062 | |
| 11063 | typedef struct { |
| 11064 | PyObject_HEAD |
| 11065 | PyObject *name; |
| 11066 | PyObject *path; |
| 11067 | PyObject *stat; |
| 11068 | PyObject *lstat; |
| 11069 | #ifdef MS_WINDOWS |
| 11070 | struct _Py_stat_struct win32_lstat; |
| 11071 | __int64 win32_file_index; |
| 11072 | int got_file_index; |
| 11073 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11074 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11075 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11076 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11077 | ino_t d_ino; |
| 11078 | #endif |
| 11079 | } DirEntry; |
| 11080 | |
| 11081 | static void |
| 11082 | DirEntry_dealloc(DirEntry *entry) |
| 11083 | { |
| 11084 | Py_XDECREF(entry->name); |
| 11085 | Py_XDECREF(entry->path); |
| 11086 | Py_XDECREF(entry->stat); |
| 11087 | Py_XDECREF(entry->lstat); |
| 11088 | Py_TYPE(entry)->tp_free((PyObject *)entry); |
| 11089 | } |
| 11090 | |
| 11091 | /* Forward reference */ |
| 11092 | static int |
| 11093 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits); |
| 11094 | |
| 11095 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 11096 | static int |
| 11097 | DirEntry_is_symlink(DirEntry *self) |
| 11098 | { |
| 11099 | #ifdef MS_WINDOWS |
| 11100 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11101 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 11102 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11103 | if (self->d_type != DT_UNKNOWN) |
| 11104 | return self->d_type == DT_LNK; |
| 11105 | else |
| 11106 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11107 | #else |
| 11108 | /* POSIX without d_type */ |
| 11109 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11110 | #endif |
| 11111 | } |
| 11112 | |
| 11113 | static PyObject * |
| 11114 | DirEntry_py_is_symlink(DirEntry *self) |
| 11115 | { |
| 11116 | int result; |
| 11117 | |
| 11118 | result = DirEntry_is_symlink(self); |
| 11119 | if (result == -1) |
| 11120 | return NULL; |
| 11121 | return PyBool_FromLong(result); |
| 11122 | } |
| 11123 | |
| 11124 | static PyObject * |
| 11125 | DirEntry_fetch_stat(DirEntry *self, int follow_symlinks) |
| 11126 | { |
| 11127 | int result; |
| 11128 | struct _Py_stat_struct st; |
| 11129 | |
| 11130 | #ifdef MS_WINDOWS |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11131 | const wchar_t *path; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11132 | |
| 11133 | path = PyUnicode_AsUnicode(self->path); |
| 11134 | if (!path) |
| 11135 | return NULL; |
| 11136 | |
| 11137 | if (follow_symlinks) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11138 | result = win32_stat(path, &st); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11139 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11140 | result = win32_lstat(path, &st); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11141 | |
| 11142 | if (result != 0) { |
| 11143 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 11144 | 0, self->path); |
| 11145 | } |
| 11146 | #else /* POSIX */ |
| 11147 | PyObject *bytes; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11148 | const char *path; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11149 | |
| 11150 | if (!PyUnicode_FSConverter(self->path, &bytes)) |
| 11151 | return NULL; |
| 11152 | path = PyBytes_AS_STRING(bytes); |
| 11153 | |
| 11154 | if (follow_symlinks) |
| 11155 | result = STAT(path, &st); |
| 11156 | else |
| 11157 | result = LSTAT(path, &st); |
| 11158 | Py_DECREF(bytes); |
| 11159 | |
| 11160 | if (result != 0) |
| 11161 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, self->path); |
| 11162 | #endif |
| 11163 | |
| 11164 | return _pystat_fromstructstat(&st); |
| 11165 | } |
| 11166 | |
| 11167 | static PyObject * |
| 11168 | DirEntry_get_lstat(DirEntry *self) |
| 11169 | { |
| 11170 | if (!self->lstat) { |
| 11171 | #ifdef MS_WINDOWS |
| 11172 | self->lstat = _pystat_fromstructstat(&self->win32_lstat); |
| 11173 | #else /* POSIX */ |
| 11174 | self->lstat = DirEntry_fetch_stat(self, 0); |
| 11175 | #endif |
| 11176 | } |
| 11177 | Py_XINCREF(self->lstat); |
| 11178 | return self->lstat; |
| 11179 | } |
| 11180 | |
| 11181 | static PyObject * |
| 11182 | DirEntry_get_stat(DirEntry *self, int follow_symlinks) |
| 11183 | { |
| 11184 | if (!follow_symlinks) |
| 11185 | return DirEntry_get_lstat(self); |
| 11186 | |
| 11187 | if (!self->stat) { |
| 11188 | int result = DirEntry_is_symlink(self); |
| 11189 | if (result == -1) |
| 11190 | return NULL; |
| 11191 | else if (result) |
| 11192 | self->stat = DirEntry_fetch_stat(self, 1); |
| 11193 | else |
| 11194 | self->stat = DirEntry_get_lstat(self); |
| 11195 | } |
| 11196 | |
| 11197 | Py_XINCREF(self->stat); |
| 11198 | return self->stat; |
| 11199 | } |
| 11200 | |
| 11201 | static PyObject * |
| 11202 | DirEntry_stat(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 11203 | { |
| 11204 | int follow_symlinks = 1; |
| 11205 | |
| 11206 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.stat", |
| 11207 | follow_symlinks_keywords, &follow_symlinks)) |
| 11208 | return NULL; |
| 11209 | |
| 11210 | return DirEntry_get_stat(self, follow_symlinks); |
| 11211 | } |
| 11212 | |
| 11213 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 11214 | static int |
| 11215 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 11216 | { |
| 11217 | PyObject *stat = NULL; |
| 11218 | PyObject *st_mode = NULL; |
| 11219 | long mode; |
| 11220 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11221 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11222 | int is_symlink; |
| 11223 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11224 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11225 | #ifdef MS_WINDOWS |
| 11226 | unsigned long dir_bits; |
| 11227 | #endif |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11228 | _Py_IDENTIFIER(st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11229 | |
| 11230 | #ifdef MS_WINDOWS |
| 11231 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 11232 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11233 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11234 | is_symlink = self->d_type == DT_LNK; |
| 11235 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 11236 | #endif |
| 11237 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11238 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11239 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11240 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11241 | stat = DirEntry_get_stat(self, follow_symlinks); |
| 11242 | if (!stat) { |
| 11243 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 11244 | /* If file doesn't exist (anymore), then return False |
| 11245 | (i.e., say it's not a file/directory) */ |
| 11246 | PyErr_Clear(); |
| 11247 | return 0; |
| 11248 | } |
| 11249 | goto error; |
| 11250 | } |
| 11251 | st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode); |
| 11252 | if (!st_mode) |
| 11253 | goto error; |
| 11254 | |
| 11255 | mode = PyLong_AsLong(st_mode); |
| 11256 | if (mode == -1 && PyErr_Occurred()) |
| 11257 | goto error; |
| 11258 | Py_CLEAR(st_mode); |
| 11259 | Py_CLEAR(stat); |
| 11260 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11261 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11262 | } |
| 11263 | else if (is_symlink) { |
| 11264 | assert(mode_bits != S_IFLNK); |
| 11265 | result = 0; |
| 11266 | } |
| 11267 | else { |
| 11268 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 11269 | #ifdef MS_WINDOWS |
| 11270 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 11271 | if (mode_bits == S_IFDIR) |
| 11272 | result = dir_bits != 0; |
| 11273 | else |
| 11274 | result = dir_bits == 0; |
| 11275 | #else /* POSIX */ |
| 11276 | if (mode_bits == S_IFDIR) |
| 11277 | result = self->d_type == DT_DIR; |
| 11278 | else |
| 11279 | result = self->d_type == DT_REG; |
| 11280 | #endif |
| 11281 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11282 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11283 | |
| 11284 | return result; |
| 11285 | |
| 11286 | error: |
| 11287 | Py_XDECREF(st_mode); |
| 11288 | Py_XDECREF(stat); |
| 11289 | return -1; |
| 11290 | } |
| 11291 | |
| 11292 | static PyObject * |
| 11293 | DirEntry_py_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 11294 | { |
| 11295 | int result; |
| 11296 | |
| 11297 | result = DirEntry_test_mode(self, follow_symlinks, mode_bits); |
| 11298 | if (result == -1) |
| 11299 | return NULL; |
| 11300 | return PyBool_FromLong(result); |
| 11301 | } |
| 11302 | |
| 11303 | static PyObject * |
| 11304 | DirEntry_is_dir(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 11305 | { |
| 11306 | int follow_symlinks = 1; |
| 11307 | |
| 11308 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_dir", |
| 11309 | follow_symlinks_keywords, &follow_symlinks)) |
| 11310 | return NULL; |
| 11311 | |
| 11312 | return DirEntry_py_test_mode(self, follow_symlinks, S_IFDIR); |
| 11313 | } |
| 11314 | |
| 11315 | static PyObject * |
| 11316 | DirEntry_is_file(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 11317 | { |
| 11318 | int follow_symlinks = 1; |
| 11319 | |
| 11320 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_file", |
| 11321 | follow_symlinks_keywords, &follow_symlinks)) |
| 11322 | return NULL; |
| 11323 | |
| 11324 | return DirEntry_py_test_mode(self, follow_symlinks, S_IFREG); |
| 11325 | } |
| 11326 | |
| 11327 | static PyObject * |
| 11328 | DirEntry_inode(DirEntry *self) |
| 11329 | { |
| 11330 | #ifdef MS_WINDOWS |
| 11331 | if (!self->got_file_index) { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11332 | const wchar_t *path; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11333 | struct _Py_stat_struct stat; |
| 11334 | |
| 11335 | path = PyUnicode_AsUnicode(self->path); |
| 11336 | if (!path) |
| 11337 | return NULL; |
| 11338 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11339 | if (win32_lstat(path, &stat) != 0) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11340 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 11341 | 0, self->path); |
| 11342 | } |
| 11343 | |
| 11344 | self->win32_file_index = stat.st_ino; |
| 11345 | self->got_file_index = 1; |
| 11346 | } |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 11347 | return PyLong_FromLongLong((long long)self->win32_file_index); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11348 | #else /* POSIX */ |
| 11349 | #ifdef HAVE_LARGEFILE_SUPPORT |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 11350 | return PyLong_FromLongLong((long long)self->d_ino); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11351 | #else |
| 11352 | return PyLong_FromLong((long)self->d_ino); |
| 11353 | #endif |
| 11354 | #endif |
| 11355 | } |
| 11356 | |
| 11357 | static PyObject * |
| 11358 | DirEntry_repr(DirEntry *self) |
| 11359 | { |
| 11360 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 11361 | } |
| 11362 | |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 11363 | static PyObject * |
| 11364 | DirEntry_fspath(DirEntry *self) |
| 11365 | { |
| 11366 | Py_INCREF(self->path); |
| 11367 | return self->path; |
| 11368 | } |
| 11369 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11370 | static PyMemberDef DirEntry_members[] = { |
| 11371 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 11372 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 11373 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 11374 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 11375 | {NULL} |
| 11376 | }; |
| 11377 | |
| 11378 | static PyMethodDef DirEntry_methods[] = { |
| 11379 | {"is_dir", (PyCFunction)DirEntry_is_dir, METH_VARARGS | METH_KEYWORDS, |
| 11380 | "return True if the entry is a directory; cached per entry" |
| 11381 | }, |
| 11382 | {"is_file", (PyCFunction)DirEntry_is_file, METH_VARARGS | METH_KEYWORDS, |
| 11383 | "return True if the entry is a file; cached per entry" |
| 11384 | }, |
| 11385 | {"is_symlink", (PyCFunction)DirEntry_py_is_symlink, METH_NOARGS, |
| 11386 | "return True if the entry is a symbolic link; cached per entry" |
| 11387 | }, |
| 11388 | {"stat", (PyCFunction)DirEntry_stat, METH_VARARGS | METH_KEYWORDS, |
| 11389 | "return stat_result object for the entry; cached per entry" |
| 11390 | }, |
| 11391 | {"inode", (PyCFunction)DirEntry_inode, METH_NOARGS, |
| 11392 | "return inode of the entry; cached per entry", |
| 11393 | }, |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 11394 | {"__fspath__", (PyCFunction)DirEntry_fspath, METH_NOARGS, |
| 11395 | "returns the path for the entry", |
| 11396 | }, |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11397 | {NULL} |
| 11398 | }; |
| 11399 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 11400 | static PyTypeObject DirEntryType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11401 | PyVarObject_HEAD_INIT(NULL, 0) |
| 11402 | MODNAME ".DirEntry", /* tp_name */ |
| 11403 | sizeof(DirEntry), /* tp_basicsize */ |
| 11404 | 0, /* tp_itemsize */ |
| 11405 | /* methods */ |
| 11406 | (destructor)DirEntry_dealloc, /* tp_dealloc */ |
| 11407 | 0, /* tp_print */ |
| 11408 | 0, /* tp_getattr */ |
| 11409 | 0, /* tp_setattr */ |
| 11410 | 0, /* tp_compare */ |
| 11411 | (reprfunc)DirEntry_repr, /* tp_repr */ |
| 11412 | 0, /* tp_as_number */ |
| 11413 | 0, /* tp_as_sequence */ |
| 11414 | 0, /* tp_as_mapping */ |
| 11415 | 0, /* tp_hash */ |
| 11416 | 0, /* tp_call */ |
| 11417 | 0, /* tp_str */ |
| 11418 | 0, /* tp_getattro */ |
| 11419 | 0, /* tp_setattro */ |
| 11420 | 0, /* tp_as_buffer */ |
| 11421 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 11422 | 0, /* tp_doc */ |
| 11423 | 0, /* tp_traverse */ |
| 11424 | 0, /* tp_clear */ |
| 11425 | 0, /* tp_richcompare */ |
| 11426 | 0, /* tp_weaklistoffset */ |
| 11427 | 0, /* tp_iter */ |
| 11428 | 0, /* tp_iternext */ |
| 11429 | DirEntry_methods, /* tp_methods */ |
| 11430 | DirEntry_members, /* tp_members */ |
| 11431 | }; |
| 11432 | |
| 11433 | #ifdef MS_WINDOWS |
| 11434 | |
| 11435 | static wchar_t * |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11436 | join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11437 | { |
| 11438 | Py_ssize_t path_len; |
| 11439 | Py_ssize_t size; |
| 11440 | wchar_t *result; |
| 11441 | wchar_t ch; |
| 11442 | |
| 11443 | if (!path_wide) { /* Default arg: "." */ |
| 11444 | path_wide = L"."; |
| 11445 | path_len = 1; |
| 11446 | } |
| 11447 | else { |
| 11448 | path_len = wcslen(path_wide); |
| 11449 | } |
| 11450 | |
| 11451 | /* The +1's are for the path separator and the NUL */ |
| 11452 | size = path_len + 1 + wcslen(filename) + 1; |
| 11453 | result = PyMem_New(wchar_t, size); |
| 11454 | if (!result) { |
| 11455 | PyErr_NoMemory(); |
| 11456 | return NULL; |
| 11457 | } |
| 11458 | wcscpy(result, path_wide); |
| 11459 | if (path_len > 0) { |
| 11460 | ch = result[path_len - 1]; |
| 11461 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 11462 | result[path_len++] = SEP; |
| 11463 | wcscpy(result + path_len, filename); |
| 11464 | } |
| 11465 | return result; |
| 11466 | } |
| 11467 | |
| 11468 | static PyObject * |
| 11469 | DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW) |
| 11470 | { |
| 11471 | DirEntry *entry; |
| 11472 | BY_HANDLE_FILE_INFORMATION file_info; |
| 11473 | ULONG reparse_tag; |
| 11474 | wchar_t *joined_path; |
| 11475 | |
| 11476 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 11477 | if (!entry) |
| 11478 | return NULL; |
| 11479 | entry->name = NULL; |
| 11480 | entry->path = NULL; |
| 11481 | entry->stat = NULL; |
| 11482 | entry->lstat = NULL; |
| 11483 | entry->got_file_index = 0; |
| 11484 | |
| 11485 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 11486 | if (!entry->name) |
| 11487 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11488 | if (path->narrow) { |
| 11489 | Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name)); |
| 11490 | if (!entry->name) |
| 11491 | goto error; |
| 11492 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11493 | |
| 11494 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 11495 | if (!joined_path) |
| 11496 | goto error; |
| 11497 | |
| 11498 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 11499 | PyMem_Free(joined_path); |
| 11500 | if (!entry->path) |
| 11501 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11502 | if (path->narrow) { |
| 11503 | Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path)); |
| 11504 | if (!entry->path) |
| 11505 | goto error; |
| 11506 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11507 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11508 | find_data_to_file_info(dataW, &file_info, &reparse_tag); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11509 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 11510 | |
| 11511 | return (PyObject *)entry; |
| 11512 | |
| 11513 | error: |
| 11514 | Py_DECREF(entry); |
| 11515 | return NULL; |
| 11516 | } |
| 11517 | |
| 11518 | #else /* POSIX */ |
| 11519 | |
| 11520 | static char * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 11521 | 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] | 11522 | { |
| 11523 | Py_ssize_t path_len; |
| 11524 | Py_ssize_t size; |
| 11525 | char *result; |
| 11526 | |
| 11527 | if (!path_narrow) { /* Default arg: "." */ |
| 11528 | path_narrow = "."; |
| 11529 | path_len = 1; |
| 11530 | } |
| 11531 | else { |
| 11532 | path_len = strlen(path_narrow); |
| 11533 | } |
| 11534 | |
| 11535 | if (filename_len == -1) |
| 11536 | filename_len = strlen(filename); |
| 11537 | |
| 11538 | /* The +1's are for the path separator and the NUL */ |
| 11539 | size = path_len + 1 + filename_len + 1; |
| 11540 | result = PyMem_New(char, size); |
| 11541 | if (!result) { |
| 11542 | PyErr_NoMemory(); |
| 11543 | return NULL; |
| 11544 | } |
| 11545 | strcpy(result, path_narrow); |
| 11546 | if (path_len > 0 && result[path_len - 1] != '/') |
| 11547 | result[path_len++] = '/'; |
| 11548 | strcpy(result + path_len, filename); |
| 11549 | return result; |
| 11550 | } |
| 11551 | |
| 11552 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 11553 | 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] | 11554 | ino_t d_ino |
| 11555 | #ifdef HAVE_DIRENT_D_TYPE |
| 11556 | , unsigned char d_type |
| 11557 | #endif |
| 11558 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11559 | { |
| 11560 | DirEntry *entry; |
| 11561 | char *joined_path; |
| 11562 | |
| 11563 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 11564 | if (!entry) |
| 11565 | return NULL; |
| 11566 | entry->name = NULL; |
| 11567 | entry->path = NULL; |
| 11568 | entry->stat = NULL; |
| 11569 | entry->lstat = NULL; |
| 11570 | |
| 11571 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 11572 | if (!joined_path) |
| 11573 | goto error; |
| 11574 | |
| 11575 | if (!path->narrow || !PyBytes_Check(path->object)) { |
| 11576 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
| 11577 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
| 11578 | } |
| 11579 | else { |
| 11580 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
| 11581 | entry->path = PyBytes_FromString(joined_path); |
| 11582 | } |
| 11583 | PyMem_Free(joined_path); |
| 11584 | if (!entry->name || !entry->path) |
| 11585 | goto error; |
| 11586 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11587 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11588 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11589 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11590 | entry->d_ino = d_ino; |
| 11591 | |
| 11592 | return (PyObject *)entry; |
| 11593 | |
| 11594 | error: |
| 11595 | Py_XDECREF(entry); |
| 11596 | return NULL; |
| 11597 | } |
| 11598 | |
| 11599 | #endif |
| 11600 | |
| 11601 | |
| 11602 | typedef struct { |
| 11603 | PyObject_HEAD |
| 11604 | path_t path; |
| 11605 | #ifdef MS_WINDOWS |
| 11606 | HANDLE handle; |
| 11607 | WIN32_FIND_DATAW file_data; |
| 11608 | int first_time; |
| 11609 | #else /* POSIX */ |
| 11610 | DIR *dirp; |
| 11611 | #endif |
| 11612 | } ScandirIterator; |
| 11613 | |
| 11614 | #ifdef MS_WINDOWS |
| 11615 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11616 | static int |
| 11617 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 11618 | { |
| 11619 | return iterator->handle == INVALID_HANDLE_VALUE; |
| 11620 | } |
| 11621 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11622 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11623 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11624 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 11625 | HANDLE handle = iterator->handle; |
| 11626 | |
| 11627 | if (handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11628 | return; |
| 11629 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11630 | iterator->handle = INVALID_HANDLE_VALUE; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 11631 | Py_BEGIN_ALLOW_THREADS |
| 11632 | FindClose(handle); |
| 11633 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11634 | } |
| 11635 | |
| 11636 | static PyObject * |
| 11637 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 11638 | { |
| 11639 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 11640 | BOOL success; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11641 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11642 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11643 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11644 | if (iterator->handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11645 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11646 | |
| 11647 | while (1) { |
| 11648 | if (!iterator->first_time) { |
| 11649 | Py_BEGIN_ALLOW_THREADS |
| 11650 | success = FindNextFileW(iterator->handle, file_data); |
| 11651 | Py_END_ALLOW_THREADS |
| 11652 | if (!success) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11653 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11654 | if (GetLastError() != ERROR_NO_MORE_FILES) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11655 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11656 | break; |
| 11657 | } |
| 11658 | } |
| 11659 | iterator->first_time = 0; |
| 11660 | |
| 11661 | /* Skip over . and .. */ |
| 11662 | if (wcscmp(file_data->cFileName, L".") != 0 && |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11663 | wcscmp(file_data->cFileName, L"..") != 0) { |
| 11664 | entry = DirEntry_from_find_data(&iterator->path, file_data); |
| 11665 | if (!entry) |
| 11666 | break; |
| 11667 | return entry; |
| 11668 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11669 | |
| 11670 | /* Loop till we get a non-dot directory or finish iterating */ |
| 11671 | } |
| 11672 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11673 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11674 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11675 | return NULL; |
| 11676 | } |
| 11677 | |
| 11678 | #else /* POSIX */ |
| 11679 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11680 | static int |
| 11681 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 11682 | { |
| 11683 | return !iterator->dirp; |
| 11684 | } |
| 11685 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11686 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11687 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11688 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 11689 | DIR *dirp = iterator->dirp; |
| 11690 | |
| 11691 | if (!dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11692 | return; |
| 11693 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11694 | iterator->dirp = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 11695 | Py_BEGIN_ALLOW_THREADS |
| 11696 | closedir(dirp); |
| 11697 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11698 | return; |
| 11699 | } |
| 11700 | |
| 11701 | static PyObject * |
| 11702 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 11703 | { |
| 11704 | struct dirent *direntp; |
| 11705 | Py_ssize_t name_len; |
| 11706 | int is_dot; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11707 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11708 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11709 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11710 | if (!iterator->dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11711 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11712 | |
| 11713 | while (1) { |
| 11714 | errno = 0; |
| 11715 | Py_BEGIN_ALLOW_THREADS |
| 11716 | direntp = readdir(iterator->dirp); |
| 11717 | Py_END_ALLOW_THREADS |
| 11718 | |
| 11719 | if (!direntp) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11720 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11721 | if (errno != 0) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11722 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11723 | break; |
| 11724 | } |
| 11725 | |
| 11726 | /* Skip over . and .. */ |
| 11727 | name_len = NAMLEN(direntp); |
| 11728 | is_dot = direntp->d_name[0] == '.' && |
| 11729 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 11730 | if (!is_dot) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11731 | entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11732 | name_len, direntp->d_ino |
| 11733 | #ifdef HAVE_DIRENT_D_TYPE |
| 11734 | , direntp->d_type |
| 11735 | #endif |
| 11736 | ); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11737 | if (!entry) |
| 11738 | break; |
| 11739 | return entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11740 | } |
| 11741 | |
| 11742 | /* Loop till we get a non-dot directory or finish iterating */ |
| 11743 | } |
| 11744 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11745 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11746 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11747 | return NULL; |
| 11748 | } |
| 11749 | |
| 11750 | #endif |
| 11751 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11752 | static PyObject * |
| 11753 | ScandirIterator_close(ScandirIterator *self, PyObject *args) |
| 11754 | { |
| 11755 | ScandirIterator_closedir(self); |
| 11756 | Py_RETURN_NONE; |
| 11757 | } |
| 11758 | |
| 11759 | static PyObject * |
| 11760 | ScandirIterator_enter(PyObject *self, PyObject *args) |
| 11761 | { |
| 11762 | Py_INCREF(self); |
| 11763 | return self; |
| 11764 | } |
| 11765 | |
| 11766 | static PyObject * |
| 11767 | ScandirIterator_exit(ScandirIterator *self, PyObject *args) |
| 11768 | { |
| 11769 | ScandirIterator_closedir(self); |
| 11770 | Py_RETURN_NONE; |
| 11771 | } |
| 11772 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11773 | static void |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 11774 | ScandirIterator_finalize(ScandirIterator *iterator) |
| 11775 | { |
| 11776 | PyObject *error_type, *error_value, *error_traceback; |
| 11777 | |
| 11778 | /* Save the current exception, if any. */ |
| 11779 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 11780 | |
| 11781 | if (!ScandirIterator_is_closed(iterator)) { |
| 11782 | ScandirIterator_closedir(iterator); |
| 11783 | |
| 11784 | if (PyErr_ResourceWarning((PyObject *)iterator, 1, |
| 11785 | "unclosed scandir iterator %R", iterator)) { |
| 11786 | /* Spurious errors can appear at shutdown */ |
| 11787 | if (PyErr_ExceptionMatches(PyExc_Warning)) { |
| 11788 | PyErr_WriteUnraisable((PyObject *) iterator); |
| 11789 | } |
| 11790 | } |
| 11791 | } |
| 11792 | |
| 11793 | Py_CLEAR(iterator->path.object); |
| 11794 | path_cleanup(&iterator->path); |
| 11795 | |
| 11796 | /* Restore the saved exception. */ |
| 11797 | PyErr_Restore(error_type, error_value, error_traceback); |
| 11798 | } |
| 11799 | |
| 11800 | static void |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11801 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 11802 | { |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 11803 | if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0) |
| 11804 | return; |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11805 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11806 | Py_TYPE(iterator)->tp_free((PyObject *)iterator); |
| 11807 | } |
| 11808 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11809 | static PyMethodDef ScandirIterator_methods[] = { |
| 11810 | {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS}, |
| 11811 | {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS}, |
| 11812 | {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS}, |
| 11813 | {NULL} |
| 11814 | }; |
| 11815 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 11816 | static PyTypeObject ScandirIteratorType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11817 | PyVarObject_HEAD_INIT(NULL, 0) |
| 11818 | MODNAME ".ScandirIterator", /* tp_name */ |
| 11819 | sizeof(ScandirIterator), /* tp_basicsize */ |
| 11820 | 0, /* tp_itemsize */ |
| 11821 | /* methods */ |
| 11822 | (destructor)ScandirIterator_dealloc, /* tp_dealloc */ |
| 11823 | 0, /* tp_print */ |
| 11824 | 0, /* tp_getattr */ |
| 11825 | 0, /* tp_setattr */ |
| 11826 | 0, /* tp_compare */ |
| 11827 | 0, /* tp_repr */ |
| 11828 | 0, /* tp_as_number */ |
| 11829 | 0, /* tp_as_sequence */ |
| 11830 | 0, /* tp_as_mapping */ |
| 11831 | 0, /* tp_hash */ |
| 11832 | 0, /* tp_call */ |
| 11833 | 0, /* tp_str */ |
| 11834 | 0, /* tp_getattro */ |
| 11835 | 0, /* tp_setattro */ |
| 11836 | 0, /* tp_as_buffer */ |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 11837 | Py_TPFLAGS_DEFAULT |
| 11838 | | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11839 | 0, /* tp_doc */ |
| 11840 | 0, /* tp_traverse */ |
| 11841 | 0, /* tp_clear */ |
| 11842 | 0, /* tp_richcompare */ |
| 11843 | 0, /* tp_weaklistoffset */ |
| 11844 | PyObject_SelfIter, /* tp_iter */ |
| 11845 | (iternextfunc)ScandirIterator_iternext, /* tp_iternext */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 11846 | ScandirIterator_methods, /* tp_methods */ |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 11847 | 0, /* tp_members */ |
| 11848 | 0, /* tp_getset */ |
| 11849 | 0, /* tp_base */ |
| 11850 | 0, /* tp_dict */ |
| 11851 | 0, /* tp_descr_get */ |
| 11852 | 0, /* tp_descr_set */ |
| 11853 | 0, /* tp_dictoffset */ |
| 11854 | 0, /* tp_init */ |
| 11855 | 0, /* tp_alloc */ |
| 11856 | 0, /* tp_new */ |
| 11857 | 0, /* tp_free */ |
| 11858 | 0, /* tp_is_gc */ |
| 11859 | 0, /* tp_bases */ |
| 11860 | 0, /* tp_mro */ |
| 11861 | 0, /* tp_cache */ |
| 11862 | 0, /* tp_subclasses */ |
| 11863 | 0, /* tp_weaklist */ |
| 11864 | 0, /* tp_del */ |
| 11865 | 0, /* tp_version_tag */ |
| 11866 | (destructor)ScandirIterator_finalize, /* tp_finalize */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11867 | }; |
| 11868 | |
| 11869 | static PyObject * |
| 11870 | posix_scandir(PyObject *self, PyObject *args, PyObject *kwargs) |
| 11871 | { |
| 11872 | ScandirIterator *iterator; |
| 11873 | static char *keywords[] = {"path", NULL}; |
| 11874 | #ifdef MS_WINDOWS |
| 11875 | wchar_t *path_strW; |
| 11876 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11877 | const char *path; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11878 | #endif |
| 11879 | |
| 11880 | iterator = PyObject_New(ScandirIterator, &ScandirIteratorType); |
| 11881 | if (!iterator) |
| 11882 | return NULL; |
| 11883 | memset(&iterator->path, 0, sizeof(path_t)); |
| 11884 | iterator->path.function_name = "scandir"; |
| 11885 | iterator->path.nullable = 1; |
| 11886 | |
| 11887 | #ifdef MS_WINDOWS |
| 11888 | iterator->handle = INVALID_HANDLE_VALUE; |
| 11889 | #else |
| 11890 | iterator->dirp = NULL; |
| 11891 | #endif |
| 11892 | |
| 11893 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:scandir", keywords, |
| 11894 | path_converter, &iterator->path)) |
| 11895 | goto error; |
| 11896 | |
| 11897 | /* path_converter doesn't keep path.object around, so do it |
| 11898 | manually for the lifetime of the iterator here (the refcount |
| 11899 | is decremented in ScandirIterator_dealloc) |
| 11900 | */ |
| 11901 | Py_XINCREF(iterator->path.object); |
| 11902 | |
| 11903 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11904 | iterator->first_time = 1; |
| 11905 | |
| 11906 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 11907 | if (!path_strW) |
| 11908 | goto error; |
| 11909 | |
| 11910 | Py_BEGIN_ALLOW_THREADS |
| 11911 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 11912 | Py_END_ALLOW_THREADS |
| 11913 | |
| 11914 | PyMem_Free(path_strW); |
| 11915 | |
| 11916 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 11917 | path_error(&iterator->path); |
| 11918 | goto error; |
| 11919 | } |
| 11920 | #else /* POSIX */ |
| 11921 | if (iterator->path.narrow) |
| 11922 | path = iterator->path.narrow; |
| 11923 | else |
| 11924 | path = "."; |
| 11925 | |
| 11926 | errno = 0; |
| 11927 | Py_BEGIN_ALLOW_THREADS |
| 11928 | iterator->dirp = opendir(path); |
| 11929 | Py_END_ALLOW_THREADS |
| 11930 | |
| 11931 | if (!iterator->dirp) { |
| 11932 | path_error(&iterator->path); |
| 11933 | goto error; |
| 11934 | } |
| 11935 | #endif |
| 11936 | |
| 11937 | return (PyObject *)iterator; |
| 11938 | |
| 11939 | error: |
| 11940 | Py_DECREF(iterator); |
| 11941 | return NULL; |
| 11942 | } |
| 11943 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 11944 | /* |
| 11945 | Return the file system path representation of the object. |
| 11946 | |
| 11947 | If the object is str or bytes, then allow it to pass through with |
| 11948 | an incremented refcount. If the object defines __fspath__(), then |
| 11949 | return the result of that method. All other types raise a TypeError. |
| 11950 | */ |
| 11951 | PyObject * |
| 11952 | PyOS_FSPath(PyObject *path) |
| 11953 | { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 11954 | /* For error message reasons, this function is manually inlined in |
| 11955 | path_converter(). */ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 11956 | _Py_IDENTIFIER(__fspath__); |
| 11957 | PyObject *func = NULL; |
| 11958 | PyObject *path_repr = NULL; |
| 11959 | |
| 11960 | if (PyUnicode_Check(path) || PyBytes_Check(path)) { |
| 11961 | Py_INCREF(path); |
| 11962 | return path; |
| 11963 | } |
| 11964 | |
| 11965 | func = _PyObject_LookupSpecial(path, &PyId___fspath__); |
| 11966 | if (NULL == func) { |
| 11967 | return PyErr_Format(PyExc_TypeError, |
| 11968 | "expected str, bytes or os.PathLike object, " |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 11969 | "not %.200s", |
| 11970 | Py_TYPE(path)->tp_name); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 11971 | } |
| 11972 | |
| 11973 | path_repr = PyObject_CallFunctionObjArgs(func, NULL); |
| 11974 | Py_DECREF(func); |
Brett Cannon | 044283a | 2016-07-15 10:41:49 -0700 | [diff] [blame] | 11975 | if (NULL == path_repr) { |
| 11976 | return NULL; |
| 11977 | } |
| 11978 | |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 11979 | if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) { |
| 11980 | PyErr_Format(PyExc_TypeError, |
| 11981 | "expected %.200s.__fspath__() to return str or bytes, " |
| 11982 | "not %.200s", Py_TYPE(path)->tp_name, |
| 11983 | Py_TYPE(path_repr)->tp_name); |
| 11984 | Py_DECREF(path_repr); |
| 11985 | return NULL; |
| 11986 | } |
| 11987 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 11988 | return path_repr; |
| 11989 | } |
| 11990 | |
| 11991 | /*[clinic input] |
| 11992 | os.fspath |
| 11993 | |
| 11994 | path: object |
| 11995 | |
| 11996 | Return the file system path representation of the object. |
| 11997 | |
Brett Cannon | b4f43e9 | 2016-06-09 14:32:08 -0700 | [diff] [blame] | 11998 | If the object is str or bytes, then allow it to pass through as-is. If the |
| 11999 | object defines __fspath__(), then return the result of that method. All other |
| 12000 | types raise a TypeError. |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12001 | [clinic start generated code]*/ |
| 12002 | |
| 12003 | static PyObject * |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 12004 | os_fspath_impl(PyObject *module, PyObject *path) |
| 12005 | /*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12006 | { |
| 12007 | return PyOS_FSPath(path); |
| 12008 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12009 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12010 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 12011 | /*[clinic input] |
| 12012 | os.getrandom |
| 12013 | |
| 12014 | size: Py_ssize_t |
| 12015 | flags: int=0 |
| 12016 | |
| 12017 | Obtain a series of random bytes. |
| 12018 | [clinic start generated code]*/ |
| 12019 | |
| 12020 | static PyObject * |
| 12021 | os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags) |
| 12022 | /*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/ |
| 12023 | { |
| 12024 | char *buffer; |
| 12025 | Py_ssize_t n; |
| 12026 | PyObject *bytes; |
| 12027 | |
| 12028 | if (size < 0) { |
| 12029 | errno = EINVAL; |
| 12030 | return posix_error(); |
| 12031 | } |
| 12032 | |
| 12033 | buffer = PyMem_Malloc(size); |
| 12034 | if (buffer == NULL) { |
| 12035 | PyErr_NoMemory(); |
| 12036 | return NULL; |
| 12037 | } |
| 12038 | |
| 12039 | while (1) { |
| 12040 | n = syscall(SYS_getrandom, buffer, size, flags); |
| 12041 | if (n < 0 && errno == EINTR) { |
| 12042 | if (PyErr_CheckSignals() < 0) { |
| 12043 | return NULL; |
| 12044 | } |
| 12045 | continue; |
| 12046 | } |
| 12047 | break; |
| 12048 | } |
| 12049 | |
| 12050 | if (n < 0) { |
| 12051 | PyMem_Free(buffer); |
| 12052 | PyErr_SetFromErrno(PyExc_OSError); |
| 12053 | return NULL; |
| 12054 | } |
| 12055 | |
| 12056 | bytes = PyBytes_FromStringAndSize(buffer, n); |
| 12057 | PyMem_Free(buffer); |
| 12058 | |
| 12059 | return bytes; |
| 12060 | } |
| 12061 | #endif /* HAVE_GETRANDOM_SYSCALL */ |
| 12062 | |
Serhiy Storchaka | a4c6bad | 2015-04-04 23:35:52 +0300 | [diff] [blame] | 12063 | #include "clinic/posixmodule.c.h" |
| 12064 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 12065 | /*[clinic input] |
| 12066 | dump buffer |
| 12067 | [clinic start generated code]*/ |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 12068 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/ |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 12069 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12070 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12071 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12072 | |
| 12073 | OS_STAT_METHODDEF |
| 12074 | OS_ACCESS_METHODDEF |
| 12075 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12076 | OS_CHDIR_METHODDEF |
| 12077 | OS_CHFLAGS_METHODDEF |
| 12078 | OS_CHMOD_METHODDEF |
| 12079 | OS_FCHMOD_METHODDEF |
| 12080 | OS_LCHMOD_METHODDEF |
| 12081 | OS_CHOWN_METHODDEF |
| 12082 | OS_FCHOWN_METHODDEF |
| 12083 | OS_LCHOWN_METHODDEF |
| 12084 | OS_LCHFLAGS_METHODDEF |
| 12085 | OS_CHROOT_METHODDEF |
| 12086 | OS_CTERMID_METHODDEF |
| 12087 | OS_GETCWD_METHODDEF |
| 12088 | OS_GETCWDB_METHODDEF |
| 12089 | OS_LINK_METHODDEF |
| 12090 | OS_LISTDIR_METHODDEF |
| 12091 | OS_LSTAT_METHODDEF |
| 12092 | OS_MKDIR_METHODDEF |
| 12093 | OS_NICE_METHODDEF |
| 12094 | OS_GETPRIORITY_METHODDEF |
| 12095 | OS_SETPRIORITY_METHODDEF |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12096 | #ifdef HAVE_READLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12097 | {"readlink", (PyCFunction)posix_readlink, |
| 12098 | METH_VARARGS | METH_KEYWORDS, |
| 12099 | readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 12100 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 12101 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12102 | {"readlink", (PyCFunction)win_readlink, |
| 12103 | METH_VARARGS | METH_KEYWORDS, |
| 12104 | readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 12105 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12106 | OS_RENAME_METHODDEF |
| 12107 | OS_REPLACE_METHODDEF |
| 12108 | OS_RMDIR_METHODDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12109 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12110 | OS_SYMLINK_METHODDEF |
| 12111 | OS_SYSTEM_METHODDEF |
| 12112 | OS_UMASK_METHODDEF |
| 12113 | OS_UNAME_METHODDEF |
| 12114 | OS_UNLINK_METHODDEF |
| 12115 | OS_REMOVE_METHODDEF |
| 12116 | OS_UTIME_METHODDEF |
| 12117 | OS_TIMES_METHODDEF |
| 12118 | OS__EXIT_METHODDEF |
| 12119 | OS_EXECV_METHODDEF |
| 12120 | OS_EXECVE_METHODDEF |
| 12121 | OS_SPAWNV_METHODDEF |
| 12122 | OS_SPAWNVE_METHODDEF |
| 12123 | OS_FORK1_METHODDEF |
| 12124 | OS_FORK_METHODDEF |
| 12125 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 12126 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 12127 | OS_SCHED_GETPARAM_METHODDEF |
| 12128 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 12129 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 12130 | OS_SCHED_SETPARAM_METHODDEF |
| 12131 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 12132 | OS_SCHED_YIELD_METHODDEF |
| 12133 | OS_SCHED_SETAFFINITY_METHODDEF |
| 12134 | OS_SCHED_GETAFFINITY_METHODDEF |
| 12135 | OS_OPENPTY_METHODDEF |
| 12136 | OS_FORKPTY_METHODDEF |
| 12137 | OS_GETEGID_METHODDEF |
| 12138 | OS_GETEUID_METHODDEF |
| 12139 | OS_GETGID_METHODDEF |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 12140 | #ifdef HAVE_GETGROUPLIST |
| 12141 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 12142 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12143 | OS_GETGROUPS_METHODDEF |
| 12144 | OS_GETPID_METHODDEF |
| 12145 | OS_GETPGRP_METHODDEF |
| 12146 | OS_GETPPID_METHODDEF |
| 12147 | OS_GETUID_METHODDEF |
| 12148 | OS_GETLOGIN_METHODDEF |
| 12149 | OS_KILL_METHODDEF |
| 12150 | OS_KILLPG_METHODDEF |
| 12151 | OS_PLOCK_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 12152 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12153 | OS_STARTFILE_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 12154 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12155 | OS_SETUID_METHODDEF |
| 12156 | OS_SETEUID_METHODDEF |
| 12157 | OS_SETREUID_METHODDEF |
| 12158 | OS_SETGID_METHODDEF |
| 12159 | OS_SETEGID_METHODDEF |
| 12160 | OS_SETREGID_METHODDEF |
| 12161 | OS_SETGROUPS_METHODDEF |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 12162 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12163 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 12164 | #endif /* HAVE_INITGROUPS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12165 | OS_GETPGID_METHODDEF |
| 12166 | OS_SETPGRP_METHODDEF |
| 12167 | OS_WAIT_METHODDEF |
| 12168 | OS_WAIT3_METHODDEF |
| 12169 | OS_WAIT4_METHODDEF |
| 12170 | OS_WAITID_METHODDEF |
| 12171 | OS_WAITPID_METHODDEF |
| 12172 | OS_GETSID_METHODDEF |
| 12173 | OS_SETSID_METHODDEF |
| 12174 | OS_SETPGID_METHODDEF |
| 12175 | OS_TCGETPGRP_METHODDEF |
| 12176 | OS_TCSETPGRP_METHODDEF |
| 12177 | OS_OPEN_METHODDEF |
| 12178 | OS_CLOSE_METHODDEF |
| 12179 | OS_CLOSERANGE_METHODDEF |
| 12180 | OS_DEVICE_ENCODING_METHODDEF |
| 12181 | OS_DUP_METHODDEF |
| 12182 | OS_DUP2_METHODDEF |
| 12183 | OS_LOCKF_METHODDEF |
| 12184 | OS_LSEEK_METHODDEF |
| 12185 | OS_READ_METHODDEF |
| 12186 | OS_READV_METHODDEF |
| 12187 | OS_PREAD_METHODDEF |
| 12188 | OS_WRITE_METHODDEF |
| 12189 | OS_WRITEV_METHODDEF |
| 12190 | OS_PWRITE_METHODDEF |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12191 | #ifdef HAVE_SENDFILE |
| 12192 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 12193 | posix_sendfile__doc__}, |
| 12194 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12195 | OS_FSTAT_METHODDEF |
| 12196 | OS_ISATTY_METHODDEF |
| 12197 | OS_PIPE_METHODDEF |
| 12198 | OS_PIPE2_METHODDEF |
| 12199 | OS_MKFIFO_METHODDEF |
| 12200 | OS_MKNOD_METHODDEF |
| 12201 | OS_MAJOR_METHODDEF |
| 12202 | OS_MINOR_METHODDEF |
| 12203 | OS_MAKEDEV_METHODDEF |
| 12204 | OS_FTRUNCATE_METHODDEF |
| 12205 | OS_TRUNCATE_METHODDEF |
| 12206 | OS_POSIX_FALLOCATE_METHODDEF |
| 12207 | OS_POSIX_FADVISE_METHODDEF |
| 12208 | OS_PUTENV_METHODDEF |
| 12209 | OS_UNSETENV_METHODDEF |
| 12210 | OS_STRERROR_METHODDEF |
| 12211 | OS_FCHDIR_METHODDEF |
| 12212 | OS_FSYNC_METHODDEF |
| 12213 | OS_SYNC_METHODDEF |
| 12214 | OS_FDATASYNC_METHODDEF |
| 12215 | OS_WCOREDUMP_METHODDEF |
| 12216 | OS_WIFCONTINUED_METHODDEF |
| 12217 | OS_WIFSTOPPED_METHODDEF |
| 12218 | OS_WIFSIGNALED_METHODDEF |
| 12219 | OS_WIFEXITED_METHODDEF |
| 12220 | OS_WEXITSTATUS_METHODDEF |
| 12221 | OS_WTERMSIG_METHODDEF |
| 12222 | OS_WSTOPSIG_METHODDEF |
| 12223 | OS_FSTATVFS_METHODDEF |
| 12224 | OS_STATVFS_METHODDEF |
| 12225 | OS_CONFSTR_METHODDEF |
| 12226 | OS_SYSCONF_METHODDEF |
| 12227 | OS_FPATHCONF_METHODDEF |
| 12228 | OS_PATHCONF_METHODDEF |
| 12229 | OS_ABORT_METHODDEF |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 12230 | OS__GETFULLPATHNAME_METHODDEF |
| 12231 | OS__ISDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12232 | OS__GETDISKUSAGE_METHODDEF |
| 12233 | OS__GETFINALPATHNAME_METHODDEF |
| 12234 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 12235 | OS_GETLOADAVG_METHODDEF |
| 12236 | OS_URANDOM_METHODDEF |
| 12237 | OS_SETRESUID_METHODDEF |
| 12238 | OS_SETRESGID_METHODDEF |
| 12239 | OS_GETRESUID_METHODDEF |
| 12240 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12241 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12242 | OS_GETXATTR_METHODDEF |
| 12243 | OS_SETXATTR_METHODDEF |
| 12244 | OS_REMOVEXATTR_METHODDEF |
| 12245 | OS_LISTXATTR_METHODDEF |
| 12246 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12247 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 12248 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 12249 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12250 | OS_CPU_COUNT_METHODDEF |
| 12251 | OS_GET_INHERITABLE_METHODDEF |
| 12252 | OS_SET_INHERITABLE_METHODDEF |
| 12253 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 12254 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12255 | #ifndef MS_WINDOWS |
| 12256 | {"get_blocking", posix_get_blocking, METH_VARARGS, get_blocking__doc__}, |
| 12257 | {"set_blocking", posix_set_blocking, METH_VARARGS, set_blocking__doc__}, |
| 12258 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12259 | {"scandir", (PyCFunction)posix_scandir, |
| 12260 | METH_VARARGS | METH_KEYWORDS, |
| 12261 | posix_scandir__doc__}, |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 12262 | OS_FSPATH_METHODDEF |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12263 | OS_GETRANDOM_METHODDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12264 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 12265 | }; |
| 12266 | |
| 12267 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12268 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12269 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12270 | enable_symlink() |
| 12271 | { |
| 12272 | HANDLE tok; |
| 12273 | TOKEN_PRIVILEGES tok_priv; |
| 12274 | LUID luid; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12275 | |
| 12276 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12277 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12278 | |
| 12279 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12280 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12281 | |
| 12282 | tok_priv.PrivilegeCount = 1; |
| 12283 | tok_priv.Privileges[0].Luid = luid; |
| 12284 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 12285 | |
| 12286 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 12287 | sizeof(TOKEN_PRIVILEGES), |
| 12288 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12289 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12290 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12291 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 12292 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12293 | } |
| 12294 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 12295 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12296 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12297 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12298 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12299 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12300 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12301 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12302 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12303 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12304 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12305 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12306 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12307 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12308 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12309 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12310 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12311 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12312 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12313 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12314 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12315 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12316 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12317 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12318 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12319 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12320 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12321 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12322 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12323 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12324 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12325 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12326 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12327 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12328 | #endif |
| 12329 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12330 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12331 | #endif |
| 12332 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12333 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12334 | #endif |
| 12335 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12336 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12337 | #endif |
| 12338 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12339 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12340 | #endif |
| 12341 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12342 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12343 | #endif |
| 12344 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12345 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12346 | #endif |
| 12347 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12348 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12349 | #endif |
| 12350 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12351 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12352 | #endif |
| 12353 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12354 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12355 | #endif |
| 12356 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12357 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12358 | #endif |
| 12359 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12360 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12361 | #endif |
| 12362 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12363 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12364 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 12365 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12366 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 12367 | #endif |
| 12368 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12369 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 12370 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12371 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12372 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12373 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12374 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12375 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12376 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 12377 | #ifndef __GNU__ |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 12378 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12379 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 12380 | #endif |
| 12381 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12382 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 12383 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 12384 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12385 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12386 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12387 | #endif |
| 12388 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12389 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12390 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 12391 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12392 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 12393 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12394 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12395 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12396 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 12397 | #ifdef O_TMPFILE |
| 12398 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 12399 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12400 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12401 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12402 | #endif |
| 12403 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12404 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12405 | #endif |
| 12406 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12407 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12408 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 12409 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12410 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 12411 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12412 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12413 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12414 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12415 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12416 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 12417 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12418 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 12419 | #endif |
| 12420 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12421 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 12422 | #endif |
| 12423 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12424 | /* MS Windows */ |
| 12425 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12426 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12427 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12428 | #endif |
| 12429 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12430 | /* Optimize for short life (keep in memory). */ |
| 12431 | /* 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] | 12432 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12433 | #endif |
| 12434 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12435 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12436 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12437 | #endif |
| 12438 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12439 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12440 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12441 | #endif |
| 12442 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12443 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12444 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12445 | #endif |
| 12446 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12447 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 12448 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12449 | /* Send a SIGIO signal whenever input or output |
| 12450 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12451 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 12452 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12453 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12454 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12455 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12456 | #endif |
| 12457 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12458 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12459 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12460 | #endif |
| 12461 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12462 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12463 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12464 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12465 | #ifdef O_NOLINKS |
| 12466 | /* 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] | 12467 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12468 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 12469 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12470 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12471 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 12472 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 12473 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12474 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12475 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12476 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12477 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12478 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12479 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12480 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12481 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12482 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12483 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12484 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12485 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12486 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12487 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12488 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12489 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12490 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12491 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12492 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12493 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12494 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12495 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12496 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12497 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12498 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12499 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12500 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12501 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12502 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12503 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12504 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12505 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12506 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12507 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12508 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12509 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12510 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12511 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12512 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12513 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12514 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12515 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12516 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12517 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12518 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12519 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12520 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12521 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12522 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12523 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12524 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12525 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12526 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 12527 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 12528 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12529 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 12530 | #endif /* ST_RDONLY */ |
| 12531 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12532 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 12533 | #endif /* ST_NOSUID */ |
| 12534 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 12535 | /* GNU extensions */ |
| 12536 | #ifdef ST_NODEV |
| 12537 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 12538 | #endif /* ST_NODEV */ |
| 12539 | #ifdef ST_NOEXEC |
| 12540 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 12541 | #endif /* ST_NOEXEC */ |
| 12542 | #ifdef ST_SYNCHRONOUS |
| 12543 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 12544 | #endif /* ST_SYNCHRONOUS */ |
| 12545 | #ifdef ST_MANDLOCK |
| 12546 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 12547 | #endif /* ST_MANDLOCK */ |
| 12548 | #ifdef ST_WRITE |
| 12549 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 12550 | #endif /* ST_WRITE */ |
| 12551 | #ifdef ST_APPEND |
| 12552 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 12553 | #endif /* ST_APPEND */ |
| 12554 | #ifdef ST_NOATIME |
| 12555 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 12556 | #endif /* ST_NOATIME */ |
| 12557 | #ifdef ST_NODIRATIME |
| 12558 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 12559 | #endif /* ST_NODIRATIME */ |
| 12560 | #ifdef ST_RELATIME |
| 12561 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 12562 | #endif /* ST_RELATIME */ |
| 12563 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12564 | /* FreeBSD sendfile() constants */ |
| 12565 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12566 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12567 | #endif |
| 12568 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12569 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12570 | #endif |
| 12571 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12572 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12573 | #endif |
| 12574 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12575 | /* constants for posix_fadvise */ |
| 12576 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12577 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12578 | #endif |
| 12579 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12580 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12581 | #endif |
| 12582 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12583 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12584 | #endif |
| 12585 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12586 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12587 | #endif |
| 12588 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12589 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12590 | #endif |
| 12591 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12592 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12593 | #endif |
| 12594 | |
| 12595 | /* constants for waitid */ |
| 12596 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12597 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 12598 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 12599 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12600 | #endif |
| 12601 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12602 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12603 | #endif |
| 12604 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12605 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12606 | #endif |
| 12607 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12608 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12609 | #endif |
| 12610 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12611 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12612 | #endif |
| 12613 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12614 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12615 | #endif |
| 12616 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12617 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12618 | #endif |
| 12619 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12620 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12621 | #endif |
| 12622 | |
| 12623 | /* constants for lockf */ |
| 12624 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12625 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12626 | #endif |
| 12627 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12628 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12629 | #endif |
| 12630 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12631 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12632 | #endif |
| 12633 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12634 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12635 | #endif |
| 12636 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 12637 | #ifdef HAVE_SPAWNV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12638 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 12639 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
| 12640 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
| 12641 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
| 12642 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 12643 | #endif |
| 12644 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12645 | #ifdef HAVE_SCHED_H |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 12646 | #ifdef SCHED_OTHER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12647 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 12648 | #endif |
| 12649 | #ifdef SCHED_FIFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12650 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 12651 | #endif |
| 12652 | #ifdef SCHED_RR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12653 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 12654 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12655 | #ifdef SCHED_SPORADIC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12656 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12657 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12658 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12659 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12660 | #endif |
| 12661 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12662 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12663 | #endif |
| 12664 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12665 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12666 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12667 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12668 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12669 | #endif |
| 12670 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12671 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12672 | #endif |
| 12673 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12674 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12675 | #endif |
| 12676 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12677 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12678 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12679 | #endif |
| 12680 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12681 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12682 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 12683 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 12684 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12685 | #endif |
| 12686 | |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 12687 | #if HAVE_DECL_RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12688 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12689 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 12690 | #if HAVE_DECL_RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12691 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12692 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 12693 | #if HAVE_DECL_RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12694 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12695 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 12696 | #if HAVE_DECL_RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12697 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12698 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 12699 | #if HAVE_DECL_RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12700 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12701 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 12702 | #if HAVE_DECL_RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12703 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12704 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 12705 | #if HAVE_DECL_RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12706 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12707 | #endif |
| 12708 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 12709 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 12710 | if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1; |
| 12711 | if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1; |
| 12712 | #endif |
| 12713 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12714 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12715 | } |
| 12716 | |
| 12717 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12718 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12719 | PyModuleDef_HEAD_INIT, |
| 12720 | MODNAME, |
| 12721 | posix__doc__, |
| 12722 | -1, |
| 12723 | posix_methods, |
| 12724 | NULL, |
| 12725 | NULL, |
| 12726 | NULL, |
| 12727 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12728 | }; |
| 12729 | |
| 12730 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12731 | static const char * const have_functions[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12732 | |
| 12733 | #ifdef HAVE_FACCESSAT |
| 12734 | "HAVE_FACCESSAT", |
| 12735 | #endif |
| 12736 | |
| 12737 | #ifdef HAVE_FCHDIR |
| 12738 | "HAVE_FCHDIR", |
| 12739 | #endif |
| 12740 | |
| 12741 | #ifdef HAVE_FCHMOD |
| 12742 | "HAVE_FCHMOD", |
| 12743 | #endif |
| 12744 | |
| 12745 | #ifdef HAVE_FCHMODAT |
| 12746 | "HAVE_FCHMODAT", |
| 12747 | #endif |
| 12748 | |
| 12749 | #ifdef HAVE_FCHOWN |
| 12750 | "HAVE_FCHOWN", |
| 12751 | #endif |
| 12752 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 12753 | #ifdef HAVE_FCHOWNAT |
| 12754 | "HAVE_FCHOWNAT", |
| 12755 | #endif |
| 12756 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12757 | #ifdef HAVE_FEXECVE |
| 12758 | "HAVE_FEXECVE", |
| 12759 | #endif |
| 12760 | |
| 12761 | #ifdef HAVE_FDOPENDIR |
| 12762 | "HAVE_FDOPENDIR", |
| 12763 | #endif |
| 12764 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12765 | #ifdef HAVE_FPATHCONF |
| 12766 | "HAVE_FPATHCONF", |
| 12767 | #endif |
| 12768 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12769 | #ifdef HAVE_FSTATAT |
| 12770 | "HAVE_FSTATAT", |
| 12771 | #endif |
| 12772 | |
| 12773 | #ifdef HAVE_FSTATVFS |
| 12774 | "HAVE_FSTATVFS", |
| 12775 | #endif |
| 12776 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 12777 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12778 | "HAVE_FTRUNCATE", |
| 12779 | #endif |
| 12780 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12781 | #ifdef HAVE_FUTIMENS |
| 12782 | "HAVE_FUTIMENS", |
| 12783 | #endif |
| 12784 | |
| 12785 | #ifdef HAVE_FUTIMES |
| 12786 | "HAVE_FUTIMES", |
| 12787 | #endif |
| 12788 | |
| 12789 | #ifdef HAVE_FUTIMESAT |
| 12790 | "HAVE_FUTIMESAT", |
| 12791 | #endif |
| 12792 | |
| 12793 | #ifdef HAVE_LINKAT |
| 12794 | "HAVE_LINKAT", |
| 12795 | #endif |
| 12796 | |
| 12797 | #ifdef HAVE_LCHFLAGS |
| 12798 | "HAVE_LCHFLAGS", |
| 12799 | #endif |
| 12800 | |
| 12801 | #ifdef HAVE_LCHMOD |
| 12802 | "HAVE_LCHMOD", |
| 12803 | #endif |
| 12804 | |
| 12805 | #ifdef HAVE_LCHOWN |
| 12806 | "HAVE_LCHOWN", |
| 12807 | #endif |
| 12808 | |
| 12809 | #ifdef HAVE_LSTAT |
| 12810 | "HAVE_LSTAT", |
| 12811 | #endif |
| 12812 | |
| 12813 | #ifdef HAVE_LUTIMES |
| 12814 | "HAVE_LUTIMES", |
| 12815 | #endif |
| 12816 | |
| 12817 | #ifdef HAVE_MKDIRAT |
| 12818 | "HAVE_MKDIRAT", |
| 12819 | #endif |
| 12820 | |
| 12821 | #ifdef HAVE_MKFIFOAT |
| 12822 | "HAVE_MKFIFOAT", |
| 12823 | #endif |
| 12824 | |
| 12825 | #ifdef HAVE_MKNODAT |
| 12826 | "HAVE_MKNODAT", |
| 12827 | #endif |
| 12828 | |
| 12829 | #ifdef HAVE_OPENAT |
| 12830 | "HAVE_OPENAT", |
| 12831 | #endif |
| 12832 | |
| 12833 | #ifdef HAVE_READLINKAT |
| 12834 | "HAVE_READLINKAT", |
| 12835 | #endif |
| 12836 | |
| 12837 | #ifdef HAVE_RENAMEAT |
| 12838 | "HAVE_RENAMEAT", |
| 12839 | #endif |
| 12840 | |
| 12841 | #ifdef HAVE_SYMLINKAT |
| 12842 | "HAVE_SYMLINKAT", |
| 12843 | #endif |
| 12844 | |
| 12845 | #ifdef HAVE_UNLINKAT |
| 12846 | "HAVE_UNLINKAT", |
| 12847 | #endif |
| 12848 | |
| 12849 | #ifdef HAVE_UTIMENSAT |
| 12850 | "HAVE_UTIMENSAT", |
| 12851 | #endif |
| 12852 | |
| 12853 | #ifdef MS_WINDOWS |
| 12854 | "MS_WINDOWS", |
| 12855 | #endif |
| 12856 | |
| 12857 | NULL |
| 12858 | }; |
| 12859 | |
| 12860 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 12861 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 12862 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12863 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12864 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12865 | PyObject *list; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12866 | const char * const *trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12867 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12868 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12869 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12870 | #endif |
| 12871 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12872 | m = PyModule_Create(&posixmodule); |
| 12873 | if (m == NULL) |
| 12874 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12875 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12876 | /* Initialize environ dictionary */ |
| 12877 | v = convertenviron(); |
| 12878 | Py_XINCREF(v); |
| 12879 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 12880 | return NULL; |
| 12881 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12882 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12883 | if (all_ins(m)) |
| 12884 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12885 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12886 | if (setup_confname_tables(m)) |
| 12887 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12888 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12889 | Py_INCREF(PyExc_OSError); |
| 12890 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 12891 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12892 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12893 | if (posix_putenv_garbage == NULL) |
| 12894 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12895 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 12896 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12897 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12898 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 12899 | waitid_result_desc.name = MODNAME ".waitid_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12900 | if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0) |
| 12901 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12902 | #endif |
| 12903 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 12904 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12905 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 12906 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 12907 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12908 | if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0) |
| 12909 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12910 | structseq_new = StatResultType.tp_new; |
| 12911 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12912 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 12913 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12914 | if (PyStructSequence_InitType2(&StatVFSResultType, |
| 12915 | &statvfs_result_desc) < 0) |
| 12916 | return NULL; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12917 | #ifdef NEED_TICKS_PER_SECOND |
| 12918 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12919 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12920 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12921 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12922 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12923 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12924 | # endif |
| 12925 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12926 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 12927 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12928 | sched_param_desc.name = MODNAME ".sched_param"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12929 | if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0) |
| 12930 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12931 | SchedParamType.tp_new = os_sched_param; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12932 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12933 | |
| 12934 | /* initialize TerminalSize_info */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12935 | if (PyStructSequence_InitType2(&TerminalSizeType, |
| 12936 | &TerminalSize_desc) < 0) |
| 12937 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12938 | |
| 12939 | /* initialize scandir types */ |
| 12940 | if (PyType_Ready(&ScandirIteratorType) < 0) |
| 12941 | return NULL; |
| 12942 | if (PyType_Ready(&DirEntryType) < 0) |
| 12943 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12944 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12945 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 12946 | Py_INCREF((PyObject*) &WaitidResultType); |
| 12947 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 12948 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12949 | Py_INCREF((PyObject*) &StatResultType); |
| 12950 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 12951 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 12952 | PyModule_AddObject(m, "statvfs_result", |
| 12953 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 12954 | |
| 12955 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12956 | Py_INCREF(&SchedParamType); |
| 12957 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 12958 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12959 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 12960 | times_result_desc.name = MODNAME ".times_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12961 | if (PyStructSequence_InitType2(&TimesResultType, ×_result_desc) < 0) |
| 12962 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 12963 | PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType); |
| 12964 | |
| 12965 | uname_result_desc.name = MODNAME ".uname_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12966 | if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0) |
| 12967 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 12968 | PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType); |
| 12969 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12970 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12971 | /* |
| 12972 | * Step 2 of weak-linking support on Mac OS X. |
| 12973 | * |
| 12974 | * The code below removes functions that are not available on the |
| 12975 | * currently active platform. |
| 12976 | * |
| 12977 | * 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] | 12978 | * 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] | 12979 | * OSX 10.4. |
| 12980 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12981 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12982 | if (fstatvfs == NULL) { |
| 12983 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 12984 | return NULL; |
| 12985 | } |
| 12986 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12987 | #endif /* HAVE_FSTATVFS */ |
| 12988 | |
| 12989 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12990 | if (statvfs == NULL) { |
| 12991 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 12992 | return NULL; |
| 12993 | } |
| 12994 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12995 | #endif /* HAVE_STATVFS */ |
| 12996 | |
| 12997 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12998 | if (lchown == NULL) { |
| 12999 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 13000 | return NULL; |
| 13001 | } |
| 13002 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13003 | #endif /* HAVE_LCHOWN */ |
| 13004 | |
| 13005 | |
| 13006 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13007 | |
Antoine Pitrou | 9d20e0e | 2012-09-12 18:01:36 +0200 | [diff] [blame] | 13008 | Py_INCREF(&TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13009 | PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); |
| 13010 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 13011 | billion = PyLong_FromLong(1000000000); |
| 13012 | if (!billion) |
| 13013 | return NULL; |
| 13014 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13015 | /* suppress "function not used" warnings */ |
| 13016 | { |
| 13017 | int ignored; |
| 13018 | fd_specified("", -1); |
| 13019 | follow_symlinks_specified("", 1); |
| 13020 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 13021 | dir_fd_converter(Py_None, &ignored); |
| 13022 | dir_fd_unavailable(Py_None, &ignored); |
| 13023 | } |
| 13024 | |
| 13025 | /* |
| 13026 | * provide list of locally available functions |
| 13027 | * so os.py can populate support_* lists |
| 13028 | */ |
| 13029 | list = PyList_New(0); |
| 13030 | if (!list) |
| 13031 | return NULL; |
| 13032 | for (trace = have_functions; *trace; trace++) { |
| 13033 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 13034 | if (!unicode) |
| 13035 | return NULL; |
| 13036 | if (PyList_Append(list, unicode)) |
| 13037 | return NULL; |
| 13038 | Py_DECREF(unicode); |
| 13039 | } |
| 13040 | PyModule_AddObject(m, "_have_functions", list); |
Ned Deily | eb3be66 | 2016-08-15 14:40:38 -0400 | [diff] [blame] | 13041 | |
| 13042 | Py_INCREF((PyObject *) &DirEntryType); |
Brett Cannon | a32c4d0 | 2016-06-24 14:14:44 -0700 | [diff] [blame] | 13043 | PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13044 | |
| 13045 | initialized = 1; |
| 13046 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13047 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 13048 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13049 | |
| 13050 | #ifdef __cplusplus |
| 13051 | } |
| 13052 | #endif |