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 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 39 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 40 | "This module provides access to operating system functionality that is\n\ |
| 41 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 42 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 43 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 45 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 46 | #ifdef HAVE_SYS_UIO_H |
| 47 | #include <sys/uio.h> |
| 48 | #endif |
| 49 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 50 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 51 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 52 | #endif /* HAVE_SYS_TYPES_H */ |
| 53 | |
| 54 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 55 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 56 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 58 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 59 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 60 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 61 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 62 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 63 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 64 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 66 | #ifdef HAVE_FCNTL_H |
| 67 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 68 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 70 | #ifdef HAVE_GRP_H |
| 71 | #include <grp.h> |
| 72 | #endif |
| 73 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 74 | #ifdef HAVE_SYSEXITS_H |
| 75 | #include <sysexits.h> |
| 76 | #endif /* HAVE_SYSEXITS_H */ |
| 77 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 78 | #ifdef HAVE_SYS_LOADAVG_H |
| 79 | #include <sys/loadavg.h> |
| 80 | #endif |
| 81 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 82 | #ifdef HAVE_LANGINFO_H |
| 83 | #include <langinfo.h> |
| 84 | #endif |
| 85 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 86 | #ifdef HAVE_SYS_SENDFILE_H |
| 87 | #include <sys/sendfile.h> |
| 88 | #endif |
| 89 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 90 | #ifdef HAVE_SCHED_H |
| 91 | #include <sched.h> |
| 92 | #endif |
| 93 | |
Benjamin Peterson | 2dbda07 | 2012-03-16 10:12:55 -0500 | [diff] [blame] | 94 | #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) |
Benjamin Peterson | 7b51b8d | 2012-03-14 22:28:25 -0500 | [diff] [blame] | 95 | #undef HAVE_SCHED_SETAFFINITY |
| 96 | #endif |
| 97 | |
doko@ubuntu.com | 4a173bc | 2014-04-17 19:47:16 +0200 | [diff] [blame] | 98 | #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] | 99 | #define USE_XATTRS |
| 100 | #endif |
| 101 | |
| 102 | #ifdef USE_XATTRS |
Benjamin Peterson | b77fe17 | 2011-09-13 17:20:47 -0400 | [diff] [blame] | 103 | #include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 104 | #endif |
| 105 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 106 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 107 | #ifdef HAVE_SYS_SOCKET_H |
| 108 | #include <sys/socket.h> |
| 109 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 110 | #endif |
| 111 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 112 | #ifdef HAVE_DLFCN_H |
| 113 | #include <dlfcn.h> |
| 114 | #endif |
| 115 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 116 | #ifdef __hpux |
| 117 | #include <sys/mpctl.h> |
| 118 | #endif |
| 119 | |
| 120 | #if defined(__DragonFly__) || \ |
| 121 | defined(__OpenBSD__) || \ |
| 122 | defined(__FreeBSD__) || \ |
| 123 | defined(__NetBSD__) || \ |
| 124 | defined(__APPLE__) |
| 125 | #include <sys/sysctl.h> |
| 126 | #endif |
| 127 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 128 | #if defined(MS_WINDOWS) |
| 129 | # define TERMSIZE_USE_CONIO |
| 130 | #elif defined(HAVE_SYS_IOCTL_H) |
| 131 | # include <sys/ioctl.h> |
| 132 | # if defined(HAVE_TERMIOS_H) |
| 133 | # include <termios.h> |
| 134 | # endif |
| 135 | # if defined(TIOCGWINSZ) |
| 136 | # define TERMSIZE_USE_IOCTL |
| 137 | # endif |
| 138 | #endif /* MS_WINDOWS */ |
| 139 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 140 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 141 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 142 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 143 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 144 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 145 | #include <process.h> |
| 146 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 147 | #ifdef _MSC_VER /* Microsoft compiler */ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 148 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 149 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 150 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 151 | #define HAVE_EXECV 1 |
| 152 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 153 | #define HAVE_SYSTEM 1 |
| 154 | #define HAVE_CWAIT 1 |
| 155 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 156 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 157 | #else |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 158 | /* Unix functions that the configure script doesn't check for */ |
| 159 | #define HAVE_EXECV 1 |
| 160 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 161 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 162 | #define HAVE_FORK1 1 |
| 163 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 164 | #define HAVE_GETEGID 1 |
| 165 | #define HAVE_GETEUID 1 |
| 166 | #define HAVE_GETGID 1 |
| 167 | #define HAVE_GETPPID 1 |
| 168 | #define HAVE_GETUID 1 |
| 169 | #define HAVE_KILL 1 |
| 170 | #define HAVE_OPENDIR 1 |
| 171 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 172 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 173 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 174 | #define HAVE_TTYNAME 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 175 | #endif /* _MSC_VER */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 176 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 177 | |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 178 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 179 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 180 | # one of the few times we lie about this name! |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 181 | module os |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 182 | [clinic start generated code]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 183 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/ |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 184 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 185 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 186 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 187 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 188 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 189 | (default) */ |
| 190 | extern char *ctermid_r(char *); |
| 191 | #endif |
| 192 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 193 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 194 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 195 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 196 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 197 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 198 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 199 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 200 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 201 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 202 | #endif |
| 203 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 204 | extern int chdir(char *); |
| 205 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 206 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 207 | extern int chdir(const char *); |
| 208 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 209 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 210 | extern int chmod(const char *, mode_t); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 211 | /*#ifdef HAVE_FCHMOD |
| 212 | extern int fchmod(int, mode_t); |
| 213 | #endif*/ |
| 214 | /*#ifdef HAVE_LCHMOD |
| 215 | extern int lchmod(const char *, mode_t); |
| 216 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 217 | extern int chown(const char *, uid_t, gid_t); |
| 218 | extern char *getcwd(char *, int); |
| 219 | extern char *strerror(int); |
| 220 | extern int link(const char *, const char *); |
| 221 | extern int rename(const char *, const char *); |
| 222 | extern int stat(const char *, struct stat *); |
| 223 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 224 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 225 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 226 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 227 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 228 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 229 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 230 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 231 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 232 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 233 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 234 | #ifdef HAVE_UTIME_H |
| 235 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 236 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 237 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 238 | #ifdef HAVE_SYS_UTIME_H |
| 239 | #include <sys/utime.h> |
| 240 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 241 | #endif /* HAVE_SYS_UTIME_H */ |
| 242 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 243 | #ifdef HAVE_SYS_TIMES_H |
| 244 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 245 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 246 | |
| 247 | #ifdef HAVE_SYS_PARAM_H |
| 248 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 249 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 250 | |
| 251 | #ifdef HAVE_SYS_UTSNAME_H |
| 252 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 253 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 255 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 256 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 257 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 258 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 259 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 260 | #include <direct.h> |
| 261 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 262 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 263 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 264 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 265 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 266 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 267 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 268 | #endif |
| 269 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 270 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 271 | #endif |
| 272 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 273 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 274 | #endif |
| 275 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 276 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 277 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 278 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 279 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 280 | #endif |
| 281 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 282 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 283 | #endif |
| 284 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 285 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 286 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 287 | #ifndef VOLUME_NAME_DOS |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 288 | #define VOLUME_NAME_DOS 0x0 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 289 | #endif |
| 290 | #ifndef VOLUME_NAME_NT |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 291 | #define VOLUME_NAME_NT 0x2 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 292 | #endif |
| 293 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 294 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 295 | #endif |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 296 | #ifndef IO_REPARSE_TAG_MOUNT_POINT |
| 297 | #define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L) |
| 298 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 299 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 300 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 301 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 302 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 303 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 304 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 305 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 306 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 307 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 308 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 309 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 310 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 311 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 312 | #define MAXPATHLEN PATH_MAX |
| 313 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 314 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 315 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 316 | #endif /* MAXPATHLEN */ |
| 317 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 318 | #ifdef UNION_WAIT |
| 319 | /* Emulate some macros on systems that have a union instead of macros */ |
| 320 | |
| 321 | #ifndef WIFEXITED |
| 322 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 323 | #endif |
| 324 | |
| 325 | #ifndef WEXITSTATUS |
| 326 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 327 | #endif |
| 328 | |
| 329 | #ifndef WTERMSIG |
| 330 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 331 | #endif |
| 332 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 333 | #define WAIT_TYPE union wait |
| 334 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 335 | |
| 336 | #else /* !UNION_WAIT */ |
| 337 | #define WAIT_TYPE int |
| 338 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 339 | #endif /* UNION_WAIT */ |
| 340 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 341 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 342 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 343 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 344 | #define USE_CTERMID_R |
| 345 | #endif |
| 346 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 347 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 348 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 349 | #undef FSTAT |
| 350 | #undef STRUCT_STAT |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 351 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 352 | # define STAT win32_stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 353 | # define LSTAT win32_lstat |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 354 | # define FSTAT _Py_fstat_noraise |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 355 | # define STRUCT_STAT struct _Py_stat_struct |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 356 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 357 | # define STAT stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 358 | # define LSTAT lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 359 | # define FSTAT fstat |
| 360 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 361 | #endif |
| 362 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 363 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 364 | #include <sys/mkdev.h> |
| 365 | #else |
| 366 | #if defined(MAJOR_IN_SYSMACROS) |
| 367 | #include <sys/sysmacros.h> |
| 368 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 369 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 370 | #include <sys/mkdev.h> |
| 371 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 372 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 373 | |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 374 | #define DWORD_MAX 4294967295U |
| 375 | |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 376 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 377 | #define INITFUNC PyInit_nt |
| 378 | #define MODNAME "nt" |
| 379 | #else |
| 380 | #define INITFUNC PyInit_posix |
| 381 | #define MODNAME "posix" |
| 382 | #endif |
| 383 | |
| 384 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 385 | /* defined in fileutils.c */ |
| 386 | PyAPI_FUNC(void) _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *); |
| 387 | PyAPI_FUNC(void) _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *, |
| 388 | ULONG, struct _Py_stat_struct *); |
| 389 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 390 | |
| 391 | #ifdef MS_WINDOWS |
| 392 | static int |
| 393 | win32_warn_bytes_api() |
| 394 | { |
| 395 | return PyErr_WarnEx(PyExc_DeprecationWarning, |
| 396 | "The Windows bytes API has been deprecated, " |
| 397 | "use Unicode filenames instead", |
| 398 | 1); |
| 399 | } |
| 400 | #endif |
| 401 | |
| 402 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 403 | #ifndef MS_WINDOWS |
| 404 | PyObject * |
| 405 | _PyLong_FromUid(uid_t uid) |
| 406 | { |
| 407 | if (uid == (uid_t)-1) |
| 408 | return PyLong_FromLong(-1); |
| 409 | return PyLong_FromUnsignedLong(uid); |
| 410 | } |
| 411 | |
| 412 | PyObject * |
| 413 | _PyLong_FromGid(gid_t gid) |
| 414 | { |
| 415 | if (gid == (gid_t)-1) |
| 416 | return PyLong_FromLong(-1); |
| 417 | return PyLong_FromUnsignedLong(gid); |
| 418 | } |
| 419 | |
| 420 | int |
| 421 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 422 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 423 | uid_t uid; |
| 424 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 425 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 426 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 427 | unsigned long uresult; |
| 428 | |
| 429 | index = PyNumber_Index(obj); |
| 430 | if (index == NULL) { |
| 431 | PyErr_Format(PyExc_TypeError, |
| 432 | "uid should be integer, not %.200s", |
| 433 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 434 | return 0; |
| 435 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 436 | |
| 437 | /* |
| 438 | * Handling uid_t is complicated for two reasons: |
| 439 | * * Although uid_t is (always?) unsigned, it still |
| 440 | * accepts -1. |
| 441 | * * We don't know its size in advance--it may be |
| 442 | * bigger than an int, or it may be smaller than |
| 443 | * a long. |
| 444 | * |
| 445 | * So a bit of defensive programming is in order. |
| 446 | * Start with interpreting the value passed |
| 447 | * in as a signed long and see if it works. |
| 448 | */ |
| 449 | |
| 450 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 451 | |
| 452 | if (!overflow) { |
| 453 | uid = (uid_t)result; |
| 454 | |
| 455 | if (result == -1) { |
| 456 | if (PyErr_Occurred()) |
| 457 | goto fail; |
| 458 | /* It's a legitimate -1, we're done. */ |
| 459 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 460 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 461 | |
| 462 | /* Any other negative number is disallowed. */ |
| 463 | if (result < 0) |
| 464 | goto underflow; |
| 465 | |
| 466 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 467 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 468 | (long)uid != result) |
| 469 | goto underflow; |
| 470 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 471 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 472 | |
| 473 | if (overflow < 0) |
| 474 | goto underflow; |
| 475 | |
| 476 | /* |
| 477 | * Okay, the value overflowed a signed long. If it |
| 478 | * fits in an *unsigned* long, it may still be okay, |
| 479 | * as uid_t may be unsigned long on this platform. |
| 480 | */ |
| 481 | uresult = PyLong_AsUnsignedLong(index); |
| 482 | if (PyErr_Occurred()) { |
| 483 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 484 | goto overflow; |
| 485 | goto fail; |
| 486 | } |
| 487 | |
| 488 | uid = (uid_t)uresult; |
| 489 | |
| 490 | /* |
| 491 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, |
| 492 | * but this value would get interpreted as (uid_t)-1 by chown |
| 493 | * and its siblings. That's not what the user meant! So we |
| 494 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 495 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 496 | */ |
| 497 | if (uid == (uid_t)-1) |
| 498 | goto overflow; |
| 499 | |
| 500 | /* Ensure the value wasn't truncated. */ |
| 501 | if (sizeof(uid_t) < sizeof(long) && |
| 502 | (unsigned long)uid != uresult) |
| 503 | goto overflow; |
| 504 | /* fallthrough */ |
| 505 | |
| 506 | success: |
| 507 | Py_DECREF(index); |
| 508 | *(uid_t *)p = uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 509 | return 1; |
| 510 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 511 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 512 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 513 | "uid is less than minimum"); |
| 514 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 515 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 516 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 517 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 518 | "uid is greater than maximum"); |
| 519 | /* fallthrough */ |
| 520 | |
| 521 | fail: |
| 522 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | int |
| 527 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 528 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 529 | gid_t gid; |
| 530 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 531 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 532 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 533 | unsigned long uresult; |
| 534 | |
| 535 | index = PyNumber_Index(obj); |
| 536 | if (index == NULL) { |
| 537 | PyErr_Format(PyExc_TypeError, |
| 538 | "gid should be integer, not %.200s", |
| 539 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 540 | return 0; |
| 541 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 542 | |
| 543 | /* |
| 544 | * Handling gid_t is complicated for two reasons: |
| 545 | * * Although gid_t is (always?) unsigned, it still |
| 546 | * accepts -1. |
| 547 | * * We don't know its size in advance--it may be |
| 548 | * bigger than an int, or it may be smaller than |
| 549 | * a long. |
| 550 | * |
| 551 | * So a bit of defensive programming is in order. |
| 552 | * Start with interpreting the value passed |
| 553 | * in as a signed long and see if it works. |
| 554 | */ |
| 555 | |
| 556 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 557 | |
| 558 | if (!overflow) { |
| 559 | gid = (gid_t)result; |
| 560 | |
| 561 | if (result == -1) { |
| 562 | if (PyErr_Occurred()) |
| 563 | goto fail; |
| 564 | /* It's a legitimate -1, we're done. */ |
| 565 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 566 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 567 | |
| 568 | /* Any other negative number is disallowed. */ |
| 569 | if (result < 0) { |
| 570 | goto underflow; |
| 571 | } |
| 572 | |
| 573 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 574 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 575 | (long)gid != result) |
| 576 | goto underflow; |
| 577 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 578 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 579 | |
| 580 | if (overflow < 0) |
| 581 | goto underflow; |
| 582 | |
| 583 | /* |
| 584 | * Okay, the value overflowed a signed long. If it |
| 585 | * fits in an *unsigned* long, it may still be okay, |
| 586 | * as gid_t may be unsigned long on this platform. |
| 587 | */ |
| 588 | uresult = PyLong_AsUnsignedLong(index); |
| 589 | if (PyErr_Occurred()) { |
| 590 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 591 | goto overflow; |
| 592 | goto fail; |
| 593 | } |
| 594 | |
| 595 | gid = (gid_t)uresult; |
| 596 | |
| 597 | /* |
| 598 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, |
| 599 | * but this value would get interpreted as (gid_t)-1 by chown |
| 600 | * and its siblings. That's not what the user meant! So we |
| 601 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 602 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 603 | */ |
| 604 | if (gid == (gid_t)-1) |
| 605 | goto overflow; |
| 606 | |
| 607 | /* Ensure the value wasn't truncated. */ |
| 608 | if (sizeof(gid_t) < sizeof(long) && |
| 609 | (unsigned long)gid != uresult) |
| 610 | goto overflow; |
| 611 | /* fallthrough */ |
| 612 | |
| 613 | success: |
| 614 | Py_DECREF(index); |
| 615 | *(gid_t *)p = gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 616 | return 1; |
| 617 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 618 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 619 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 620 | "gid is less than minimum"); |
| 621 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 622 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 623 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 624 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 625 | "gid is greater than maximum"); |
| 626 | /* fallthrough */ |
| 627 | |
| 628 | fail: |
| 629 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 630 | return 0; |
| 631 | } |
| 632 | #endif /* MS_WINDOWS */ |
| 633 | |
| 634 | |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 635 | #ifdef HAVE_LONG_LONG |
| 636 | # define _PyLong_FromDev PyLong_FromLongLong |
| 637 | #else |
| 638 | # define _PyLong_FromDev PyLong_FromLong |
| 639 | #endif |
| 640 | |
| 641 | |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 642 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 643 | static int |
| 644 | _Py_Dev_Converter(PyObject *obj, void *p) |
| 645 | { |
| 646 | #ifdef HAVE_LONG_LONG |
| 647 | *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj); |
| 648 | #else |
| 649 | *((dev_t *)p) = PyLong_AsUnsignedLong(obj); |
| 650 | #endif |
| 651 | if (PyErr_Occurred()) |
| 652 | return 0; |
| 653 | return 1; |
| 654 | } |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 655 | #endif /* HAVE_MKNOD && HAVE_MAKEDEV */ |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 656 | |
| 657 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 658 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 659 | /* |
| 660 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 661 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 662 | * which doesn't play nicely with all the initializer lines in this file that |
| 663 | * look like this: |
| 664 | * int dir_fd = DEFAULT_DIR_FD; |
| 665 | */ |
| 666 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 667 | #else |
| 668 | #define DEFAULT_DIR_FD (-100) |
| 669 | #endif |
| 670 | |
| 671 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 672 | _fd_converter(PyObject *o, int *p, const char *allowed) |
| 673 | { |
| 674 | int overflow; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 675 | long long_value; |
| 676 | |
| 677 | PyObject *index = PyNumber_Index(o); |
| 678 | if (index == NULL) { |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 679 | PyErr_Format(PyExc_TypeError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 680 | "argument should be %s, not %.200s", |
| 681 | allowed, Py_TYPE(o)->tp_name); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 682 | return 0; |
| 683 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 684 | |
| 685 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
| 686 | Py_DECREF(index); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 687 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 688 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 689 | "fd is greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 690 | return 0; |
| 691 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 692 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 693 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 694 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 695 | return 0; |
| 696 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 697 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 698 | *p = (int)long_value; |
| 699 | return 1; |
| 700 | } |
| 701 | |
| 702 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 703 | dir_fd_converter(PyObject *o, void *p) |
| 704 | { |
| 705 | if (o == Py_None) { |
| 706 | *(int *)p = DEFAULT_DIR_FD; |
| 707 | return 1; |
| 708 | } |
| 709 | return _fd_converter(o, (int *)p, "integer"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 713 | /* |
| 714 | * A PyArg_ParseTuple "converter" function |
| 715 | * that handles filesystem paths in the manner |
| 716 | * preferred by the os module. |
| 717 | * |
| 718 | * path_converter accepts (Unicode) strings and their |
| 719 | * subclasses, and bytes and their subclasses. What |
| 720 | * it does with the argument depends on the platform: |
| 721 | * |
| 722 | * * On Windows, if we get a (Unicode) string we |
| 723 | * extract the wchar_t * and return it; if we get |
| 724 | * bytes we extract the char * and return that. |
| 725 | * |
| 726 | * * On all other platforms, strings are encoded |
| 727 | * to bytes using PyUnicode_FSConverter, then we |
| 728 | * extract the char * from the bytes object and |
| 729 | * return that. |
| 730 | * |
| 731 | * path_converter also optionally accepts signed |
| 732 | * integers (representing open file descriptors) instead |
| 733 | * of path strings. |
| 734 | * |
| 735 | * Input fields: |
| 736 | * path.nullable |
| 737 | * If nonzero, the path is permitted to be None. |
| 738 | * path.allow_fd |
| 739 | * If nonzero, the path is permitted to be a file handle |
| 740 | * (a signed int) instead of a string. |
| 741 | * path.function_name |
| 742 | * If non-NULL, path_converter will use that as the name |
| 743 | * of the function in error messages. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 744 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 745 | * path.argument_name |
| 746 | * If non-NULL, path_converter will use that as the name |
| 747 | * of the parameter in error messages. |
| 748 | * (If path.argument_name is NULL it uses "path".) |
| 749 | * |
| 750 | * Output fields: |
| 751 | * path.wide |
| 752 | * Points to the path if it was expressed as Unicode |
| 753 | * and was not encoded. (Only used on Windows.) |
| 754 | * path.narrow |
| 755 | * Points to the path if it was expressed as bytes, |
| 756 | * or it was Unicode and was encoded to bytes. |
| 757 | * path.fd |
| 758 | * Contains a file descriptor if path.accept_fd was true |
| 759 | * and the caller provided a signed integer instead of any |
| 760 | * sort of string. |
| 761 | * |
| 762 | * WARNING: if your "path" parameter is optional, and is |
| 763 | * unspecified, path_converter will never get called. |
| 764 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 765 | * yourself! |
| 766 | * path.length |
| 767 | * The length of the path in characters, if specified as |
| 768 | * a string. |
| 769 | * path.object |
| 770 | * The original object passed in. |
| 771 | * path.cleanup |
| 772 | * For internal use only. May point to a temporary object. |
| 773 | * (Pay no attention to the man behind the curtain.) |
| 774 | * |
| 775 | * At most one of path.wide or path.narrow will be non-NULL. |
| 776 | * If path was None and path.nullable was set, |
| 777 | * or if path was an integer and path.allow_fd was set, |
| 778 | * both path.wide and path.narrow will be NULL |
| 779 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 780 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 781 | * path_converter takes care to not write to the path_t |
| 782 | * unless it's successful. However it must reset the |
| 783 | * "cleanup" field each time it's called. |
| 784 | * |
| 785 | * Use as follows: |
| 786 | * path_t path; |
| 787 | * memset(&path, 0, sizeof(path)); |
| 788 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 789 | * // ... use values from path ... |
| 790 | * path_cleanup(&path); |
| 791 | * |
| 792 | * (Note that if PyArg_Parse fails you don't need to call |
| 793 | * path_cleanup(). However it is safe to do so.) |
| 794 | */ |
| 795 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 796 | const char *function_name; |
| 797 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 798 | int nullable; |
| 799 | int allow_fd; |
| 800 | wchar_t *wide; |
| 801 | char *narrow; |
| 802 | int fd; |
| 803 | Py_ssize_t length; |
| 804 | PyObject *object; |
| 805 | PyObject *cleanup; |
| 806 | } path_t; |
| 807 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 808 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 809 | {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL} |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 810 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 811 | static void |
| 812 | path_cleanup(path_t *path) { |
| 813 | if (path->cleanup) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 814 | Py_CLEAR(path->cleanup); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | |
| 818 | static int |
| 819 | path_converter(PyObject *o, void *p) { |
| 820 | path_t *path = (path_t *)p; |
| 821 | PyObject *unicode, *bytes; |
| 822 | Py_ssize_t length; |
| 823 | char *narrow; |
| 824 | |
| 825 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 826 | PyErr_Format(exc, "%s%s" fmt, \ |
| 827 | path->function_name ? path->function_name : "", \ |
| 828 | path->function_name ? ": " : "", \ |
| 829 | path->argument_name ? path->argument_name : "path") |
| 830 | |
| 831 | /* Py_CLEANUP_SUPPORTED support */ |
| 832 | if (o == NULL) { |
| 833 | path_cleanup(path); |
| 834 | return 1; |
| 835 | } |
| 836 | |
| 837 | /* ensure it's always safe to call path_cleanup() */ |
| 838 | path->cleanup = NULL; |
| 839 | |
| 840 | if (o == Py_None) { |
| 841 | if (!path->nullable) { |
| 842 | FORMAT_EXCEPTION(PyExc_TypeError, |
| 843 | "can't specify None for %s argument"); |
| 844 | return 0; |
| 845 | } |
| 846 | path->wide = NULL; |
| 847 | path->narrow = NULL; |
| 848 | path->length = 0; |
| 849 | path->object = o; |
| 850 | path->fd = -1; |
| 851 | return 1; |
| 852 | } |
| 853 | |
| 854 | unicode = PyUnicode_FromObject(o); |
| 855 | if (unicode) { |
| 856 | #ifdef MS_WINDOWS |
| 857 | wchar_t *wide; |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 858 | |
| 859 | wide = PyUnicode_AsUnicodeAndSize(unicode, &length); |
| 860 | if (!wide) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 861 | Py_DECREF(unicode); |
| 862 | return 0; |
| 863 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 864 | if (length > 32767) { |
| 865 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 866 | Py_DECREF(unicode); |
| 867 | return 0; |
| 868 | } |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 869 | if (wcslen(wide) != length) { |
Serhiy Storchaka | 7e9d1d1 | 2015-04-20 10:12:28 +0300 | [diff] [blame] | 870 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character"); |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 871 | Py_DECREF(unicode); |
| 872 | return 0; |
| 873 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 874 | |
| 875 | path->wide = wide; |
| 876 | path->narrow = NULL; |
| 877 | path->length = length; |
| 878 | path->object = o; |
| 879 | path->fd = -1; |
| 880 | path->cleanup = unicode; |
| 881 | return Py_CLEANUP_SUPPORTED; |
| 882 | #else |
| 883 | int converted = PyUnicode_FSConverter(unicode, &bytes); |
| 884 | Py_DECREF(unicode); |
| 885 | if (!converted) |
| 886 | bytes = NULL; |
| 887 | #endif |
| 888 | } |
| 889 | else { |
| 890 | PyErr_Clear(); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 891 | if (PyObject_CheckBuffer(o)) |
| 892 | bytes = PyBytes_FromObject(o); |
| 893 | else |
| 894 | bytes = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 895 | if (!bytes) { |
| 896 | PyErr_Clear(); |
| 897 | if (path->allow_fd) { |
| 898 | int fd; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 899 | int result = _fd_converter(o, &fd, |
| 900 | "string, bytes or integer"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 901 | if (result) { |
| 902 | path->wide = NULL; |
| 903 | path->narrow = NULL; |
| 904 | path->length = 0; |
| 905 | path->object = o; |
| 906 | path->fd = fd; |
| 907 | return result; |
| 908 | } |
| 909 | } |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | if (!bytes) { |
| 914 | if (!PyErr_Occurred()) |
| 915 | FORMAT_EXCEPTION(PyExc_TypeError, "illegal type for %s parameter"); |
| 916 | return 0; |
| 917 | } |
| 918 | |
| 919 | #ifdef MS_WINDOWS |
| 920 | if (win32_warn_bytes_api()) { |
| 921 | Py_DECREF(bytes); |
| 922 | return 0; |
| 923 | } |
| 924 | #endif |
| 925 | |
| 926 | length = PyBytes_GET_SIZE(bytes); |
| 927 | #ifdef MS_WINDOWS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 928 | if (length > MAX_PATH-1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 929 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
| 930 | Py_DECREF(bytes); |
| 931 | return 0; |
| 932 | } |
| 933 | #endif |
| 934 | |
| 935 | narrow = PyBytes_AS_STRING(bytes); |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 936 | if ((size_t)length != strlen(narrow)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 937 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 938 | Py_DECREF(bytes); |
| 939 | return 0; |
| 940 | } |
| 941 | |
| 942 | path->wide = NULL; |
| 943 | path->narrow = narrow; |
| 944 | path->length = length; |
| 945 | path->object = o; |
| 946 | path->fd = -1; |
| 947 | path->cleanup = bytes; |
| 948 | return Py_CLEANUP_SUPPORTED; |
| 949 | } |
| 950 | |
| 951 | static void |
| 952 | argument_unavailable_error(char *function_name, char *argument_name) { |
| 953 | PyErr_Format(PyExc_NotImplementedError, |
| 954 | "%s%s%s unavailable on this platform", |
| 955 | (function_name != NULL) ? function_name : "", |
| 956 | (function_name != NULL) ? ": ": "", |
| 957 | argument_name); |
| 958 | } |
| 959 | |
| 960 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 961 | dir_fd_unavailable(PyObject *o, void *p) |
| 962 | { |
| 963 | int dir_fd; |
| 964 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 965 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 966 | if (dir_fd != DEFAULT_DIR_FD) { |
| 967 | argument_unavailable_error(NULL, "dir_fd"); |
| 968 | return 0; |
| 969 | } |
| 970 | *(int *)p = dir_fd; |
| 971 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | static int |
| 975 | fd_specified(char *function_name, int fd) { |
| 976 | if (fd == -1) |
| 977 | return 0; |
| 978 | |
| 979 | argument_unavailable_error(function_name, "fd"); |
| 980 | return 1; |
| 981 | } |
| 982 | |
| 983 | static int |
| 984 | follow_symlinks_specified(char *function_name, int follow_symlinks) { |
| 985 | if (follow_symlinks) |
| 986 | return 0; |
| 987 | |
| 988 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 989 | return 1; |
| 990 | } |
| 991 | |
| 992 | static int |
| 993 | path_and_dir_fd_invalid(char *function_name, path_t *path, int dir_fd) { |
| 994 | if (!path->narrow && !path->wide && (dir_fd != DEFAULT_DIR_FD)) { |
| 995 | PyErr_Format(PyExc_ValueError, |
| 996 | "%s: can't specify dir_fd without matching path", |
| 997 | function_name); |
| 998 | return 1; |
| 999 | } |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | static int |
| 1004 | dir_fd_and_fd_invalid(char *function_name, int dir_fd, int fd) { |
| 1005 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 1006 | PyErr_Format(PyExc_ValueError, |
| 1007 | "%s: can't specify both dir_fd and fd", |
| 1008 | function_name); |
| 1009 | return 1; |
| 1010 | } |
| 1011 | return 0; |
| 1012 | } |
| 1013 | |
| 1014 | static int |
| 1015 | fd_and_follow_symlinks_invalid(char *function_name, int fd, |
| 1016 | int follow_symlinks) { |
| 1017 | if ((fd > 0) && (!follow_symlinks)) { |
| 1018 | PyErr_Format(PyExc_ValueError, |
| 1019 | "%s: cannot use fd and follow_symlinks together", |
| 1020 | function_name); |
| 1021 | return 1; |
| 1022 | } |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
| 1026 | static int |
| 1027 | dir_fd_and_follow_symlinks_invalid(char *function_name, int dir_fd, |
| 1028 | int follow_symlinks) { |
| 1029 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 1030 | PyErr_Format(PyExc_ValueError, |
| 1031 | "%s: cannot use dir_fd and follow_symlinks together", |
| 1032 | function_name); |
| 1033 | return 1; |
| 1034 | } |
| 1035 | return 0; |
| 1036 | } |
| 1037 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1038 | #ifdef MS_WINDOWS |
| 1039 | typedef PY_LONG_LONG Py_off_t; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1040 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1041 | typedef off_t Py_off_t; |
| 1042 | #endif |
| 1043 | |
| 1044 | static int |
| 1045 | Py_off_t_converter(PyObject *arg, void *addr) |
| 1046 | { |
| 1047 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1048 | *((Py_off_t *)addr) = PyLong_AsLongLong(arg); |
| 1049 | #else |
| 1050 | *((Py_off_t *)addr) = PyLong_AsLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1051 | #endif |
| 1052 | if (PyErr_Occurred()) |
| 1053 | return 0; |
| 1054 | return 1; |
| 1055 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1056 | |
| 1057 | static PyObject * |
| 1058 | PyLong_FromPy_off_t(Py_off_t offset) |
| 1059 | { |
| 1060 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1061 | return PyLong_FromLongLong(offset); |
| 1062 | #else |
| 1063 | return PyLong_FromLong(offset); |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1064 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1065 | } |
| 1066 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1067 | |
Steve Dower | d81431f | 2015-03-06 14:47:02 -0800 | [diff] [blame] | 1068 | #if defined _MSC_VER && _MSC_VER >= 1400 && _MSC_VER < 1900 |
| 1069 | /* Legacy implementation of _PyVerify_fd_dup2 while transitioning to |
| 1070 | * MSVC 14.0. This should eventually be removed. (issue23524) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1071 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1072 | #define IOINFO_L2E 5 |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1073 | #define IOINFO_ARRAYS 64 |
Steve Dower | 65e4cb1 | 2014-11-22 12:54:57 -0800 | [diff] [blame] | 1074 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1075 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1076 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 1077 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1078 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 1079 | static int |
| 1080 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 1081 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1082 | if (!_PyVerify_fd(fd1)) |
| 1083 | return 0; |
| 1084 | if (fd2 == _NO_CONSOLE_FILENO) |
| 1085 | return 0; |
| 1086 | if ((unsigned)fd2 < _NHANDLE_) |
| 1087 | return 1; |
| 1088 | else |
| 1089 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1090 | } |
| 1091 | #else |
Steve Dower | d81431f | 2015-03-06 14:47:02 -0800 | [diff] [blame] | 1092 | #define _PyVerify_fd_dup2(fd1, fd2) (_PyVerify_fd(fd1) && (fd2) >= 0) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1093 | #endif |
| 1094 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1095 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1096 | |
| 1097 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1098 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1099 | { |
| 1100 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1101 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 1102 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1103 | |
| 1104 | if (0 == DeviceIoControl( |
| 1105 | reparse_point_handle, |
| 1106 | FSCTL_GET_REPARSE_POINT, |
| 1107 | NULL, 0, /* in buffer */ |
| 1108 | target_buffer, sizeof(target_buffer), |
| 1109 | &n_bytes_returned, |
| 1110 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1111 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1112 | |
| 1113 | if (reparse_tag) |
| 1114 | *reparse_tag = rdb->ReparseTag; |
| 1115 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1116 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1117 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1118 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1119 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1120 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1121 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1122 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1123 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1124 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1125 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1126 | */ |
| 1127 | #include <crt_externs.h> |
| 1128 | static char **environ; |
| 1129 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1130 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1131 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1132 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1133 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1134 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1135 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1136 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1137 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1138 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1139 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1140 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1141 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1142 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1143 | d = PyDict_New(); |
| 1144 | if (d == NULL) |
| 1145 | return NULL; |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1146 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1147 | if (environ == NULL) |
| 1148 | environ = *_NSGetEnviron(); |
| 1149 | #endif |
| 1150 | #ifdef MS_WINDOWS |
| 1151 | /* _wenviron must be initialized in this way if the program is started |
| 1152 | through main() instead of wmain(). */ |
| 1153 | _wgetenv(L""); |
| 1154 | if (_wenviron == NULL) |
| 1155 | return d; |
| 1156 | /* This part ignores errors */ |
| 1157 | for (e = _wenviron; *e != NULL; e++) { |
| 1158 | PyObject *k; |
| 1159 | PyObject *v; |
| 1160 | wchar_t *p = wcschr(*e, L'='); |
| 1161 | if (p == NULL) |
| 1162 | continue; |
| 1163 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1164 | if (k == NULL) { |
| 1165 | PyErr_Clear(); |
| 1166 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1167 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1168 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1169 | if (v == NULL) { |
| 1170 | PyErr_Clear(); |
| 1171 | Py_DECREF(k); |
| 1172 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1173 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1174 | if (PyDict_GetItem(d, k) == NULL) { |
| 1175 | if (PyDict_SetItem(d, k, v) != 0) |
| 1176 | PyErr_Clear(); |
| 1177 | } |
| 1178 | Py_DECREF(k); |
| 1179 | Py_DECREF(v); |
| 1180 | } |
| 1181 | #else |
| 1182 | if (environ == NULL) |
| 1183 | return d; |
| 1184 | /* This part ignores errors */ |
| 1185 | for (e = environ; *e != NULL; e++) { |
| 1186 | PyObject *k; |
| 1187 | PyObject *v; |
| 1188 | char *p = strchr(*e, '='); |
| 1189 | if (p == NULL) |
| 1190 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1191 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1192 | if (k == NULL) { |
| 1193 | PyErr_Clear(); |
| 1194 | continue; |
| 1195 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1196 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1197 | if (v == NULL) { |
| 1198 | PyErr_Clear(); |
| 1199 | Py_DECREF(k); |
| 1200 | continue; |
| 1201 | } |
| 1202 | if (PyDict_GetItem(d, k) == NULL) { |
| 1203 | if (PyDict_SetItem(d, k, v) != 0) |
| 1204 | PyErr_Clear(); |
| 1205 | } |
| 1206 | Py_DECREF(k); |
| 1207 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1208 | } |
| 1209 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1210 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1213 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1214 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1215 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1216 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1217 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1218 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1219 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1220 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1221 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1222 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1223 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1224 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1225 | /* XXX We should pass the function name along in the future. |
| 1226 | (winreg.c also wants to pass the function name.) |
| 1227 | This would however require an additional param to the |
| 1228 | Windows error object, which is non-trivial. |
| 1229 | */ |
| 1230 | errno = GetLastError(); |
| 1231 | if (filename) |
| 1232 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1233 | else |
| 1234 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1235 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1236 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1237 | static PyObject * |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1238 | win32_error_object(char* function, PyObject* filename) |
| 1239 | { |
| 1240 | /* XXX - see win32_error for comments on 'function' */ |
| 1241 | errno = GetLastError(); |
| 1242 | if (filename) |
| 1243 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1244 | PyExc_OSError, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1245 | errno, |
| 1246 | filename); |
| 1247 | else |
| 1248 | return PyErr_SetFromWindowsErr(errno); |
| 1249 | } |
| 1250 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1251 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1252 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1253 | static PyObject * |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1254 | path_error(path_t *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1255 | { |
| 1256 | #ifdef MS_WINDOWS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1257 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 1258 | 0, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1259 | #else |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1260 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1261 | #endif |
| 1262 | } |
| 1263 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1264 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1265 | static PyObject * |
| 1266 | path_error2(path_t *path, path_t *path2) |
| 1267 | { |
| 1268 | #ifdef MS_WINDOWS |
| 1269 | return PyErr_SetExcFromWindowsErrWithFilenameObjects(PyExc_OSError, |
| 1270 | 0, path->object, path2->object); |
| 1271 | #else |
| 1272 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, |
| 1273 | path->object, path2->object); |
| 1274 | #endif |
| 1275 | } |
| 1276 | |
| 1277 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1278 | /* POSIX generic methods */ |
| 1279 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1280 | static int |
| 1281 | fildes_converter(PyObject *o, void *p) |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1282 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1283 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1284 | int *pointer = (int *)p; |
| 1285 | fd = PyObject_AsFileDescriptor(o); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1286 | if (fd < 0) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1287 | return 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1288 | *pointer = fd; |
| 1289 | return 1; |
| 1290 | } |
| 1291 | |
| 1292 | static PyObject * |
| 1293 | posix_fildes_fd(int fd, int (*func)(int)) |
| 1294 | { |
| 1295 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1296 | int async_err = 0; |
| 1297 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1298 | if (!_PyVerify_fd(fd)) |
| 1299 | return posix_error(); |
| 1300 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1301 | do { |
| 1302 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1303 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1304 | res = (*func)(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1305 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1306 | Py_END_ALLOW_THREADS |
| 1307 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 1308 | if (res != 0) |
| 1309 | return (!async_err) ? posix_error() : NULL; |
| 1310 | Py_RETURN_NONE; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1311 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1312 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1313 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1314 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1315 | /* This is a reimplementation of the C library's chdir function, |
| 1316 | but one that produces Win32 errors instead of DOS error codes. |
| 1317 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1318 | it also needs to set "magic" environment variables indicating |
| 1319 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1320 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1321 | win32_chdir(LPCSTR path) |
| 1322 | { |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1323 | char new_path[MAX_PATH]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1324 | int result; |
| 1325 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1326 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1327 | if(!SetCurrentDirectoryA(path)) |
| 1328 | return FALSE; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1329 | result = GetCurrentDirectoryA(Py_ARRAY_LENGTH(new_path), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1330 | if (!result) |
| 1331 | return FALSE; |
| 1332 | /* In the ANSI API, there should not be any paths longer |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1333 | than MAX_PATH-1 (not including the final null character). */ |
| 1334 | assert(result < Py_ARRAY_LENGTH(new_path)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1335 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 1336 | strncmp(new_path, "//", 2) == 0) |
| 1337 | /* UNC path, nothing to do. */ |
| 1338 | return TRUE; |
| 1339 | env[1] = new_path[0]; |
| 1340 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | /* The Unicode version differs from the ANSI version |
| 1344 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1345 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1346 | win32_wchdir(LPCWSTR path) |
| 1347 | { |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1348 | wchar_t path_buf[MAX_PATH], *new_path = path_buf; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1349 | int result; |
| 1350 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1351 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1352 | if(!SetCurrentDirectoryW(path)) |
| 1353 | return FALSE; |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1354 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1355 | if (!result) |
| 1356 | return FALSE; |
Victor Stinner | e847d71 | 2015-12-14 00:21:50 +0100 | [diff] [blame] | 1357 | if (result > Py_ARRAY_LENGTH(path_buf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1358 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1359 | if (!new_path) { |
| 1360 | SetLastError(ERROR_OUTOFMEMORY); |
| 1361 | return FALSE; |
| 1362 | } |
| 1363 | result = GetCurrentDirectoryW(result, new_path); |
| 1364 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1365 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1366 | return FALSE; |
| 1367 | } |
| 1368 | } |
| 1369 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1370 | wcsncmp(new_path, L"//", 2) == 0) |
| 1371 | /* UNC path, nothing to do. */ |
| 1372 | return TRUE; |
| 1373 | env[1] = new_path[0]; |
| 1374 | result = SetEnvironmentVariableW(env, new_path); |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1375 | if (new_path != path_buf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1376 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1377 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1378 | } |
| 1379 | #endif |
| 1380 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1381 | #ifdef MS_WINDOWS |
| 1382 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1383 | - time stamps are restricted to second resolution |
| 1384 | - file modification times suffer from forth-and-back conversions between |
| 1385 | UTC and local time |
| 1386 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1387 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1388 | #define HAVE_STAT_NSEC 1 |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1389 | #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1390 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1391 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1392 | attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1393 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1394 | HANDLE hFindFile; |
| 1395 | WIN32_FIND_DATAA FileData; |
| 1396 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1397 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1398 | return FALSE; |
| 1399 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1400 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1401 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1402 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1403 | info->ftCreationTime = FileData.ftCreationTime; |
| 1404 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1405 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1406 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1407 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1408 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1409 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1410 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1411 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1414 | static void |
| 1415 | find_data_to_file_info_w(WIN32_FIND_DATAW *pFileData, |
| 1416 | BY_HANDLE_FILE_INFORMATION *info, |
| 1417 | ULONG *reparse_tag) |
| 1418 | { |
| 1419 | memset(info, 0, sizeof(*info)); |
| 1420 | info->dwFileAttributes = pFileData->dwFileAttributes; |
| 1421 | info->ftCreationTime = pFileData->ftCreationTime; |
| 1422 | info->ftLastAccessTime = pFileData->ftLastAccessTime; |
| 1423 | info->ftLastWriteTime = pFileData->ftLastWriteTime; |
| 1424 | info->nFileSizeHigh = pFileData->nFileSizeHigh; |
| 1425 | info->nFileSizeLow = pFileData->nFileSizeLow; |
| 1426 | /* info->nNumberOfLinks = 1; */ |
| 1427 | if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1428 | *reparse_tag = pFileData->dwReserved0; |
| 1429 | else |
| 1430 | *reparse_tag = 0; |
| 1431 | } |
| 1432 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1433 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1434 | attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1435 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1436 | HANDLE hFindFile; |
| 1437 | WIN32_FIND_DATAW FileData; |
| 1438 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1439 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1440 | return FALSE; |
| 1441 | FindClose(hFindFile); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1442 | find_data_to_file_info_w(&FileData, info, reparse_tag); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1443 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1446 | static BOOL |
| 1447 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1448 | { |
| 1449 | int buf_size, result_length; |
| 1450 | wchar_t *buf; |
| 1451 | |
| 1452 | /* We have a good handle to the target, use it to determine |
| 1453 | the target path name (then we'll call lstat on it). */ |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1454 | buf_size = GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1455 | VOLUME_NAME_DOS); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1456 | if(!buf_size) |
| 1457 | return FALSE; |
| 1458 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 1459 | buf = PyMem_New(wchar_t, buf_size+1); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1460 | if (!buf) { |
| 1461 | SetLastError(ERROR_OUTOFMEMORY); |
| 1462 | return FALSE; |
| 1463 | } |
| 1464 | |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1465 | result_length = GetFinalPathNameByHandleW(hdl, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1466 | buf, buf_size, VOLUME_NAME_DOS); |
| 1467 | |
| 1468 | if(!result_length) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1469 | PyMem_Free(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1470 | return FALSE; |
| 1471 | } |
| 1472 | |
| 1473 | if(!CloseHandle(hdl)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1474 | PyMem_Free(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1475 | return FALSE; |
| 1476 | } |
| 1477 | |
| 1478 | buf[result_length] = 0; |
| 1479 | |
| 1480 | *target_path = buf; |
| 1481 | return TRUE; |
| 1482 | } |
| 1483 | |
| 1484 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1485 | win32_xstat_impl_w(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1486 | BOOL traverse); |
| 1487 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1488 | win32_xstat_impl(const char *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1489 | BOOL traverse) |
| 1490 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1491 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1492 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1493 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1494 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1495 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1496 | const char *dot; |
| 1497 | |
| 1498 | hFile = CreateFileA( |
| 1499 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1500 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1501 | 0, /* share mode */ |
| 1502 | NULL, /* security attributes */ |
| 1503 | OPEN_EXISTING, |
| 1504 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1505 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1506 | Because of this, calls like GetFinalPathNameByHandle will return |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 1507 | the symlink path again and not the actual final path. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1508 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1509 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1510 | NULL); |
| 1511 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1512 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1513 | /* Either the target doesn't exist, or we don't have access to |
| 1514 | get a handle to it. If the former, we need to return an error. |
| 1515 | If the latter, we can use attributes_from_dir. */ |
| 1516 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1517 | return -1; |
| 1518 | /* Could not get attributes on open file. Fall back to |
| 1519 | reading the directory. */ |
| 1520 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1521 | /* Very strange. This should not fail now */ |
| 1522 | return -1; |
| 1523 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1524 | if (traverse) { |
| 1525 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1526 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1527 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1528 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1529 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1530 | } else { |
| 1531 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1532 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1533 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1534 | } |
| 1535 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1536 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1537 | return -1; |
| 1538 | |
| 1539 | /* Close the outer open file handle now that we're about to |
| 1540 | reopen it with different flags. */ |
| 1541 | if (!CloseHandle(hFile)) |
| 1542 | return -1; |
| 1543 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1544 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1545 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1546 | the file without the reparse handling flag set. */ |
| 1547 | hFile2 = CreateFileA( |
| 1548 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1549 | NULL, OPEN_EXISTING, |
| 1550 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1551 | NULL); |
| 1552 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1553 | return -1; |
| 1554 | |
| 1555 | if (!get_target_path(hFile2, &target_path)) |
| 1556 | return -1; |
| 1557 | |
| 1558 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1559 | PyMem_Free(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1560 | return code; |
| 1561 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1562 | } else |
| 1563 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1564 | } |
Steve Dower | a2af1a5 | 2015-02-21 10:04:10 -0800 | [diff] [blame] | 1565 | _Py_attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1566 | |
| 1567 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1568 | dot = strrchr(path, '.'); |
| 1569 | if (dot) { |
| 1570 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1571 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1572 | result->st_mode |= 0111; |
| 1573 | } |
| 1574 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1578 | win32_xstat_impl_w(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1579 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1580 | { |
| 1581 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1582 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1583 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1584 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1585 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1586 | const wchar_t *dot; |
| 1587 | |
| 1588 | hFile = CreateFileW( |
| 1589 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1590 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1591 | 0, /* share mode */ |
| 1592 | NULL, /* security attributes */ |
| 1593 | OPEN_EXISTING, |
| 1594 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1595 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1596 | Because of this, calls like GetFinalPathNameByHandle will return |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 1597 | the symlink path again and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1598 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1599 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1600 | NULL); |
| 1601 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1602 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1603 | /* Either the target doesn't exist, or we don't have access to |
| 1604 | get a handle to it. If the former, we need to return an error. |
| 1605 | If the latter, we can use attributes_from_dir. */ |
| 1606 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1607 | return -1; |
| 1608 | /* Could not get attributes on open file. Fall back to |
| 1609 | reading the directory. */ |
| 1610 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1611 | /* Very strange. This should not fail now */ |
| 1612 | return -1; |
| 1613 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1614 | if (traverse) { |
| 1615 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1616 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1617 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1618 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1619 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1620 | } else { |
| 1621 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1622 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1623 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1624 | } |
| 1625 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1626 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1627 | return -1; |
| 1628 | |
| 1629 | /* Close the outer open file handle now that we're about to |
| 1630 | reopen it with different flags. */ |
| 1631 | if (!CloseHandle(hFile)) |
| 1632 | return -1; |
| 1633 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1634 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1635 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1636 | the file without the reparse handling flag set. */ |
| 1637 | hFile2 = CreateFileW( |
| 1638 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1639 | NULL, OPEN_EXISTING, |
| 1640 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1641 | NULL); |
| 1642 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1643 | return -1; |
| 1644 | |
| 1645 | if (!get_target_path(hFile2, &target_path)) |
| 1646 | return -1; |
| 1647 | |
| 1648 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1649 | PyMem_Free(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1650 | return code; |
| 1651 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1652 | } else |
| 1653 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1654 | } |
Steve Dower | a2af1a5 | 2015-02-21 10:04:10 -0800 | [diff] [blame] | 1655 | _Py_attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1656 | |
| 1657 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1658 | dot = wcsrchr(path, '.'); |
| 1659 | if (dot) { |
| 1660 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1661 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1662 | result->st_mode |= 0111; |
| 1663 | } |
| 1664 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1668 | win32_xstat(const char *path, struct _Py_stat_struct *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1669 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1670 | /* Protocol violation: we explicitly clear errno, instead of |
| 1671 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1672 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1673 | errno = 0; |
| 1674 | return code; |
| 1675 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1676 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1677 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1678 | win32_xstat_w(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1679 | { |
| 1680 | /* Protocol violation: we explicitly clear errno, instead of |
| 1681 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1682 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1683 | errno = 0; |
| 1684 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1685 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1686 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1687 | |
| 1688 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1689 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1690 | default does not traverse symlinks and instead returns attributes for |
| 1691 | the symlink. |
| 1692 | |
| 1693 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1694 | win32_stat will first explicitly resolve the symlink target and then will |
| 1695 | call win32_lstat on that result. |
| 1696 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1697 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1698 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1699 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1700 | win32_lstat(const char* path, struct _Py_stat_struct *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1701 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1702 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1705 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1706 | win32_lstat_w(const wchar_t* path, struct _Py_stat_struct *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1707 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1708 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1712 | win32_stat(const char* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1713 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1714 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1717 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1718 | win32_stat_w(const wchar_t* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1719 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1720 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1723 | #endif /* MS_WINDOWS */ |
| 1724 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1725 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1726 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1727 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1728 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1729 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1730 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1731 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1732 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1733 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1734 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1735 | |
| 1736 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1737 | {"st_mode", "protection bits"}, |
| 1738 | {"st_ino", "inode"}, |
| 1739 | {"st_dev", "device"}, |
| 1740 | {"st_nlink", "number of hard links"}, |
| 1741 | {"st_uid", "user ID of owner"}, |
| 1742 | {"st_gid", "group ID of owner"}, |
| 1743 | {"st_size", "total size, in bytes"}, |
| 1744 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1745 | {NULL, "integer time of last access"}, |
| 1746 | {NULL, "integer time of last modification"}, |
| 1747 | {NULL, "integer time of last change"}, |
| 1748 | {"st_atime", "time of last access"}, |
| 1749 | {"st_mtime", "time of last modification"}, |
| 1750 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1751 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1752 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1753 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1754 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1755 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1756 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1757 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1758 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1759 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1760 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1761 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1762 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1763 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1764 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1765 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1766 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1767 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1768 | #endif |
| 1769 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1770 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1771 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1772 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1773 | {"st_file_attributes", "Windows file attribute bits"}, |
| 1774 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1775 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1776 | }; |
| 1777 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1778 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1779 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1780 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1781 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1782 | #endif |
| 1783 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1784 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1785 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1786 | #else |
| 1787 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1788 | #endif |
| 1789 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1790 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1791 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1792 | #else |
| 1793 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1794 | #endif |
| 1795 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1796 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1797 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1798 | #else |
| 1799 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1800 | #endif |
| 1801 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1802 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1803 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1804 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1805 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1806 | #endif |
| 1807 | |
| 1808 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1809 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1810 | #else |
| 1811 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1812 | #endif |
| 1813 | |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1814 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1815 | #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1) |
| 1816 | #else |
| 1817 | #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX |
| 1818 | #endif |
| 1819 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1820 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1821 | "stat_result", /* name */ |
| 1822 | stat_result__doc__, /* doc */ |
| 1823 | stat_result_fields, |
| 1824 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1825 | }; |
| 1826 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1827 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1828 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1829 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1830 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1831 | 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] | 1832 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1833 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1834 | |
| 1835 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1836 | {"f_bsize", }, |
| 1837 | {"f_frsize", }, |
| 1838 | {"f_blocks", }, |
| 1839 | {"f_bfree", }, |
| 1840 | {"f_bavail", }, |
| 1841 | {"f_files", }, |
| 1842 | {"f_ffree", }, |
| 1843 | {"f_favail", }, |
| 1844 | {"f_flag", }, |
| 1845 | {"f_namemax",}, |
| 1846 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1847 | }; |
| 1848 | |
| 1849 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1850 | "statvfs_result", /* name */ |
| 1851 | statvfs_result__doc__, /* doc */ |
| 1852 | statvfs_result_fields, |
| 1853 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1854 | }; |
| 1855 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1856 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1857 | PyDoc_STRVAR(waitid_result__doc__, |
| 1858 | "waitid_result: Result from waitid.\n\n\ |
| 1859 | This object may be accessed either as a tuple of\n\ |
| 1860 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1861 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1862 | \n\ |
| 1863 | See os.waitid for more information."); |
| 1864 | |
| 1865 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1866 | {"si_pid", }, |
| 1867 | {"si_uid", }, |
| 1868 | {"si_signo", }, |
| 1869 | {"si_status", }, |
| 1870 | {"si_code", }, |
| 1871 | {0} |
| 1872 | }; |
| 1873 | |
| 1874 | static PyStructSequence_Desc waitid_result_desc = { |
| 1875 | "waitid_result", /* name */ |
| 1876 | waitid_result__doc__, /* doc */ |
| 1877 | waitid_result_fields, |
| 1878 | 5 |
| 1879 | }; |
| 1880 | static PyTypeObject WaitidResultType; |
| 1881 | #endif |
| 1882 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1883 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1884 | static PyTypeObject StatResultType; |
| 1885 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1886 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 1887 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1888 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1889 | static newfunc structseq_new; |
| 1890 | |
| 1891 | static PyObject * |
| 1892 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1893 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1894 | PyStructSequence *result; |
| 1895 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1896 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1897 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1898 | if (!result) |
| 1899 | return NULL; |
| 1900 | /* If we have been initialized from a tuple, |
| 1901 | st_?time might be set to None. Initialize it |
| 1902 | from the int slots. */ |
| 1903 | for (i = 7; i <= 9; i++) { |
| 1904 | if (result->ob_item[i+3] == Py_None) { |
| 1905 | Py_DECREF(Py_None); |
| 1906 | Py_INCREF(result->ob_item[i]); |
| 1907 | result->ob_item[i+3] = result->ob_item[i]; |
| 1908 | } |
| 1909 | } |
| 1910 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | |
| 1914 | |
| 1915 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1916 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1917 | |
| 1918 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1919 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1920 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1921 | \n\ |
| 1922 | If value is True, future calls to stat() return floats; if it is False,\n\ |
| 1923 | future calls return ints.\n\ |
| 1924 | If value is omitted, return the current setting.\n"); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1925 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1926 | /* 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] | 1927 | static PyObject* |
| 1928 | stat_float_times(PyObject* self, PyObject *args) |
| 1929 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1930 | int newval = -1; |
| 1931 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1932 | return NULL; |
Victor Stinner | 034d0aa | 2012-06-05 01:22:15 +0200 | [diff] [blame] | 1933 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 1934 | "stat_float_times() is deprecated", |
| 1935 | 1)) |
| 1936 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1937 | if (newval == -1) |
| 1938 | /* Return old value */ |
| 1939 | return PyBool_FromLong(_stat_float_times); |
| 1940 | _stat_float_times = newval; |
| 1941 | Py_INCREF(Py_None); |
| 1942 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1943 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1944 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1945 | static PyObject *billion = NULL; |
| 1946 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1947 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1948 | 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] | 1949 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1950 | PyObject *s = _PyLong_FromTime_t(sec); |
| 1951 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 1952 | PyObject *s_in_ns = NULL; |
| 1953 | PyObject *ns_total = NULL; |
| 1954 | PyObject *float_s = NULL; |
| 1955 | |
| 1956 | if (!(s && ns_fractional)) |
| 1957 | goto exit; |
| 1958 | |
| 1959 | s_in_ns = PyNumber_Multiply(s, billion); |
| 1960 | if (!s_in_ns) |
| 1961 | goto exit; |
| 1962 | |
| 1963 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 1964 | if (!ns_total) |
| 1965 | goto exit; |
| 1966 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1967 | if (_stat_float_times) { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1968 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1969 | if (!float_s) |
| 1970 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1971 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1972 | else { |
| 1973 | float_s = s; |
| 1974 | Py_INCREF(float_s); |
| 1975 | } |
| 1976 | |
| 1977 | PyStructSequence_SET_ITEM(v, index, s); |
| 1978 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 1979 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 1980 | s = NULL; |
| 1981 | float_s = NULL; |
| 1982 | ns_total = NULL; |
| 1983 | exit: |
| 1984 | Py_XDECREF(s); |
| 1985 | Py_XDECREF(ns_fractional); |
| 1986 | Py_XDECREF(s_in_ns); |
| 1987 | Py_XDECREF(ns_total); |
| 1988 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1991 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1992 | (used by posix_stat() and posix_fstat()) */ |
| 1993 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1994 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1995 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1996 | unsigned long ansec, mnsec, cnsec; |
| 1997 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 1998 | if (v == NULL) |
| 1999 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2000 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2001 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2002 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2003 | PyStructSequence_SET_ITEM(v, 1, |
| 2004 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2005 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2006 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2007 | #endif |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2008 | #ifdef MS_WINDOWS |
| 2009 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2010 | #else |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 2011 | PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2012 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2013 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2014 | #if defined(MS_WINDOWS) |
| 2015 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2016 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2017 | #else |
| 2018 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2019 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2020 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2021 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2022 | PyStructSequence_SET_ITEM(v, 6, |
| 2023 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2024 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2025 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2026 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2027 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2028 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2029 | ansec = st->st_atim.tv_nsec; |
| 2030 | mnsec = st->st_mtim.tv_nsec; |
| 2031 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2032 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2033 | ansec = st->st_atimespec.tv_nsec; |
| 2034 | mnsec = st->st_mtimespec.tv_nsec; |
| 2035 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2036 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2037 | ansec = st->st_atime_nsec; |
| 2038 | mnsec = st->st_mtime_nsec; |
| 2039 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2040 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2041 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2042 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2043 | fill_time(v, 7, st->st_atime, ansec); |
| 2044 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2045 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2046 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2047 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2048 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2049 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2050 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2051 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2052 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2053 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2054 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2055 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2056 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2057 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2058 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2059 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2060 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2061 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2062 | #endif |
| 2063 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2064 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2065 | PyObject *val; |
| 2066 | unsigned long bsec,bnsec; |
| 2067 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2068 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2069 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2070 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2071 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2072 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2073 | if (_stat_float_times) { |
| 2074 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 2075 | } else { |
| 2076 | val = PyLong_FromLong((long)bsec); |
| 2077 | } |
| 2078 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2079 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2080 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2081 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2082 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2083 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2084 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2085 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2086 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2087 | PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX, |
| 2088 | PyLong_FromUnsignedLong(st->st_file_attributes)); |
| 2089 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2090 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2091 | if (PyErr_Occurred()) { |
| 2092 | Py_DECREF(v); |
| 2093 | return NULL; |
| 2094 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2095 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2096 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2099 | /* POSIX methods */ |
| 2100 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2101 | |
| 2102 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2103 | posix_do_stat(char *function_name, path_t *path, |
| 2104 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2105 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2106 | STRUCT_STAT st; |
| 2107 | int result; |
| 2108 | |
| 2109 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2110 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2111 | return NULL; |
| 2112 | #endif |
| 2113 | |
| 2114 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2115 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2116 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2117 | return NULL; |
| 2118 | |
| 2119 | Py_BEGIN_ALLOW_THREADS |
| 2120 | if (path->fd != -1) |
| 2121 | result = FSTAT(path->fd, &st); |
| 2122 | else |
| 2123 | #ifdef MS_WINDOWS |
| 2124 | if (path->wide) { |
| 2125 | if (follow_symlinks) |
| 2126 | result = win32_stat_w(path->wide, &st); |
| 2127 | else |
| 2128 | result = win32_lstat_w(path->wide, &st); |
| 2129 | } |
| 2130 | else |
| 2131 | #endif |
| 2132 | #if defined(HAVE_LSTAT) || defined(MS_WINDOWS) |
| 2133 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2134 | result = LSTAT(path->narrow, &st); |
| 2135 | else |
| 2136 | #endif |
| 2137 | #ifdef HAVE_FSTATAT |
| 2138 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2139 | result = fstatat(dir_fd, path->narrow, &st, |
| 2140 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2141 | else |
| 2142 | #endif |
| 2143 | result = STAT(path->narrow, &st); |
| 2144 | Py_END_ALLOW_THREADS |
| 2145 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2146 | if (result != 0) { |
| 2147 | return path_error(path); |
| 2148 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2149 | |
| 2150 | return _pystat_fromstructstat(&st); |
| 2151 | } |
| 2152 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2153 | /*[python input] |
| 2154 | |
| 2155 | for s in """ |
| 2156 | |
| 2157 | FACCESSAT |
| 2158 | FCHMODAT |
| 2159 | FCHOWNAT |
| 2160 | FSTATAT |
| 2161 | LINKAT |
| 2162 | MKDIRAT |
| 2163 | MKFIFOAT |
| 2164 | MKNODAT |
| 2165 | OPENAT |
| 2166 | READLINKAT |
| 2167 | SYMLINKAT |
| 2168 | UNLINKAT |
| 2169 | |
| 2170 | """.strip().split(): |
| 2171 | s = s.strip() |
| 2172 | print(""" |
| 2173 | #ifdef HAVE_{s} |
| 2174 | #define {s}_DIR_FD_CONVERTER dir_fd_converter |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2175 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2176 | #define {s}_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2177 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2178 | """.rstrip().format(s=s)) |
| 2179 | |
| 2180 | for s in """ |
| 2181 | |
| 2182 | FCHDIR |
| 2183 | FCHMOD |
| 2184 | FCHOWN |
| 2185 | FDOPENDIR |
| 2186 | FEXECVE |
| 2187 | FPATHCONF |
| 2188 | FSTATVFS |
| 2189 | FTRUNCATE |
| 2190 | |
| 2191 | """.strip().split(): |
| 2192 | s = s.strip() |
| 2193 | print(""" |
| 2194 | #ifdef HAVE_{s} |
| 2195 | #define PATH_HAVE_{s} 1 |
| 2196 | #else |
| 2197 | #define PATH_HAVE_{s} 0 |
| 2198 | #endif |
| 2199 | |
| 2200 | """.rstrip().format(s=s)) |
| 2201 | [python start generated code]*/ |
| 2202 | |
| 2203 | #ifdef HAVE_FACCESSAT |
| 2204 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter |
| 2205 | #else |
| 2206 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2207 | #endif |
| 2208 | |
| 2209 | #ifdef HAVE_FCHMODAT |
| 2210 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2211 | #else |
| 2212 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2213 | #endif |
| 2214 | |
| 2215 | #ifdef HAVE_FCHOWNAT |
| 2216 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter |
| 2217 | #else |
| 2218 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2219 | #endif |
| 2220 | |
| 2221 | #ifdef HAVE_FSTATAT |
| 2222 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter |
| 2223 | #else |
| 2224 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2225 | #endif |
| 2226 | |
| 2227 | #ifdef HAVE_LINKAT |
| 2228 | #define LINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2229 | #else |
| 2230 | #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2231 | #endif |
| 2232 | |
| 2233 | #ifdef HAVE_MKDIRAT |
| 2234 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter |
| 2235 | #else |
| 2236 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2237 | #endif |
| 2238 | |
| 2239 | #ifdef HAVE_MKFIFOAT |
| 2240 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter |
| 2241 | #else |
| 2242 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2243 | #endif |
| 2244 | |
| 2245 | #ifdef HAVE_MKNODAT |
| 2246 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2247 | #else |
| 2248 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2249 | #endif |
| 2250 | |
| 2251 | #ifdef HAVE_OPENAT |
| 2252 | #define OPENAT_DIR_FD_CONVERTER dir_fd_converter |
| 2253 | #else |
| 2254 | #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2255 | #endif |
| 2256 | |
| 2257 | #ifdef HAVE_READLINKAT |
| 2258 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2259 | #else |
| 2260 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2261 | #endif |
| 2262 | |
| 2263 | #ifdef HAVE_SYMLINKAT |
| 2264 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2265 | #else |
| 2266 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2267 | #endif |
| 2268 | |
| 2269 | #ifdef HAVE_UNLINKAT |
| 2270 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2271 | #else |
| 2272 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2273 | #endif |
| 2274 | |
| 2275 | #ifdef HAVE_FCHDIR |
| 2276 | #define PATH_HAVE_FCHDIR 1 |
| 2277 | #else |
| 2278 | #define PATH_HAVE_FCHDIR 0 |
| 2279 | #endif |
| 2280 | |
| 2281 | #ifdef HAVE_FCHMOD |
| 2282 | #define PATH_HAVE_FCHMOD 1 |
| 2283 | #else |
| 2284 | #define PATH_HAVE_FCHMOD 0 |
| 2285 | #endif |
| 2286 | |
| 2287 | #ifdef HAVE_FCHOWN |
| 2288 | #define PATH_HAVE_FCHOWN 1 |
| 2289 | #else |
| 2290 | #define PATH_HAVE_FCHOWN 0 |
| 2291 | #endif |
| 2292 | |
| 2293 | #ifdef HAVE_FDOPENDIR |
| 2294 | #define PATH_HAVE_FDOPENDIR 1 |
| 2295 | #else |
| 2296 | #define PATH_HAVE_FDOPENDIR 0 |
| 2297 | #endif |
| 2298 | |
| 2299 | #ifdef HAVE_FEXECVE |
| 2300 | #define PATH_HAVE_FEXECVE 1 |
| 2301 | #else |
| 2302 | #define PATH_HAVE_FEXECVE 0 |
| 2303 | #endif |
| 2304 | |
| 2305 | #ifdef HAVE_FPATHCONF |
| 2306 | #define PATH_HAVE_FPATHCONF 1 |
| 2307 | #else |
| 2308 | #define PATH_HAVE_FPATHCONF 0 |
| 2309 | #endif |
| 2310 | |
| 2311 | #ifdef HAVE_FSTATVFS |
| 2312 | #define PATH_HAVE_FSTATVFS 1 |
| 2313 | #else |
| 2314 | #define PATH_HAVE_FSTATVFS 0 |
| 2315 | #endif |
| 2316 | |
| 2317 | #ifdef HAVE_FTRUNCATE |
| 2318 | #define PATH_HAVE_FTRUNCATE 1 |
| 2319 | #else |
| 2320 | #define PATH_HAVE_FTRUNCATE 0 |
| 2321 | #endif |
| 2322 | /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2323 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 2324 | #ifdef MS_WINDOWS |
| 2325 | #undef PATH_HAVE_FTRUNCATE |
| 2326 | #define PATH_HAVE_FTRUNCATE 1 |
| 2327 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2328 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2329 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2330 | |
| 2331 | class path_t_converter(CConverter): |
| 2332 | |
| 2333 | type = "path_t" |
| 2334 | impl_by_reference = True |
| 2335 | parse_by_reference = True |
| 2336 | |
| 2337 | converter = 'path_converter' |
| 2338 | |
| 2339 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2340 | # right now path_t doesn't support default values. |
| 2341 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2342 | if self.default not in (unspecified, None): |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2343 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2344 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2345 | if self.c_default not in (None, 'Py_None'): |
| 2346 | 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] | 2347 | |
| 2348 | self.nullable = nullable |
| 2349 | self.allow_fd = allow_fd |
| 2350 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2351 | def pre_render(self): |
| 2352 | def strify(value): |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2353 | if isinstance(value, str): |
| 2354 | return value |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2355 | return str(int(bool(value))) |
| 2356 | |
| 2357 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2358 | self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format( |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2359 | self.function.name, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2360 | self.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2361 | strify(self.nullable), |
| 2362 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2363 | ) |
| 2364 | |
| 2365 | def cleanup(self): |
| 2366 | return "path_cleanup(&" + self.name + ");\n" |
| 2367 | |
| 2368 | |
| 2369 | class dir_fd_converter(CConverter): |
| 2370 | type = 'int' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2371 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2372 | def converter_init(self, requires=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2373 | if self.default in (unspecified, None): |
| 2374 | self.c_default = 'DEFAULT_DIR_FD' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2375 | if isinstance(requires, str): |
| 2376 | self.converter = requires.upper() + '_DIR_FD_CONVERTER' |
| 2377 | else: |
| 2378 | self.converter = 'dir_fd_converter' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2379 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2380 | class fildes_converter(CConverter): |
| 2381 | type = 'int' |
| 2382 | converter = 'fildes_converter' |
| 2383 | |
| 2384 | class uid_t_converter(CConverter): |
| 2385 | type = "uid_t" |
| 2386 | converter = '_Py_Uid_Converter' |
| 2387 | |
| 2388 | class gid_t_converter(CConverter): |
| 2389 | type = "gid_t" |
| 2390 | converter = '_Py_Gid_Converter' |
| 2391 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2392 | class dev_t_converter(CConverter): |
| 2393 | type = 'dev_t' |
| 2394 | converter = '_Py_Dev_Converter' |
| 2395 | |
| 2396 | class dev_t_return_converter(unsigned_long_return_converter): |
| 2397 | type = 'dev_t' |
| 2398 | conversion_fn = '_PyLong_FromDev' |
| 2399 | unsigned_cast = '(dev_t)' |
| 2400 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2401 | class FSConverter_converter(CConverter): |
| 2402 | type = 'PyObject *' |
| 2403 | converter = 'PyUnicode_FSConverter' |
| 2404 | def converter_init(self): |
| 2405 | if self.default is not unspecified: |
| 2406 | fail("FSConverter_converter does not support default values") |
| 2407 | self.c_default = 'NULL' |
| 2408 | |
| 2409 | def cleanup(self): |
| 2410 | return "Py_XDECREF(" + self.name + ");\n" |
| 2411 | |
| 2412 | class pid_t_converter(CConverter): |
| 2413 | type = 'pid_t' |
| 2414 | format_unit = '" _Py_PARSE_PID "' |
| 2415 | |
| 2416 | class idtype_t_converter(int_converter): |
| 2417 | type = 'idtype_t' |
| 2418 | |
| 2419 | class id_t_converter(CConverter): |
| 2420 | type = 'id_t' |
| 2421 | format_unit = '" _Py_PARSE_PID "' |
| 2422 | |
| 2423 | class Py_intptr_t_converter(CConverter): |
| 2424 | type = 'Py_intptr_t' |
| 2425 | format_unit = '" _Py_PARSE_INTPTR "' |
| 2426 | |
| 2427 | class Py_off_t_converter(CConverter): |
| 2428 | type = 'Py_off_t' |
| 2429 | converter = 'Py_off_t_converter' |
| 2430 | |
| 2431 | class Py_off_t_return_converter(long_return_converter): |
| 2432 | type = 'Py_off_t' |
| 2433 | conversion_fn = 'PyLong_FromPy_off_t' |
| 2434 | |
| 2435 | class path_confname_converter(CConverter): |
| 2436 | type="int" |
| 2437 | converter="conv_path_confname" |
| 2438 | |
| 2439 | class confstr_confname_converter(path_confname_converter): |
| 2440 | converter='conv_confstr_confname' |
| 2441 | |
| 2442 | class sysconf_confname_converter(path_confname_converter): |
| 2443 | converter="conv_sysconf_confname" |
| 2444 | |
| 2445 | class sched_param_converter(CConverter): |
| 2446 | type = 'struct sched_param' |
| 2447 | converter = 'convert_sched_param' |
| 2448 | impl_by_reference = True; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2449 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2450 | [python start generated code]*/ |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2451 | /*[python end generated code: output=da39a3ee5e6b4b0d input=affe68316f160401]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2452 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2453 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2454 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2455 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2456 | |
| 2457 | path : path_t(allow_fd=True) |
| 2458 | Path to be examined; can be string, bytes, or open-file-descriptor int. |
| 2459 | |
| 2460 | * |
| 2461 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2462 | dir_fd : dir_fd(requires='fstatat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2463 | If not None, it should be a file descriptor open to a directory, |
| 2464 | and path should be a relative string; path will then be relative to |
| 2465 | that directory. |
| 2466 | |
| 2467 | follow_symlinks: bool = True |
| 2468 | If False, and the last element of the path is a symbolic link, |
| 2469 | stat will examine the symbolic link itself instead of the file |
| 2470 | the link points to. |
| 2471 | |
| 2472 | Perform a stat system call on the given path. |
| 2473 | |
| 2474 | dir_fd and follow_symlinks may not be implemented |
| 2475 | on your platform. If they are unavailable, using them will raise a |
| 2476 | NotImplementedError. |
| 2477 | |
| 2478 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2479 | an open file descriptor. |
| 2480 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2481 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2482 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2483 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2484 | os_stat_impl(PyModuleDef *module, path_t *path, int dir_fd, |
| 2485 | int follow_symlinks) |
| 2486 | /*[clinic end generated code: output=e4f7569f95d523ca input=099d356c306fa24a]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2487 | { |
| 2488 | return posix_do_stat("stat", path, dir_fd, follow_symlinks); |
| 2489 | } |
| 2490 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2491 | |
| 2492 | /*[clinic input] |
| 2493 | os.lstat |
| 2494 | |
| 2495 | path : path_t |
| 2496 | |
| 2497 | * |
| 2498 | |
| 2499 | dir_fd : dir_fd(requires='fstatat') = None |
| 2500 | |
| 2501 | Perform a stat system call on the given path, without following symbolic links. |
| 2502 | |
| 2503 | Like stat(), but do not follow symbolic links. |
| 2504 | Equivalent to stat(path, follow_symlinks=False). |
| 2505 | [clinic start generated code]*/ |
| 2506 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2507 | static PyObject * |
| 2508 | os_lstat_impl(PyModuleDef *module, path_t *path, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2509 | /*[clinic end generated code: output=7a748e333fcb39bd input=0b7474765927b925]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2510 | { |
| 2511 | int follow_symlinks = 0; |
| 2512 | return posix_do_stat("lstat", path, dir_fd, follow_symlinks); |
| 2513 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2514 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2515 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2516 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2517 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2518 | |
| 2519 | path: path_t(allow_fd=True) |
| 2520 | Path to be tested; can be string, bytes, or open-file-descriptor int. |
| 2521 | |
| 2522 | mode: int |
| 2523 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2524 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2525 | |
| 2526 | * |
| 2527 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2528 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2529 | If not None, it should be a file descriptor open to a directory, |
| 2530 | and path should be relative; path will then be relative to that |
| 2531 | directory. |
| 2532 | |
| 2533 | effective_ids: bool = False |
| 2534 | If True, access will use the effective uid/gid instead of |
| 2535 | the real uid/gid. |
| 2536 | |
| 2537 | follow_symlinks: bool = True |
| 2538 | If False, and the last element of the path is a symbolic link, |
| 2539 | access will examine the symbolic link itself instead of the file |
| 2540 | the link points to. |
| 2541 | |
| 2542 | Use the real uid/gid to test for access to a path. |
| 2543 | |
| 2544 | {parameters} |
| 2545 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2546 | on your platform. If they are unavailable, using them will raise a |
| 2547 | NotImplementedError. |
| 2548 | |
| 2549 | Note that most operations will use the effective uid/gid, therefore this |
| 2550 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2551 | has the specified access to the path. |
| 2552 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2553 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2554 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2555 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2556 | os_access_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, |
| 2557 | int effective_ids, int follow_symlinks) |
| 2558 | /*[clinic end generated code: output=abaa53340210088d input=b75a756797af45ec]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2559 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2560 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2561 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2562 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2563 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2564 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2565 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2566 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2567 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2568 | #ifndef HAVE_FACCESSAT |
| 2569 | if (follow_symlinks_specified("access", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2570 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2571 | |
| 2572 | if (effective_ids) { |
| 2573 | argument_unavailable_error("access", "effective_ids"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2574 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2575 | } |
| 2576 | #endif |
| 2577 | |
| 2578 | #ifdef MS_WINDOWS |
| 2579 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | ebe83f9 | 2013-10-19 22:36:17 +0200 | [diff] [blame] | 2580 | if (path->wide != NULL) |
| 2581 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2582 | else |
Christian Heimes | ebe83f9 | 2013-10-19 22:36:17 +0200 | [diff] [blame] | 2583 | attr = GetFileAttributesA(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2584 | Py_END_ALLOW_THREADS |
| 2585 | |
| 2586 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2587 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2588 | * * we didn't get a -1, and |
| 2589 | * * write access wasn't requested, |
| 2590 | * * or the file isn't read-only, |
| 2591 | * * or it's a directory. |
| 2592 | * (Directories cannot be read-only on Windows.) |
| 2593 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2594 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2595 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2596 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2597 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2598 | #else |
| 2599 | |
| 2600 | Py_BEGIN_ALLOW_THREADS |
| 2601 | #ifdef HAVE_FACCESSAT |
| 2602 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2603 | effective_ids || |
| 2604 | !follow_symlinks) { |
| 2605 | int flags = 0; |
| 2606 | if (!follow_symlinks) |
| 2607 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2608 | if (effective_ids) |
| 2609 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2610 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2611 | } |
| 2612 | else |
| 2613 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2614 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2615 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2616 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2617 | #endif |
| 2618 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2619 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2622 | #ifndef F_OK |
| 2623 | #define F_OK 0 |
| 2624 | #endif |
| 2625 | #ifndef R_OK |
| 2626 | #define R_OK 4 |
| 2627 | #endif |
| 2628 | #ifndef W_OK |
| 2629 | #define W_OK 2 |
| 2630 | #endif |
| 2631 | #ifndef X_OK |
| 2632 | #define X_OK 1 |
| 2633 | #endif |
| 2634 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2635 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2636 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2637 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2638 | os.ttyname -> DecodeFSDefault |
| 2639 | |
| 2640 | fd: int |
| 2641 | Integer file descriptor handle. |
| 2642 | |
| 2643 | / |
| 2644 | |
| 2645 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2646 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2647 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2648 | static char * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2649 | os_ttyname_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2650 | /*[clinic end generated code: output=03ad3d5ccaef75c3 input=5f72ca83e76b3b45]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2651 | { |
| 2652 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2653 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2654 | ret = ttyname(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2655 | if (ret == NULL) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2656 | posix_error(); |
| 2657 | return ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2658 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2659 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2660 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2661 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2662 | /*[clinic input] |
| 2663 | os.ctermid |
| 2664 | |
| 2665 | Return the name of the controlling terminal for this process. |
| 2666 | [clinic start generated code]*/ |
| 2667 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2668 | static PyObject * |
| 2669 | os_ctermid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2670 | /*[clinic end generated code: output=1b73788201e0aebd input=3b87fdd52556382d]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2671 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2672 | char *ret; |
| 2673 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2674 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2675 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2676 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2677 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2678 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2679 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2680 | if (ret == NULL) |
| 2681 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2682 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2683 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2684 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2685 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2686 | |
| 2687 | /*[clinic input] |
| 2688 | os.chdir |
| 2689 | |
| 2690 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') |
| 2691 | |
| 2692 | Change the current working directory to the specified path. |
| 2693 | |
| 2694 | path may always be specified as a string. |
| 2695 | On some platforms, path may also be specified as an open file descriptor. |
| 2696 | If this functionality is unavailable, using it raises an exception. |
| 2697 | [clinic start generated code]*/ |
| 2698 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2699 | static PyObject * |
| 2700 | os_chdir_impl(PyModuleDef *module, path_t *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2701 | /*[clinic end generated code: output=7358e3a20fb5aa93 input=1a4a15b4d12cb15d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2702 | { |
| 2703 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2704 | |
| 2705 | Py_BEGIN_ALLOW_THREADS |
| 2706 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2707 | if (path->wide) |
| 2708 | result = win32_wchdir(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2709 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2710 | result = win32_chdir(path->narrow); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 2711 | result = !result; /* on unix, success = 0, on windows, success = !0 */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2712 | #else |
| 2713 | #ifdef HAVE_FCHDIR |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2714 | if (path->fd != -1) |
| 2715 | result = fchdir(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2716 | else |
| 2717 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2718 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2719 | #endif |
| 2720 | Py_END_ALLOW_THREADS |
| 2721 | |
| 2722 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2723 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2724 | } |
| 2725 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2726 | Py_RETURN_NONE; |
| 2727 | } |
| 2728 | |
| 2729 | |
| 2730 | #ifdef HAVE_FCHDIR |
| 2731 | /*[clinic input] |
| 2732 | os.fchdir |
| 2733 | |
| 2734 | fd: fildes |
| 2735 | |
| 2736 | Change to the directory of the given file descriptor. |
| 2737 | |
| 2738 | fd must be opened on a directory, not a file. |
| 2739 | Equivalent to os.chdir(fd). |
| 2740 | |
| 2741 | [clinic start generated code]*/ |
| 2742 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2743 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2744 | os_fchdir_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2745 | /*[clinic end generated code: output=361d30df6b2d3418 input=18e816479a2fa985]*/ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2746 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2747 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2748 | } |
| 2749 | #endif /* HAVE_FCHDIR */ |
| 2750 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2751 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2752 | /*[clinic input] |
| 2753 | os.chmod |
| 2754 | |
| 2755 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
| 2756 | Path to be modified. May always be specified as a str or bytes. |
| 2757 | On some platforms, path may also be specified as an open file descriptor. |
| 2758 | If this functionality is unavailable, using it raises an exception. |
| 2759 | |
| 2760 | mode: int |
| 2761 | Operating-system mode bitfield. |
| 2762 | |
| 2763 | * |
| 2764 | |
| 2765 | dir_fd : dir_fd(requires='fchmodat') = None |
| 2766 | If not None, it should be a file descriptor open to a directory, |
| 2767 | and path should be relative; path will then be relative to that |
| 2768 | directory. |
| 2769 | |
| 2770 | follow_symlinks: bool = True |
| 2771 | If False, and the last element of the path is a symbolic link, |
| 2772 | chmod will modify the symbolic link itself instead of the file |
| 2773 | the link points to. |
| 2774 | |
| 2775 | Change the access permissions of a file. |
| 2776 | |
| 2777 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 2778 | an open file descriptor. |
| 2779 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 2780 | If they are unavailable, using them will raise a NotImplementedError. |
| 2781 | |
| 2782 | [clinic start generated code]*/ |
| 2783 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2784 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2785 | os_chmod_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, |
| 2786 | int follow_symlinks) |
| 2787 | /*[clinic end generated code: output=05e7f73b1a843ba2 input=7f1618e5e15cc196]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2788 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2789 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2790 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2791 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2792 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2793 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2794 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2795 | #ifdef HAVE_FCHMODAT |
| 2796 | int fchmodat_nofollow_unsupported = 0; |
| 2797 | #endif |
| 2798 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2799 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2800 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2801 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2802 | #endif |
| 2803 | |
| 2804 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2805 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2806 | if (path->wide) |
| 2807 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2808 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2809 | attr = GetFileAttributesA(path->narrow); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 2810 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2811 | result = 0; |
| 2812 | else { |
| 2813 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2814 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2815 | else |
| 2816 | attr |= FILE_ATTRIBUTE_READONLY; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2817 | if (path->wide) |
| 2818 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2819 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2820 | result = SetFileAttributesA(path->narrow, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2821 | } |
| 2822 | Py_END_ALLOW_THREADS |
| 2823 | |
| 2824 | if (!result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2825 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2826 | } |
| 2827 | #else /* MS_WINDOWS */ |
| 2828 | Py_BEGIN_ALLOW_THREADS |
| 2829 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2830 | if (path->fd != -1) |
| 2831 | result = fchmod(path->fd, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2832 | else |
| 2833 | #endif |
| 2834 | #ifdef HAVE_LCHMOD |
| 2835 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2836 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2837 | else |
| 2838 | #endif |
| 2839 | #ifdef HAVE_FCHMODAT |
| 2840 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2841 | /* |
| 2842 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2843 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2844 | * and then says it isn't implemented yet. |
| 2845 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2846 | * |
| 2847 | * Once it is supported, os.chmod will automatically |
| 2848 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2849 | * Until then, we need to be careful what exception we raise. |
| 2850 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2851 | result = fchmodat(dir_fd, path->narrow, mode, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2852 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2853 | /* |
| 2854 | * But wait! We can't throw the exception without allowing threads, |
| 2855 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2856 | */ |
| 2857 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2858 | result && |
| 2859 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2860 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2861 | } |
| 2862 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2863 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2864 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2865 | Py_END_ALLOW_THREADS |
| 2866 | |
| 2867 | if (result) { |
| 2868 | #ifdef HAVE_FCHMODAT |
| 2869 | if (fchmodat_nofollow_unsupported) { |
| 2870 | if (dir_fd != DEFAULT_DIR_FD) |
| 2871 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 2872 | dir_fd, follow_symlinks); |
| 2873 | else |
| 2874 | follow_symlinks_specified("chmod", follow_symlinks); |
| 2875 | } |
| 2876 | else |
| 2877 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2878 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2879 | } |
| 2880 | #endif |
| 2881 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2882 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2885 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2886 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2887 | /*[clinic input] |
| 2888 | os.fchmod |
| 2889 | |
| 2890 | fd: int |
| 2891 | mode: int |
| 2892 | |
| 2893 | Change the access permissions of the file given by file descriptor fd. |
| 2894 | |
| 2895 | Equivalent to os.chmod(fd, mode). |
| 2896 | [clinic start generated code]*/ |
| 2897 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2898 | static PyObject * |
| 2899 | os_fchmod_impl(PyModuleDef *module, int fd, int mode) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2900 | /*[clinic end generated code: output=2ee31ca226d1ed33 input=8ab11975ca01ee5b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2901 | { |
| 2902 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2903 | int async_err = 0; |
| 2904 | |
| 2905 | do { |
| 2906 | Py_BEGIN_ALLOW_THREADS |
| 2907 | res = fchmod(fd, mode); |
| 2908 | Py_END_ALLOW_THREADS |
| 2909 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 2910 | if (res != 0) |
| 2911 | return (!async_err) ? posix_error() : NULL; |
| 2912 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2913 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2914 | } |
| 2915 | #endif /* HAVE_FCHMOD */ |
| 2916 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2917 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2918 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2919 | /*[clinic input] |
| 2920 | os.lchmod |
| 2921 | |
| 2922 | path: path_t |
| 2923 | mode: int |
| 2924 | |
| 2925 | Change the access permissions of a file, without following symbolic links. |
| 2926 | |
| 2927 | If path is a symlink, this affects the link itself rather than the target. |
| 2928 | Equivalent to chmod(path, mode, follow_symlinks=False)." |
| 2929 | [clinic start generated code]*/ |
| 2930 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2931 | static PyObject * |
| 2932 | os_lchmod_impl(PyModuleDef *module, path_t *path, int mode) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2933 | /*[clinic end generated code: output=7c0cc46588d89e46 input=90c5663c7465d24f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2934 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2935 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2936 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 2937 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2938 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2939 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2940 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2941 | return NULL; |
| 2942 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2943 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2944 | } |
| 2945 | #endif /* HAVE_LCHMOD */ |
| 2946 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2947 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2948 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2949 | /*[clinic input] |
| 2950 | os.chflags |
| 2951 | |
| 2952 | path: path_t |
| 2953 | flags: unsigned_long(bitwise=True) |
| 2954 | follow_symlinks: bool=True |
| 2955 | |
| 2956 | Set file flags. |
| 2957 | |
| 2958 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 2959 | link, chflags will change flags on the symbolic link itself instead of the |
| 2960 | file the link points to. |
| 2961 | follow_symlinks may not be implemented on your platform. If it is |
| 2962 | unavailable, using it will raise a NotImplementedError. |
| 2963 | |
| 2964 | [clinic start generated code]*/ |
| 2965 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2966 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2967 | os_chflags_impl(PyModuleDef *module, path_t *path, unsigned long flags, |
| 2968 | int follow_symlinks) |
| 2969 | /*[clinic end generated code: output=ff2d6e73534a95b9 input=0327e29feb876236]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2970 | { |
| 2971 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2972 | |
| 2973 | #ifndef HAVE_LCHFLAGS |
| 2974 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2975 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2976 | #endif |
| 2977 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2978 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2979 | #ifdef HAVE_LCHFLAGS |
| 2980 | if (!follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2981 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2982 | else |
| 2983 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2984 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2985 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2986 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2987 | if (result) |
| 2988 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2989 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2990 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2991 | } |
| 2992 | #endif /* HAVE_CHFLAGS */ |
| 2993 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2994 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2995 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2996 | /*[clinic input] |
| 2997 | os.lchflags |
| 2998 | |
| 2999 | path: path_t |
| 3000 | flags: unsigned_long(bitwise=True) |
| 3001 | |
| 3002 | Set file flags. |
| 3003 | |
| 3004 | This function will not follow symbolic links. |
| 3005 | Equivalent to chflags(path, flags, follow_symlinks=False). |
| 3006 | [clinic start generated code]*/ |
| 3007 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3008 | static PyObject * |
| 3009 | os_lchflags_impl(PyModuleDef *module, path_t *path, unsigned long flags) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3010 | /*[clinic end generated code: output=6741322fb949661b input=f9f82ea8b585ca9d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3011 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3012 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3013 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3014 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3015 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3016 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3017 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3018 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3019 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3020 | } |
| 3021 | #endif /* HAVE_LCHFLAGS */ |
| 3022 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3023 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3024 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3025 | /*[clinic input] |
| 3026 | os.chroot |
| 3027 | path: path_t |
| 3028 | |
| 3029 | Change root directory to path. |
| 3030 | |
| 3031 | [clinic start generated code]*/ |
| 3032 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3033 | static PyObject * |
| 3034 | os_chroot_impl(PyModuleDef *module, path_t *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3035 | /*[clinic end generated code: output=b6dbfabe74ecaa9d input=14822965652c3dc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3036 | { |
| 3037 | int res; |
| 3038 | Py_BEGIN_ALLOW_THREADS |
| 3039 | res = chroot(path->narrow); |
| 3040 | Py_END_ALLOW_THREADS |
| 3041 | if (res < 0) |
| 3042 | return path_error(path); |
| 3043 | Py_RETURN_NONE; |
| 3044 | } |
| 3045 | #endif /* HAVE_CHROOT */ |
| 3046 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3047 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3048 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3049 | /*[clinic input] |
| 3050 | os.fsync |
| 3051 | |
| 3052 | fd: fildes |
| 3053 | |
| 3054 | Force write of fd to disk. |
| 3055 | [clinic start generated code]*/ |
| 3056 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3057 | static PyObject * |
| 3058 | os_fsync_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3059 | /*[clinic end generated code: output=83a350851064aea7 input=21c3645c056967f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3060 | { |
| 3061 | return posix_fildes_fd(fd, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3062 | } |
| 3063 | #endif /* HAVE_FSYNC */ |
| 3064 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3065 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3066 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3067 | /*[clinic input] |
| 3068 | os.sync |
| 3069 | |
| 3070 | Force write of everything to disk. |
| 3071 | [clinic start generated code]*/ |
| 3072 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3073 | static PyObject * |
| 3074 | os_sync_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3075 | /*[clinic end generated code: output=ba524f656c201c40 input=84749fe5e9b404ff]*/ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3076 | { |
| 3077 | Py_BEGIN_ALLOW_THREADS |
| 3078 | sync(); |
| 3079 | Py_END_ALLOW_THREADS |
| 3080 | Py_RETURN_NONE; |
| 3081 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3082 | #endif /* HAVE_SYNC */ |
| 3083 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3084 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3085 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3086 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3087 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3088 | #endif |
| 3089 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3090 | /*[clinic input] |
| 3091 | os.fdatasync |
| 3092 | |
| 3093 | fd: fildes |
| 3094 | |
| 3095 | Force write of fd to disk without forcing update of metadata. |
| 3096 | [clinic start generated code]*/ |
| 3097 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3098 | static PyObject * |
| 3099 | os_fdatasync_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3100 | /*[clinic end generated code: output=e0f04a3aff515b75 input=bc74791ee54dd291]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3101 | { |
| 3102 | return posix_fildes_fd(fd, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3103 | } |
| 3104 | #endif /* HAVE_FDATASYNC */ |
| 3105 | |
| 3106 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3107 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3108 | /*[clinic input] |
| 3109 | os.chown |
| 3110 | |
| 3111 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') |
| 3112 | Path to be examined; can be string, bytes, or open-file-descriptor int. |
| 3113 | |
| 3114 | uid: uid_t |
| 3115 | |
| 3116 | gid: gid_t |
| 3117 | |
| 3118 | * |
| 3119 | |
| 3120 | dir_fd : dir_fd(requires='fchownat') = None |
| 3121 | If not None, it should be a file descriptor open to a directory, |
| 3122 | and path should be relative; path will then be relative to that |
| 3123 | directory. |
| 3124 | |
| 3125 | follow_symlinks: bool = True |
| 3126 | If False, and the last element of the path is a symbolic link, |
| 3127 | stat will examine the symbolic link itself instead of the file |
| 3128 | the link points to. |
| 3129 | |
| 3130 | Change the owner and group id of path to the numeric uid and gid.\ |
| 3131 | |
| 3132 | path may always be specified as a string. |
| 3133 | On some platforms, path may also be specified as an open file descriptor. |
| 3134 | If this functionality is unavailable, using it raises an exception. |
| 3135 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3136 | and path should be relative; path will then be relative to that directory. |
| 3137 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3138 | link, chown will modify the symbolic link itself instead of the file the |
| 3139 | link points to. |
| 3140 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3141 | an open file descriptor. |
| 3142 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3143 | If they are unavailable, using them will raise a NotImplementedError. |
| 3144 | |
| 3145 | [clinic start generated code]*/ |
| 3146 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3147 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3148 | os_chown_impl(PyModuleDef *module, path_t *path, uid_t uid, gid_t gid, |
| 3149 | int dir_fd, int follow_symlinks) |
| 3150 | /*[clinic end generated code: output=e0a4559f394dbd91 input=a61cc35574814d5d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3151 | { |
| 3152 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3153 | |
| 3154 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3155 | if (follow_symlinks_specified("chown", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3156 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3157 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3158 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
| 3159 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) |
| 3160 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3161 | |
| 3162 | #ifdef __APPLE__ |
| 3163 | /* |
| 3164 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3165 | * (But we still have an lchown symbol because of weak-linking.) |
| 3166 | * It doesn't have fchownat either. So there's no possibility |
| 3167 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3168 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3169 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3170 | follow_symlinks_specified("chown", follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3171 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3172 | } |
| 3173 | #endif |
| 3174 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3175 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3176 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3177 | if (path->fd != -1) |
| 3178 | result = fchown(path->fd, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3179 | else |
| 3180 | #endif |
| 3181 | #ifdef HAVE_LCHOWN |
| 3182 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3183 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3184 | else |
| 3185 | #endif |
| 3186 | #ifdef HAVE_FCHOWNAT |
| 3187 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3188 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3189 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3190 | else |
| 3191 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3192 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3193 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3194 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3195 | if (result) |
| 3196 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3197 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3198 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3199 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3200 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3201 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3202 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3203 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3204 | /*[clinic input] |
| 3205 | os.fchown |
| 3206 | |
| 3207 | fd: int |
| 3208 | uid: uid_t |
| 3209 | gid: gid_t |
| 3210 | |
| 3211 | Change the owner and group id of the file specified by file descriptor. |
| 3212 | |
| 3213 | Equivalent to os.chown(fd, uid, gid). |
| 3214 | |
| 3215 | [clinic start generated code]*/ |
| 3216 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3217 | static PyObject * |
| 3218 | os_fchown_impl(PyModuleDef *module, int fd, uid_t uid, gid_t gid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3219 | /*[clinic end generated code: output=7545abf8f6086d76 input=3af544ba1b13a0d7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3220 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3221 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3222 | int async_err = 0; |
| 3223 | |
| 3224 | do { |
| 3225 | Py_BEGIN_ALLOW_THREADS |
| 3226 | res = fchown(fd, uid, gid); |
| 3227 | Py_END_ALLOW_THREADS |
| 3228 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3229 | if (res != 0) |
| 3230 | return (!async_err) ? posix_error() : NULL; |
| 3231 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3232 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3233 | } |
| 3234 | #endif /* HAVE_FCHOWN */ |
| 3235 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3236 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3237 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3238 | /*[clinic input] |
| 3239 | os.lchown |
| 3240 | |
| 3241 | path : path_t |
| 3242 | uid: uid_t |
| 3243 | gid: gid_t |
| 3244 | |
| 3245 | Change the owner and group id of path to the numeric uid and gid. |
| 3246 | |
| 3247 | This function will not follow symbolic links. |
| 3248 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). |
| 3249 | [clinic start generated code]*/ |
| 3250 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3251 | static PyObject * |
| 3252 | os_lchown_impl(PyModuleDef *module, path_t *path, uid_t uid, gid_t gid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3253 | /*[clinic end generated code: output=bb0d2da1579ac275 input=b1c6014d563a7161]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3254 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3255 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3256 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3257 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3258 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3259 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3260 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3261 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3262 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3263 | } |
| 3264 | #endif /* HAVE_LCHOWN */ |
| 3265 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3266 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3267 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3268 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3269 | { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3270 | char *buf, *tmpbuf; |
| 3271 | char *cwd; |
| 3272 | const size_t chunk = 1024; |
| 3273 | size_t buflen = 0; |
| 3274 | PyObject *obj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3275 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3276 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3277 | if (!use_bytes) { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3278 | wchar_t wbuf[MAXPATHLEN]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3279 | wchar_t *wbuf2 = wbuf; |
| 3280 | PyObject *resobj; |
| 3281 | DWORD len; |
| 3282 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3283 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3284 | /* If the buffer is large enough, len does not include the |
| 3285 | terminating \0. If the buffer is too small, len includes |
| 3286 | the space needed for the terminator. */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3287 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3288 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3289 | if (wbuf2) |
| 3290 | len = GetCurrentDirectoryW(len, wbuf2); |
| 3291 | } |
| 3292 | Py_END_ALLOW_THREADS |
| 3293 | if (!wbuf2) { |
| 3294 | PyErr_NoMemory(); |
| 3295 | return NULL; |
| 3296 | } |
| 3297 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3298 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3299 | PyMem_RawFree(wbuf2); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3300 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3301 | } |
| 3302 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3303 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3304 | PyMem_RawFree(wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3305 | return resobj; |
| 3306 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3307 | |
| 3308 | if (win32_warn_bytes_api()) |
| 3309 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3310 | #endif |
| 3311 | |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3312 | buf = cwd = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3313 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3314 | do { |
| 3315 | buflen += chunk; |
| 3316 | tmpbuf = PyMem_RawRealloc(buf, buflen); |
| 3317 | if (tmpbuf == NULL) |
| 3318 | break; |
| 3319 | |
| 3320 | buf = tmpbuf; |
| 3321 | cwd = getcwd(buf, buflen); |
| 3322 | } while (cwd == NULL && errno == ERANGE); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3323 | Py_END_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3324 | |
| 3325 | if (cwd == NULL) { |
| 3326 | PyMem_RawFree(buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3327 | return posix_error(); |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3328 | } |
| 3329 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3330 | if (use_bytes) |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3331 | obj = PyBytes_FromStringAndSize(buf, strlen(buf)); |
| 3332 | else |
| 3333 | obj = PyUnicode_DecodeFSDefault(buf); |
| 3334 | PyMem_RawFree(buf); |
| 3335 | |
| 3336 | return obj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3337 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3338 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3339 | |
| 3340 | /*[clinic input] |
| 3341 | os.getcwd |
| 3342 | |
| 3343 | Return a unicode string representing the current working directory. |
| 3344 | [clinic start generated code]*/ |
| 3345 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3346 | static PyObject * |
| 3347 | os_getcwd_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3348 | /*[clinic end generated code: output=efe3a8c0121525ea input=f069211bb70e3d39]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3349 | { |
| 3350 | return posix_getcwd(0); |
| 3351 | } |
| 3352 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3353 | |
| 3354 | /*[clinic input] |
| 3355 | os.getcwdb |
| 3356 | |
| 3357 | Return a bytes string representing the current working directory. |
| 3358 | [clinic start generated code]*/ |
| 3359 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3360 | static PyObject * |
| 3361 | os_getcwdb_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3362 | /*[clinic end generated code: output=7fce42ee4b2a296a input=f6f6a378dad3d9cb]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3363 | { |
| 3364 | return posix_getcwd(1); |
| 3365 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3366 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3367 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3368 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3369 | #define HAVE_LINK 1 |
| 3370 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3371 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3372 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3373 | /*[clinic input] |
| 3374 | |
| 3375 | os.link |
| 3376 | |
| 3377 | src : path_t |
| 3378 | dst : path_t |
| 3379 | * |
| 3380 | src_dir_fd : dir_fd = None |
| 3381 | dst_dir_fd : dir_fd = None |
| 3382 | follow_symlinks: bool = True |
| 3383 | |
| 3384 | Create a hard link to a file. |
| 3385 | |
| 3386 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 3387 | descriptor open to a directory, and the respective path string (src or dst) |
| 3388 | should be relative; the path will then be relative to that directory. |
| 3389 | If follow_symlinks is False, and the last element of src is a symbolic |
| 3390 | link, link will create a link to the symbolic link itself instead of the |
| 3391 | file the link points to. |
| 3392 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 3393 | platform. If they are unavailable, using them will raise a |
| 3394 | NotImplementedError. |
| 3395 | [clinic start generated code]*/ |
| 3396 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3397 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3398 | os_link_impl(PyModuleDef *module, path_t *src, path_t *dst, int src_dir_fd, |
| 3399 | int dst_dir_fd, int follow_symlinks) |
| 3400 | /*[clinic end generated code: output=f47a7e88f7b391b6 input=b0095ebbcbaa7e04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3401 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3402 | #ifdef MS_WINDOWS |
| 3403 | BOOL result; |
| 3404 | #else |
| 3405 | int result; |
| 3406 | #endif |
| 3407 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3408 | #ifndef HAVE_LINKAT |
| 3409 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3410 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3411 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3412 | } |
| 3413 | #endif |
| 3414 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3415 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3416 | PyErr_SetString(PyExc_NotImplementedError, |
| 3417 | "link: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3418 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3419 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3420 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3421 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3422 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3423 | if (src->wide) |
| 3424 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3425 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3426 | result = CreateHardLinkA(dst->narrow, src->narrow, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3427 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3428 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3429 | if (!result) |
| 3430 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3431 | #else |
| 3432 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3433 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3434 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3435 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3436 | (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3437 | result = linkat(src_dir_fd, src->narrow, |
| 3438 | dst_dir_fd, dst->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3439 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3440 | else |
| 3441 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3442 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3443 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3444 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3445 | if (result) |
| 3446 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3447 | #endif |
| 3448 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3449 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3450 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3451 | #endif |
| 3452 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3453 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3454 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3455 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3456 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3457 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3458 | PyObject *v; |
| 3459 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3460 | BOOL result; |
| 3461 | WIN32_FIND_DATA FileData; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3462 | char namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3463 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3464 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3465 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3466 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3467 | if (!path->narrow) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3468 | WIN32_FIND_DATAW wFileData; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3469 | wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3470 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3471 | if (!path->wide) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3472 | po_wchars = L"."; |
| 3473 | len = 1; |
| 3474 | } else { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3475 | po_wchars = path->wide; |
| 3476 | len = wcslen(path->wide); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3477 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3478 | /* The +5 is so we can append "\\*.*\0" */ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3479 | wnamebuf = PyMem_New(wchar_t, len + 5); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3480 | if (!wnamebuf) { |
| 3481 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3482 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3483 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3484 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3485 | if (len > 0) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3486 | wchar_t wch = wnamebuf[len-1]; |
Tim Golden | 781bbeb | 2013-10-25 20:24:06 +0100 | [diff] [blame] | 3487 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3488 | wnamebuf[len++] = SEP; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3489 | wcscpy(wnamebuf + len, L"*.*"); |
| 3490 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3491 | if ((list = PyList_New(0)) == NULL) { |
| 3492 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3493 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3494 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3495 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3496 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3497 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3498 | int error = GetLastError(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3499 | if (error == ERROR_FILE_NOT_FOUND) |
| 3500 | goto exit; |
| 3501 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3502 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3503 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3504 | } |
| 3505 | do { |
| 3506 | /* Skip over . and .. */ |
| 3507 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3508 | wcscmp(wFileData.cFileName, L"..") != 0) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3509 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3510 | wcslen(wFileData.cFileName)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3511 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3512 | Py_DECREF(list); |
| 3513 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3514 | break; |
| 3515 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3516 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3517 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3518 | Py_DECREF(list); |
| 3519 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3520 | break; |
| 3521 | } |
| 3522 | Py_DECREF(v); |
| 3523 | } |
| 3524 | Py_BEGIN_ALLOW_THREADS |
| 3525 | result = FindNextFileW(hFindFile, &wFileData); |
| 3526 | Py_END_ALLOW_THREADS |
| 3527 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3528 | it got to the end of the directory. */ |
| 3529 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3530 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3531 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3532 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3533 | } |
| 3534 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3535 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3536 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3537 | } |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3538 | strcpy(namebuf, path->narrow); |
| 3539 | len = path->length; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3540 | if (len > 0) { |
| 3541 | char ch = namebuf[len-1]; |
Tim Golden | 781bbeb | 2013-10-25 20:24:06 +0100 | [diff] [blame] | 3542 | if (ch != '\\' && ch != '/' && ch != ':') |
| 3543 | namebuf[len++] = '\\'; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3544 | strcpy(namebuf + len, "*.*"); |
| 3545 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3546 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3547 | if ((list = PyList_New(0)) == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3548 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3549 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3550 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3551 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3552 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3553 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3554 | int error = GetLastError(); |
| 3555 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3556 | goto exit; |
| 3557 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3558 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3559 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3560 | } |
| 3561 | do { |
| 3562 | /* Skip over . and .. */ |
| 3563 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 3564 | strcmp(FileData.cFileName, "..") != 0) { |
| 3565 | v = PyBytes_FromString(FileData.cFileName); |
| 3566 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3567 | Py_DECREF(list); |
| 3568 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3569 | break; |
| 3570 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3571 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3572 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3573 | Py_DECREF(list); |
| 3574 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3575 | break; |
| 3576 | } |
| 3577 | Py_DECREF(v); |
| 3578 | } |
| 3579 | Py_BEGIN_ALLOW_THREADS |
| 3580 | result = FindNextFile(hFindFile, &FileData); |
| 3581 | Py_END_ALLOW_THREADS |
| 3582 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3583 | it got to the end of the directory. */ |
| 3584 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3585 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3586 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3587 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3588 | } |
| 3589 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3590 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3591 | exit: |
| 3592 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3593 | if (FindClose(hFindFile) == FALSE) { |
| 3594 | if (list != NULL) { |
| 3595 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3596 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3597 | } |
| 3598 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3599 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3600 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3601 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3602 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3603 | } /* end of _listdir_windows_no_opendir */ |
| 3604 | |
| 3605 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3606 | |
| 3607 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3608 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3609 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3610 | PyObject *v; |
| 3611 | DIR *dirp = NULL; |
| 3612 | struct dirent *ep; |
| 3613 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3614 | #ifdef HAVE_FDOPENDIR |
| 3615 | int fd = -1; |
| 3616 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3617 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3618 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3619 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3620 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3621 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3622 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3623 | if (fd == -1) |
| 3624 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3625 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3626 | return_str = 1; |
| 3627 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3628 | Py_BEGIN_ALLOW_THREADS |
| 3629 | dirp = fdopendir(fd); |
| 3630 | Py_END_ALLOW_THREADS |
| 3631 | } |
| 3632 | else |
| 3633 | #endif |
| 3634 | { |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3635 | char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3636 | if (path->narrow) { |
| 3637 | name = path->narrow; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3638 | /* only return bytes if they specified a bytes object */ |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3639 | return_str = !(PyBytes_Check(path->object)); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3640 | } |
| 3641 | else { |
| 3642 | name = "."; |
| 3643 | return_str = 1; |
| 3644 | } |
| 3645 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3646 | Py_BEGIN_ALLOW_THREADS |
| 3647 | dirp = opendir(name); |
| 3648 | Py_END_ALLOW_THREADS |
| 3649 | } |
| 3650 | |
| 3651 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3652 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3653 | #ifdef HAVE_FDOPENDIR |
| 3654 | if (fd != -1) { |
| 3655 | Py_BEGIN_ALLOW_THREADS |
| 3656 | close(fd); |
| 3657 | Py_END_ALLOW_THREADS |
| 3658 | } |
| 3659 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3660 | goto exit; |
| 3661 | } |
| 3662 | if ((list = PyList_New(0)) == NULL) { |
| 3663 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3664 | } |
| 3665 | for (;;) { |
| 3666 | errno = 0; |
| 3667 | Py_BEGIN_ALLOW_THREADS |
| 3668 | ep = readdir(dirp); |
| 3669 | Py_END_ALLOW_THREADS |
| 3670 | if (ep == NULL) { |
| 3671 | if (errno == 0) { |
| 3672 | break; |
| 3673 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3674 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3675 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3676 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3677 | } |
| 3678 | } |
| 3679 | if (ep->d_name[0] == '.' && |
| 3680 | (NAMLEN(ep) == 1 || |
| 3681 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3682 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3683 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3684 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3685 | else |
| 3686 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3687 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3688 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3689 | break; |
| 3690 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3691 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3692 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3693 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3694 | break; |
| 3695 | } |
| 3696 | Py_DECREF(v); |
| 3697 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3698 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3699 | exit: |
| 3700 | if (dirp != NULL) { |
| 3701 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3702 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3703 | if (fd > -1) |
| 3704 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3705 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3706 | closedir(dirp); |
| 3707 | Py_END_ALLOW_THREADS |
| 3708 | } |
| 3709 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3710 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3711 | } /* end of _posix_listdir */ |
| 3712 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3713 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3714 | |
| 3715 | /*[clinic input] |
| 3716 | os.listdir |
| 3717 | |
| 3718 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
| 3719 | |
| 3720 | Return a list containing the names of the files in the directory. |
| 3721 | |
| 3722 | path can be specified as either str or bytes. If path is bytes, |
| 3723 | the filenames returned will also be bytes; in all other circumstances |
| 3724 | the filenames returned will be str. |
| 3725 | If path is None, uses the path='.'. |
| 3726 | On some platforms, path may also be specified as an open file descriptor;\ |
| 3727 | the file descriptor must refer to a directory. |
| 3728 | If this functionality is unavailable, using it raises NotImplementedError. |
| 3729 | |
| 3730 | The list is in arbitrary order. It does not include the special |
| 3731 | entries '.' and '..' even if they are present in the directory. |
| 3732 | |
| 3733 | |
| 3734 | [clinic start generated code]*/ |
| 3735 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3736 | static PyObject * |
| 3737 | os_listdir_impl(PyModuleDef *module, path_t *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3738 | /*[clinic end generated code: output=1fbe67c1f780c8b7 input=09e300416e3cd729]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3739 | { |
| 3740 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3741 | return _listdir_windows_no_opendir(path, NULL); |
| 3742 | #else |
| 3743 | return _posix_listdir(path, NULL); |
| 3744 | #endif |
| 3745 | } |
| 3746 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3747 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3748 | /* A helper function for abspath on win32 */ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3749 | /*[clinic input] |
| 3750 | os._getfullpathname |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3751 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3752 | path: path_t |
| 3753 | / |
| 3754 | |
| 3755 | [clinic start generated code]*/ |
| 3756 | |
| 3757 | static PyObject * |
| 3758 | os__getfullpathname_impl(PyModuleDef *module, path_t *path) |
| 3759 | /*[clinic end generated code: output=b90b1f103b08773f input=332ed537c29d0a3e]*/ |
| 3760 | { |
| 3761 | if (!path->narrow) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3762 | { |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3763 | wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3764 | wchar_t *wtemp; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3765 | DWORD result; |
| 3766 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3767 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3768 | result = GetFullPathNameW(path->wide, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3769 | Py_ARRAY_LENGTH(woutbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3770 | woutbuf, &wtemp); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3771 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3772 | woutbufp = PyMem_New(wchar_t, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3773 | if (!woutbufp) |
| 3774 | return PyErr_NoMemory(); |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3775 | result = GetFullPathNameW(path->wide, result, woutbufp, &wtemp); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3776 | } |
| 3777 | if (result) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3778 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3779 | else |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3780 | v = win32_error_object("GetFullPathNameW", path->object); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3781 | if (woutbufp != woutbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3782 | PyMem_Free(woutbufp); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3783 | return v; |
| 3784 | } |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3785 | else { |
| 3786 | char outbuf[MAX_PATH]; |
| 3787 | char *temp; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3788 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3789 | if (!GetFullPathName(path->narrow, Py_ARRAY_LENGTH(outbuf), |
| 3790 | outbuf, &temp)) { |
| 3791 | win32_error_object("GetFullPathName", path->object); |
| 3792 | return NULL; |
| 3793 | } |
| 3794 | return PyBytes_FromString(outbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3795 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3796 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3797 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3798 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3799 | /*[clinic input] |
| 3800 | os._getfinalpathname |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3801 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3802 | path: unicode |
| 3803 | / |
| 3804 | |
| 3805 | A helper function for samepath on windows. |
| 3806 | [clinic start generated code]*/ |
| 3807 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3808 | static PyObject * |
| 3809 | os__getfinalpathname_impl(PyModuleDef *module, PyObject *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3810 | /*[clinic end generated code: output=8be81a5f51a34bcf input=71d5e89334891bf4]*/ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3811 | { |
| 3812 | HANDLE hFile; |
| 3813 | int buf_size; |
| 3814 | wchar_t *target_path; |
| 3815 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3816 | PyObject *result; |
| 3817 | wchar_t *path_wchar; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3818 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3819 | path_wchar = PyUnicode_AsUnicode(path); |
| 3820 | if (path_wchar == NULL) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3821 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3822 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3823 | hFile = CreateFileW( |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3824 | path_wchar, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3825 | 0, /* desired access */ |
| 3826 | 0, /* share mode */ |
| 3827 | NULL, /* security attributes */ |
| 3828 | OPEN_EXISTING, |
| 3829 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3830 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3831 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3832 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3833 | if(hFile == INVALID_HANDLE_VALUE) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3834 | return win32_error_object("CreateFileW", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3835 | |
| 3836 | /* We have a good handle to the target, use it to determine the |
| 3837 | target path name. */ |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 3838 | buf_size = GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3839 | |
| 3840 | if(!buf_size) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3841 | return win32_error_object("GetFinalPathNameByHandle", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3842 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3843 | target_path = PyMem_New(wchar_t, buf_size+1); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3844 | if(!target_path) |
| 3845 | return PyErr_NoMemory(); |
| 3846 | |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 3847 | result_length = GetFinalPathNameByHandleW(hFile, target_path, |
| 3848 | buf_size, VOLUME_NAME_DOS); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3849 | if(!result_length) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3850 | return win32_error_object("GetFinalPathNamyByHandle", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3851 | |
| 3852 | if(!CloseHandle(hFile)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3853 | return win32_error_object("CloseHandle", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3854 | |
| 3855 | target_path[result_length] = 0; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3856 | result = PyUnicode_FromWideChar(target_path, result_length); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3857 | PyMem_Free(target_path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3858 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3859 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3860 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3861 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3862 | "Return true if the pathname refers to an existing directory."); |
| 3863 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3864 | /*[clinic input] |
| 3865 | os._isdir |
| 3866 | |
| 3867 | path: path_t |
| 3868 | / |
| 3869 | |
| 3870 | [clinic start generated code]*/ |
| 3871 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3872 | static PyObject * |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3873 | os__isdir_impl(PyModuleDef *module, path_t *path) |
| 3874 | /*[clinic end generated code: output=f17b2d4e1994b0ff input=e794f12faab62a2a]*/ |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3875 | { |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3876 | DWORD attributes; |
| 3877 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3878 | if (!path->narrow) |
| 3879 | attributes = GetFileAttributesW(path->wide); |
| 3880 | else |
| 3881 | attributes = GetFileAttributesA(path->narrow); |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3882 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3883 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3884 | Py_RETURN_FALSE; |
| 3885 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3886 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3887 | Py_RETURN_TRUE; |
| 3888 | else |
| 3889 | Py_RETURN_FALSE; |
| 3890 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3891 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3892 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3893 | /*[clinic input] |
| 3894 | os._getvolumepathname |
| 3895 | |
| 3896 | path: unicode |
| 3897 | |
| 3898 | A helper function for ismount on Win32. |
| 3899 | [clinic start generated code]*/ |
| 3900 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3901 | static PyObject * |
| 3902 | os__getvolumepathname_impl(PyModuleDef *module, PyObject *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3903 | /*[clinic end generated code: output=79a0ba729f956dbe input=7eacadc40acbda6b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3904 | { |
| 3905 | PyObject *result; |
| 3906 | wchar_t *path_wchar, *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3907 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3908 | BOOL ret; |
| 3909 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3910 | path_wchar = PyUnicode_AsUnicodeAndSize(path, &buflen); |
| 3911 | if (path_wchar == NULL) |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3912 | return NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3913 | buflen += 1; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3914 | |
| 3915 | /* Volume path should be shorter than entire path */ |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3916 | buflen = Py_MAX(buflen, MAX_PATH); |
| 3917 | |
| 3918 | if (buflen > DWORD_MAX) { |
| 3919 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 3920 | return NULL; |
| 3921 | } |
| 3922 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3923 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3924 | if (mountpath == NULL) |
| 3925 | return PyErr_NoMemory(); |
| 3926 | |
| 3927 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3928 | ret = GetVolumePathNameW(path_wchar, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3929 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3930 | Py_END_ALLOW_THREADS |
| 3931 | |
| 3932 | if (!ret) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3933 | result = win32_error_object("_getvolumepathname", path); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3934 | goto exit; |
| 3935 | } |
| 3936 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
| 3937 | |
| 3938 | exit: |
| 3939 | PyMem_Free(mountpath); |
| 3940 | return result; |
| 3941 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3942 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3943 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3944 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3945 | |
| 3946 | /*[clinic input] |
| 3947 | os.mkdir |
| 3948 | |
| 3949 | path : path_t |
| 3950 | |
| 3951 | mode: int = 0o777 |
| 3952 | |
| 3953 | * |
| 3954 | |
| 3955 | dir_fd : dir_fd(requires='mkdirat') = None |
| 3956 | |
| 3957 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 3958 | |
| 3959 | Create a directory. |
| 3960 | |
| 3961 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3962 | and path should be relative; path will then be relative to that directory. |
| 3963 | dir_fd may not be implemented on your platform. |
| 3964 | If it is unavailable, using it will raise a NotImplementedError. |
| 3965 | |
| 3966 | The mode argument is ignored on Windows. |
| 3967 | [clinic start generated code]*/ |
| 3968 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3969 | static PyObject * |
| 3970 | os_mkdir_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3971 | /*[clinic end generated code: output=8bf1f738873ef2c5 input=e965f68377e9b1ce]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3972 | { |
| 3973 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3974 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3975 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3976 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3977 | if (path->wide) |
| 3978 | result = CreateDirectoryW(path->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3979 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3980 | result = CreateDirectoryA(path->narrow, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3981 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3982 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3983 | if (!result) |
| 3984 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3985 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3986 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3987 | #if HAVE_MKDIRAT |
| 3988 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3989 | result = mkdirat(dir_fd, path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3990 | else |
| 3991 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3992 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3993 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3994 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3995 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3996 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3997 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3998 | if (result < 0) |
| 3999 | return path_error(path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4000 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4001 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4004 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4005 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 4006 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4007 | #include <sys/resource.h> |
| 4008 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4009 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4010 | |
| 4011 | #ifdef HAVE_NICE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4012 | /*[clinic input] |
| 4013 | os.nice |
| 4014 | |
| 4015 | increment: int |
| 4016 | / |
| 4017 | |
| 4018 | Add increment to the priority of process and return the new priority. |
| 4019 | [clinic start generated code]*/ |
| 4020 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4021 | static PyObject * |
| 4022 | os_nice_impl(PyModuleDef *module, int increment) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4023 | /*[clinic end generated code: output=8870418a3fc07b51 input=864be2d402a21da2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4024 | { |
| 4025 | int value; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4026 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4027 | /* There are two flavours of 'nice': one that returns the new |
| 4028 | priority (as required by almost all standards out there) and the |
| 4029 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 4030 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4031 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4032 | If we are of the nice family that returns the new priority, we |
| 4033 | need to clear errno before the call, and check if errno is filled |
| 4034 | before calling posix_error() on a returnvalue of -1, because the |
| 4035 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4036 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4037 | errno = 0; |
| 4038 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4039 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4040 | if (value == 0) |
| 4041 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4042 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4043 | if (value == -1 && errno != 0) |
| 4044 | /* either nice() or getpriority() returned an error */ |
| 4045 | return posix_error(); |
| 4046 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4047 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4048 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4049 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4050 | |
| 4051 | #ifdef HAVE_GETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4052 | /*[clinic input] |
| 4053 | os.getpriority |
| 4054 | |
| 4055 | which: int |
| 4056 | who: int |
| 4057 | |
| 4058 | Return program scheduling priority. |
| 4059 | [clinic start generated code]*/ |
| 4060 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4061 | static PyObject * |
| 4062 | os_getpriority_impl(PyModuleDef *module, int which, int who) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4063 | /*[clinic end generated code: output=4759937aa5b67ed6 input=9be615d40e2544ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4064 | { |
| 4065 | int retval; |
| 4066 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4067 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4068 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4069 | if (errno != 0) |
| 4070 | return posix_error(); |
| 4071 | return PyLong_FromLong((long)retval); |
| 4072 | } |
| 4073 | #endif /* HAVE_GETPRIORITY */ |
| 4074 | |
| 4075 | |
| 4076 | #ifdef HAVE_SETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4077 | /*[clinic input] |
| 4078 | os.setpriority |
| 4079 | |
| 4080 | which: int |
| 4081 | who: int |
| 4082 | priority: int |
| 4083 | |
| 4084 | Set program scheduling priority. |
| 4085 | [clinic start generated code]*/ |
| 4086 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4087 | static PyObject * |
| 4088 | os_setpriority_impl(PyModuleDef *module, int which, int who, int priority) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4089 | /*[clinic end generated code: output=6497d3301547e7d5 input=710ccbf65b9dc513]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4090 | { |
| 4091 | int retval; |
| 4092 | |
| 4093 | retval = setpriority(which, who, priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4094 | if (retval == -1) |
| 4095 | return posix_error(); |
| 4096 | Py_RETURN_NONE; |
| 4097 | } |
| 4098 | #endif /* HAVE_SETPRIORITY */ |
| 4099 | |
| 4100 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4101 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4102 | 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] | 4103 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4104 | char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4105 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4106 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4107 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4108 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4109 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4110 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4111 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4112 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4113 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4114 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4115 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4116 | #ifndef HAVE_RENAMEAT |
| 4117 | if (dir_fd_specified) { |
| 4118 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4119 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4120 | } |
| 4121 | #endif |
| 4122 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4123 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4124 | PyErr_Format(PyExc_ValueError, |
| 4125 | "%s: src and dst must be the same type", function_name); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4126 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4127 | } |
| 4128 | |
| 4129 | #ifdef MS_WINDOWS |
| 4130 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4131 | if (src->wide) |
| 4132 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4133 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4134 | result = MoveFileExA(src->narrow, dst->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4135 | Py_END_ALLOW_THREADS |
| 4136 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4137 | if (!result) |
| 4138 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4139 | |
| 4140 | #else |
| 4141 | Py_BEGIN_ALLOW_THREADS |
| 4142 | #ifdef HAVE_RENAMEAT |
| 4143 | if (dir_fd_specified) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4144 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4145 | else |
| 4146 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4147 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4148 | Py_END_ALLOW_THREADS |
| 4149 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4150 | if (result) |
| 4151 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4152 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4153 | Py_RETURN_NONE; |
| 4154 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4155 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4156 | |
| 4157 | /*[clinic input] |
| 4158 | os.rename |
| 4159 | |
| 4160 | src : path_t |
| 4161 | dst : path_t |
| 4162 | * |
| 4163 | src_dir_fd : dir_fd = None |
| 4164 | dst_dir_fd : dir_fd = None |
| 4165 | |
| 4166 | Rename a file or directory. |
| 4167 | |
| 4168 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4169 | descriptor open to a directory, and the respective path string (src or dst) |
| 4170 | should be relative; the path will then be relative to that directory. |
| 4171 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4172 | If they are unavailable, using them will raise a NotImplementedError. |
| 4173 | [clinic start generated code]*/ |
| 4174 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4175 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4176 | os_rename_impl(PyModuleDef *module, path_t *src, path_t *dst, int src_dir_fd, |
| 4177 | int dst_dir_fd) |
| 4178 | /*[clinic end generated code: output=08033bb2ec27fb5f input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4179 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4180 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4181 | } |
| 4182 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4183 | |
| 4184 | /*[clinic input] |
| 4185 | os.replace = os.rename |
| 4186 | |
| 4187 | Rename a file or directory, overwriting the destination. |
| 4188 | |
| 4189 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4190 | descriptor open to a directory, and the respective path string (src or dst) |
| 4191 | should be relative; the path will then be relative to that directory. |
| 4192 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4193 | If they are unavailable, using them will raise a NotImplementedError." |
| 4194 | [clinic start generated code]*/ |
| 4195 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4196 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4197 | os_replace_impl(PyModuleDef *module, path_t *src, path_t *dst, |
| 4198 | int src_dir_fd, int dst_dir_fd) |
| 4199 | /*[clinic end generated code: output=131d012eed8d3b8b input=25515dfb107c8421]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4200 | { |
| 4201 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); |
| 4202 | } |
| 4203 | |
| 4204 | |
| 4205 | /*[clinic input] |
| 4206 | os.rmdir |
| 4207 | |
| 4208 | path: path_t |
| 4209 | * |
| 4210 | dir_fd: dir_fd(requires='unlinkat') = None |
| 4211 | |
| 4212 | Remove a directory. |
| 4213 | |
| 4214 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4215 | and path should be relative; path will then be relative to that directory. |
| 4216 | dir_fd may not be implemented on your platform. |
| 4217 | If it is unavailable, using it will raise a NotImplementedError. |
| 4218 | [clinic start generated code]*/ |
| 4219 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4220 | static PyObject * |
| 4221 | os_rmdir_impl(PyModuleDef *module, path_t *path, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4222 | /*[clinic end generated code: output=cabadec80d5a77c7 input=38c8b375ca34a7e2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4223 | { |
| 4224 | int result; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4225 | |
| 4226 | Py_BEGIN_ALLOW_THREADS |
| 4227 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4228 | if (path->wide) |
| 4229 | result = RemoveDirectoryW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4230 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4231 | result = RemoveDirectoryA(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4232 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4233 | #else |
| 4234 | #ifdef HAVE_UNLINKAT |
| 4235 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4236 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4237 | else |
| 4238 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4239 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4240 | #endif |
| 4241 | Py_END_ALLOW_THREADS |
| 4242 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4243 | if (result) |
| 4244 | return path_error(path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4245 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4246 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4247 | } |
| 4248 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4249 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4250 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4251 | #ifdef MS_WINDOWS |
| 4252 | /*[clinic input] |
| 4253 | os.system -> long |
| 4254 | |
| 4255 | command: Py_UNICODE |
| 4256 | |
| 4257 | Execute the command in a subshell. |
| 4258 | [clinic start generated code]*/ |
| 4259 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4260 | static long |
| 4261 | os_system_impl(PyModuleDef *module, Py_UNICODE *command) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4262 | /*[clinic end generated code: output=4c3bd5abcd9c29e7 input=303f5ce97df606b0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4263 | { |
| 4264 | long result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4265 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4266 | result = _wsystem(command); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4267 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4268 | return result; |
| 4269 | } |
| 4270 | #else /* MS_WINDOWS */ |
| 4271 | /*[clinic input] |
| 4272 | os.system -> long |
| 4273 | |
| 4274 | command: FSConverter |
| 4275 | |
| 4276 | Execute the command in a subshell. |
| 4277 | [clinic start generated code]*/ |
| 4278 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4279 | static long |
| 4280 | os_system_impl(PyModuleDef *module, PyObject *command) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4281 | /*[clinic end generated code: output=800f775e10b7be55 input=86a58554ba6094af]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4282 | { |
| 4283 | long result; |
| 4284 | char *bytes = PyBytes_AsString(command); |
| 4285 | Py_BEGIN_ALLOW_THREADS |
| 4286 | result = system(bytes); |
| 4287 | Py_END_ALLOW_THREADS |
| 4288 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4289 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4290 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4291 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4292 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4293 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4294 | /*[clinic input] |
| 4295 | os.umask |
| 4296 | |
| 4297 | mask: int |
| 4298 | / |
| 4299 | |
| 4300 | Set the current numeric umask and return the previous umask. |
| 4301 | [clinic start generated code]*/ |
| 4302 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4303 | static PyObject * |
| 4304 | os_umask_impl(PyModuleDef *module, int mask) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4305 | /*[clinic end generated code: output=9e1fe3c9f14d6a05 input=ab6bfd9b24d8a7e8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4306 | { |
| 4307 | int i = (int)umask(mask); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4308 | if (i < 0) |
| 4309 | return posix_error(); |
| 4310 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4311 | } |
| 4312 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4313 | #ifdef MS_WINDOWS |
| 4314 | |
| 4315 | /* override the default DeleteFileW behavior so that directory |
| 4316 | symlinks can be removed with this function, the same as with |
| 4317 | Unix symlinks */ |
| 4318 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4319 | { |
| 4320 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4321 | WIN32_FIND_DATAW find_data; |
| 4322 | HANDLE find_data_handle; |
| 4323 | int is_directory = 0; |
| 4324 | int is_link = 0; |
| 4325 | |
| 4326 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4327 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4328 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4329 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4330 | it is a symlink */ |
| 4331 | if(is_directory && |
| 4332 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4333 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4334 | |
| 4335 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4336 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 4337 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 4338 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 4339 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4340 | FindClose(find_data_handle); |
| 4341 | } |
| 4342 | } |
| 4343 | } |
| 4344 | |
| 4345 | if (is_directory && is_link) |
| 4346 | return RemoveDirectoryW(lpFileName); |
| 4347 | |
| 4348 | return DeleteFileW(lpFileName); |
| 4349 | } |
| 4350 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4351 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4352 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4353 | /*[clinic input] |
| 4354 | os.unlink |
| 4355 | |
| 4356 | path: path_t |
| 4357 | * |
| 4358 | dir_fd: dir_fd(requires='unlinkat')=None |
| 4359 | |
| 4360 | Remove a file (same as remove()). |
| 4361 | |
| 4362 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4363 | and path should be relative; path will then be relative to that directory. |
| 4364 | dir_fd may not be implemented on your platform. |
| 4365 | If it is unavailable, using it will raise a NotImplementedError. |
| 4366 | |
| 4367 | [clinic start generated code]*/ |
| 4368 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4369 | static PyObject * |
| 4370 | os_unlink_impl(PyModuleDef *module, path_t *path, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4371 | /*[clinic end generated code: output=474afd5cd09b237e input=d7bcde2b1b2a2552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4372 | { |
| 4373 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4374 | |
| 4375 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4376 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4377 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4378 | if (path->wide) |
| 4379 | result = Py_DeleteFileW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4380 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4381 | result = DeleteFileA(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4382 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4383 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4384 | #ifdef HAVE_UNLINKAT |
| 4385 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4386 | result = unlinkat(dir_fd, path->narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4387 | else |
| 4388 | #endif /* HAVE_UNLINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4389 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4390 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4391 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4392 | Py_END_ALLOW_THREADS |
| 4393 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4394 | if (result) |
| 4395 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4396 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4397 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4398 | } |
| 4399 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4400 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4401 | /*[clinic input] |
| 4402 | os.remove = os.unlink |
| 4403 | |
| 4404 | Remove a file (same as unlink()). |
| 4405 | |
| 4406 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4407 | and path should be relative; path will then be relative to that directory. |
| 4408 | dir_fd may not be implemented on your platform. |
| 4409 | If it is unavailable, using it will raise a NotImplementedError. |
| 4410 | [clinic start generated code]*/ |
| 4411 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4412 | static PyObject * |
| 4413 | os_remove_impl(PyModuleDef *module, path_t *path, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4414 | /*[clinic end generated code: output=d0d5149e64832b9e input=e05c5ab55cd30983]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4415 | { |
| 4416 | return os_unlink_impl(module, path, dir_fd); |
| 4417 | } |
| 4418 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4419 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4420 | static PyStructSequence_Field uname_result_fields[] = { |
| 4421 | {"sysname", "operating system name"}, |
| 4422 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4423 | {"release", "operating system release"}, |
| 4424 | {"version", "operating system version"}, |
| 4425 | {"machine", "hardware identifier"}, |
| 4426 | {NULL} |
| 4427 | }; |
| 4428 | |
| 4429 | PyDoc_STRVAR(uname_result__doc__, |
| 4430 | "uname_result: Result from os.uname().\n\n\ |
| 4431 | This object may be accessed either as a tuple of\n\ |
| 4432 | (sysname, nodename, release, version, machine),\n\ |
| 4433 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4434 | \n\ |
| 4435 | See os.uname for more information."); |
| 4436 | |
| 4437 | static PyStructSequence_Desc uname_result_desc = { |
| 4438 | "uname_result", /* name */ |
| 4439 | uname_result__doc__, /* doc */ |
| 4440 | uname_result_fields, |
| 4441 | 5 |
| 4442 | }; |
| 4443 | |
| 4444 | static PyTypeObject UnameResultType; |
| 4445 | |
| 4446 | |
| 4447 | #ifdef HAVE_UNAME |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4448 | /*[clinic input] |
| 4449 | os.uname |
| 4450 | |
| 4451 | Return an object identifying the current operating system. |
| 4452 | |
| 4453 | The object behaves like a named tuple with the following fields: |
| 4454 | (sysname, nodename, release, version, machine) |
| 4455 | |
| 4456 | [clinic start generated code]*/ |
| 4457 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4458 | static PyObject * |
| 4459 | os_uname_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4460 | /*[clinic end generated code: output=01e1421b757e753f input=e68bd246db3043ed]*/ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4461 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4462 | struct utsname u; |
| 4463 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4464 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4465 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4466 | Py_BEGIN_ALLOW_THREADS |
| 4467 | res = uname(&u); |
| 4468 | Py_END_ALLOW_THREADS |
| 4469 | if (res < 0) |
| 4470 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4471 | |
| 4472 | value = PyStructSequence_New(&UnameResultType); |
| 4473 | if (value == NULL) |
| 4474 | return NULL; |
| 4475 | |
| 4476 | #define SET(i, field) \ |
| 4477 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4478 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4479 | if (!o) { \ |
| 4480 | Py_DECREF(value); \ |
| 4481 | return NULL; \ |
| 4482 | } \ |
| 4483 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4484 | } \ |
| 4485 | |
| 4486 | SET(0, u.sysname); |
| 4487 | SET(1, u.nodename); |
| 4488 | SET(2, u.release); |
| 4489 | SET(3, u.version); |
| 4490 | SET(4, u.machine); |
| 4491 | |
| 4492 | #undef SET |
| 4493 | |
| 4494 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4495 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4496 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4497 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4498 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4499 | |
| 4500 | typedef struct { |
| 4501 | int now; |
| 4502 | time_t atime_s; |
| 4503 | long atime_ns; |
| 4504 | time_t mtime_s; |
| 4505 | long mtime_ns; |
| 4506 | } utime_t; |
| 4507 | |
| 4508 | /* |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4509 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4510 | * they also intentionally leak the declaration of a pointer named "time" |
| 4511 | */ |
| 4512 | #define UTIME_TO_TIMESPEC \ |
| 4513 | struct timespec ts[2]; \ |
| 4514 | struct timespec *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4515 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4516 | time = NULL; \ |
| 4517 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4518 | ts[0].tv_sec = ut->atime_s; \ |
| 4519 | ts[0].tv_nsec = ut->atime_ns; \ |
| 4520 | ts[1].tv_sec = ut->mtime_s; \ |
| 4521 | ts[1].tv_nsec = ut->mtime_ns; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4522 | time = ts; \ |
| 4523 | } \ |
| 4524 | |
| 4525 | #define UTIME_TO_TIMEVAL \ |
| 4526 | struct timeval tv[2]; \ |
| 4527 | struct timeval *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4528 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4529 | time = NULL; \ |
| 4530 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4531 | tv[0].tv_sec = ut->atime_s; \ |
| 4532 | tv[0].tv_usec = ut->atime_ns / 1000; \ |
| 4533 | tv[1].tv_sec = ut->mtime_s; \ |
| 4534 | tv[1].tv_usec = ut->mtime_ns / 1000; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4535 | time = tv; \ |
| 4536 | } \ |
| 4537 | |
| 4538 | #define UTIME_TO_UTIMBUF \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4539 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4540 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4541 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4542 | time = NULL; \ |
| 4543 | else { \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4544 | u.actime = ut->atime_s; \ |
| 4545 | u.modtime = ut->mtime_s; \ |
| 4546 | time = &u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4547 | } |
| 4548 | |
| 4549 | #define UTIME_TO_TIME_T \ |
| 4550 | time_t timet[2]; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4551 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4552 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4553 | time = NULL; \ |
| 4554 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4555 | timet[0] = ut->atime_s; \ |
| 4556 | timet[1] = ut->mtime_s; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4557 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4558 | } \ |
| 4559 | |
| 4560 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4561 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4562 | |
| 4563 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4564 | utime_dir_fd(utime_t *ut, int dir_fd, char *path, int follow_symlinks) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4565 | { |
| 4566 | #ifdef HAVE_UTIMENSAT |
| 4567 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4568 | UTIME_TO_TIMESPEC; |
| 4569 | return utimensat(dir_fd, path, time, flags); |
| 4570 | #elif defined(HAVE_FUTIMESAT) |
| 4571 | UTIME_TO_TIMEVAL; |
| 4572 | /* |
| 4573 | * follow_symlinks will never be false here; |
| 4574 | * we only allow !follow_symlinks and dir_fd together |
| 4575 | * if we have utimensat() |
| 4576 | */ |
| 4577 | assert(follow_symlinks); |
| 4578 | return futimesat(dir_fd, path, time); |
| 4579 | #endif |
| 4580 | } |
| 4581 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4582 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
| 4583 | #else |
| 4584 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4585 | #endif |
| 4586 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4587 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4588 | |
| 4589 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4590 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4591 | { |
| 4592 | #ifdef HAVE_FUTIMENS |
| 4593 | UTIME_TO_TIMESPEC; |
| 4594 | return futimens(fd, time); |
| 4595 | #else |
| 4596 | UTIME_TO_TIMEVAL; |
| 4597 | return futimes(fd, time); |
| 4598 | #endif |
| 4599 | } |
| 4600 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4601 | #define PATH_UTIME_HAVE_FD 1 |
| 4602 | #else |
| 4603 | #define PATH_UTIME_HAVE_FD 0 |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4604 | #endif |
| 4605 | |
Victor Stinner | 5ebae87 | 2015-09-22 01:29:33 +0200 | [diff] [blame] | 4606 | #if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES) |
| 4607 | # define UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4608 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4609 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4610 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4611 | |
| 4612 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4613 | utime_nofollow_symlinks(utime_t *ut, char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4614 | { |
| 4615 | #ifdef HAVE_UTIMENSAT |
| 4616 | UTIME_TO_TIMESPEC; |
| 4617 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4618 | #else |
| 4619 | UTIME_TO_TIMEVAL; |
| 4620 | return lutimes(path, time); |
| 4621 | #endif |
| 4622 | } |
| 4623 | |
| 4624 | #endif |
| 4625 | |
| 4626 | #ifndef MS_WINDOWS |
| 4627 | |
| 4628 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4629 | utime_default(utime_t *ut, char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4630 | { |
| 4631 | #ifdef HAVE_UTIMENSAT |
| 4632 | UTIME_TO_TIMESPEC; |
| 4633 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4634 | #elif defined(HAVE_UTIMES) |
| 4635 | UTIME_TO_TIMEVAL; |
| 4636 | return utimes(path, time); |
| 4637 | #elif defined(HAVE_UTIME_H) |
| 4638 | UTIME_TO_UTIMBUF; |
| 4639 | return utime(path, time); |
| 4640 | #else |
| 4641 | UTIME_TO_TIME_T; |
| 4642 | return utime(path, time); |
| 4643 | #endif |
| 4644 | } |
| 4645 | |
| 4646 | #endif |
| 4647 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4648 | static int |
| 4649 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4650 | { |
| 4651 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4652 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4653 | divmod = PyNumber_Divmod(py_long, billion); |
| 4654 | if (!divmod) |
| 4655 | goto exit; |
| 4656 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4657 | if ((*s == -1) && PyErr_Occurred()) |
| 4658 | goto exit; |
| 4659 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4660 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4661 | goto exit; |
| 4662 | |
| 4663 | result = 1; |
| 4664 | exit: |
| 4665 | Py_XDECREF(divmod); |
| 4666 | return result; |
| 4667 | } |
| 4668 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4669 | |
| 4670 | /*[clinic input] |
| 4671 | os.utime |
| 4672 | |
| 4673 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') |
| 4674 | times: object = NULL |
| 4675 | * |
| 4676 | ns: object = NULL |
| 4677 | dir_fd: dir_fd(requires='futimensat') = None |
| 4678 | follow_symlinks: bool=True |
| 4679 | |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4680 | # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4681 | |
| 4682 | Set the access and modified time of path. |
| 4683 | |
| 4684 | path may always be specified as a string. |
| 4685 | On some platforms, path may also be specified as an open file descriptor. |
| 4686 | If this functionality is unavailable, using it raises an exception. |
| 4687 | |
| 4688 | If times is not None, it must be a tuple (atime, mtime); |
| 4689 | atime and mtime should be expressed as float seconds since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4690 | 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] | 4691 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 4692 | since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4693 | 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] | 4694 | Specifying tuples for both times and ns is an error. |
| 4695 | |
| 4696 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4697 | and path should be relative; path will then be relative to that directory. |
| 4698 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 4699 | link, utime will modify the symbolic link itself instead of the file the |
| 4700 | link points to. |
| 4701 | It is an error to use dir_fd or follow_symlinks when specifying path |
| 4702 | as an open file descriptor. |
| 4703 | dir_fd and follow_symlinks may not be available on your platform. |
| 4704 | If they are unavailable, using them will raise a NotImplementedError. |
| 4705 | |
| 4706 | [clinic start generated code]*/ |
| 4707 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4708 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4709 | os_utime_impl(PyModuleDef *module, path_t *path, PyObject *times, |
| 4710 | PyObject *ns, int dir_fd, int follow_symlinks) |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4711 | /*[clinic end generated code: output=31f3434e560ba2f0 input=081cdc54ca685385]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4712 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4713 | #ifdef MS_WINDOWS |
| 4714 | HANDLE hFile; |
| 4715 | FILETIME atime, mtime; |
| 4716 | #else |
| 4717 | int result; |
| 4718 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4719 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4720 | PyObject *return_value = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4721 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4722 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4723 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4724 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4725 | if (times && (times != Py_None) && ns) { |
| 4726 | PyErr_SetString(PyExc_ValueError, |
| 4727 | "utime: you may specify either 'times'" |
| 4728 | " or 'ns' but not both"); |
| 4729 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4730 | } |
| 4731 | |
| 4732 | if (times && (times != Py_None)) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4733 | time_t a_sec, m_sec; |
| 4734 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4735 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4736 | PyErr_SetString(PyExc_TypeError, |
| 4737 | "utime: 'times' must be either" |
| 4738 | " a tuple of two ints or None"); |
| 4739 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4740 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4741 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4742 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4743 | &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4744 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4745 | &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4746 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4747 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4748 | utime.atime_s = a_sec; |
| 4749 | utime.atime_ns = a_nsec; |
| 4750 | utime.mtime_s = m_sec; |
| 4751 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4752 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4753 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4754 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4755 | PyErr_SetString(PyExc_TypeError, |
| 4756 | "utime: 'ns' must be a tuple of two ints"); |
| 4757 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4758 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4759 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4760 | 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] | 4761 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4762 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4763 | &utime.mtime_s, &utime.mtime_ns)) { |
| 4764 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4765 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4766 | } |
| 4767 | else { |
| 4768 | /* times and ns are both None/unspecified. use "now". */ |
| 4769 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4770 | } |
| 4771 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4772 | #if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4773 | if (follow_symlinks_specified("utime", follow_symlinks)) |
| 4774 | goto exit; |
| 4775 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4776 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4777 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
| 4778 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || |
| 4779 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4780 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4781 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4782 | #if !defined(HAVE_UTIMENSAT) |
| 4783 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4784 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4785 | "utime: cannot use dir_fd and follow_symlinks " |
| 4786 | "together on this platform"); |
| 4787 | goto exit; |
| 4788 | } |
| 4789 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4790 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4791 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4792 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4793 | if (path->wide) |
| 4794 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4795 | NULL, OPEN_EXISTING, |
| 4796 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4797 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4798 | hFile = CreateFileA(path->narrow, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4799 | NULL, OPEN_EXISTING, |
| 4800 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4801 | Py_END_ALLOW_THREADS |
| 4802 | if (hFile == INVALID_HANDLE_VALUE) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4803 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4804 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4805 | } |
| 4806 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4807 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 4808 | GetSystemTimeAsFileTime(&mtime); |
| 4809 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4810 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4811 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 4812 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4813 | _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] | 4814 | } |
| 4815 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4816 | /* Avoid putting the file name into the error here, |
| 4817 | as that may confuse the user into believing that |
| 4818 | something is wrong with the file, when it also |
| 4819 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4820 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4821 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4822 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4823 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4824 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4825 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4826 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4827 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4828 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4829 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4830 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4831 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4832 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4833 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4834 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4835 | else |
| 4836 | #endif |
| 4837 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4838 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4839 | if (path->fd != -1) |
| 4840 | result = utime_fd(&utime, path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4841 | else |
| 4842 | #endif |
| 4843 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4844 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4845 | |
| 4846 | Py_END_ALLOW_THREADS |
| 4847 | |
| 4848 | if (result < 0) { |
| 4849 | /* see previous comment about not putting filename in error here */ |
| 4850 | return_value = posix_error(); |
| 4851 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4852 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4853 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4854 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4855 | |
| 4856 | Py_INCREF(Py_None); |
| 4857 | return_value = Py_None; |
| 4858 | |
| 4859 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4860 | #ifdef MS_WINDOWS |
| 4861 | if (hFile != INVALID_HANDLE_VALUE) |
| 4862 | CloseHandle(hFile); |
| 4863 | #endif |
| 4864 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4865 | } |
| 4866 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4867 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4868 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4869 | |
| 4870 | /*[clinic input] |
| 4871 | os._exit |
| 4872 | |
| 4873 | status: int |
| 4874 | |
| 4875 | Exit to the system with specified status, without normal exit processing. |
| 4876 | [clinic start generated code]*/ |
| 4877 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4878 | static PyObject * |
| 4879 | os__exit_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4880 | /*[clinic end generated code: output=472a3cbaf68f3621 input=5e6d57556b0c4a62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4881 | { |
| 4882 | _exit(status); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4883 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4884 | } |
| 4885 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4886 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 4887 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4888 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4889 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4890 | Py_ssize_t i; |
| 4891 | for (i = 0; i < count; i++) |
| 4892 | PyMem_Free(array[i]); |
| 4893 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4894 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4895 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 4896 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4897 | int fsconvert_strdup(PyObject *o, char**out) |
| 4898 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4899 | PyObject *bytes; |
| 4900 | Py_ssize_t size; |
| 4901 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 4902 | return 0; |
| 4903 | size = PyBytes_GET_SIZE(bytes); |
| 4904 | *out = PyMem_Malloc(size+1); |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 4905 | if (!*out) { |
| 4906 | PyErr_NoMemory(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4907 | return 0; |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 4908 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4909 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 4910 | Py_DECREF(bytes); |
| 4911 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4912 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4913 | #endif |
| 4914 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4915 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4916 | static char** |
| 4917 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 4918 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4919 | char **envlist; |
| 4920 | Py_ssize_t i, pos, envc; |
| 4921 | PyObject *keys=NULL, *vals=NULL; |
| 4922 | PyObject *key, *val, *key2, *val2; |
| 4923 | char *p, *k, *v; |
| 4924 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4925 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4926 | i = PyMapping_Size(env); |
| 4927 | if (i < 0) |
| 4928 | return NULL; |
| 4929 | envlist = PyMem_NEW(char *, i + 1); |
| 4930 | if (envlist == NULL) { |
| 4931 | PyErr_NoMemory(); |
| 4932 | return NULL; |
| 4933 | } |
| 4934 | envc = 0; |
| 4935 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4936 | if (!keys) |
| 4937 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4938 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4939 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4940 | goto error; |
| 4941 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 4942 | PyErr_Format(PyExc_TypeError, |
| 4943 | "env.keys() or env.values() is not a list"); |
| 4944 | goto error; |
| 4945 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4946 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4947 | for (pos = 0; pos < i; pos++) { |
| 4948 | key = PyList_GetItem(keys, pos); |
| 4949 | val = PyList_GetItem(vals, pos); |
| 4950 | if (!key || !val) |
| 4951 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4952 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4953 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 4954 | goto error; |
| 4955 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 4956 | Py_DECREF(key2); |
| 4957 | goto error; |
| 4958 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4959 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4960 | k = PyBytes_AsString(key2); |
| 4961 | v = PyBytes_AsString(val2); |
| 4962 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4963 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4964 | p = PyMem_NEW(char, len); |
| 4965 | if (p == NULL) { |
| 4966 | PyErr_NoMemory(); |
| 4967 | Py_DECREF(key2); |
| 4968 | Py_DECREF(val2); |
| 4969 | goto error; |
| 4970 | } |
| 4971 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 4972 | envlist[envc++] = p; |
| 4973 | Py_DECREF(key2); |
| 4974 | Py_DECREF(val2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4975 | } |
| 4976 | Py_DECREF(vals); |
| 4977 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4978 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4979 | envlist[envc] = 0; |
| 4980 | *envc_ptr = envc; |
| 4981 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4982 | |
| 4983 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4984 | Py_XDECREF(keys); |
| 4985 | Py_XDECREF(vals); |
| 4986 | while (--envc >= 0) |
| 4987 | PyMem_DEL(envlist[envc]); |
| 4988 | PyMem_DEL(envlist); |
| 4989 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4990 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4991 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4992 | static char** |
| 4993 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 4994 | { |
| 4995 | int i; |
| 4996 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 4997 | if (argvlist == NULL) { |
| 4998 | PyErr_NoMemory(); |
| 4999 | return NULL; |
| 5000 | } |
| 5001 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5002 | PyObject* item = PySequence_ITEM(argv, i); |
| 5003 | if (item == NULL) |
| 5004 | goto fail; |
| 5005 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 5006 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5007 | goto fail; |
| 5008 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5009 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5010 | } |
| 5011 | argvlist[*argc] = NULL; |
| 5012 | return argvlist; |
| 5013 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5014 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5015 | free_string_array(argvlist, *argc); |
| 5016 | return NULL; |
| 5017 | } |
| 5018 | #endif |
| 5019 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5020 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5021 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5022 | /*[clinic input] |
| 5023 | os.execv |
| 5024 | |
| 5025 | path: FSConverter |
| 5026 | Path of executable file. |
| 5027 | argv: object |
| 5028 | Tuple or list of strings. |
| 5029 | / |
| 5030 | |
| 5031 | Execute an executable path with arguments, replacing current process. |
| 5032 | [clinic start generated code]*/ |
| 5033 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5034 | static PyObject * |
| 5035 | os_execv_impl(PyModuleDef *module, PyObject *path, PyObject *argv) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5036 | /*[clinic end generated code: output=9221f08143146fff input=96041559925e5229]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5037 | { |
| 5038 | char *path_char; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5039 | char **argvlist; |
| 5040 | Py_ssize_t argc; |
| 5041 | |
| 5042 | /* execv has two arguments: (path, argv), where |
| 5043 | argv is a list or tuple of strings. */ |
| 5044 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5045 | path_char = PyBytes_AsString(path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5046 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 5047 | PyErr_SetString(PyExc_TypeError, |
| 5048 | "execv() arg 2 must be a tuple or list"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5049 | return NULL; |
| 5050 | } |
| 5051 | argc = PySequence_Size(argv); |
| 5052 | if (argc < 1) { |
| 5053 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5054 | return NULL; |
| 5055 | } |
| 5056 | |
| 5057 | argvlist = parse_arglist(argv, &argc); |
| 5058 | if (argvlist == NULL) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5059 | return NULL; |
| 5060 | } |
| 5061 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5062 | execv(path_char, argvlist); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5063 | |
| 5064 | /* If we get here it's definitely an error */ |
| 5065 | |
| 5066 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5067 | return posix_error(); |
| 5068 | } |
| 5069 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5070 | |
| 5071 | /*[clinic input] |
| 5072 | os.execve |
| 5073 | |
| 5074 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') |
| 5075 | Path of executable file. |
| 5076 | argv: object |
| 5077 | Tuple or list of strings. |
| 5078 | env: object |
| 5079 | Dictionary of strings mapping to strings. |
| 5080 | |
| 5081 | Execute an executable path with arguments, replacing current process. |
| 5082 | [clinic start generated code]*/ |
| 5083 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5084 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 5085 | os_execve_impl(PyModuleDef *module, path_t *path, PyObject *argv, |
| 5086 | PyObject *env) |
| 5087 | /*[clinic end generated code: output=181884fcdb21508e input=626804fa092606d9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5088 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5089 | char **argvlist = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5090 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5091 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5092 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5093 | /* execve has three arguments: (path, argv, env), where |
| 5094 | argv is a list or tuple of strings and env is a dictionary |
| 5095 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5096 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5097 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5098 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5099 | "execve: argv must be a tuple or list"); |
| 5100 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5101 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5102 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5103 | if (!PyMapping_Check(env)) { |
| 5104 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5105 | "execve: environment must be a mapping object"); |
| 5106 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5107 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5108 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5109 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5110 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5111 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5112 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5113 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5114 | envlist = parse_envlist(env, &envc); |
| 5115 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5116 | goto fail; |
| 5117 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5118 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5119 | if (path->fd > -1) |
| 5120 | fexecve(path->fd, argvlist, envlist); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5121 | else |
| 5122 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5123 | execve(path->narrow, argvlist, envlist); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5124 | |
| 5125 | /* If we get here it's definitely an error */ |
| 5126 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5127 | path_error(path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5128 | |
| 5129 | while (--envc >= 0) |
| 5130 | PyMem_DEL(envlist[envc]); |
| 5131 | PyMem_DEL(envlist); |
| 5132 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5133 | if (argvlist) |
| 5134 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5135 | return NULL; |
| 5136 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5137 | #endif /* HAVE_EXECV */ |
| 5138 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5139 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5140 | #ifdef HAVE_SPAWNV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5141 | /*[clinic input] |
| 5142 | os.spawnv |
| 5143 | |
| 5144 | mode: int |
| 5145 | Mode of process creation. |
| 5146 | path: FSConverter |
| 5147 | Path of executable file. |
| 5148 | argv: object |
| 5149 | Tuple or list of strings. |
| 5150 | / |
| 5151 | |
| 5152 | Execute the program specified by path in a new process. |
| 5153 | [clinic start generated code]*/ |
| 5154 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5155 | static PyObject * |
| 5156 | os_spawnv_impl(PyModuleDef *module, int mode, PyObject *path, PyObject *argv) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5157 | /*[clinic end generated code: output=140a7945484c8cc5 input=042c91dfc1e6debc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5158 | { |
| 5159 | char *path_char; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5160 | char **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5161 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5162 | Py_ssize_t argc; |
| 5163 | Py_intptr_t spawnval; |
| 5164 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5165 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5166 | /* spawnv has three arguments: (mode, path, argv), where |
| 5167 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5168 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5169 | path_char = PyBytes_AsString(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5170 | if (PyList_Check(argv)) { |
| 5171 | argc = PyList_Size(argv); |
| 5172 | getitem = PyList_GetItem; |
| 5173 | } |
| 5174 | else if (PyTuple_Check(argv)) { |
| 5175 | argc = PyTuple_Size(argv); |
| 5176 | getitem = PyTuple_GetItem; |
| 5177 | } |
| 5178 | else { |
| 5179 | PyErr_SetString(PyExc_TypeError, |
| 5180 | "spawnv() arg 2 must be a tuple or list"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5181 | return NULL; |
| 5182 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5183 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5184 | argvlist = PyMem_NEW(char *, argc+1); |
| 5185 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5186 | return PyErr_NoMemory(); |
| 5187 | } |
| 5188 | for (i = 0; i < argc; i++) { |
| 5189 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5190 | &argvlist[i])) { |
| 5191 | free_string_array(argvlist, i); |
| 5192 | PyErr_SetString( |
| 5193 | PyExc_TypeError, |
| 5194 | "spawnv() arg 2 must contain only strings"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5195 | return NULL; |
| 5196 | } |
| 5197 | } |
| 5198 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5199 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5200 | if (mode == _OLD_P_OVERLAY) |
| 5201 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5202 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5203 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5204 | spawnval = _spawnv(mode, path_char, argvlist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5205 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5206 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5207 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5208 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5209 | if (spawnval == -1) |
| 5210 | return posix_error(); |
| 5211 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5212 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5213 | } |
| 5214 | |
| 5215 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5216 | /*[clinic input] |
| 5217 | os.spawnve |
| 5218 | |
| 5219 | mode: int |
| 5220 | Mode of process creation. |
| 5221 | path: FSConverter |
| 5222 | Path of executable file. |
| 5223 | argv: object |
| 5224 | Tuple or list of strings. |
| 5225 | env: object |
| 5226 | Dictionary of strings mapping to strings. |
| 5227 | / |
| 5228 | |
| 5229 | Execute the program specified by path in a new process. |
| 5230 | [clinic start generated code]*/ |
| 5231 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5232 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 5233 | os_spawnve_impl(PyModuleDef *module, int mode, PyObject *path, |
| 5234 | PyObject *argv, PyObject *env) |
| 5235 | /*[clinic end generated code: output=e7f5f0703610531f input=02362fd937963f8f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5236 | { |
| 5237 | char *path_char; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5238 | char **argvlist; |
| 5239 | char **envlist; |
| 5240 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5241 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5242 | Py_intptr_t spawnval; |
| 5243 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 5244 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5245 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5246 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5247 | argv is a list or tuple of strings and env is a dictionary |
| 5248 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5249 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5250 | path_char = PyBytes_AsString(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5251 | if (PyList_Check(argv)) { |
| 5252 | argc = PyList_Size(argv); |
| 5253 | getitem = PyList_GetItem; |
| 5254 | } |
| 5255 | else if (PyTuple_Check(argv)) { |
| 5256 | argc = PyTuple_Size(argv); |
| 5257 | getitem = PyTuple_GetItem; |
| 5258 | } |
| 5259 | else { |
| 5260 | PyErr_SetString(PyExc_TypeError, |
| 5261 | "spawnve() arg 2 must be a tuple or list"); |
| 5262 | goto fail_0; |
| 5263 | } |
| 5264 | if (!PyMapping_Check(env)) { |
| 5265 | PyErr_SetString(PyExc_TypeError, |
| 5266 | "spawnve() arg 3 must be a mapping object"); |
| 5267 | goto fail_0; |
| 5268 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5269 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5270 | argvlist = PyMem_NEW(char *, argc+1); |
| 5271 | if (argvlist == NULL) { |
| 5272 | PyErr_NoMemory(); |
| 5273 | goto fail_0; |
| 5274 | } |
| 5275 | for (i = 0; i < argc; i++) { |
| 5276 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5277 | &argvlist[i])) |
| 5278 | { |
| 5279 | lastarg = i; |
| 5280 | goto fail_1; |
| 5281 | } |
| 5282 | } |
| 5283 | lastarg = argc; |
| 5284 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5285 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5286 | envlist = parse_envlist(env, &envc); |
| 5287 | if (envlist == NULL) |
| 5288 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5289 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5290 | if (mode == _OLD_P_OVERLAY) |
| 5291 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5292 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5293 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5294 | spawnval = _spawnve(mode, path_char, argvlist, envlist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5295 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5296 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5297 | if (spawnval == -1) |
| 5298 | (void) posix_error(); |
| 5299 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5300 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5301 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5302 | while (--envc >= 0) |
| 5303 | PyMem_DEL(envlist[envc]); |
| 5304 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5305 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5306 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5307 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5308 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5309 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5310 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5311 | #endif /* HAVE_SPAWNV */ |
| 5312 | |
| 5313 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5314 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5315 | /*[clinic input] |
| 5316 | os.fork1 |
| 5317 | |
| 5318 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 5319 | |
| 5320 | Return 0 to child process and PID of child to parent process. |
| 5321 | [clinic start generated code]*/ |
| 5322 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5323 | static PyObject * |
| 5324 | os_fork1_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5325 | /*[clinic end generated code: output=e27b4f66419c9dcf input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5326 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5327 | pid_t pid; |
| 5328 | int result = 0; |
| 5329 | _PyImport_AcquireLock(); |
| 5330 | pid = fork1(); |
| 5331 | if (pid == 0) { |
| 5332 | /* child: this clobbers and resets the import lock. */ |
| 5333 | PyOS_AfterFork(); |
| 5334 | } else { |
| 5335 | /* parent: release the import lock. */ |
| 5336 | result = _PyImport_ReleaseLock(); |
| 5337 | } |
| 5338 | if (pid == -1) |
| 5339 | return posix_error(); |
| 5340 | if (result < 0) { |
| 5341 | /* Don't clobber the OSError if the fork failed. */ |
| 5342 | PyErr_SetString(PyExc_RuntimeError, |
| 5343 | "not holding the import lock"); |
| 5344 | return NULL; |
| 5345 | } |
| 5346 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5347 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5348 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5349 | |
| 5350 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5351 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5352 | /*[clinic input] |
| 5353 | os.fork |
| 5354 | |
| 5355 | Fork a child process. |
| 5356 | |
| 5357 | Return 0 to child process and PID of child to parent process. |
| 5358 | [clinic start generated code]*/ |
| 5359 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5360 | static PyObject * |
| 5361 | os_fork_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5362 | /*[clinic end generated code: output=898b1ecd3498ba12 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5363 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5364 | pid_t pid; |
| 5365 | int result = 0; |
| 5366 | _PyImport_AcquireLock(); |
| 5367 | pid = fork(); |
| 5368 | if (pid == 0) { |
| 5369 | /* child: this clobbers and resets the import lock. */ |
| 5370 | PyOS_AfterFork(); |
| 5371 | } else { |
| 5372 | /* parent: release the import lock. */ |
| 5373 | result = _PyImport_ReleaseLock(); |
| 5374 | } |
| 5375 | if (pid == -1) |
| 5376 | return posix_error(); |
| 5377 | if (result < 0) { |
| 5378 | /* Don't clobber the OSError if the fork failed. */ |
| 5379 | PyErr_SetString(PyExc_RuntimeError, |
| 5380 | "not holding the import lock"); |
| 5381 | return NULL; |
| 5382 | } |
| 5383 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5384 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5385 | #endif /* HAVE_FORK */ |
| 5386 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5387 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5388 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5389 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5390 | /*[clinic input] |
| 5391 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5392 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5393 | policy: int |
| 5394 | |
| 5395 | Get the maximum scheduling priority for policy. |
| 5396 | [clinic start generated code]*/ |
| 5397 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5398 | static PyObject * |
| 5399 | os_sched_get_priority_max_impl(PyModuleDef *module, int policy) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5400 | /*[clinic end generated code: output=a6a30fa5071f2d81 input=2097b7998eca6874]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5401 | { |
| 5402 | int max; |
| 5403 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5404 | max = sched_get_priority_max(policy); |
| 5405 | if (max < 0) |
| 5406 | return posix_error(); |
| 5407 | return PyLong_FromLong(max); |
| 5408 | } |
| 5409 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5410 | |
| 5411 | /*[clinic input] |
| 5412 | os.sched_get_priority_min |
| 5413 | |
| 5414 | policy: int |
| 5415 | |
| 5416 | Get the minimum scheduling priority for policy. |
| 5417 | [clinic start generated code]*/ |
| 5418 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5419 | static PyObject * |
| 5420 | os_sched_get_priority_min_impl(PyModuleDef *module, int policy) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5421 | /*[clinic end generated code: output=5ca3ed6bc43e9b20 input=21bc8fa0d70983bf]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5422 | { |
| 5423 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5424 | if (min < 0) |
| 5425 | return posix_error(); |
| 5426 | return PyLong_FromLong(min); |
| 5427 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5428 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 5429 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5430 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5431 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5432 | /*[clinic input] |
| 5433 | os.sched_getscheduler |
| 5434 | pid: pid_t |
| 5435 | / |
| 5436 | |
| 5437 | Get the scheduling policy for the process identifiedy by pid. |
| 5438 | |
| 5439 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 5440 | [clinic start generated code]*/ |
| 5441 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5442 | static PyObject * |
| 5443 | os_sched_getscheduler_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5444 | /*[clinic end generated code: output=8cd63c15caf54fa9 input=5f14cfd1f189e1a0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5445 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5446 | int policy; |
| 5447 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5448 | policy = sched_getscheduler(pid); |
| 5449 | if (policy < 0) |
| 5450 | return posix_error(); |
| 5451 | return PyLong_FromLong(policy); |
| 5452 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5453 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5454 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5455 | |
| 5456 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5457 | /*[clinic input] |
| 5458 | class os.sched_param "PyObject *" "&SchedParamType" |
| 5459 | |
| 5460 | @classmethod |
| 5461 | os.sched_param.__new__ |
| 5462 | |
| 5463 | sched_priority: object |
| 5464 | A scheduling parameter. |
| 5465 | |
| 5466 | Current has only one field: sched_priority"); |
| 5467 | [clinic start generated code]*/ |
| 5468 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5469 | static PyObject * |
| 5470 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5471 | /*[clinic end generated code: output=48f4067d60f48c13 input=73a4c22f7071fc62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5472 | { |
| 5473 | PyObject *res; |
| 5474 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5475 | res = PyStructSequence_New(type); |
| 5476 | if (!res) |
| 5477 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5478 | Py_INCREF(sched_priority); |
| 5479 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5480 | return res; |
| 5481 | } |
| 5482 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5483 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5484 | PyDoc_VAR(os_sched_param__doc__); |
| 5485 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5486 | static PyStructSequence_Field sched_param_fields[] = { |
| 5487 | {"sched_priority", "the scheduling priority"}, |
| 5488 | {0} |
| 5489 | }; |
| 5490 | |
| 5491 | static PyStructSequence_Desc sched_param_desc = { |
| 5492 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5493 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5494 | sched_param_fields, |
| 5495 | 1 |
| 5496 | }; |
| 5497 | |
| 5498 | static int |
| 5499 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 5500 | { |
| 5501 | long priority; |
| 5502 | |
| 5503 | if (Py_TYPE(param) != &SchedParamType) { |
| 5504 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 5505 | return 0; |
| 5506 | } |
| 5507 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 5508 | if (priority == -1 && PyErr_Occurred()) |
| 5509 | return 0; |
| 5510 | if (priority > INT_MAX || priority < INT_MIN) { |
| 5511 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 5512 | return 0; |
| 5513 | } |
| 5514 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 5515 | return 1; |
| 5516 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5517 | #endif /* defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5518 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5519 | |
| 5520 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5521 | /*[clinic input] |
| 5522 | os.sched_setscheduler |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5523 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5524 | pid: pid_t |
| 5525 | policy: int |
| 5526 | param: sched_param |
| 5527 | / |
| 5528 | |
| 5529 | Set the scheduling policy for the process identified by pid. |
| 5530 | |
| 5531 | If pid is 0, the calling process is changed. |
| 5532 | param is an instance of sched_param. |
| 5533 | [clinic start generated code]*/ |
| 5534 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5535 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 5536 | os_sched_setscheduler_impl(PyModuleDef *module, pid_t pid, int policy, |
| 5537 | struct sched_param *param) |
| 5538 | /*[clinic end generated code: output=37053e5c528c35c9 input=c581f9469a5327dd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5539 | { |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5540 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 5541 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 5542 | ** scheduling policy under Solaris/Illumos, and others. |
| 5543 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5544 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5545 | if (sched_setscheduler(pid, policy, param) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5546 | return posix_error(); |
| 5547 | Py_RETURN_NONE; |
| 5548 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5549 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5550 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5551 | |
| 5552 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5553 | /*[clinic input] |
| 5554 | os.sched_getparam |
| 5555 | pid: pid_t |
| 5556 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5557 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5558 | Returns scheduling parameters for the process identified by pid. |
| 5559 | |
| 5560 | If pid is 0, returns parameters for the calling process. |
| 5561 | Return value is an instance of sched_param. |
| 5562 | [clinic start generated code]*/ |
| 5563 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5564 | static PyObject * |
| 5565 | os_sched_getparam_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5566 | /*[clinic end generated code: output=f42c5bd2604ecd08 input=18a1ef9c2efae296]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5567 | { |
| 5568 | struct sched_param param; |
| 5569 | PyObject *result; |
| 5570 | PyObject *priority; |
| 5571 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5572 | if (sched_getparam(pid, ¶m)) |
| 5573 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5574 | result = PyStructSequence_New(&SchedParamType); |
| 5575 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5576 | return NULL; |
| 5577 | priority = PyLong_FromLong(param.sched_priority); |
| 5578 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5579 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5580 | return NULL; |
| 5581 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5582 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 5583 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5584 | } |
| 5585 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5586 | |
| 5587 | /*[clinic input] |
| 5588 | os.sched_setparam |
| 5589 | pid: pid_t |
| 5590 | param: sched_param |
| 5591 | / |
| 5592 | |
| 5593 | Set scheduling parameters for the process identified by pid. |
| 5594 | |
| 5595 | If pid is 0, sets parameters for the calling process. |
| 5596 | param should be an instance of sched_param. |
| 5597 | [clinic start generated code]*/ |
| 5598 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5599 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 5600 | os_sched_setparam_impl(PyModuleDef *module, pid_t pid, |
| 5601 | struct sched_param *param) |
| 5602 | /*[clinic end generated code: output=b7a3c589436cec9b input=6b8d6dfcecdc21bd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5603 | { |
| 5604 | if (sched_setparam(pid, param)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5605 | return posix_error(); |
| 5606 | Py_RETURN_NONE; |
| 5607 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5608 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5609 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5610 | |
| 5611 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5612 | /*[clinic input] |
| 5613 | os.sched_rr_get_interval -> double |
| 5614 | pid: pid_t |
| 5615 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5616 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5617 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 5618 | |
| 5619 | Value returned is a float. |
| 5620 | [clinic start generated code]*/ |
| 5621 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5622 | static double |
| 5623 | os_sched_rr_get_interval_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5624 | /*[clinic end generated code: output=7adc137a86dea581 input=2a973da15cca6fae]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5625 | { |
| 5626 | struct timespec interval; |
| 5627 | if (sched_rr_get_interval(pid, &interval)) { |
| 5628 | posix_error(); |
| 5629 | return -1.0; |
| 5630 | } |
| 5631 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 5632 | } |
| 5633 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5634 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5635 | |
| 5636 | /*[clinic input] |
| 5637 | os.sched_yield |
| 5638 | |
| 5639 | Voluntarily relinquish the CPU. |
| 5640 | [clinic start generated code]*/ |
| 5641 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5642 | static PyObject * |
| 5643 | os_sched_yield_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5644 | /*[clinic end generated code: output=d7bd51869c4cb6a8 input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5645 | { |
| 5646 | if (sched_yield()) |
| 5647 | return posix_error(); |
| 5648 | Py_RETURN_NONE; |
| 5649 | } |
| 5650 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5651 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5652 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 5653 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5654 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5655 | /*[clinic input] |
| 5656 | os.sched_setaffinity |
| 5657 | pid: pid_t |
| 5658 | mask : object |
| 5659 | / |
| 5660 | |
| 5661 | Set the CPU affinity of the process identified by pid to mask. |
| 5662 | |
| 5663 | mask should be an iterable of integers identifying CPUs. |
| 5664 | [clinic start generated code]*/ |
| 5665 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5666 | static PyObject * |
| 5667 | os_sched_setaffinity_impl(PyModuleDef *module, pid_t pid, PyObject *mask) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5668 | /*[clinic end generated code: output=582bcbf40d3253a9 input=a0791a597c7085ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5669 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5670 | int ncpus; |
| 5671 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5672 | cpu_set_t *cpu_set = NULL; |
| 5673 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5674 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5675 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5676 | if (iterator == NULL) |
| 5677 | return NULL; |
| 5678 | |
| 5679 | ncpus = NCPUS_START; |
| 5680 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5681 | cpu_set = CPU_ALLOC(ncpus); |
| 5682 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5683 | PyErr_NoMemory(); |
| 5684 | goto error; |
| 5685 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5686 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5687 | |
| 5688 | while ((item = PyIter_Next(iterator))) { |
| 5689 | long cpu; |
| 5690 | if (!PyLong_Check(item)) { |
| 5691 | PyErr_Format(PyExc_TypeError, |
| 5692 | "expected an iterator of ints, " |
| 5693 | "but iterator yielded %R", |
| 5694 | Py_TYPE(item)); |
| 5695 | Py_DECREF(item); |
| 5696 | goto error; |
| 5697 | } |
| 5698 | cpu = PyLong_AsLong(item); |
| 5699 | Py_DECREF(item); |
| 5700 | if (cpu < 0) { |
| 5701 | if (!PyErr_Occurred()) |
| 5702 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 5703 | goto error; |
| 5704 | } |
| 5705 | if (cpu > INT_MAX - 1) { |
| 5706 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 5707 | goto error; |
| 5708 | } |
| 5709 | if (cpu >= ncpus) { |
| 5710 | /* Grow CPU mask to fit the CPU number */ |
| 5711 | int newncpus = ncpus; |
| 5712 | cpu_set_t *newmask; |
| 5713 | size_t newsetsize; |
| 5714 | while (newncpus <= cpu) { |
| 5715 | if (newncpus > INT_MAX / 2) |
| 5716 | newncpus = cpu + 1; |
| 5717 | else |
| 5718 | newncpus = newncpus * 2; |
| 5719 | } |
| 5720 | newmask = CPU_ALLOC(newncpus); |
| 5721 | if (newmask == NULL) { |
| 5722 | PyErr_NoMemory(); |
| 5723 | goto error; |
| 5724 | } |
| 5725 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 5726 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5727 | memcpy(newmask, cpu_set, setsize); |
| 5728 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5729 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5730 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5731 | ncpus = newncpus; |
| 5732 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5733 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5734 | } |
| 5735 | Py_CLEAR(iterator); |
| 5736 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5737 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5738 | posix_error(); |
| 5739 | goto error; |
| 5740 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5741 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5742 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5743 | |
| 5744 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5745 | if (cpu_set) |
| 5746 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5747 | Py_XDECREF(iterator); |
| 5748 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5749 | } |
| 5750 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5751 | |
| 5752 | /*[clinic input] |
| 5753 | os.sched_getaffinity |
| 5754 | pid: pid_t |
| 5755 | / |
| 5756 | |
Charles-François Natali | dc87e4b | 2015-07-13 21:01:39 +0100 | [diff] [blame] | 5757 | 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] | 5758 | |
| 5759 | The affinity is returned as a set of CPU identifiers. |
| 5760 | [clinic start generated code]*/ |
| 5761 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5762 | static PyObject * |
| 5763 | os_sched_getaffinity_impl(PyModuleDef *module, pid_t pid) |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 5764 | /*[clinic end generated code: output=b431a8f310e369e7 input=983ce7cb4a565980]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5765 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5766 | int cpu, ncpus, count; |
| 5767 | size_t setsize; |
| 5768 | cpu_set_t *mask = NULL; |
| 5769 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5770 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5771 | ncpus = NCPUS_START; |
| 5772 | while (1) { |
| 5773 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5774 | mask = CPU_ALLOC(ncpus); |
| 5775 | if (mask == NULL) |
| 5776 | return PyErr_NoMemory(); |
| 5777 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 5778 | break; |
| 5779 | CPU_FREE(mask); |
| 5780 | if (errno != EINVAL) |
| 5781 | return posix_error(); |
| 5782 | if (ncpus > INT_MAX / 2) { |
| 5783 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 5784 | "a large enough CPU set"); |
| 5785 | return NULL; |
| 5786 | } |
| 5787 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5788 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5789 | |
| 5790 | res = PySet_New(NULL); |
| 5791 | if (res == NULL) |
| 5792 | goto error; |
| 5793 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 5794 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 5795 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 5796 | --count; |
| 5797 | if (cpu_num == NULL) |
| 5798 | goto error; |
| 5799 | if (PySet_Add(res, cpu_num)) { |
| 5800 | Py_DECREF(cpu_num); |
| 5801 | goto error; |
| 5802 | } |
| 5803 | Py_DECREF(cpu_num); |
| 5804 | } |
| 5805 | } |
| 5806 | CPU_FREE(mask); |
| 5807 | return res; |
| 5808 | |
| 5809 | error: |
| 5810 | if (mask) |
| 5811 | CPU_FREE(mask); |
| 5812 | Py_XDECREF(res); |
| 5813 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5814 | } |
| 5815 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5816 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5817 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5818 | #endif /* HAVE_SCHED_H */ |
| 5819 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5820 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5821 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5822 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5823 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5824 | #define DEV_PTY_FILE "/dev/ptc" |
| 5825 | #define HAVE_DEV_PTMX |
| 5826 | #else |
| 5827 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5828 | #endif |
| 5829 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5830 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5831 | #ifdef HAVE_PTY_H |
| 5832 | #include <pty.h> |
| 5833 | #else |
| 5834 | #ifdef HAVE_LIBUTIL_H |
| 5835 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5836 | #else |
| 5837 | #ifdef HAVE_UTIL_H |
| 5838 | #include <util.h> |
| 5839 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5840 | #endif /* HAVE_LIBUTIL_H */ |
| 5841 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5842 | #ifdef HAVE_STROPTS_H |
| 5843 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5844 | #endif |
| 5845 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5846 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5847 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5848 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5849 | /*[clinic input] |
| 5850 | os.openpty |
| 5851 | |
| 5852 | Open a pseudo-terminal. |
| 5853 | |
| 5854 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 5855 | for both the master and slave ends. |
| 5856 | [clinic start generated code]*/ |
| 5857 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5858 | static PyObject * |
| 5859 | os_openpty_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5860 | /*[clinic end generated code: output=358e571c1ba135ee input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5861 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5862 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5863 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5864 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5865 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5866 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5867 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5868 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5869 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5870 | #endif |
| 5871 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5872 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5873 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5874 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5875 | goto posix_error; |
| 5876 | |
| 5877 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5878 | goto error; |
| 5879 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 5880 | goto error; |
| 5881 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5882 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5883 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5884 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5885 | goto posix_error; |
| 5886 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5887 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5888 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5889 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5890 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 5891 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5892 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5893 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 5894 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5895 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5896 | goto posix_error; |
| 5897 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5898 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5899 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5900 | /* change permission of slave */ |
| 5901 | if (grantpt(master_fd) < 0) { |
| 5902 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5903 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5904 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5905 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5906 | /* unlock slave */ |
| 5907 | if (unlockpt(master_fd) < 0) { |
| 5908 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5909 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5910 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5911 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5912 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5913 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5914 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 5915 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5916 | goto posix_error; |
| 5917 | |
| 5918 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 5919 | if (slave_fd == -1) |
| 5920 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 5921 | |
| 5922 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5923 | goto posix_error; |
| 5924 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5925 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5926 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 5927 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5928 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5929 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5930 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5931 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 5932 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5933 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5934 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5935 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5936 | posix_error: |
| 5937 | posix_error(); |
| 5938 | error: |
| 5939 | if (master_fd != -1) |
| 5940 | close(master_fd); |
| 5941 | if (slave_fd != -1) |
| 5942 | close(slave_fd); |
| 5943 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5944 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5945 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5946 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5947 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5948 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5949 | /*[clinic input] |
| 5950 | os.forkpty |
| 5951 | |
| 5952 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 5953 | |
| 5954 | Returns a tuple of (pid, master_fd). |
| 5955 | Like fork(), return pid of 0 to the child process, |
| 5956 | and pid of child to the parent process. |
| 5957 | To both, return fd of newly opened pseudo-terminal. |
| 5958 | [clinic start generated code]*/ |
| 5959 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5960 | static PyObject * |
| 5961 | os_forkpty_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5962 | /*[clinic end generated code: output=a11b8391dce3cb57 input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5963 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5964 | int master_fd = -1, result = 0; |
| 5965 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5966 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5967 | _PyImport_AcquireLock(); |
| 5968 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 5969 | if (pid == 0) { |
| 5970 | /* child: this clobbers and resets the import lock. */ |
| 5971 | PyOS_AfterFork(); |
| 5972 | } else { |
| 5973 | /* parent: release the import lock. */ |
| 5974 | result = _PyImport_ReleaseLock(); |
| 5975 | } |
| 5976 | if (pid == -1) |
| 5977 | return posix_error(); |
| 5978 | if (result < 0) { |
| 5979 | /* Don't clobber the OSError if the fork failed. */ |
| 5980 | PyErr_SetString(PyExc_RuntimeError, |
| 5981 | "not holding the import lock"); |
| 5982 | return NULL; |
| 5983 | } |
| 5984 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5985 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5986 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5987 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5988 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5989 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5990 | /*[clinic input] |
| 5991 | os.getegid |
| 5992 | |
| 5993 | Return the current process's effective group id. |
| 5994 | [clinic start generated code]*/ |
| 5995 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5996 | static PyObject * |
| 5997 | os_getegid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5998 | /*[clinic end generated code: output=90f433a8c0b1d919 input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5999 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6000 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6001 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6002 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6003 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6004 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6005 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6006 | /*[clinic input] |
| 6007 | os.geteuid |
| 6008 | |
| 6009 | Return the current process's effective user id. |
| 6010 | [clinic start generated code]*/ |
| 6011 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6012 | static PyObject * |
| 6013 | os_geteuid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6014 | /*[clinic end generated code: output=1a532c4a66874357 input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6015 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6016 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6017 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6018 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6019 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6020 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6021 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6022 | /*[clinic input] |
| 6023 | os.getgid |
| 6024 | |
| 6025 | Return the current process's group id. |
| 6026 | [clinic start generated code]*/ |
| 6027 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6028 | static PyObject * |
| 6029 | os_getgid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6030 | /*[clinic end generated code: output=91a22021b74ea46b input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6031 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6032 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6033 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6034 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6035 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6036 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6037 | /*[clinic input] |
| 6038 | os.getpid |
| 6039 | |
| 6040 | Return the current process id. |
| 6041 | [clinic start generated code]*/ |
| 6042 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6043 | static PyObject * |
| 6044 | os_getpid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6045 | /*[clinic end generated code: output=8fbf3a934ee09e62 input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6046 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6047 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6048 | } |
| 6049 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6050 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6051 | |
| 6052 | /* AC 3.5: funny apple logic below */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6053 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 6054 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 6055 | Returns a list of groups to which a user belongs.\n\n\ |
| 6056 | user: username to lookup\n\ |
| 6057 | group: base group id of the user"); |
| 6058 | |
| 6059 | static PyObject * |
| 6060 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 6061 | { |
| 6062 | #ifdef NGROUPS_MAX |
| 6063 | #define MAX_GROUPS NGROUPS_MAX |
| 6064 | #else |
| 6065 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6066 | #define MAX_GROUPS 64 |
| 6067 | #endif |
| 6068 | |
| 6069 | const char *user; |
| 6070 | int i, ngroups; |
| 6071 | PyObject *list; |
| 6072 | #ifdef __APPLE__ |
| 6073 | int *groups, basegid; |
| 6074 | #else |
| 6075 | gid_t *groups, basegid; |
| 6076 | #endif |
| 6077 | ngroups = MAX_GROUPS; |
| 6078 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6079 | #ifdef __APPLE__ |
| 6080 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6081 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6082 | #else |
| 6083 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 6084 | _Py_Gid_Converter, &basegid)) |
| 6085 | return NULL; |
| 6086 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6087 | |
| 6088 | #ifdef __APPLE__ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6089 | groups = PyMem_New(int, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6090 | #else |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6091 | groups = PyMem_New(gid_t, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6092 | #endif |
| 6093 | if (groups == NULL) |
| 6094 | return PyErr_NoMemory(); |
| 6095 | |
| 6096 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 6097 | PyMem_Del(groups); |
| 6098 | return posix_error(); |
| 6099 | } |
| 6100 | |
| 6101 | list = PyList_New(ngroups); |
| 6102 | if (list == NULL) { |
| 6103 | PyMem_Del(groups); |
| 6104 | return NULL; |
| 6105 | } |
| 6106 | |
| 6107 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6108 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6109 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6110 | #else |
| 6111 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 6112 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6113 | if (o == NULL) { |
| 6114 | Py_DECREF(list); |
| 6115 | PyMem_Del(groups); |
| 6116 | return NULL; |
| 6117 | } |
| 6118 | PyList_SET_ITEM(list, i, o); |
| 6119 | } |
| 6120 | |
| 6121 | PyMem_Del(groups); |
| 6122 | |
| 6123 | return list; |
| 6124 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6125 | #endif /* HAVE_GETGROUPLIST */ |
| 6126 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6127 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6128 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6129 | /*[clinic input] |
| 6130 | os.getgroups |
| 6131 | |
| 6132 | Return list of supplemental group IDs for the process. |
| 6133 | [clinic start generated code]*/ |
| 6134 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6135 | static PyObject * |
| 6136 | os_getgroups_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6137 | /*[clinic end generated code: output=6e7c4fd2db6d5c60 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6138 | { |
| 6139 | PyObject *result = NULL; |
| 6140 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6141 | #ifdef NGROUPS_MAX |
| 6142 | #define MAX_GROUPS NGROUPS_MAX |
| 6143 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6144 | /* 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] | 6145 | #define MAX_GROUPS 64 |
| 6146 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6147 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6148 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6149 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6150 | * This is a helper variable to store the intermediate result when |
| 6151 | * that happens. |
| 6152 | * |
| 6153 | * To keep the code readable the OSX behaviour is unconditional, |
| 6154 | * according to the POSIX spec this should be safe on all unix-y |
| 6155 | * systems. |
| 6156 | */ |
| 6157 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6158 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6159 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6160 | #ifdef __APPLE__ |
| 6161 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 6162 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 6163 | * always first call getgroups with length 0 to get the actual number |
| 6164 | * of groups. |
| 6165 | */ |
| 6166 | n = getgroups(0, NULL); |
| 6167 | if (n < 0) { |
| 6168 | return posix_error(); |
| 6169 | } else if (n <= MAX_GROUPS) { |
| 6170 | /* groups will fit in existing array */ |
| 6171 | alt_grouplist = grouplist; |
| 6172 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6173 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6174 | if (alt_grouplist == NULL) { |
| 6175 | errno = EINVAL; |
| 6176 | return posix_error(); |
| 6177 | } |
| 6178 | } |
| 6179 | |
| 6180 | n = getgroups(n, alt_grouplist); |
| 6181 | if (n == -1) { |
| 6182 | if (alt_grouplist != grouplist) { |
| 6183 | PyMem_Free(alt_grouplist); |
| 6184 | } |
| 6185 | return posix_error(); |
| 6186 | } |
| 6187 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6188 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6189 | if (n < 0) { |
| 6190 | if (errno == EINVAL) { |
| 6191 | n = getgroups(0, NULL); |
| 6192 | if (n == -1) { |
| 6193 | return posix_error(); |
| 6194 | } |
| 6195 | if (n == 0) { |
| 6196 | /* Avoid malloc(0) */ |
| 6197 | alt_grouplist = grouplist; |
| 6198 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6199 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6200 | if (alt_grouplist == NULL) { |
| 6201 | errno = EINVAL; |
| 6202 | return posix_error(); |
| 6203 | } |
| 6204 | n = getgroups(n, alt_grouplist); |
| 6205 | if (n == -1) { |
| 6206 | PyMem_Free(alt_grouplist); |
| 6207 | return posix_error(); |
| 6208 | } |
| 6209 | } |
| 6210 | } else { |
| 6211 | return posix_error(); |
| 6212 | } |
| 6213 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6214 | #endif |
| 6215 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6216 | result = PyList_New(n); |
| 6217 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6218 | int i; |
| 6219 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6220 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6221 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6222 | Py_DECREF(result); |
| 6223 | result = NULL; |
| 6224 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6225 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6226 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6227 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6228 | } |
| 6229 | |
| 6230 | if (alt_grouplist != grouplist) { |
| 6231 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6232 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6233 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6234 | return result; |
| 6235 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6236 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6237 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6238 | #ifdef HAVE_INITGROUPS |
| 6239 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 6240 | "initgroups(username, gid) -> None\n\n\ |
| 6241 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 6242 | the groups of which the specified username is a member, plus the specified\n\ |
| 6243 | group id."); |
| 6244 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6245 | /* AC 3.5: funny apple logic */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6246 | static PyObject * |
| 6247 | posix_initgroups(PyObject *self, PyObject *args) |
| 6248 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6249 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6250 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6251 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6252 | #ifdef __APPLE__ |
| 6253 | int gid; |
| 6254 | #else |
| 6255 | gid_t gid; |
| 6256 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6257 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6258 | #ifdef __APPLE__ |
| 6259 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 6260 | PyUnicode_FSConverter, &oname, |
| 6261 | &gid)) |
| 6262 | #else |
| 6263 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 6264 | PyUnicode_FSConverter, &oname, |
| 6265 | _Py_Gid_Converter, &gid)) |
| 6266 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6267 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6268 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6269 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6270 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6271 | Py_DECREF(oname); |
| 6272 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6273 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6274 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6275 | Py_INCREF(Py_None); |
| 6276 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6277 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6278 | #endif /* HAVE_INITGROUPS */ |
| 6279 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6280 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6281 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6282 | /*[clinic input] |
| 6283 | os.getpgid |
| 6284 | |
| 6285 | pid: pid_t |
| 6286 | |
| 6287 | Call the system call getpgid(), and return the result. |
| 6288 | [clinic start generated code]*/ |
| 6289 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6290 | static PyObject * |
| 6291 | os_getpgid_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6292 | /*[clinic end generated code: output=70e713b4d54b7c61 input=39d710ae3baaf1c7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6293 | { |
| 6294 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6295 | if (pgid < 0) |
| 6296 | return posix_error(); |
| 6297 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6298 | } |
| 6299 | #endif /* HAVE_GETPGID */ |
| 6300 | |
| 6301 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6302 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6303 | /*[clinic input] |
| 6304 | os.getpgrp |
| 6305 | |
| 6306 | Return the current process group id. |
| 6307 | [clinic start generated code]*/ |
| 6308 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6309 | static PyObject * |
| 6310 | os_getpgrp_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6311 | /*[clinic end generated code: output=cf3403585846811f input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6312 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6313 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6314 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6315 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6316 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6317 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6318 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6319 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6320 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6321 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6322 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6323 | /*[clinic input] |
| 6324 | os.setpgrp |
| 6325 | |
| 6326 | Make the current process the leader of its process group. |
| 6327 | [clinic start generated code]*/ |
| 6328 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6329 | static PyObject * |
| 6330 | os_setpgrp_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6331 | /*[clinic end generated code: output=59650f55a963d7ac input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6332 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6333 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6334 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6335 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6336 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6337 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6338 | return posix_error(); |
| 6339 | Py_INCREF(Py_None); |
| 6340 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6341 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6342 | #endif /* HAVE_SETPGRP */ |
| 6343 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6344 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6345 | |
| 6346 | #ifdef MS_WINDOWS |
| 6347 | #include <tlhelp32.h> |
| 6348 | |
| 6349 | static PyObject* |
| 6350 | win32_getppid() |
| 6351 | { |
| 6352 | HANDLE snapshot; |
| 6353 | pid_t mypid; |
| 6354 | PyObject* result = NULL; |
| 6355 | BOOL have_record; |
| 6356 | PROCESSENTRY32 pe; |
| 6357 | |
| 6358 | mypid = getpid(); /* This function never fails */ |
| 6359 | |
| 6360 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 6361 | if (snapshot == INVALID_HANDLE_VALUE) |
| 6362 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 6363 | |
| 6364 | pe.dwSize = sizeof(pe); |
| 6365 | have_record = Process32First(snapshot, &pe); |
| 6366 | while (have_record) { |
| 6367 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 6368 | /* We could cache the ulong value in a static variable. */ |
| 6369 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 6370 | break; |
| 6371 | } |
| 6372 | |
| 6373 | have_record = Process32Next(snapshot, &pe); |
| 6374 | } |
| 6375 | |
| 6376 | /* If our loop exits and our pid was not found (result will be NULL) |
| 6377 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 6378 | * error anyway, so let's raise it. */ |
| 6379 | if (!result) |
| 6380 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6381 | |
| 6382 | CloseHandle(snapshot); |
| 6383 | |
| 6384 | return result; |
| 6385 | } |
| 6386 | #endif /*MS_WINDOWS*/ |
| 6387 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6388 | |
| 6389 | /*[clinic input] |
| 6390 | os.getppid |
| 6391 | |
| 6392 | Return the parent's process id. |
| 6393 | |
| 6394 | If the parent process has already exited, Windows machines will still |
| 6395 | return its id; others systems will return the id of the 'init' process (1). |
| 6396 | [clinic start generated code]*/ |
| 6397 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6398 | static PyObject * |
| 6399 | os_getppid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6400 | /*[clinic end generated code: output=4e49c8e7a8738cd2 input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6401 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6402 | #ifdef MS_WINDOWS |
| 6403 | return win32_getppid(); |
| 6404 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6405 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6406 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6407 | } |
| 6408 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6409 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6410 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6411 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6412 | /*[clinic input] |
| 6413 | os.getlogin |
| 6414 | |
| 6415 | Return the actual login name. |
| 6416 | [clinic start generated code]*/ |
| 6417 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6418 | static PyObject * |
| 6419 | os_getlogin_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6420 | /*[clinic end generated code: output=037ebdb3e4b5dac1 input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6421 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6422 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6423 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6424 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6425 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6426 | |
| 6427 | if (GetUserNameW(user_name, &num_chars)) { |
| 6428 | /* num_chars is the number of unicode chars plus null terminator */ |
| 6429 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6430 | } |
| 6431 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6432 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6433 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6434 | char *name; |
| 6435 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6436 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6437 | errno = 0; |
| 6438 | name = getlogin(); |
| 6439 | if (name == NULL) { |
| 6440 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6441 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6442 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6443 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6444 | } |
| 6445 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6446 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6447 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6448 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6449 | return result; |
| 6450 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6451 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6452 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6453 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6454 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6455 | /*[clinic input] |
| 6456 | os.getuid |
| 6457 | |
| 6458 | Return the current process's user id. |
| 6459 | [clinic start generated code]*/ |
| 6460 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6461 | static PyObject * |
| 6462 | os_getuid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6463 | /*[clinic end generated code: output=03a8b894cefb3fa5 input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6464 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6465 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6466 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6467 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6468 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6469 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6470 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6471 | #define HAVE_KILL |
| 6472 | #endif /* MS_WINDOWS */ |
| 6473 | |
| 6474 | #ifdef HAVE_KILL |
| 6475 | /*[clinic input] |
| 6476 | os.kill |
| 6477 | |
| 6478 | pid: pid_t |
| 6479 | signal: Py_ssize_t |
| 6480 | / |
| 6481 | |
| 6482 | Kill a process with a signal. |
| 6483 | [clinic start generated code]*/ |
| 6484 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6485 | static PyObject * |
| 6486 | os_kill_impl(PyModuleDef *module, pid_t pid, Py_ssize_t signal) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6487 | /*[clinic end generated code: output=74f907dd00a83c26 input=61a36b86ca275ab9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6488 | #ifndef MS_WINDOWS |
| 6489 | { |
| 6490 | if (kill(pid, (int)signal) == -1) |
| 6491 | return posix_error(); |
| 6492 | Py_RETURN_NONE; |
| 6493 | } |
| 6494 | #else /* !MS_WINDOWS */ |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6495 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 6496 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6497 | DWORD sig = (DWORD)signal; |
| 6498 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6499 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6500 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6501 | /* Console processes which share a common console can be sent CTRL+C or |
| 6502 | CTRL+BREAK events, provided they handle said events. */ |
| 6503 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6504 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6505 | err = GetLastError(); |
| 6506 | PyErr_SetFromWindowsErr(err); |
| 6507 | } |
| 6508 | else |
| 6509 | Py_RETURN_NONE; |
| 6510 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6511 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6512 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 6513 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6514 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6515 | if (handle == NULL) { |
| 6516 | err = GetLastError(); |
| 6517 | return PyErr_SetFromWindowsErr(err); |
| 6518 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6519 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6520 | if (TerminateProcess(handle, sig) == 0) { |
| 6521 | err = GetLastError(); |
| 6522 | result = PyErr_SetFromWindowsErr(err); |
| 6523 | } else { |
| 6524 | Py_INCREF(Py_None); |
| 6525 | result = Py_None; |
| 6526 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6527 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6528 | CloseHandle(handle); |
| 6529 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6530 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6531 | #endif /* !MS_WINDOWS */ |
| 6532 | #endif /* HAVE_KILL */ |
| 6533 | |
| 6534 | |
| 6535 | #ifdef HAVE_KILLPG |
| 6536 | /*[clinic input] |
| 6537 | os.killpg |
| 6538 | |
| 6539 | pgid: pid_t |
| 6540 | signal: int |
| 6541 | / |
| 6542 | |
| 6543 | Kill a process group with a signal. |
| 6544 | [clinic start generated code]*/ |
| 6545 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6546 | static PyObject * |
| 6547 | os_killpg_impl(PyModuleDef *module, pid_t pgid, int signal) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6548 | /*[clinic end generated code: output=3434a766ef945f93 input=38b5449eb8faec19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6549 | { |
| 6550 | /* XXX some man pages make the `pgid` parameter an int, others |
| 6551 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 6552 | take the same type. Moreover, pid_t is always at least as wide as |
| 6553 | int (else compilation of this module fails), which is safe. */ |
| 6554 | if (killpg(pgid, signal) == -1) |
| 6555 | return posix_error(); |
| 6556 | Py_RETURN_NONE; |
| 6557 | } |
| 6558 | #endif /* HAVE_KILLPG */ |
| 6559 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6560 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6561 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6562 | #ifdef HAVE_SYS_LOCK_H |
| 6563 | #include <sys/lock.h> |
| 6564 | #endif |
| 6565 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6566 | /*[clinic input] |
| 6567 | os.plock |
| 6568 | op: int |
| 6569 | / |
| 6570 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6571 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6572 | [clinic start generated code]*/ |
| 6573 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6574 | static PyObject * |
| 6575 | os_plock_impl(PyModuleDef *module, int op) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6576 | /*[clinic end generated code: output=5cb851f81b914984 input=e6e5e348e1525f60]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6577 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6578 | if (plock(op) == -1) |
| 6579 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6580 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6581 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6582 | #endif /* HAVE_PLOCK */ |
| 6583 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6584 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6585 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6586 | /*[clinic input] |
| 6587 | os.setuid |
| 6588 | |
| 6589 | uid: uid_t |
| 6590 | / |
| 6591 | |
| 6592 | Set the current process's user id. |
| 6593 | [clinic start generated code]*/ |
| 6594 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6595 | static PyObject * |
| 6596 | os_setuid_impl(PyModuleDef *module, uid_t uid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6597 | /*[clinic end generated code: output=941ea9a8d1e5d565 input=c921a3285aa22256]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6598 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6599 | if (setuid(uid) < 0) |
| 6600 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6601 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6602 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6603 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6604 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6605 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6606 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6607 | /*[clinic input] |
| 6608 | os.seteuid |
| 6609 | |
| 6610 | euid: uid_t |
| 6611 | / |
| 6612 | |
| 6613 | Set the current process's effective user id. |
| 6614 | [clinic start generated code]*/ |
| 6615 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6616 | static PyObject * |
| 6617 | os_seteuid_impl(PyModuleDef *module, uid_t euid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6618 | /*[clinic end generated code: output=66f4f6823a648d6d input=ba93d927e4781aa9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6619 | { |
| 6620 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6621 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6622 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6623 | } |
| 6624 | #endif /* HAVE_SETEUID */ |
| 6625 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6626 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6627 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6628 | /*[clinic input] |
| 6629 | os.setegid |
| 6630 | |
| 6631 | egid: gid_t |
| 6632 | / |
| 6633 | |
| 6634 | Set the current process's effective group id. |
| 6635 | [clinic start generated code]*/ |
| 6636 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6637 | static PyObject * |
| 6638 | os_setegid_impl(PyModuleDef *module, gid_t egid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6639 | /*[clinic end generated code: output=ca094a69a081a60f input=4080526d0ccd6ce3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6640 | { |
| 6641 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6642 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6643 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6644 | } |
| 6645 | #endif /* HAVE_SETEGID */ |
| 6646 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6647 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6648 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6649 | /*[clinic input] |
| 6650 | os.setreuid |
| 6651 | |
| 6652 | ruid: uid_t |
| 6653 | euid: uid_t |
| 6654 | / |
| 6655 | |
| 6656 | Set the current process's real and effective user ids. |
| 6657 | [clinic start generated code]*/ |
| 6658 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6659 | static PyObject * |
| 6660 | os_setreuid_impl(PyModuleDef *module, uid_t ruid, uid_t euid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6661 | /*[clinic end generated code: output=b2938c3e73d27ec7 input=0ca8978de663880c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6662 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6663 | if (setreuid(ruid, euid) < 0) { |
| 6664 | return posix_error(); |
| 6665 | } else { |
| 6666 | Py_INCREF(Py_None); |
| 6667 | return Py_None; |
| 6668 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6669 | } |
| 6670 | #endif /* HAVE_SETREUID */ |
| 6671 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6672 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6673 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6674 | /*[clinic input] |
| 6675 | os.setregid |
| 6676 | |
| 6677 | rgid: gid_t |
| 6678 | egid: gid_t |
| 6679 | / |
| 6680 | |
| 6681 | Set the current process's real and effective group ids. |
| 6682 | [clinic start generated code]*/ |
| 6683 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6684 | static PyObject * |
| 6685 | os_setregid_impl(PyModuleDef *module, gid_t rgid, gid_t egid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6686 | /*[clinic end generated code: output=db18f1839ababe3d input=c59499f72846db78]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6687 | { |
| 6688 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6689 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6690 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6691 | } |
| 6692 | #endif /* HAVE_SETREGID */ |
| 6693 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6694 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6695 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6696 | /*[clinic input] |
| 6697 | os.setgid |
| 6698 | gid: gid_t |
| 6699 | / |
| 6700 | |
| 6701 | Set the current process's group id. |
| 6702 | [clinic start generated code]*/ |
| 6703 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6704 | static PyObject * |
| 6705 | os_setgid_impl(PyModuleDef *module, gid_t gid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6706 | /*[clinic end generated code: output=756cb42c6abd9d87 input=27d30c4059045dc6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6707 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6708 | if (setgid(gid) < 0) |
| 6709 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6710 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6711 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6712 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6713 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6714 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6715 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6716 | /*[clinic input] |
| 6717 | os.setgroups |
| 6718 | |
| 6719 | groups: object |
| 6720 | / |
| 6721 | |
| 6722 | Set the groups of the current process to list. |
| 6723 | [clinic start generated code]*/ |
| 6724 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6725 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6726 | os_setgroups(PyModuleDef *module, PyObject *groups) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6727 | /*[clinic end generated code: output=7945c2e3cc817c58 input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6728 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6729 | int i, len; |
| 6730 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6731 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6732 | if (!PySequence_Check(groups)) { |
| 6733 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6734 | return NULL; |
| 6735 | } |
| 6736 | len = PySequence_Size(groups); |
| 6737 | if (len > MAX_GROUPS) { |
| 6738 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6739 | return NULL; |
| 6740 | } |
| 6741 | for(i = 0; i < len; i++) { |
| 6742 | PyObject *elem; |
| 6743 | elem = PySequence_GetItem(groups, i); |
| 6744 | if (!elem) |
| 6745 | return NULL; |
| 6746 | if (!PyLong_Check(elem)) { |
| 6747 | PyErr_SetString(PyExc_TypeError, |
| 6748 | "groups must be integers"); |
| 6749 | Py_DECREF(elem); |
| 6750 | return NULL; |
| 6751 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6752 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6753 | Py_DECREF(elem); |
| 6754 | return NULL; |
| 6755 | } |
| 6756 | } |
| 6757 | Py_DECREF(elem); |
| 6758 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6759 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6760 | if (setgroups(len, grouplist) < 0) |
| 6761 | return posix_error(); |
| 6762 | Py_INCREF(Py_None); |
| 6763 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6764 | } |
| 6765 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6766 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6767 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6768 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6769 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6770 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6771 | PyObject *result; |
| 6772 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6773 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6774 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6775 | if (pid == -1) |
| 6776 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6777 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6778 | if (struct_rusage == NULL) { |
| 6779 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6780 | if (m == NULL) |
| 6781 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6782 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6783 | Py_DECREF(m); |
| 6784 | if (struct_rusage == NULL) |
| 6785 | return NULL; |
| 6786 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6787 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6788 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6789 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6790 | if (!result) |
| 6791 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6792 | |
| 6793 | #ifndef doubletime |
| 6794 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6795 | #endif |
| 6796 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6797 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6798 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6799 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6800 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6801 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6802 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6803 | SET_INT(result, 2, ru->ru_maxrss); |
| 6804 | SET_INT(result, 3, ru->ru_ixrss); |
| 6805 | SET_INT(result, 4, ru->ru_idrss); |
| 6806 | SET_INT(result, 5, ru->ru_isrss); |
| 6807 | SET_INT(result, 6, ru->ru_minflt); |
| 6808 | SET_INT(result, 7, ru->ru_majflt); |
| 6809 | SET_INT(result, 8, ru->ru_nswap); |
| 6810 | SET_INT(result, 9, ru->ru_inblock); |
| 6811 | SET_INT(result, 10, ru->ru_oublock); |
| 6812 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6813 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6814 | SET_INT(result, 13, ru->ru_nsignals); |
| 6815 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6816 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6817 | #undef SET_INT |
| 6818 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6819 | if (PyErr_Occurred()) { |
| 6820 | Py_DECREF(result); |
| 6821 | return NULL; |
| 6822 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6823 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6824 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6825 | } |
| 6826 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6827 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6828 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6829 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6830 | /*[clinic input] |
| 6831 | os.wait3 |
| 6832 | |
| 6833 | options: int |
| 6834 | Wait for completion of a child process. |
| 6835 | |
| 6836 | Returns a tuple of information about the child process: |
| 6837 | (pid, status, rusage) |
| 6838 | [clinic start generated code]*/ |
| 6839 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6840 | static PyObject * |
| 6841 | os_wait3_impl(PyModuleDef *module, int options) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6842 | /*[clinic end generated code: output=e18af4924dc54945 input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6843 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6844 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6845 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6846 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6847 | WAIT_TYPE status; |
| 6848 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6849 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6850 | do { |
| 6851 | Py_BEGIN_ALLOW_THREADS |
| 6852 | pid = wait3(&status, options, &ru); |
| 6853 | Py_END_ALLOW_THREADS |
| 6854 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6855 | if (pid < 0) |
| 6856 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6857 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6858 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6859 | } |
| 6860 | #endif /* HAVE_WAIT3 */ |
| 6861 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6862 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6863 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6864 | /*[clinic input] |
| 6865 | |
| 6866 | os.wait4 |
| 6867 | |
| 6868 | pid: pid_t |
| 6869 | options: int |
| 6870 | |
| 6871 | Wait for completion of a specific child process. |
| 6872 | |
| 6873 | Returns a tuple of information about the child process: |
| 6874 | (pid, status, rusage) |
| 6875 | [clinic start generated code]*/ |
| 6876 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6877 | static PyObject * |
| 6878 | os_wait4_impl(PyModuleDef *module, pid_t pid, int options) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6879 | /*[clinic end generated code: output=714f19e6ff01e099 input=d11deed0750600ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6880 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6881 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6882 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6883 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6884 | WAIT_TYPE status; |
| 6885 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6886 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6887 | do { |
| 6888 | Py_BEGIN_ALLOW_THREADS |
| 6889 | res = wait4(pid, &status, options, &ru); |
| 6890 | Py_END_ALLOW_THREADS |
| 6891 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6892 | if (res < 0) |
| 6893 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6894 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6895 | return wait_helper(res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6896 | } |
| 6897 | #endif /* HAVE_WAIT4 */ |
| 6898 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6899 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6900 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6901 | /*[clinic input] |
| 6902 | os.waitid |
| 6903 | |
| 6904 | idtype: idtype_t |
| 6905 | Must be one of be P_PID, P_PGID or P_ALL. |
| 6906 | id: id_t |
| 6907 | The id to wait on. |
| 6908 | options: int |
| 6909 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 6910 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 6911 | / |
| 6912 | |
| 6913 | Returns the result of waiting for a process or processes. |
| 6914 | |
| 6915 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 6916 | no children in a waitable state. |
| 6917 | [clinic start generated code]*/ |
| 6918 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6919 | static PyObject * |
| 6920 | os_waitid_impl(PyModuleDef *module, idtype_t idtype, id_t id, int options) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6921 | /*[clinic end generated code: output=5c0192750e22fa2e input=d8e7f76e052b7920]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6922 | { |
| 6923 | PyObject *result; |
| 6924 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6925 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6926 | siginfo_t si; |
| 6927 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6928 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6929 | do { |
| 6930 | Py_BEGIN_ALLOW_THREADS |
| 6931 | res = waitid(idtype, id, &si, options); |
| 6932 | Py_END_ALLOW_THREADS |
| 6933 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6934 | if (res < 0) |
| 6935 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6936 | |
| 6937 | if (si.si_pid == 0) |
| 6938 | Py_RETURN_NONE; |
| 6939 | |
| 6940 | result = PyStructSequence_New(&WaitidResultType); |
| 6941 | if (!result) |
| 6942 | return NULL; |
| 6943 | |
| 6944 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6945 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6946 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6947 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6948 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6949 | if (PyErr_Occurred()) { |
| 6950 | Py_DECREF(result); |
| 6951 | return NULL; |
| 6952 | } |
| 6953 | |
| 6954 | return result; |
| 6955 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6956 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6957 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6958 | |
| 6959 | #if defined(HAVE_WAITPID) |
| 6960 | /*[clinic input] |
| 6961 | os.waitpid |
| 6962 | pid: pid_t |
| 6963 | options: int |
| 6964 | / |
| 6965 | |
| 6966 | Wait for completion of a given child process. |
| 6967 | |
| 6968 | Returns a tuple of information regarding the child process: |
| 6969 | (pid, status) |
| 6970 | |
| 6971 | The options argument is ignored on Windows. |
| 6972 | [clinic start generated code]*/ |
| 6973 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6974 | static PyObject * |
| 6975 | os_waitpid_impl(PyModuleDef *module, pid_t pid, int options) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6976 | /*[clinic end generated code: output=5e3593353d54b15b input=0bf1666b8758fda3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6977 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6978 | pid_t res; |
| 6979 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6980 | WAIT_TYPE status; |
| 6981 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6982 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6983 | do { |
| 6984 | Py_BEGIN_ALLOW_THREADS |
| 6985 | res = waitpid(pid, &status, options); |
| 6986 | Py_END_ALLOW_THREADS |
| 6987 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6988 | if (res < 0) |
| 6989 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6990 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6991 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6992 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6993 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6994 | /* 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] | 6995 | /*[clinic input] |
| 6996 | os.waitpid |
| 6997 | pid: Py_intptr_t |
| 6998 | options: int |
| 6999 | / |
| 7000 | |
| 7001 | Wait for completion of a given process. |
| 7002 | |
| 7003 | Returns a tuple of information regarding the process: |
| 7004 | (pid, status << 8) |
| 7005 | |
| 7006 | The options argument is ignored on Windows. |
| 7007 | [clinic start generated code]*/ |
| 7008 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7009 | static PyObject * |
| 7010 | os_waitpid_impl(PyModuleDef *module, Py_intptr_t pid, int options) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7011 | /*[clinic end generated code: output=fc1d520db019625f input=444c8f51cca5b862]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7012 | { |
| 7013 | int status; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7014 | Py_intptr_t res; |
| 7015 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7016 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7017 | do { |
| 7018 | Py_BEGIN_ALLOW_THREADS |
| 7019 | res = _cwait(&status, pid, options); |
| 7020 | Py_END_ALLOW_THREADS |
| 7021 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 7022 | if (res < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7023 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7024 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7025 | /* 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] | 7026 | return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7027 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7028 | #endif |
| 7029 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7030 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7031 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7032 | /*[clinic input] |
| 7033 | os.wait |
| 7034 | |
| 7035 | Wait for completion of a child process. |
| 7036 | |
| 7037 | Returns a tuple of information about the child process: |
| 7038 | (pid, status) |
| 7039 | [clinic start generated code]*/ |
| 7040 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7041 | static PyObject * |
| 7042 | os_wait_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7043 | /*[clinic end generated code: output=4a7f4978393e0654 input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7044 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7045 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7046 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7047 | WAIT_TYPE status; |
| 7048 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7049 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7050 | do { |
| 7051 | Py_BEGIN_ALLOW_THREADS |
| 7052 | pid = wait(&status); |
| 7053 | Py_END_ALLOW_THREADS |
| 7054 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7055 | if (pid < 0) |
| 7056 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7057 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7058 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7059 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7060 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7061 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7062 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7063 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
| 7064 | PyDoc_STRVAR(readlink__doc__, |
| 7065 | "readlink(path, *, dir_fd=None) -> path\n\n\ |
| 7066 | Return a string representing the path to which the symbolic link points.\n\ |
| 7067 | \n\ |
| 7068 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7069 | and path should be relative; path will then be relative to that directory.\n\ |
| 7070 | dir_fd may not be implemented on your platform.\n\ |
| 7071 | If it is unavailable, using it will raise a NotImplementedError."); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7072 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7073 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7074 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7075 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7076 | /* AC 3.5: merge win32 and not together */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7077 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7078 | posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7079 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7080 | path_t path; |
| 7081 | int dir_fd = DEFAULT_DIR_FD; |
| 7082 | char buffer[MAXPATHLEN]; |
| 7083 | ssize_t length; |
| 7084 | PyObject *return_value = NULL; |
| 7085 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7086 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7087 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7088 | path.function_name = "readlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7089 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords, |
| 7090 | path_converter, &path, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7091 | READLINKAT_DIR_FD_CONVERTER, &dir_fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7092 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7093 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7094 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7095 | #ifdef HAVE_READLINKAT |
| 7096 | if (dir_fd != DEFAULT_DIR_FD) |
| 7097 | length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer)); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 7098 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7099 | #endif |
| 7100 | length = readlink(path.narrow, buffer, sizeof(buffer)); |
| 7101 | Py_END_ALLOW_THREADS |
| 7102 | |
| 7103 | if (length < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7104 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7105 | goto exit; |
| 7106 | } |
| 7107 | |
| 7108 | if (PyUnicode_Check(path.object)) |
| 7109 | return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 7110 | else |
| 7111 | return_value = PyBytes_FromStringAndSize(buffer, length); |
| 7112 | exit: |
| 7113 | path_cleanup(&path); |
| 7114 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7115 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7116 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7117 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7118 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7119 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 7120 | |
| 7121 | static PyObject * |
| 7122 | win_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
| 7123 | { |
| 7124 | wchar_t *path; |
| 7125 | DWORD n_bytes_returned; |
| 7126 | DWORD io_result; |
| 7127 | PyObject *po, *result; |
| 7128 | int dir_fd; |
| 7129 | HANDLE reparse_point_handle; |
| 7130 | |
| 7131 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 7132 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 7133 | wchar_t *print_name; |
| 7134 | |
| 7135 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 7136 | |
| 7137 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords, |
| 7138 | &po, |
| 7139 | dir_fd_unavailable, &dir_fd |
| 7140 | )) |
| 7141 | return NULL; |
| 7142 | |
| 7143 | path = PyUnicode_AsUnicode(po); |
| 7144 | if (path == NULL) |
| 7145 | return NULL; |
| 7146 | |
| 7147 | /* First get a handle to the reparse point */ |
| 7148 | Py_BEGIN_ALLOW_THREADS |
| 7149 | reparse_point_handle = CreateFileW( |
| 7150 | path, |
| 7151 | 0, |
| 7152 | 0, |
| 7153 | 0, |
| 7154 | OPEN_EXISTING, |
| 7155 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 7156 | 0); |
| 7157 | Py_END_ALLOW_THREADS |
| 7158 | |
| 7159 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
| 7160 | return win32_error_object("readlink", po); |
| 7161 | |
| 7162 | Py_BEGIN_ALLOW_THREADS |
| 7163 | /* New call DeviceIoControl to read the reparse point */ |
| 7164 | io_result = DeviceIoControl( |
| 7165 | reparse_point_handle, |
| 7166 | FSCTL_GET_REPARSE_POINT, |
| 7167 | 0, 0, /* in buffer */ |
| 7168 | target_buffer, sizeof(target_buffer), |
| 7169 | &n_bytes_returned, |
| 7170 | 0 /* we're not using OVERLAPPED_IO */ |
| 7171 | ); |
| 7172 | CloseHandle(reparse_point_handle); |
| 7173 | Py_END_ALLOW_THREADS |
| 7174 | |
| 7175 | if (io_result==0) |
| 7176 | return win32_error_object("readlink", po); |
| 7177 | |
| 7178 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 7179 | { |
| 7180 | PyErr_SetString(PyExc_ValueError, |
| 7181 | "not a symbolic link"); |
| 7182 | return NULL; |
| 7183 | } |
| 7184 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7185 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 7186 | |
| 7187 | result = PyUnicode_FromWideChar(print_name, |
| 7188 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
| 7189 | return result; |
| 7190 | } |
| 7191 | |
| 7192 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 7193 | |
| 7194 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7195 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7196 | #ifdef HAVE_SYMLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7197 | |
| 7198 | #if defined(MS_WINDOWS) |
| 7199 | |
| 7200 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 7201 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD) = NULL; |
| 7202 | static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPSTR, LPSTR, DWORD) = NULL; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7203 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7204 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7205 | check_CreateSymbolicLink(void) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7206 | { |
| 7207 | HINSTANCE hKernel32; |
| 7208 | /* only recheck */ |
| 7209 | if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA) |
| 7210 | return 1; |
| 7211 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 7212 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 7213 | "CreateSymbolicLinkW"); |
| 7214 | *(FARPROC*)&Py_CreateSymbolicLinkA = GetProcAddress(hKernel32, |
| 7215 | "CreateSymbolicLinkA"); |
| 7216 | return (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA); |
| 7217 | } |
| 7218 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7219 | /* Remove the last portion of the path */ |
| 7220 | static void |
| 7221 | _dirnameW(WCHAR *path) |
| 7222 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7223 | WCHAR *ptr; |
| 7224 | |
| 7225 | /* walk the path from the end until a backslash is encountered */ |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7226 | for(ptr = path + wcslen(path); ptr != path; ptr--) { |
Victor Stinner | 072318b | 2013-06-05 02:07:46 +0200 | [diff] [blame] | 7227 | if (*ptr == L'\\' || *ptr == L'/') |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7228 | break; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7229 | } |
| 7230 | *ptr = 0; |
| 7231 | } |
| 7232 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7233 | /* Remove the last portion of the path */ |
| 7234 | static void |
| 7235 | _dirnameA(char *path) |
| 7236 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7237 | char *ptr; |
| 7238 | |
| 7239 | /* walk the path from the end until a backslash is encountered */ |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7240 | for(ptr = path + strlen(path); ptr != path; ptr--) { |
| 7241 | if (*ptr == '\\' || *ptr == '/') |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7242 | break; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7243 | } |
| 7244 | *ptr = 0; |
| 7245 | } |
| 7246 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7247 | /* Is this path absolute? */ |
| 7248 | static int |
| 7249 | _is_absW(const WCHAR *path) |
| 7250 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7251 | return path[0] == L'\\' || path[0] == L'/' || path[1] == L':'; |
| 7252 | |
| 7253 | } |
| 7254 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7255 | /* Is this path absolute? */ |
| 7256 | static int |
| 7257 | _is_absA(const char *path) |
| 7258 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7259 | return path[0] == '\\' || path[0] == '/' || path[1] == ':'; |
| 7260 | |
| 7261 | } |
| 7262 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7263 | /* join root and rest with a backslash */ |
| 7264 | static void |
| 7265 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 7266 | { |
Victor Stinner | e7e7eba | 2013-06-05 00:35:54 +0200 | [diff] [blame] | 7267 | size_t root_len; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7268 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7269 | if (_is_absW(rest)) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7270 | wcscpy(dest_path, rest); |
| 7271 | return; |
| 7272 | } |
| 7273 | |
| 7274 | root_len = wcslen(root); |
| 7275 | |
| 7276 | wcscpy(dest_path, root); |
| 7277 | if(root_len) { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7278 | dest_path[root_len] = L'\\'; |
| 7279 | root_len++; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7280 | } |
| 7281 | wcscpy(dest_path+root_len, rest); |
| 7282 | } |
| 7283 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7284 | /* join root and rest with a backslash */ |
| 7285 | static void |
| 7286 | _joinA(char *dest_path, const char *root, const char *rest) |
| 7287 | { |
Victor Stinner | e7e7eba | 2013-06-05 00:35:54 +0200 | [diff] [blame] | 7288 | size_t root_len; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7289 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7290 | if (_is_absA(rest)) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7291 | strcpy(dest_path, rest); |
| 7292 | return; |
| 7293 | } |
| 7294 | |
| 7295 | root_len = strlen(root); |
| 7296 | |
| 7297 | strcpy(dest_path, root); |
| 7298 | if(root_len) { |
| 7299 | dest_path[root_len] = '\\'; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7300 | root_len++; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7301 | } |
| 7302 | strcpy(dest_path+root_len, rest); |
| 7303 | } |
| 7304 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7305 | /* Return True if the path at src relative to dest is a directory */ |
| 7306 | static int |
| 7307 | _check_dirW(WCHAR *src, WCHAR *dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7308 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7309 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7310 | WCHAR dest_parent[MAX_PATH]; |
| 7311 | WCHAR src_resolved[MAX_PATH] = L""; |
| 7312 | |
| 7313 | /* dest_parent = os.path.dirname(dest) */ |
| 7314 | wcscpy(dest_parent, dest); |
| 7315 | _dirnameW(dest_parent); |
| 7316 | /* src_resolved = os.path.join(dest_parent, src) */ |
| 7317 | _joinW(src_resolved, dest_parent, src); |
| 7318 | return ( |
| 7319 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 7320 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7321 | ); |
| 7322 | } |
| 7323 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7324 | /* Return True if the path at src relative to dest is a directory */ |
| 7325 | static int |
| 7326 | _check_dirA(char *src, char *dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7327 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7328 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7329 | char dest_parent[MAX_PATH]; |
| 7330 | char src_resolved[MAX_PATH] = ""; |
| 7331 | |
| 7332 | /* dest_parent = os.path.dirname(dest) */ |
| 7333 | strcpy(dest_parent, dest); |
Victor Stinner | 5a43676 | 2013-06-05 00:37:12 +0200 | [diff] [blame] | 7334 | _dirnameA(dest_parent); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7335 | /* src_resolved = os.path.join(dest_parent, src) */ |
Victor Stinner | 5a43676 | 2013-06-05 00:37:12 +0200 | [diff] [blame] | 7336 | _joinA(src_resolved, dest_parent, src); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7337 | return ( |
| 7338 | GetFileAttributesExA(src_resolved, GetFileExInfoStandard, &src_info) |
| 7339 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7340 | ); |
| 7341 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7342 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7343 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7344 | |
| 7345 | /*[clinic input] |
| 7346 | os.symlink |
| 7347 | src: path_t |
| 7348 | dst: path_t |
| 7349 | target_is_directory: bool = False |
| 7350 | * |
| 7351 | dir_fd: dir_fd(requires='symlinkat')=None |
| 7352 | |
| 7353 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 7354 | |
| 7355 | Create a symbolic link pointing to src named dst. |
| 7356 | |
| 7357 | target_is_directory is required on Windows if the target is to be |
| 7358 | interpreted as a directory. (On Windows, symlink requires |
| 7359 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 7360 | target_is_directory is ignored on non-Windows platforms. |
| 7361 | |
| 7362 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7363 | and path should be relative; path will then be relative to that directory. |
| 7364 | dir_fd may not be implemented on your platform. |
| 7365 | If it is unavailable, using it will raise a NotImplementedError. |
| 7366 | |
| 7367 | [clinic start generated code]*/ |
| 7368 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7369 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7370 | os_symlink_impl(PyModuleDef *module, path_t *src, path_t *dst, |
| 7371 | int target_is_directory, int dir_fd) |
| 7372 | /*[clinic end generated code: output=a01b4bcf32403ccd input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7373 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7374 | #ifdef MS_WINDOWS |
| 7375 | DWORD result; |
| 7376 | #else |
| 7377 | int result; |
| 7378 | #endif |
| 7379 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7380 | #ifdef MS_WINDOWS |
| 7381 | if (!check_CreateSymbolicLink()) { |
| 7382 | PyErr_SetString(PyExc_NotImplementedError, |
| 7383 | "CreateSymbolicLink functions not found"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7384 | return NULL; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7385 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7386 | if (!win32_can_symlink) { |
| 7387 | PyErr_SetString(PyExc_OSError, "symbolic link privilege not held"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7388 | return NULL; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7389 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7390 | #endif |
| 7391 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7392 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7393 | PyErr_SetString(PyExc_ValueError, |
| 7394 | "symlink: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7395 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7396 | } |
| 7397 | |
| 7398 | #ifdef MS_WINDOWS |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7399 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7400 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7401 | if (dst->wide) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7402 | /* if src is a directory, ensure target_is_directory==1 */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7403 | target_is_directory |= _check_dirW(src->wide, dst->wide); |
| 7404 | result = Py_CreateSymbolicLinkW(dst->wide, src->wide, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7405 | target_is_directory); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7406 | } |
| 7407 | else { |
| 7408 | /* if src is a directory, ensure target_is_directory==1 */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7409 | target_is_directory |= _check_dirA(src->narrow, dst->narrow); |
| 7410 | result = Py_CreateSymbolicLinkA(dst->narrow, src->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7411 | target_is_directory); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7412 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7413 | Py_END_ALLOW_THREADS |
| 7414 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7415 | if (!result) |
| 7416 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7417 | |
| 7418 | #else |
| 7419 | |
| 7420 | Py_BEGIN_ALLOW_THREADS |
| 7421 | #if HAVE_SYMLINKAT |
| 7422 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7423 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7424 | else |
| 7425 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7426 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7427 | Py_END_ALLOW_THREADS |
| 7428 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7429 | if (result) |
| 7430 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7431 | #endif |
| 7432 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7433 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7434 | } |
| 7435 | #endif /* HAVE_SYMLINK */ |
| 7436 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7437 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7438 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7439 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7440 | static PyStructSequence_Field times_result_fields[] = { |
| 7441 | {"user", "user time"}, |
| 7442 | {"system", "system time"}, |
| 7443 | {"children_user", "user time of children"}, |
| 7444 | {"children_system", "system time of children"}, |
| 7445 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 7446 | {NULL} |
| 7447 | }; |
| 7448 | |
| 7449 | PyDoc_STRVAR(times_result__doc__, |
| 7450 | "times_result: Result from os.times().\n\n\ |
| 7451 | This object may be accessed either as a tuple of\n\ |
| 7452 | (user, system, children_user, children_system, elapsed),\n\ |
| 7453 | or via the attributes user, system, children_user, children_system,\n\ |
| 7454 | and elapsed.\n\ |
| 7455 | \n\ |
| 7456 | See os.times for more information."); |
| 7457 | |
| 7458 | static PyStructSequence_Desc times_result_desc = { |
| 7459 | "times_result", /* name */ |
| 7460 | times_result__doc__, /* doc */ |
| 7461 | times_result_fields, |
| 7462 | 5 |
| 7463 | }; |
| 7464 | |
| 7465 | static PyTypeObject TimesResultType; |
| 7466 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7467 | #ifdef MS_WINDOWS |
| 7468 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 7469 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7470 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7471 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7472 | |
| 7473 | static PyObject * |
| 7474 | build_times_result(double user, double system, |
| 7475 | double children_user, double children_system, |
| 7476 | double elapsed) |
| 7477 | { |
| 7478 | PyObject *value = PyStructSequence_New(&TimesResultType); |
| 7479 | if (value == NULL) |
| 7480 | return NULL; |
| 7481 | |
| 7482 | #define SET(i, field) \ |
| 7483 | { \ |
| 7484 | PyObject *o = PyFloat_FromDouble(field); \ |
| 7485 | if (!o) { \ |
| 7486 | Py_DECREF(value); \ |
| 7487 | return NULL; \ |
| 7488 | } \ |
| 7489 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 7490 | } \ |
| 7491 | |
| 7492 | SET(0, user); |
| 7493 | SET(1, system); |
| 7494 | SET(2, children_user); |
| 7495 | SET(3, children_system); |
| 7496 | SET(4, elapsed); |
| 7497 | |
| 7498 | #undef SET |
| 7499 | |
| 7500 | return value; |
| 7501 | } |
| 7502 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7503 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7504 | #ifndef MS_WINDOWS |
| 7505 | #define NEED_TICKS_PER_SECOND |
| 7506 | static long ticks_per_second = -1; |
| 7507 | #endif /* MS_WINDOWS */ |
| 7508 | |
| 7509 | /*[clinic input] |
| 7510 | os.times |
| 7511 | |
| 7512 | Return a collection containing process timing information. |
| 7513 | |
| 7514 | The object returned behaves like a named tuple with these fields: |
| 7515 | (utime, stime, cutime, cstime, elapsed_time) |
| 7516 | All fields are floating point numbers. |
| 7517 | [clinic start generated code]*/ |
| 7518 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7519 | static PyObject * |
| 7520 | os_times_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7521 | /*[clinic end generated code: output=df0a63ebe6e6f091 input=2bf9df3d6ab2e48b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7522 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7523 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7524 | FILETIME create, exit, kernel, user; |
| 7525 | HANDLE hProc; |
| 7526 | hProc = GetCurrentProcess(); |
| 7527 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 7528 | /* The fields of a FILETIME structure are the hi and lo part |
| 7529 | of a 64-bit value expressed in 100 nanosecond units. |
| 7530 | 1e7 is one second in such units; 1e-7 the inverse. |
| 7531 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 7532 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7533 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7534 | (double)(user.dwHighDateTime*429.4967296 + |
| 7535 | user.dwLowDateTime*1e-7), |
| 7536 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 7537 | kernel.dwLowDateTime*1e-7), |
| 7538 | (double)0, |
| 7539 | (double)0, |
| 7540 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7541 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7542 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7543 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7544 | |
| 7545 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7546 | struct tms t; |
| 7547 | clock_t c; |
| 7548 | errno = 0; |
| 7549 | c = times(&t); |
| 7550 | if (c == (clock_t) -1) |
| 7551 | return posix_error(); |
| 7552 | return build_times_result( |
| 7553 | (double)t.tms_utime / ticks_per_second, |
| 7554 | (double)t.tms_stime / ticks_per_second, |
| 7555 | (double)t.tms_cutime / ticks_per_second, |
| 7556 | (double)t.tms_cstime / ticks_per_second, |
| 7557 | (double)c / ticks_per_second); |
| 7558 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7559 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7560 | #endif /* HAVE_TIMES */ |
| 7561 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7562 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7563 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7564 | /*[clinic input] |
| 7565 | os.getsid |
| 7566 | |
| 7567 | pid: pid_t |
| 7568 | / |
| 7569 | |
| 7570 | Call the system call getsid(pid) and return the result. |
| 7571 | [clinic start generated code]*/ |
| 7572 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7573 | static PyObject * |
| 7574 | os_getsid_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7575 | /*[clinic end generated code: output=a074f80c0e6bfb38 input=eeb2b923a30ce04e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7576 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7577 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7578 | sid = getsid(pid); |
| 7579 | if (sid < 0) |
| 7580 | return posix_error(); |
| 7581 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7582 | } |
| 7583 | #endif /* HAVE_GETSID */ |
| 7584 | |
| 7585 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7586 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7587 | /*[clinic input] |
| 7588 | os.setsid |
| 7589 | |
| 7590 | Call the system call setsid(). |
| 7591 | [clinic start generated code]*/ |
| 7592 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7593 | static PyObject * |
| 7594 | os_setsid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7595 | /*[clinic end generated code: output=398fc152ae327330 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7596 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7597 | if (setsid() < 0) |
| 7598 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7599 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7600 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7601 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7602 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7603 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7604 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7605 | /*[clinic input] |
| 7606 | os.setpgid |
| 7607 | |
| 7608 | pid: pid_t |
| 7609 | pgrp: pid_t |
| 7610 | / |
| 7611 | |
| 7612 | Call the system call setpgid(pid, pgrp). |
| 7613 | [clinic start generated code]*/ |
| 7614 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7615 | static PyObject * |
| 7616 | os_setpgid_impl(PyModuleDef *module, pid_t pid, pid_t pgrp) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7617 | /*[clinic end generated code: output=7079a8e932912841 input=fceb395eca572e1a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7618 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7619 | if (setpgid(pid, pgrp) < 0) |
| 7620 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7621 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7622 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7623 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7624 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7625 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7626 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7627 | /*[clinic input] |
| 7628 | os.tcgetpgrp |
| 7629 | |
| 7630 | fd: int |
| 7631 | / |
| 7632 | |
| 7633 | Return the process group associated with the terminal specified by fd. |
| 7634 | [clinic start generated code]*/ |
| 7635 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7636 | static PyObject * |
| 7637 | os_tcgetpgrp_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7638 | /*[clinic end generated code: output=ebb6dc5f111c7dc0 input=7f6c18eac10ada86]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7639 | { |
| 7640 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7641 | if (pgid < 0) |
| 7642 | return posix_error(); |
| 7643 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7644 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7645 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7646 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7647 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7648 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7649 | /*[clinic input] |
| 7650 | os.tcsetpgrp |
| 7651 | |
| 7652 | fd: int |
| 7653 | pgid: pid_t |
| 7654 | / |
| 7655 | |
| 7656 | Set the process group associated with the terminal specified by fd. |
| 7657 | [clinic start generated code]*/ |
| 7658 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7659 | static PyObject * |
| 7660 | os_tcsetpgrp_impl(PyModuleDef *module, int fd, pid_t pgid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7661 | /*[clinic end generated code: output=3e4b05177462cd22 input=5bdc997c6a619020]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7662 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7663 | if (tcsetpgrp(fd, pgid) < 0) |
| 7664 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7665 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7666 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7667 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7668 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7669 | /* Functions acting on file descriptors */ |
| 7670 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7671 | #ifdef O_CLOEXEC |
| 7672 | extern int _Py_open_cloexec_works; |
| 7673 | #endif |
| 7674 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7675 | |
| 7676 | /*[clinic input] |
| 7677 | os.open -> int |
| 7678 | path: path_t |
| 7679 | flags: int |
| 7680 | mode: int = 0o777 |
| 7681 | * |
| 7682 | dir_fd: dir_fd(requires='openat') = None |
| 7683 | |
| 7684 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 7685 | |
| 7686 | Open a file for low level IO. Returns a file descriptor (integer). |
| 7687 | |
| 7688 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7689 | and path should be relative; path will then be relative to that directory. |
| 7690 | dir_fd may not be implemented on your platform. |
| 7691 | If it is unavailable, using it will raise a NotImplementedError. |
| 7692 | [clinic start generated code]*/ |
| 7693 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7694 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7695 | os_open_impl(PyModuleDef *module, path_t *path, int flags, int mode, |
| 7696 | int dir_fd) |
| 7697 | /*[clinic end generated code: output=47e8cc63559f5ddd input=ad8623b29acd2934]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7698 | { |
| 7699 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7700 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7701 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7702 | #ifdef O_CLOEXEC |
| 7703 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 7704 | #elif !defined(MS_WINDOWS) |
| 7705 | int *atomic_flag_works = NULL; |
| 7706 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7707 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7708 | #ifdef MS_WINDOWS |
| 7709 | flags |= O_NOINHERIT; |
| 7710 | #elif defined(O_CLOEXEC) |
| 7711 | flags |= O_CLOEXEC; |
| 7712 | #endif |
| 7713 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7714 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7715 | do { |
| 7716 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7717 | #ifdef MS_WINDOWS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7718 | if (path->wide) |
| 7719 | fd = _wopen(path->wide, flags, mode); |
| 7720 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7721 | #endif |
| 7722 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7723 | if (dir_fd != DEFAULT_DIR_FD) |
| 7724 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 7725 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7726 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7727 | fd = open(path->narrow, flags, mode); |
| 7728 | Py_END_ALLOW_THREADS |
| 7729 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7730 | _Py_END_SUPPRESS_IPH |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7731 | |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 7732 | if (fd < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7733 | if (!async_err) |
| 7734 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7735 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7736 | } |
| 7737 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7738 | #ifndef MS_WINDOWS |
| 7739 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 7740 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7741 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7742 | } |
| 7743 | #endif |
| 7744 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7745 | return fd; |
| 7746 | } |
| 7747 | |
| 7748 | |
| 7749 | /*[clinic input] |
| 7750 | os.close |
| 7751 | |
| 7752 | fd: int |
| 7753 | |
| 7754 | Close a file descriptor. |
| 7755 | [clinic start generated code]*/ |
| 7756 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7757 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7758 | os_close_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7759 | /*[clinic end generated code: output=47bf2ea536445a26 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7760 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7761 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7762 | if (!_PyVerify_fd(fd)) |
| 7763 | return posix_error(); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7764 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 7765 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 7766 | * for more details. |
| 7767 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7768 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7769 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7770 | res = close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7771 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7772 | Py_END_ALLOW_THREADS |
| 7773 | if (res < 0) |
| 7774 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7775 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7776 | } |
| 7777 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7778 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7779 | /*[clinic input] |
| 7780 | os.closerange |
| 7781 | |
| 7782 | fd_low: int |
| 7783 | fd_high: int |
| 7784 | / |
| 7785 | |
| 7786 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 7787 | [clinic start generated code]*/ |
| 7788 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7789 | static PyObject * |
| 7790 | os_closerange_impl(PyModuleDef *module, int fd_low, int fd_high) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7791 | /*[clinic end generated code: output=70e6adb95220ba96 input=5855a3d053ebd4ec]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7792 | { |
| 7793 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7794 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7795 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7796 | for (i = fd_low; i < fd_high; i++) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7797 | if (_PyVerify_fd(i)) |
| 7798 | close(i); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7799 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7800 | Py_END_ALLOW_THREADS |
| 7801 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7802 | } |
| 7803 | |
| 7804 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7805 | /*[clinic input] |
| 7806 | os.dup -> int |
| 7807 | |
| 7808 | fd: int |
| 7809 | / |
| 7810 | |
| 7811 | Return a duplicate of a file descriptor. |
| 7812 | [clinic start generated code]*/ |
| 7813 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7814 | static int |
| 7815 | os_dup_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7816 | /*[clinic end generated code: output=f4bbac8c7652d05e input=6f10f7ea97f7852a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7817 | { |
| 7818 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7819 | } |
| 7820 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7821 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7822 | /*[clinic input] |
| 7823 | os.dup2 |
| 7824 | fd: int |
| 7825 | fd2: int |
| 7826 | inheritable: bool=True |
| 7827 | |
| 7828 | Duplicate file descriptor. |
| 7829 | [clinic start generated code]*/ |
| 7830 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7831 | static PyObject * |
| 7832 | os_dup2_impl(PyModuleDef *module, int fd, int fd2, int inheritable) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7833 | /*[clinic end generated code: output=9a099d95881a7923 input=76e96f511be0352f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7834 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7835 | int res; |
| 7836 | #if defined(HAVE_DUP3) && \ |
| 7837 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 7838 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
| 7839 | int dup3_works = -1; |
| 7840 | #endif |
| 7841 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7842 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 7843 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7844 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7845 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 7846 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 7847 | * upon close(), and therefore below. |
| 7848 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7849 | #ifdef MS_WINDOWS |
| 7850 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7851 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7852 | res = dup2(fd, fd2); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7853 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7854 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7855 | if (res < 0) |
| 7856 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7857 | |
| 7858 | /* Character files like console cannot be make non-inheritable */ |
| 7859 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7860 | close(fd2); |
| 7861 | return NULL; |
| 7862 | } |
| 7863 | |
| 7864 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 7865 | Py_BEGIN_ALLOW_THREADS |
| 7866 | if (!inheritable) |
| 7867 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 7868 | else |
| 7869 | res = dup2(fd, fd2); |
| 7870 | Py_END_ALLOW_THREADS |
| 7871 | if (res < 0) |
| 7872 | return posix_error(); |
| 7873 | |
| 7874 | #else |
| 7875 | |
| 7876 | #ifdef HAVE_DUP3 |
| 7877 | if (!inheritable && dup3_works != 0) { |
| 7878 | Py_BEGIN_ALLOW_THREADS |
| 7879 | res = dup3(fd, fd2, O_CLOEXEC); |
| 7880 | Py_END_ALLOW_THREADS |
| 7881 | if (res < 0) { |
| 7882 | if (dup3_works == -1) |
| 7883 | dup3_works = (errno != ENOSYS); |
| 7884 | if (dup3_works) |
| 7885 | return posix_error(); |
| 7886 | } |
| 7887 | } |
| 7888 | |
| 7889 | if (inheritable || dup3_works == 0) |
| 7890 | { |
| 7891 | #endif |
| 7892 | Py_BEGIN_ALLOW_THREADS |
| 7893 | res = dup2(fd, fd2); |
| 7894 | Py_END_ALLOW_THREADS |
| 7895 | if (res < 0) |
| 7896 | return posix_error(); |
| 7897 | |
| 7898 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7899 | close(fd2); |
| 7900 | return NULL; |
| 7901 | } |
| 7902 | #ifdef HAVE_DUP3 |
| 7903 | } |
| 7904 | #endif |
| 7905 | |
| 7906 | #endif |
| 7907 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7908 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7909 | } |
| 7910 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7911 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7912 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7913 | /*[clinic input] |
| 7914 | os.lockf |
| 7915 | |
| 7916 | fd: int |
| 7917 | An open file descriptor. |
| 7918 | command: int |
| 7919 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 7920 | length: Py_off_t |
| 7921 | The number of bytes to lock, starting at the current position. |
| 7922 | / |
| 7923 | |
| 7924 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 7925 | |
| 7926 | [clinic start generated code]*/ |
| 7927 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7928 | static PyObject * |
| 7929 | os_lockf_impl(PyModuleDef *module, int fd, int command, Py_off_t length) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7930 | /*[clinic end generated code: output=25ff778f9e2fbf1b input=65da41d2106e9b79]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7931 | { |
| 7932 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7933 | |
| 7934 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7935 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7936 | Py_END_ALLOW_THREADS |
| 7937 | |
| 7938 | if (res < 0) |
| 7939 | return posix_error(); |
| 7940 | |
| 7941 | Py_RETURN_NONE; |
| 7942 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7943 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7944 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7945 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7946 | /*[clinic input] |
| 7947 | os.lseek -> Py_off_t |
| 7948 | |
| 7949 | fd: int |
| 7950 | position: Py_off_t |
| 7951 | how: int |
| 7952 | / |
| 7953 | |
| 7954 | Set the position of a file descriptor. Return the new position. |
| 7955 | |
| 7956 | Return the new cursor position in number of bytes |
| 7957 | relative to the beginning of the file. |
| 7958 | [clinic start generated code]*/ |
| 7959 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7960 | static Py_off_t |
| 7961 | os_lseek_impl(PyModuleDef *module, int fd, Py_off_t position, int how) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7962 | /*[clinic end generated code: output=65d4ab96d664998c input=902654ad3f96a6d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7963 | { |
| 7964 | Py_off_t result; |
| 7965 | |
| 7966 | if (!_PyVerify_fd(fd)) { |
| 7967 | posix_error(); |
| 7968 | return -1; |
| 7969 | } |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7970 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7971 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 7972 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7973 | case 0: how = SEEK_SET; break; |
| 7974 | case 1: how = SEEK_CUR; break; |
| 7975 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7976 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7977 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7978 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7979 | if (PyErr_Occurred()) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7980 | return -1; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7981 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7982 | if (!_PyVerify_fd(fd)) { |
| 7983 | posix_error(); |
| 7984 | return -1; |
| 7985 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7986 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7987 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 7988 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7989 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7990 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7991 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7992 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7993 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7994 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7995 | if (result < 0) |
| 7996 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7997 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7998 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7999 | } |
| 8000 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8001 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8002 | /*[clinic input] |
| 8003 | os.read |
| 8004 | fd: int |
| 8005 | length: Py_ssize_t |
| 8006 | / |
| 8007 | |
| 8008 | Read from a file descriptor. Returns a bytes object. |
| 8009 | [clinic start generated code]*/ |
| 8010 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8011 | static PyObject * |
| 8012 | os_read_impl(PyModuleDef *module, int fd, Py_ssize_t length) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8013 | /*[clinic end generated code: output=be24f44178455e8b input=1df2eaa27c0bf1d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8014 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8015 | Py_ssize_t n; |
| 8016 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8017 | |
| 8018 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8019 | errno = EINVAL; |
| 8020 | return posix_error(); |
| 8021 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8022 | |
| 8023 | #ifdef MS_WINDOWS |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8024 | /* On Windows, the count parameter of read() is an int */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8025 | if (length > INT_MAX) |
| 8026 | length = INT_MAX; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8027 | #endif |
| 8028 | |
| 8029 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8030 | if (buffer == NULL) |
| 8031 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8032 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8033 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 8034 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8035 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8036 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8037 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8038 | |
| 8039 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8040 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8041 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8042 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8043 | } |
| 8044 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8045 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 8046 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8047 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8048 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 8049 | { |
| 8050 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8051 | Py_ssize_t blen, total = 0; |
| 8052 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8053 | *iov = PyMem_New(struct iovec, cnt); |
| 8054 | if (*iov == NULL) { |
| 8055 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8056 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8057 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8058 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8059 | *buf = PyMem_New(Py_buffer, cnt); |
| 8060 | if (*buf == NULL) { |
| 8061 | PyMem_Del(*iov); |
| 8062 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8063 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8064 | } |
| 8065 | |
| 8066 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8067 | PyObject *item = PySequence_GetItem(seq, i); |
| 8068 | if (item == NULL) |
| 8069 | goto fail; |
| 8070 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 8071 | Py_DECREF(item); |
| 8072 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8073 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8074 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8075 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8076 | blen = (*buf)[i].len; |
| 8077 | (*iov)[i].iov_len = blen; |
| 8078 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8079 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8080 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8081 | |
| 8082 | fail: |
| 8083 | PyMem_Del(*iov); |
| 8084 | for (j = 0; j < i; j++) { |
| 8085 | PyBuffer_Release(&(*buf)[j]); |
| 8086 | } |
| 8087 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8088 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8089 | } |
| 8090 | |
| 8091 | static void |
| 8092 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 8093 | { |
| 8094 | int i; |
| 8095 | PyMem_Del(iov); |
| 8096 | for (i = 0; i < cnt; i++) { |
| 8097 | PyBuffer_Release(&buf[i]); |
| 8098 | } |
| 8099 | PyMem_Del(buf); |
| 8100 | } |
| 8101 | #endif |
| 8102 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8103 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8104 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8105 | /*[clinic input] |
| 8106 | os.readv -> Py_ssize_t |
| 8107 | |
| 8108 | fd: int |
| 8109 | buffers: object |
| 8110 | / |
| 8111 | |
| 8112 | Read from a file descriptor fd into an iterable of buffers. |
| 8113 | |
| 8114 | The buffers should be mutable buffers accepting bytes. |
| 8115 | readv will transfer data into each buffer until it is full |
| 8116 | and then move on to the next buffer in the sequence to hold |
| 8117 | the rest of the data. |
| 8118 | |
| 8119 | readv returns the total number of bytes read, |
| 8120 | which may be less than the total capacity of all the buffers. |
| 8121 | [clinic start generated code]*/ |
| 8122 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8123 | static Py_ssize_t |
| 8124 | os_readv_impl(PyModuleDef *module, int fd, PyObject *buffers) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8125 | /*[clinic end generated code: output=00fc56ff1800059f input=e679eb5dbfa0357d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8126 | { |
| 8127 | int cnt; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8128 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8129 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8130 | struct iovec *iov; |
| 8131 | Py_buffer *buf; |
| 8132 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8133 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8134 | PyErr_SetString(PyExc_TypeError, |
| 8135 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8136 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8137 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8138 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8139 | cnt = PySequence_Size(buffers); |
| 8140 | |
| 8141 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 8142 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8143 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8144 | do { |
| 8145 | Py_BEGIN_ALLOW_THREADS |
| 8146 | n = readv(fd, iov, cnt); |
| 8147 | Py_END_ALLOW_THREADS |
| 8148 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8149 | |
| 8150 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8151 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8152 | if (!async_err) |
| 8153 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8154 | return -1; |
| 8155 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8156 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8157 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8158 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8159 | #endif /* HAVE_READV */ |
| 8160 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8161 | |
| 8162 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8163 | /*[clinic input] |
| 8164 | # TODO length should be size_t! but Python doesn't support parsing size_t yet. |
| 8165 | os.pread |
| 8166 | |
| 8167 | fd: int |
| 8168 | length: int |
| 8169 | offset: Py_off_t |
| 8170 | / |
| 8171 | |
| 8172 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 8173 | |
| 8174 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 8175 | the beginning of the file. The file offset remains unchanged. |
| 8176 | [clinic start generated code]*/ |
| 8177 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8178 | static PyObject * |
| 8179 | os_pread_impl(PyModuleDef *module, int fd, int length, Py_off_t offset) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8180 | /*[clinic end generated code: output=90d1fed87f68fa33 input=084948dcbaa35d4c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8181 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8182 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8183 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8184 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8185 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8186 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8187 | errno = EINVAL; |
| 8188 | return posix_error(); |
| 8189 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8190 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8191 | if (buffer == NULL) |
| 8192 | return NULL; |
| 8193 | if (!_PyVerify_fd(fd)) { |
| 8194 | Py_DECREF(buffer); |
| 8195 | return posix_error(); |
| 8196 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8197 | |
| 8198 | do { |
| 8199 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8200 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8201 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8202 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8203 | Py_END_ALLOW_THREADS |
| 8204 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8205 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8206 | if (n < 0) { |
| 8207 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8208 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8209 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8210 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8211 | _PyBytes_Resize(&buffer, n); |
| 8212 | return buffer; |
| 8213 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8214 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8215 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8216 | |
| 8217 | /*[clinic input] |
| 8218 | os.write -> Py_ssize_t |
| 8219 | |
| 8220 | fd: int |
| 8221 | data: Py_buffer |
| 8222 | / |
| 8223 | |
| 8224 | Write a bytes object to a file descriptor. |
| 8225 | [clinic start generated code]*/ |
| 8226 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8227 | static Py_ssize_t |
| 8228 | os_write_impl(PyModuleDef *module, int fd, Py_buffer *data) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8229 | /*[clinic end generated code: output=58845c93c9ee1dda input=3207e28963234f3c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8230 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8231 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8232 | } |
| 8233 | |
| 8234 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8235 | PyDoc_STRVAR(posix_sendfile__doc__, |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8236 | "sendfile(out, in, offset, count) -> byteswritten\n\ |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8237 | sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8238 | -> byteswritten\n\ |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8239 | Copy count bytes from file descriptor in to file descriptor out."); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8240 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8241 | /* AC 3.5: don't bother converting, has optional group*/ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8242 | static PyObject * |
| 8243 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 8244 | { |
| 8245 | int in, out; |
| 8246 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8247 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8248 | off_t offset; |
| 8249 | |
| 8250 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 8251 | #ifndef __APPLE__ |
| 8252 | Py_ssize_t len; |
| 8253 | #endif |
| 8254 | PyObject *headers = NULL, *trailers = NULL; |
| 8255 | Py_buffer *hbuf, *tbuf; |
| 8256 | off_t sbytes; |
| 8257 | struct sf_hdtr sf; |
| 8258 | int flags = 0; |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8259 | /* Beware that "in" clashes with Python's own "in" operator keyword */ |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 8260 | static char *keywords[] = {"out", "in", |
| 8261 | "offset", "count", |
| 8262 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8263 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 8264 | sf.headers = NULL; |
| 8265 | sf.trailers = NULL; |
| 8266 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8267 | #ifdef __APPLE__ |
| 8268 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8269 | 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] | 8270 | #else |
| 8271 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8272 | keywords, &out, &in, Py_off_t_converter, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8273 | #endif |
| 8274 | &headers, &trailers, &flags)) |
| 8275 | return NULL; |
| 8276 | if (headers != NULL) { |
| 8277 | if (!PySequence_Check(headers)) { |
| 8278 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8279 | "sendfile() headers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8280 | return NULL; |
| 8281 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8282 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8283 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8284 | if (sf.hdr_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8285 | (i = iov_setup(&(sf.headers), &hbuf, |
| 8286 | headers, sf.hdr_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8287 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8288 | #ifdef __APPLE__ |
| 8289 | sbytes += i; |
| 8290 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8291 | } |
| 8292 | } |
| 8293 | if (trailers != NULL) { |
| 8294 | if (!PySequence_Check(trailers)) { |
| 8295 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8296 | "sendfile() trailers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8297 | return NULL; |
| 8298 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8299 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8300 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8301 | if (sf.trl_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8302 | (i = iov_setup(&(sf.trailers), &tbuf, |
| 8303 | trailers, sf.trl_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8304 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8305 | #ifdef __APPLE__ |
| 8306 | sbytes += i; |
| 8307 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8308 | } |
| 8309 | } |
| 8310 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8311 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8312 | do { |
| 8313 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8314 | #ifdef __APPLE__ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8315 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8316 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8317 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8318 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8319 | Py_END_ALLOW_THREADS |
| 8320 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8321 | _Py_END_SUPPRESS_IPH |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8322 | |
| 8323 | if (sf.headers != NULL) |
| 8324 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 8325 | if (sf.trailers != NULL) |
| 8326 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 8327 | |
| 8328 | if (ret < 0) { |
| 8329 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 8330 | if (sbytes != 0) { |
| 8331 | // some data has been sent |
| 8332 | goto done; |
| 8333 | } |
| 8334 | else { |
| 8335 | // no data has been sent; upper application is supposed |
| 8336 | // to retry on EAGAIN or EBUSY |
| 8337 | return posix_error(); |
| 8338 | } |
| 8339 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8340 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8341 | } |
| 8342 | goto done; |
| 8343 | |
| 8344 | done: |
| 8345 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 8346 | return Py_BuildValue("l", sbytes); |
| 8347 | #else |
| 8348 | return Py_BuildValue("L", sbytes); |
| 8349 | #endif |
| 8350 | |
| 8351 | #else |
| 8352 | Py_ssize_t count; |
| 8353 | PyObject *offobj; |
| 8354 | static char *keywords[] = {"out", "in", |
| 8355 | "offset", "count", NULL}; |
| 8356 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 8357 | keywords, &out, &in, &offobj, &count)) |
| 8358 | return NULL; |
| 8359 | #ifdef linux |
| 8360 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8361 | do { |
| 8362 | Py_BEGIN_ALLOW_THREADS |
| 8363 | ret = sendfile(out, in, NULL, count); |
| 8364 | Py_END_ALLOW_THREADS |
| 8365 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8366 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8367 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 8368 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8369 | } |
| 8370 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8371 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 8372 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8373 | |
| 8374 | do { |
| 8375 | Py_BEGIN_ALLOW_THREADS |
| 8376 | ret = sendfile(out, in, &offset, count); |
| 8377 | Py_END_ALLOW_THREADS |
| 8378 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8379 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8380 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8381 | return Py_BuildValue("n", ret); |
| 8382 | #endif |
| 8383 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8384 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8385 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8386 | |
| 8387 | /*[clinic input] |
| 8388 | os.fstat |
| 8389 | |
| 8390 | fd : int |
| 8391 | |
| 8392 | Perform a stat system call on the given file descriptor. |
| 8393 | |
| 8394 | Like stat(), but for an open file descriptor. |
| 8395 | Equivalent to os.stat(fd). |
| 8396 | [clinic start generated code]*/ |
| 8397 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8398 | static PyObject * |
| 8399 | os_fstat_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8400 | /*[clinic end generated code: output=d71fe98bf042b626 input=27e0e0ebbe5600c9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8401 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8402 | STRUCT_STAT st; |
| 8403 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8404 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8405 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8406 | do { |
| 8407 | Py_BEGIN_ALLOW_THREADS |
| 8408 | res = FSTAT(fd, &st); |
| 8409 | Py_END_ALLOW_THREADS |
| 8410 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8411 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8412 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8413 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8414 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8415 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8416 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8417 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8418 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8419 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8420 | } |
| 8421 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8422 | |
| 8423 | /*[clinic input] |
| 8424 | os.isatty -> bool |
| 8425 | fd: int |
| 8426 | / |
| 8427 | |
| 8428 | Return True if the fd is connected to a terminal. |
| 8429 | |
| 8430 | Return True if the file descriptor is an open file descriptor |
| 8431 | connected to the slave end of a terminal. |
| 8432 | [clinic start generated code]*/ |
| 8433 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8434 | static int |
| 8435 | os_isatty_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8436 | /*[clinic end generated code: output=acec9d3c29d16d33 input=08ce94aa1eaf7b5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8437 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8438 | int return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8439 | if (!_PyVerify_fd(fd)) |
| 8440 | return 0; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8441 | _Py_BEGIN_SUPPRESS_IPH |
| 8442 | return_value = isatty(fd); |
| 8443 | _Py_END_SUPPRESS_IPH |
| 8444 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8445 | } |
| 8446 | |
| 8447 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8448 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8449 | /*[clinic input] |
| 8450 | os.pipe |
| 8451 | |
| 8452 | Create a pipe. |
| 8453 | |
| 8454 | Returns a tuple of two file descriptors: |
| 8455 | (read_fd, write_fd) |
| 8456 | [clinic start generated code]*/ |
| 8457 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8458 | static PyObject * |
| 8459 | os_pipe_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8460 | /*[clinic end generated code: output=6b0cd3f868ec3c40 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8461 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8462 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8463 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8464 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8465 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8466 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8467 | #else |
| 8468 | int res; |
| 8469 | #endif |
| 8470 | |
| 8471 | #ifdef MS_WINDOWS |
| 8472 | attr.nLength = sizeof(attr); |
| 8473 | attr.lpSecurityDescriptor = NULL; |
| 8474 | attr.bInheritHandle = FALSE; |
| 8475 | |
| 8476 | Py_BEGIN_ALLOW_THREADS |
| 8477 | ok = CreatePipe(&read, &write, &attr, 0); |
| 8478 | if (ok) { |
| 8479 | fds[0] = _open_osfhandle((Py_intptr_t)read, _O_RDONLY); |
| 8480 | fds[1] = _open_osfhandle((Py_intptr_t)write, _O_WRONLY); |
| 8481 | if (fds[0] == -1 || fds[1] == -1) { |
| 8482 | CloseHandle(read); |
| 8483 | CloseHandle(write); |
| 8484 | ok = 0; |
| 8485 | } |
| 8486 | } |
| 8487 | Py_END_ALLOW_THREADS |
| 8488 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8489 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8490 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8491 | #else |
| 8492 | |
| 8493 | #ifdef HAVE_PIPE2 |
| 8494 | Py_BEGIN_ALLOW_THREADS |
| 8495 | res = pipe2(fds, O_CLOEXEC); |
| 8496 | Py_END_ALLOW_THREADS |
| 8497 | |
| 8498 | if (res != 0 && errno == ENOSYS) |
| 8499 | { |
| 8500 | #endif |
| 8501 | Py_BEGIN_ALLOW_THREADS |
| 8502 | res = pipe(fds); |
| 8503 | Py_END_ALLOW_THREADS |
| 8504 | |
| 8505 | if (res == 0) { |
| 8506 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 8507 | close(fds[0]); |
| 8508 | close(fds[1]); |
| 8509 | return NULL; |
| 8510 | } |
| 8511 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 8512 | close(fds[0]); |
| 8513 | close(fds[1]); |
| 8514 | return NULL; |
| 8515 | } |
| 8516 | } |
| 8517 | #ifdef HAVE_PIPE2 |
| 8518 | } |
| 8519 | #endif |
| 8520 | |
| 8521 | if (res != 0) |
| 8522 | return PyErr_SetFromErrno(PyExc_OSError); |
| 8523 | #endif /* !MS_WINDOWS */ |
| 8524 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8525 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8526 | #endif /* HAVE_PIPE */ |
| 8527 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8528 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8529 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8530 | /*[clinic input] |
| 8531 | os.pipe2 |
| 8532 | |
| 8533 | flags: int |
| 8534 | / |
| 8535 | |
| 8536 | Create a pipe with flags set atomically. |
| 8537 | |
| 8538 | Returns a tuple of two file descriptors: |
| 8539 | (read_fd, write_fd) |
| 8540 | |
| 8541 | flags can be constructed by ORing together one or more of these values: |
| 8542 | O_NONBLOCK, O_CLOEXEC. |
| 8543 | [clinic start generated code]*/ |
| 8544 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8545 | static PyObject * |
| 8546 | os_pipe2_impl(PyModuleDef *module, int flags) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8547 | /*[clinic end generated code: output=c15b6075d0c6b2e7 input=f261b6e7e63c6817]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8548 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8549 | int fds[2]; |
| 8550 | int res; |
| 8551 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8552 | res = pipe2(fds, flags); |
| 8553 | if (res != 0) |
| 8554 | return posix_error(); |
| 8555 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 8556 | } |
| 8557 | #endif /* HAVE_PIPE2 */ |
| 8558 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8559 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8560 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8561 | /*[clinic input] |
| 8562 | os.writev -> Py_ssize_t |
| 8563 | fd: int |
| 8564 | buffers: object |
| 8565 | / |
| 8566 | |
| 8567 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 8568 | |
| 8569 | Returns the total number of bytes written. |
| 8570 | buffers must be a sequence of bytes-like objects. |
| 8571 | [clinic start generated code]*/ |
| 8572 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8573 | static Py_ssize_t |
| 8574 | os_writev_impl(PyModuleDef *module, int fd, PyObject *buffers) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8575 | /*[clinic end generated code: output=a48925dbf2d5c238 input=5b8d17fe4189d2fe]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8576 | { |
| 8577 | int cnt; |
| 8578 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8579 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8580 | struct iovec *iov; |
| 8581 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8582 | |
| 8583 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8584 | PyErr_SetString(PyExc_TypeError, |
| 8585 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8586 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8587 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8588 | cnt = PySequence_Size(buffers); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8589 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8590 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 8591 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8592 | } |
| 8593 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8594 | do { |
| 8595 | Py_BEGIN_ALLOW_THREADS |
| 8596 | result = writev(fd, iov, cnt); |
| 8597 | Py_END_ALLOW_THREADS |
| 8598 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8599 | |
| 8600 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8601 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8602 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8603 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8604 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8605 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8606 | #endif /* HAVE_WRITEV */ |
| 8607 | |
| 8608 | |
| 8609 | #ifdef HAVE_PWRITE |
| 8610 | /*[clinic input] |
| 8611 | os.pwrite -> Py_ssize_t |
| 8612 | |
| 8613 | fd: int |
| 8614 | buffer: Py_buffer |
| 8615 | offset: Py_off_t |
| 8616 | / |
| 8617 | |
| 8618 | Write bytes to a file descriptor starting at a particular offset. |
| 8619 | |
| 8620 | Write buffer to fd, starting at offset bytes from the beginning of |
| 8621 | the file. Returns the number of bytes writte. Does not change the |
| 8622 | current file offset. |
| 8623 | [clinic start generated code]*/ |
| 8624 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8625 | static Py_ssize_t |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8626 | os_pwrite_impl(PyModuleDef *module, int fd, Py_buffer *buffer, |
| 8627 | Py_off_t offset) |
| 8628 | /*[clinic end generated code: output=93aabdb40e17d325 input=19903f1b3dd26377]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8629 | { |
| 8630 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8631 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8632 | |
| 8633 | if (!_PyVerify_fd(fd)) { |
| 8634 | posix_error(); |
| 8635 | return -1; |
| 8636 | } |
| 8637 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8638 | do { |
| 8639 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8640 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8641 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8642 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8643 | Py_END_ALLOW_THREADS |
| 8644 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8645 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8646 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8647 | posix_error(); |
| 8648 | return size; |
| 8649 | } |
| 8650 | #endif /* HAVE_PWRITE */ |
| 8651 | |
| 8652 | |
| 8653 | #ifdef HAVE_MKFIFO |
| 8654 | /*[clinic input] |
| 8655 | os.mkfifo |
| 8656 | |
| 8657 | path: path_t |
| 8658 | mode: int=0o666 |
| 8659 | * |
| 8660 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 8661 | |
| 8662 | Create a "fifo" (a POSIX named pipe). |
| 8663 | |
| 8664 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8665 | and path should be relative; path will then be relative to that directory. |
| 8666 | dir_fd may not be implemented on your platform. |
| 8667 | If it is unavailable, using it will raise a NotImplementedError. |
| 8668 | [clinic start generated code]*/ |
| 8669 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8670 | static PyObject * |
| 8671 | os_mkfifo_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8672 | /*[clinic end generated code: output=8f5f5e72c630049a input=73032e98a36e0e19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8673 | { |
| 8674 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8675 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8676 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8677 | do { |
| 8678 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8679 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8680 | if (dir_fd != DEFAULT_DIR_FD) |
| 8681 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 8682 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8683 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8684 | result = mkfifo(path->narrow, mode); |
| 8685 | Py_END_ALLOW_THREADS |
| 8686 | } while (result != 0 && errno == EINTR && |
| 8687 | !(async_err = PyErr_CheckSignals())); |
| 8688 | if (result != 0) |
| 8689 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8690 | |
| 8691 | Py_RETURN_NONE; |
| 8692 | } |
| 8693 | #endif /* HAVE_MKFIFO */ |
| 8694 | |
| 8695 | |
| 8696 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 8697 | /*[clinic input] |
| 8698 | os.mknod |
| 8699 | |
| 8700 | path: path_t |
| 8701 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8702 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8703 | * |
| 8704 | dir_fd: dir_fd(requires='mknodat')=None |
| 8705 | |
| 8706 | Create a node in the file system. |
| 8707 | |
| 8708 | Create a node in the file system (file, device special file or named pipe) |
| 8709 | at path. mode specifies both the permissions to use and the |
| 8710 | type of node to be created, being combined (bitwise OR) with one of |
| 8711 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 8712 | device defines the newly created device special file (probably using |
| 8713 | os.makedev()). Otherwise device is ignored. |
| 8714 | |
| 8715 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8716 | and path should be relative; path will then be relative to that directory. |
| 8717 | dir_fd may not be implemented on your platform. |
| 8718 | If it is unavailable, using it will raise a NotImplementedError. |
| 8719 | [clinic start generated code]*/ |
| 8720 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8721 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8722 | os_mknod_impl(PyModuleDef *module, path_t *path, int mode, dev_t device, |
| 8723 | int dir_fd) |
| 8724 | /*[clinic end generated code: output=5151a8a9f754d272 input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8725 | { |
| 8726 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8727 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8728 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8729 | do { |
| 8730 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8731 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8732 | if (dir_fd != DEFAULT_DIR_FD) |
| 8733 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 8734 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8735 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8736 | result = mknod(path->narrow, mode, device); |
| 8737 | Py_END_ALLOW_THREADS |
| 8738 | } while (result != 0 && errno == EINTR && |
| 8739 | !(async_err = PyErr_CheckSignals())); |
| 8740 | if (result != 0) |
| 8741 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8742 | |
| 8743 | Py_RETURN_NONE; |
| 8744 | } |
| 8745 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 8746 | |
| 8747 | |
| 8748 | #ifdef HAVE_DEVICE_MACROS |
| 8749 | /*[clinic input] |
| 8750 | os.major -> unsigned_int |
| 8751 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8752 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8753 | / |
| 8754 | |
| 8755 | Extracts a device major number from a raw device number. |
| 8756 | [clinic start generated code]*/ |
| 8757 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8758 | static unsigned int |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8759 | os_major_impl(PyModuleDef *module, dev_t device) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8760 | /*[clinic end generated code: output=ba55693ab49bac34 input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8761 | { |
| 8762 | return major(device); |
| 8763 | } |
| 8764 | |
| 8765 | |
| 8766 | /*[clinic input] |
| 8767 | os.minor -> unsigned_int |
| 8768 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8769 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8770 | / |
| 8771 | |
| 8772 | Extracts a device minor number from a raw device number. |
| 8773 | [clinic start generated code]*/ |
| 8774 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8775 | static unsigned int |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8776 | os_minor_impl(PyModuleDef *module, dev_t device) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8777 | /*[clinic end generated code: output=2867219ebf274e27 input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8778 | { |
| 8779 | return minor(device); |
| 8780 | } |
| 8781 | |
| 8782 | |
| 8783 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8784 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8785 | |
| 8786 | major: int |
| 8787 | minor: int |
| 8788 | / |
| 8789 | |
| 8790 | Composes a raw device number from the major and minor device numbers. |
| 8791 | [clinic start generated code]*/ |
| 8792 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8793 | static dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8794 | os_makedev_impl(PyModuleDef *module, int major, int minor) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8795 | /*[clinic end generated code: output=7cb6264352437660 input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8796 | { |
| 8797 | return makedev(major, minor); |
| 8798 | } |
| 8799 | #endif /* HAVE_DEVICE_MACROS */ |
| 8800 | |
| 8801 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8802 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8803 | /*[clinic input] |
| 8804 | os.ftruncate |
| 8805 | |
| 8806 | fd: int |
| 8807 | length: Py_off_t |
| 8808 | / |
| 8809 | |
| 8810 | Truncate a file, specified by file descriptor, to a specific length. |
| 8811 | [clinic start generated code]*/ |
| 8812 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8813 | static PyObject * |
| 8814 | os_ftruncate_impl(PyModuleDef *module, int fd, Py_off_t length) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8815 | /*[clinic end generated code: output=3666f401d76bf834 input=63b43641e52818f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8816 | { |
| 8817 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8818 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8819 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8820 | if (!_PyVerify_fd(fd)) |
| 8821 | return posix_error(); |
| 8822 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8823 | do { |
| 8824 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8825 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8826 | #ifdef MS_WINDOWS |
| 8827 | result = _chsize_s(fd, length); |
| 8828 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8829 | result = ftruncate(fd, length); |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8830 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8831 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8832 | Py_END_ALLOW_THREADS |
| 8833 | } while (result != 0 && errno == EINTR && |
| 8834 | !(async_err = PyErr_CheckSignals())); |
| 8835 | if (result != 0) |
| 8836 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8837 | Py_RETURN_NONE; |
| 8838 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8839 | #endif /* HAVE_FTRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8840 | |
| 8841 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8842 | #if defined HAVE_TRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8843 | /*[clinic input] |
| 8844 | os.truncate |
| 8845 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 8846 | length: Py_off_t |
| 8847 | |
| 8848 | Truncate a file, specified by path, to a specific length. |
| 8849 | |
| 8850 | On some platforms, path may also be specified as an open file descriptor. |
| 8851 | If this functionality is unavailable, using it raises an exception. |
| 8852 | [clinic start generated code]*/ |
| 8853 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8854 | static PyObject * |
| 8855 | os_truncate_impl(PyModuleDef *module, path_t *path, Py_off_t length) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8856 | /*[clinic end generated code: output=f60a9e08370e9e2e input=77229cf0b50a9b77]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8857 | { |
| 8858 | int result; |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8859 | #ifdef MS_WINDOWS |
| 8860 | int fd; |
| 8861 | #endif |
| 8862 | |
| 8863 | if (path->fd != -1) |
| 8864 | return os_ftruncate_impl(module, path->fd, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8865 | |
| 8866 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8867 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8868 | #ifdef MS_WINDOWS |
| 8869 | if (path->wide) |
| 8870 | fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8871 | else |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8872 | fd = _open(path->narrow, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Victor Stinner | cc0bbbc | 2015-04-25 00:21:52 +0200 | [diff] [blame] | 8873 | if (fd < 0) |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8874 | result = -1; |
| 8875 | else { |
| 8876 | result = _chsize_s(fd, length); |
| 8877 | close(fd); |
| 8878 | if (result < 0) |
| 8879 | errno = result; |
| 8880 | } |
| 8881 | #else |
| 8882 | result = truncate(path->narrow, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8883 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8884 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8885 | Py_END_ALLOW_THREADS |
| 8886 | if (result < 0) |
| 8887 | return path_error(path); |
| 8888 | |
| 8889 | Py_RETURN_NONE; |
| 8890 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8891 | #endif /* HAVE_TRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8892 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8893 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 8894 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 8895 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 8896 | defined, which is the case in Python on AIX. AIX bug report: |
| 8897 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 8898 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 8899 | # define POSIX_FADVISE_AIX_BUG |
| 8900 | #endif |
| 8901 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 8902 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 8903 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8904 | /*[clinic input] |
| 8905 | os.posix_fallocate |
| 8906 | |
| 8907 | fd: int |
| 8908 | offset: Py_off_t |
| 8909 | length: Py_off_t |
| 8910 | / |
| 8911 | |
| 8912 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 8913 | |
| 8914 | Ensure that the file specified by fd encompasses a range of bytes |
| 8915 | starting at offset bytes from the beginning and continuing for length bytes. |
| 8916 | [clinic start generated code]*/ |
| 8917 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8918 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8919 | os_posix_fallocate_impl(PyModuleDef *module, int fd, Py_off_t offset, |
| 8920 | Py_off_t length) |
| 8921 | /*[clinic end generated code: output=7f6f87a8c751e1b4 input=d7a2ef0ab2ca52fb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8922 | { |
| 8923 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8924 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8925 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8926 | do { |
| 8927 | Py_BEGIN_ALLOW_THREADS |
| 8928 | result = posix_fallocate(fd, offset, length); |
| 8929 | Py_END_ALLOW_THREADS |
| 8930 | } while (result != 0 && errno == EINTR && |
| 8931 | !(async_err = PyErr_CheckSignals())); |
| 8932 | if (result != 0) |
| 8933 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8934 | Py_RETURN_NONE; |
| 8935 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 8936 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8937 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8938 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 8939 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8940 | /*[clinic input] |
| 8941 | os.posix_fadvise |
| 8942 | |
| 8943 | fd: int |
| 8944 | offset: Py_off_t |
| 8945 | length: Py_off_t |
| 8946 | advice: int |
| 8947 | / |
| 8948 | |
| 8949 | Announce an intention to access data in a specific pattern. |
| 8950 | |
| 8951 | Announce an intention to access data in a specific pattern, thus allowing |
| 8952 | the kernel to make optimizations. |
| 8953 | The advice applies to the region of the file specified by fd starting at |
| 8954 | offset and continuing for length bytes. |
| 8955 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 8956 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 8957 | POSIX_FADV_DONTNEED. |
| 8958 | [clinic start generated code]*/ |
| 8959 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8960 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8961 | os_posix_fadvise_impl(PyModuleDef *module, int fd, Py_off_t offset, |
| 8962 | Py_off_t length, int advice) |
| 8963 | /*[clinic end generated code: output=457ce6a67189e10d input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8964 | { |
| 8965 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8966 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8967 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8968 | do { |
| 8969 | Py_BEGIN_ALLOW_THREADS |
| 8970 | result = posix_fadvise(fd, offset, length, advice); |
| 8971 | Py_END_ALLOW_THREADS |
| 8972 | } while (result != 0 && errno == EINTR && |
| 8973 | !(async_err = PyErr_CheckSignals())); |
| 8974 | if (result != 0) |
| 8975 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8976 | Py_RETURN_NONE; |
| 8977 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 8978 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8979 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8980 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8981 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8982 | /* Save putenv() parameters as values here, so we can collect them when they |
| 8983 | * get re-set with another call for the same key. */ |
| 8984 | static PyObject *posix_putenv_garbage; |
| 8985 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8986 | static void |
| 8987 | posix_putenv_garbage_setitem(PyObject *name, PyObject *value) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8988 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8989 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 8990 | * this will cause previous value to be collected. This has to |
| 8991 | * happen after the real putenv() call because the old value |
| 8992 | * was still accessible until then. */ |
| 8993 | if (PyDict_SetItem(posix_putenv_garbage, name, value)) |
| 8994 | /* really not much we can do; just leak */ |
| 8995 | PyErr_Clear(); |
| 8996 | else |
| 8997 | Py_DECREF(value); |
| 8998 | } |
| 8999 | |
| 9000 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 9001 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9002 | /*[clinic input] |
| 9003 | os.putenv |
| 9004 | |
| 9005 | name: unicode |
| 9006 | value: unicode |
| 9007 | / |
| 9008 | |
| 9009 | Change or add an environment variable. |
| 9010 | [clinic start generated code]*/ |
| 9011 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9012 | static PyObject * |
| 9013 | os_putenv_impl(PyModuleDef *module, PyObject *name, PyObject *value) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9014 | /*[clinic end generated code: output=a2438cf95e5a0c1c input=ba586581c2e6105f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9015 | { |
| 9016 | wchar_t *env; |
| 9017 | |
| 9018 | PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 9019 | if (unicode == NULL) { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9020 | PyErr_NoMemory(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9021 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9022 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9023 | if (_MAX_ENV < PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9024 | PyErr_Format(PyExc_ValueError, |
| 9025 | "the environment variable is longer than %u characters", |
| 9026 | _MAX_ENV); |
| 9027 | goto error; |
| 9028 | } |
| 9029 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9030 | env = PyUnicode_AsUnicode(unicode); |
| 9031 | if (env == NULL) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9032 | goto error; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9033 | if (_wputenv(env)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9034 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9035 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9036 | } |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9037 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9038 | posix_putenv_garbage_setitem(name, unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9039 | Py_RETURN_NONE; |
| 9040 | |
| 9041 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9042 | Py_DECREF(unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9043 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9044 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9045 | #else /* MS_WINDOWS */ |
| 9046 | /*[clinic input] |
| 9047 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9048 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9049 | name: FSConverter |
| 9050 | value: FSConverter |
| 9051 | / |
| 9052 | |
| 9053 | Change or add an environment variable. |
| 9054 | [clinic start generated code]*/ |
| 9055 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9056 | static PyObject * |
| 9057 | os_putenv_impl(PyModuleDef *module, PyObject *name, PyObject *value) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9058 | /*[clinic end generated code: output=a2438cf95e5a0c1c input=a97bc6152f688d31]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9059 | { |
| 9060 | PyObject *bytes = NULL; |
| 9061 | char *env; |
| 9062 | char *name_string = PyBytes_AsString(name); |
| 9063 | char *value_string = PyBytes_AsString(value); |
| 9064 | |
| 9065 | bytes = PyBytes_FromFormat("%s=%s", name_string, value_string); |
| 9066 | if (bytes == NULL) { |
| 9067 | PyErr_NoMemory(); |
| 9068 | return NULL; |
| 9069 | } |
| 9070 | |
| 9071 | env = PyBytes_AS_STRING(bytes); |
| 9072 | if (putenv(env)) { |
| 9073 | Py_DECREF(bytes); |
| 9074 | return posix_error(); |
| 9075 | } |
| 9076 | |
| 9077 | posix_putenv_garbage_setitem(name, bytes); |
| 9078 | Py_RETURN_NONE; |
| 9079 | } |
| 9080 | #endif /* MS_WINDOWS */ |
| 9081 | #endif /* HAVE_PUTENV */ |
| 9082 | |
| 9083 | |
| 9084 | #ifdef HAVE_UNSETENV |
| 9085 | /*[clinic input] |
| 9086 | os.unsetenv |
| 9087 | name: FSConverter |
| 9088 | / |
| 9089 | |
| 9090 | Delete an environment variable. |
| 9091 | [clinic start generated code]*/ |
| 9092 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9093 | static PyObject * |
| 9094 | os_unsetenv_impl(PyModuleDef *module, PyObject *name) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9095 | /*[clinic end generated code: output=25994b57016a2dc9 input=2bb5288a599c7107]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9096 | { |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9097 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 9098 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9099 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9100 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9101 | #ifdef HAVE_BROKEN_UNSETENV |
| 9102 | unsetenv(PyBytes_AS_STRING(name)); |
| 9103 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9104 | err = unsetenv(PyBytes_AS_STRING(name)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9105 | if (err) |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 9106 | return posix_error(); |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9107 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9108 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9109 | /* Remove the key from posix_putenv_garbage; |
| 9110 | * this will cause it to be collected. This has to |
| 9111 | * happen after the real unsetenv() call because the |
| 9112 | * old value was still accessible until then. |
| 9113 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9114 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9115 | /* really not much we can do; just leak */ |
| 9116 | PyErr_Clear(); |
| 9117 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9118 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9119 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9120 | #endif /* HAVE_UNSETENV */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9121 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9122 | |
| 9123 | /*[clinic input] |
| 9124 | os.strerror |
| 9125 | |
| 9126 | code: int |
| 9127 | / |
| 9128 | |
| 9129 | Translate an error code to a message string. |
| 9130 | [clinic start generated code]*/ |
| 9131 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9132 | static PyObject * |
| 9133 | os_strerror_impl(PyModuleDef *module, int code) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9134 | /*[clinic end generated code: output=0280c6af51e5c9fe input=75a8673d97915a91]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9135 | { |
| 9136 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9137 | if (message == NULL) { |
| 9138 | PyErr_SetString(PyExc_ValueError, |
| 9139 | "strerror() argument out of range"); |
| 9140 | return NULL; |
| 9141 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 9142 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9143 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9144 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9145 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9146 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9147 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9148 | /*[clinic input] |
| 9149 | os.WCOREDUMP -> bool |
| 9150 | |
| 9151 | status: int |
| 9152 | / |
| 9153 | |
| 9154 | Return True if the process returning status was dumped to a core file. |
| 9155 | [clinic start generated code]*/ |
| 9156 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9157 | static int |
| 9158 | os_WCOREDUMP_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9159 | /*[clinic end generated code: output=134f70bbe63fbf41 input=8b05e7ab38528d04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9160 | { |
| 9161 | WAIT_TYPE wait_status; |
| 9162 | WAIT_STATUS_INT(wait_status) = status; |
| 9163 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9164 | } |
| 9165 | #endif /* WCOREDUMP */ |
| 9166 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9167 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9168 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9169 | /*[clinic input] |
| 9170 | os.WIFCONTINUED -> bool |
| 9171 | |
| 9172 | status: int |
| 9173 | |
| 9174 | Return True if a particular process was continued from a job control stop. |
| 9175 | |
| 9176 | Return True if the process returning status was continued from a |
| 9177 | job control stop. |
| 9178 | [clinic start generated code]*/ |
| 9179 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9180 | static int |
| 9181 | os_WIFCONTINUED_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9182 | /*[clinic end generated code: output=9cdd26543ebb6dcd input=e777e7d38eb25bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9183 | { |
| 9184 | WAIT_TYPE wait_status; |
| 9185 | WAIT_STATUS_INT(wait_status) = status; |
| 9186 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9187 | } |
| 9188 | #endif /* WIFCONTINUED */ |
| 9189 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9190 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9191 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9192 | /*[clinic input] |
| 9193 | os.WIFSTOPPED -> bool |
| 9194 | |
| 9195 | status: int |
| 9196 | |
| 9197 | Return True if the process returning status was stopped. |
| 9198 | [clinic start generated code]*/ |
| 9199 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9200 | static int |
| 9201 | os_WIFSTOPPED_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9202 | /*[clinic end generated code: output=73bf35e44994a724 input=043cb7f1289ef904]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9203 | { |
| 9204 | WAIT_TYPE wait_status; |
| 9205 | WAIT_STATUS_INT(wait_status) = status; |
| 9206 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9207 | } |
| 9208 | #endif /* WIFSTOPPED */ |
| 9209 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9210 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9211 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9212 | /*[clinic input] |
| 9213 | os.WIFSIGNALED -> bool |
| 9214 | |
| 9215 | status: int |
| 9216 | |
| 9217 | Return True if the process returning status was terminated by a signal. |
| 9218 | [clinic start generated code]*/ |
| 9219 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9220 | static int |
| 9221 | os_WIFSIGNALED_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9222 | /*[clinic end generated code: output=2697975771872420 input=d55ba7cc9ce5dc43]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9223 | { |
| 9224 | WAIT_TYPE wait_status; |
| 9225 | WAIT_STATUS_INT(wait_status) = status; |
| 9226 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9227 | } |
| 9228 | #endif /* WIFSIGNALED */ |
| 9229 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9230 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9231 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9232 | /*[clinic input] |
| 9233 | os.WIFEXITED -> bool |
| 9234 | |
| 9235 | status: int |
| 9236 | |
| 9237 | Return True if the process returning status exited via the exit() system call. |
| 9238 | [clinic start generated code]*/ |
| 9239 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9240 | static int |
| 9241 | os_WIFEXITED_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9242 | /*[clinic end generated code: output=ca8f8c61f0b8532e input=d63775a6791586c0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9243 | { |
| 9244 | WAIT_TYPE wait_status; |
| 9245 | WAIT_STATUS_INT(wait_status) = status; |
| 9246 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9247 | } |
| 9248 | #endif /* WIFEXITED */ |
| 9249 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9250 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 9251 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9252 | /*[clinic input] |
| 9253 | os.WEXITSTATUS -> int |
| 9254 | |
| 9255 | status: int |
| 9256 | |
| 9257 | Return the process return code from status. |
| 9258 | [clinic start generated code]*/ |
| 9259 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9260 | static int |
| 9261 | os_WEXITSTATUS_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9262 | /*[clinic end generated code: output=ea54da23d9e0f6af input=e1fb4944e377585b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9263 | { |
| 9264 | WAIT_TYPE wait_status; |
| 9265 | WAIT_STATUS_INT(wait_status) = status; |
| 9266 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9267 | } |
| 9268 | #endif /* WEXITSTATUS */ |
| 9269 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9270 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9271 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9272 | /*[clinic input] |
| 9273 | os.WTERMSIG -> int |
| 9274 | |
| 9275 | status: int |
| 9276 | |
| 9277 | Return the signal that terminated the process that provided the status value. |
| 9278 | [clinic start generated code]*/ |
| 9279 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9280 | static int |
| 9281 | os_WTERMSIG_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9282 | /*[clinic end generated code: output=4d25367026cb852c input=727fd7f84ec3f243]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9283 | { |
| 9284 | WAIT_TYPE wait_status; |
| 9285 | WAIT_STATUS_INT(wait_status) = status; |
| 9286 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9287 | } |
| 9288 | #endif /* WTERMSIG */ |
| 9289 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9290 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9291 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9292 | /*[clinic input] |
| 9293 | os.WSTOPSIG -> int |
| 9294 | |
| 9295 | status: int |
| 9296 | |
| 9297 | Return the signal that stopped the process that provided the status value. |
| 9298 | [clinic start generated code]*/ |
| 9299 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9300 | static int |
| 9301 | os_WSTOPSIG_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9302 | /*[clinic end generated code: output=54eb9c13b001adb4 input=46ebf1d1b293c5c1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9303 | { |
| 9304 | WAIT_TYPE wait_status; |
| 9305 | WAIT_STATUS_INT(wait_status) = status; |
| 9306 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9307 | } |
| 9308 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9309 | #endif /* HAVE_SYS_WAIT_H */ |
| 9310 | |
| 9311 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9312 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 9313 | #ifdef _SCO_DS |
| 9314 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 9315 | needed definitions in sys/statvfs.h */ |
| 9316 | #define _SVID3 |
| 9317 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9318 | #include <sys/statvfs.h> |
| 9319 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9320 | static PyObject* |
| 9321 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9322 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 9323 | if (v == NULL) |
| 9324 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9325 | |
| 9326 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9327 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9328 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9329 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 9330 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 9331 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 9332 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 9333 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 9334 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 9335 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9336 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9337 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9338 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9339 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9340 | PyStructSequence_SET_ITEM(v, 2, |
| 9341 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 9342 | PyStructSequence_SET_ITEM(v, 3, |
| 9343 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 9344 | PyStructSequence_SET_ITEM(v, 4, |
| 9345 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 9346 | PyStructSequence_SET_ITEM(v, 5, |
| 9347 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 9348 | PyStructSequence_SET_ITEM(v, 6, |
| 9349 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 9350 | PyStructSequence_SET_ITEM(v, 7, |
| 9351 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 9352 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9353 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9354 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 9355 | if (PyErr_Occurred()) { |
| 9356 | Py_DECREF(v); |
| 9357 | return NULL; |
| 9358 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9359 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9360 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9361 | } |
| 9362 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9363 | |
| 9364 | /*[clinic input] |
| 9365 | os.fstatvfs |
| 9366 | fd: int |
| 9367 | / |
| 9368 | |
| 9369 | Perform an fstatvfs system call on the given fd. |
| 9370 | |
| 9371 | Equivalent to statvfs(fd). |
| 9372 | [clinic start generated code]*/ |
| 9373 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9374 | static PyObject * |
| 9375 | os_fstatvfs_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9376 | /*[clinic end generated code: output=584a94a754497ac0 input=d8122243ac50975e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9377 | { |
| 9378 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9379 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9380 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9381 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9382 | do { |
| 9383 | Py_BEGIN_ALLOW_THREADS |
| 9384 | result = fstatvfs(fd, &st); |
| 9385 | Py_END_ALLOW_THREADS |
| 9386 | } while (result != 0 && errno == EINTR && |
| 9387 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9388 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9389 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9390 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9391 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9392 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9393 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9394 | |
| 9395 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9396 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9397 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9398 | /*[clinic input] |
| 9399 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9400 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9401 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 9402 | |
| 9403 | Perform a statvfs system call on the given path. |
| 9404 | |
| 9405 | path may always be specified as a string. |
| 9406 | On some platforms, path may also be specified as an open file descriptor. |
| 9407 | If this functionality is unavailable, using it raises an exception. |
| 9408 | [clinic start generated code]*/ |
| 9409 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9410 | static PyObject * |
| 9411 | os_statvfs_impl(PyModuleDef *module, path_t *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9412 | /*[clinic end generated code: output=5ced07a2cf931f41 input=3f5c35791c669bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9413 | { |
| 9414 | int result; |
| 9415 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9416 | |
| 9417 | Py_BEGIN_ALLOW_THREADS |
| 9418 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9419 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9420 | #ifdef __APPLE__ |
| 9421 | /* handle weak-linking on Mac OS X 10.3 */ |
| 9422 | if (fstatvfs == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9423 | fd_specified("statvfs", path->fd); |
| 9424 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9425 | } |
| 9426 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9427 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9428 | } |
| 9429 | else |
| 9430 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9431 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9432 | Py_END_ALLOW_THREADS |
| 9433 | |
| 9434 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9435 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9436 | } |
| 9437 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9438 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9439 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9440 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 9441 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9442 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9443 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9444 | /*[clinic input] |
| 9445 | os._getdiskusage |
| 9446 | |
| 9447 | path: Py_UNICODE |
| 9448 | |
| 9449 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 9450 | [clinic start generated code]*/ |
| 9451 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9452 | static PyObject * |
| 9453 | os__getdiskusage_impl(PyModuleDef *module, Py_UNICODE *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9454 | /*[clinic end generated code: output=60a9cf33449db1dd input=6458133aed893c78]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9455 | { |
| 9456 | BOOL retval; |
| 9457 | ULARGE_INTEGER _, total, free; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9458 | |
| 9459 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 9460 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9461 | Py_END_ALLOW_THREADS |
| 9462 | if (retval == 0) |
| 9463 | return PyErr_SetFromWindowsErr(0); |
| 9464 | |
| 9465 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 9466 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9467 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9468 | |
| 9469 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9470 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 9471 | * It maps strings representing configuration variable names to |
| 9472 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 9473 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9474 | * rarely-used constants. There are three separate tables that use |
| 9475 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9476 | * |
| 9477 | * This code is always included, even if none of the interfaces that |
| 9478 | * need it are included. The #if hackery needed to avoid it would be |
| 9479 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9480 | */ |
| 9481 | struct constdef { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame^] | 9482 | const char *name; |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 9483 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9484 | }; |
| 9485 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9486 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9487 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 9488 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9489 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9490 | if (PyLong_Check(arg)) { |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 9491 | int value = _PyLong_AsInt(arg); |
| 9492 | if (value == -1 && PyErr_Occurred()) |
| 9493 | return 0; |
| 9494 | *valuep = value; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9495 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9496 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 9497 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9498 | /* look up the value in the table using a binary search */ |
| 9499 | size_t lo = 0; |
| 9500 | size_t mid; |
| 9501 | size_t hi = tablesize; |
| 9502 | int cmp; |
| 9503 | const char *confname; |
| 9504 | if (!PyUnicode_Check(arg)) { |
| 9505 | PyErr_SetString(PyExc_TypeError, |
| 9506 | "configuration names must be strings or integers"); |
| 9507 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9508 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9509 | confname = _PyUnicode_AsString(arg); |
| 9510 | if (confname == NULL) |
| 9511 | return 0; |
| 9512 | while (lo < hi) { |
| 9513 | mid = (lo + hi) / 2; |
| 9514 | cmp = strcmp(confname, table[mid].name); |
| 9515 | if (cmp < 0) |
| 9516 | hi = mid; |
| 9517 | else if (cmp > 0) |
| 9518 | lo = mid + 1; |
| 9519 | else { |
| 9520 | *valuep = table[mid].value; |
| 9521 | return 1; |
| 9522 | } |
| 9523 | } |
| 9524 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 9525 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9526 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9527 | } |
| 9528 | |
| 9529 | |
| 9530 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 9531 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9532 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9533 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9534 | #endif |
| 9535 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9536 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9537 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9538 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9539 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9540 | #endif |
| 9541 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9542 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9543 | #endif |
| 9544 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9545 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9546 | #endif |
| 9547 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9548 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9549 | #endif |
| 9550 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9551 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9552 | #endif |
| 9553 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9554 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9555 | #endif |
| 9556 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9557 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9558 | #endif |
| 9559 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9560 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9561 | #endif |
| 9562 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9563 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9564 | #endif |
| 9565 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9566 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9567 | #endif |
| 9568 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9569 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9570 | #endif |
| 9571 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9572 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9573 | #endif |
| 9574 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9575 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9576 | #endif |
| 9577 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9578 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9579 | #endif |
| 9580 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9581 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9582 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 9583 | #ifdef _PC_ACL_ENABLED |
| 9584 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 9585 | #endif |
| 9586 | #ifdef _PC_MIN_HOLE_SIZE |
| 9587 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 9588 | #endif |
| 9589 | #ifdef _PC_ALLOC_SIZE_MIN |
| 9590 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 9591 | #endif |
| 9592 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 9593 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 9594 | #endif |
| 9595 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 9596 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 9597 | #endif |
| 9598 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 9599 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 9600 | #endif |
| 9601 | #ifdef _PC_REC_XFER_ALIGN |
| 9602 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 9603 | #endif |
| 9604 | #ifdef _PC_SYMLINK_MAX |
| 9605 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 9606 | #endif |
| 9607 | #ifdef _PC_XATTR_ENABLED |
| 9608 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 9609 | #endif |
| 9610 | #ifdef _PC_XATTR_EXISTS |
| 9611 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 9612 | #endif |
| 9613 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 9614 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 9615 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9616 | }; |
| 9617 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9618 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9619 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9620 | { |
| 9621 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 9622 | sizeof(posix_constants_pathconf) |
| 9623 | / sizeof(struct constdef)); |
| 9624 | } |
| 9625 | #endif |
| 9626 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9627 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9628 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9629 | /*[clinic input] |
| 9630 | os.fpathconf -> long |
| 9631 | |
| 9632 | fd: int |
| 9633 | name: path_confname |
| 9634 | / |
| 9635 | |
| 9636 | Return the configuration limit name for the file descriptor fd. |
| 9637 | |
| 9638 | If there is no limit, return -1. |
| 9639 | [clinic start generated code]*/ |
| 9640 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9641 | static long |
| 9642 | os_fpathconf_impl(PyModuleDef *module, int fd, int name) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9643 | /*[clinic end generated code: output=082b2922d4441de7 input=5942a024d3777810]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9644 | { |
| 9645 | long limit; |
| 9646 | |
| 9647 | errno = 0; |
| 9648 | limit = fpathconf(fd, name); |
| 9649 | if (limit == -1 && errno != 0) |
| 9650 | posix_error(); |
| 9651 | |
| 9652 | return limit; |
| 9653 | } |
| 9654 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9655 | |
| 9656 | |
| 9657 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9658 | /*[clinic input] |
| 9659 | os.pathconf -> long |
| 9660 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 9661 | name: path_confname |
| 9662 | |
| 9663 | Return the configuration limit name for the file or directory path. |
| 9664 | |
| 9665 | If there is no limit, return -1. |
| 9666 | On some platforms, path may also be specified as an open file descriptor. |
| 9667 | If this functionality is unavailable, using it raises an exception. |
| 9668 | [clinic start generated code]*/ |
| 9669 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9670 | static long |
| 9671 | os_pathconf_impl(PyModuleDef *module, path_t *path, int name) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9672 | /*[clinic end generated code: output=3713029e9501f5ab input=bc3e2a985af27e5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9673 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9674 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9675 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9676 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9677 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9678 | if (path->fd != -1) |
| 9679 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9680 | else |
| 9681 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9682 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9683 | if (limit == -1 && errno != 0) { |
| 9684 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 9685 | /* could be a path or name problem */ |
| 9686 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9687 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9688 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9689 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9690 | |
| 9691 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9692 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9693 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9694 | |
| 9695 | #ifdef HAVE_CONFSTR |
| 9696 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9697 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9698 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9699 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9700 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9701 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9702 | #endif |
| 9703 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9704 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9705 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9706 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9707 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9708 | #endif |
| 9709 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9710 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9711 | #endif |
| 9712 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9713 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9714 | #endif |
| 9715 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9716 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9717 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9718 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9719 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9720 | #endif |
| 9721 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9722 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9723 | #endif |
| 9724 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9725 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9726 | #endif |
| 9727 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9728 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9729 | #endif |
| 9730 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9731 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9732 | #endif |
| 9733 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9734 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9735 | #endif |
| 9736 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9737 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9738 | #endif |
| 9739 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9740 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9741 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9742 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9743 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9744 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9745 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9746 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9747 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9748 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9749 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9750 | #endif |
| 9751 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9752 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9753 | #endif |
| 9754 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9755 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9756 | #endif |
| 9757 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9758 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9759 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9760 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9761 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9762 | #endif |
| 9763 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9764 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9765 | #endif |
| 9766 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9767 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9768 | #endif |
| 9769 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9770 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9771 | #endif |
| 9772 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9773 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9774 | #endif |
| 9775 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9776 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9777 | #endif |
| 9778 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9779 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9780 | #endif |
| 9781 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9782 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9783 | #endif |
| 9784 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9785 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9786 | #endif |
| 9787 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9788 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9789 | #endif |
| 9790 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9791 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9792 | #endif |
| 9793 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9794 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9795 | #endif |
| 9796 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9797 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9798 | #endif |
| 9799 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9800 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9801 | #endif |
| 9802 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9803 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9804 | #endif |
| 9805 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9806 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9807 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9808 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9809 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9810 | #endif |
| 9811 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9812 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9813 | #endif |
| 9814 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9815 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9816 | #endif |
| 9817 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9818 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9819 | #endif |
| 9820 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9821 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9822 | #endif |
| 9823 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9824 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9825 | #endif |
| 9826 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9827 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9828 | #endif |
| 9829 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9830 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9831 | #endif |
| 9832 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9833 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9834 | #endif |
| 9835 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9836 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9837 | #endif |
| 9838 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9839 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9840 | #endif |
| 9841 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9842 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9843 | #endif |
| 9844 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9845 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9846 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9847 | }; |
| 9848 | |
| 9849 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9850 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9851 | { |
| 9852 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 9853 | sizeof(posix_constants_confstr) |
| 9854 | / sizeof(struct constdef)); |
| 9855 | } |
| 9856 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9857 | |
| 9858 | /*[clinic input] |
| 9859 | os.confstr |
| 9860 | |
| 9861 | name: confstr_confname |
| 9862 | / |
| 9863 | |
| 9864 | Return a string-valued system configuration variable. |
| 9865 | [clinic start generated code]*/ |
| 9866 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9867 | static PyObject * |
| 9868 | os_confstr_impl(PyModuleDef *module, int name) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9869 | /*[clinic end generated code: output=6ff79c9eed8c2daf input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9870 | { |
| 9871 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9872 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9873 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9874 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9875 | errno = 0; |
| 9876 | len = confstr(name, buffer, sizeof(buffer)); |
| 9877 | if (len == 0) { |
| 9878 | if (errno) { |
| 9879 | posix_error(); |
| 9880 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9881 | } |
| 9882 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9883 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9884 | } |
| 9885 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9886 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9887 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 9888 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9889 | char *buf = PyMem_Malloc(len); |
| 9890 | if (buf == NULL) |
| 9891 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 9892 | len2 = confstr(name, buf, len); |
| 9893 | assert(len == len2); |
Christian Heimes | 8714cfd | 2015-04-21 10:57:41 +0200 | [diff] [blame] | 9894 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9895 | PyMem_Free(buf); |
| 9896 | } |
| 9897 | else |
| 9898 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9899 | return result; |
| 9900 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9901 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9902 | |
| 9903 | |
| 9904 | #ifdef HAVE_SYSCONF |
| 9905 | static struct constdef posix_constants_sysconf[] = { |
| 9906 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9907 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9908 | #endif |
| 9909 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9910 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9911 | #endif |
| 9912 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9913 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9914 | #endif |
| 9915 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9916 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9917 | #endif |
| 9918 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9919 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9920 | #endif |
| 9921 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9922 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9923 | #endif |
| 9924 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9925 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9926 | #endif |
| 9927 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9928 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9929 | #endif |
| 9930 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9931 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9932 | #endif |
| 9933 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9934 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9935 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9936 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9937 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9938 | #endif |
| 9939 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9940 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9941 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9942 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9943 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9944 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9945 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9946 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9947 | #endif |
| 9948 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9949 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9950 | #endif |
| 9951 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9952 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9953 | #endif |
| 9954 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9955 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9956 | #endif |
| 9957 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9958 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9959 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9960 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9961 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9962 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9963 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9964 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9965 | #endif |
| 9966 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9967 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9968 | #endif |
| 9969 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9970 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9971 | #endif |
| 9972 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9973 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9974 | #endif |
| 9975 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9976 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9977 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9978 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9979 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9980 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9981 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9982 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9983 | #endif |
| 9984 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9985 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9986 | #endif |
| 9987 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9988 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9989 | #endif |
| 9990 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9991 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9992 | #endif |
| 9993 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9994 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9995 | #endif |
| 9996 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9997 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9998 | #endif |
| 9999 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10000 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10001 | #endif |
| 10002 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10003 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10004 | #endif |
| 10005 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10006 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10007 | #endif |
| 10008 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10009 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10010 | #endif |
| 10011 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10012 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10013 | #endif |
| 10014 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10015 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10016 | #endif |
| 10017 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10018 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10019 | #endif |
| 10020 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10021 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10022 | #endif |
| 10023 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10024 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10025 | #endif |
| 10026 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10027 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10028 | #endif |
| 10029 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10030 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10031 | #endif |
| 10032 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10033 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10034 | #endif |
| 10035 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10036 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10037 | #endif |
| 10038 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10039 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10040 | #endif |
| 10041 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10042 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10043 | #endif |
| 10044 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10045 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10046 | #endif |
| 10047 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10048 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10049 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10050 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10051 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10052 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10053 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10054 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10055 | #endif |
| 10056 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10057 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10058 | #endif |
| 10059 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10060 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10061 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10062 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10063 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10064 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10065 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10066 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10067 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10068 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10069 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10070 | #endif |
| 10071 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10072 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10073 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10074 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10075 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10076 | #endif |
| 10077 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10078 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10079 | #endif |
| 10080 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10081 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10082 | #endif |
| 10083 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10084 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10085 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10086 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10087 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10088 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10089 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10090 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10091 | #endif |
| 10092 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10093 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10094 | #endif |
| 10095 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10096 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10097 | #endif |
| 10098 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10099 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10100 | #endif |
| 10101 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10102 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10103 | #endif |
| 10104 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10105 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10106 | #endif |
| 10107 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10108 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10109 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10110 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10111 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10112 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10113 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10114 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10115 | #endif |
| 10116 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10117 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10118 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10119 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10120 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10121 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10122 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10123 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10124 | #endif |
| 10125 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10126 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10127 | #endif |
| 10128 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10129 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10130 | #endif |
| 10131 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10132 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10133 | #endif |
| 10134 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10135 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10136 | #endif |
| 10137 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10138 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10139 | #endif |
| 10140 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10141 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10142 | #endif |
| 10143 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10144 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10145 | #endif |
| 10146 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10147 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10148 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10149 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10150 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10151 | #endif |
| 10152 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10153 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10154 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10155 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10156 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10157 | #endif |
| 10158 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10159 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10160 | #endif |
| 10161 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10162 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10163 | #endif |
| 10164 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10165 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10166 | #endif |
| 10167 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10168 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10169 | #endif |
| 10170 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10171 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10172 | #endif |
| 10173 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10174 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10175 | #endif |
| 10176 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10177 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10178 | #endif |
| 10179 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10180 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10181 | #endif |
| 10182 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10183 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10184 | #endif |
| 10185 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10186 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10187 | #endif |
| 10188 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10189 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10190 | #endif |
| 10191 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10192 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10193 | #endif |
| 10194 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10195 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10196 | #endif |
| 10197 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10198 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10199 | #endif |
| 10200 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10201 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10202 | #endif |
| 10203 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10204 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10205 | #endif |
| 10206 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10207 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10208 | #endif |
| 10209 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10210 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10211 | #endif |
| 10212 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10213 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10214 | #endif |
| 10215 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10216 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10217 | #endif |
| 10218 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10219 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10220 | #endif |
| 10221 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10222 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10223 | #endif |
| 10224 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10225 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10226 | #endif |
| 10227 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10228 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10229 | #endif |
| 10230 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10231 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10232 | #endif |
| 10233 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10234 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10235 | #endif |
| 10236 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10237 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10238 | #endif |
| 10239 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10240 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10241 | #endif |
| 10242 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10243 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10244 | #endif |
| 10245 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10246 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10247 | #endif |
| 10248 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10249 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10250 | #endif |
| 10251 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10252 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10253 | #endif |
| 10254 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10255 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10256 | #endif |
| 10257 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10258 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10259 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10260 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10261 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10262 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10263 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10264 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10265 | #endif |
| 10266 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10267 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10268 | #endif |
| 10269 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10270 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10271 | #endif |
| 10272 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10273 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10274 | #endif |
| 10275 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10276 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10277 | #endif |
| 10278 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10279 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10280 | #endif |
| 10281 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10282 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10283 | #endif |
| 10284 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10285 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10286 | #endif |
| 10287 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10288 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10289 | #endif |
| 10290 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10291 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10292 | #endif |
| 10293 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10294 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10295 | #endif |
| 10296 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10297 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10298 | #endif |
| 10299 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10300 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10301 | #endif |
| 10302 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10303 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10304 | #endif |
| 10305 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10306 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10307 | #endif |
| 10308 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10309 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10310 | #endif |
| 10311 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10312 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10313 | #endif |
| 10314 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10315 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10316 | #endif |
| 10317 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10318 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10319 | #endif |
| 10320 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10321 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10322 | #endif |
| 10323 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10324 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10325 | #endif |
| 10326 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10327 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10328 | #endif |
| 10329 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10330 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10331 | #endif |
| 10332 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10333 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10334 | #endif |
| 10335 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10336 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10337 | #endif |
| 10338 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10339 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10340 | #endif |
| 10341 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10342 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10343 | #endif |
| 10344 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10345 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10346 | #endif |
| 10347 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10348 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10349 | #endif |
| 10350 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10351 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10352 | #endif |
| 10353 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10354 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10355 | #endif |
| 10356 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10357 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10358 | #endif |
| 10359 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10360 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10361 | #endif |
| 10362 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10363 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10364 | #endif |
| 10365 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10366 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10367 | #endif |
| 10368 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10369 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10370 | #endif |
| 10371 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10372 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10373 | #endif |
| 10374 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10375 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10376 | #endif |
| 10377 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10378 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10379 | #endif |
| 10380 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10381 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10382 | #endif |
| 10383 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10384 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10385 | #endif |
| 10386 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10387 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10388 | #endif |
| 10389 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10390 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10391 | #endif |
| 10392 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10393 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10394 | #endif |
| 10395 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10396 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10397 | #endif |
| 10398 | }; |
| 10399 | |
| 10400 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10401 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10402 | { |
| 10403 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 10404 | sizeof(posix_constants_sysconf) |
| 10405 | / sizeof(struct constdef)); |
| 10406 | } |
| 10407 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10408 | |
| 10409 | /*[clinic input] |
| 10410 | os.sysconf -> long |
| 10411 | name: sysconf_confname |
| 10412 | / |
| 10413 | |
| 10414 | Return an integer-valued system configuration variable. |
| 10415 | [clinic start generated code]*/ |
| 10416 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10417 | static long |
| 10418 | os_sysconf_impl(PyModuleDef *module, int name) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10419 | /*[clinic end generated code: output=ed567306f58d69c4 input=279e3430a33f29e4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10420 | { |
| 10421 | long value; |
| 10422 | |
| 10423 | errno = 0; |
| 10424 | value = sysconf(name); |
| 10425 | if (value == -1 && errno != 0) |
| 10426 | posix_error(); |
| 10427 | return value; |
| 10428 | } |
| 10429 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10430 | |
| 10431 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10432 | /* 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] | 10433 | * 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] | 10434 | * the exported dictionaries that are used to publish information about the |
| 10435 | * names available on the host platform. |
| 10436 | * |
| 10437 | * Sorting the table at runtime ensures that the table is properly ordered |
| 10438 | * when used, even for platforms we're not able to test on. It also makes |
| 10439 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10440 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10441 | |
| 10442 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10443 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10444 | { |
| 10445 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10446 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10447 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10448 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10449 | |
| 10450 | return strcmp(c1->name, c2->name); |
| 10451 | } |
| 10452 | |
| 10453 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10454 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10455 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10456 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10457 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10458 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10459 | |
| 10460 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 10461 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10462 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10463 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10464 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10465 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10466 | PyObject *o = PyLong_FromLong(table[i].value); |
| 10467 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 10468 | Py_XDECREF(o); |
| 10469 | Py_DECREF(d); |
| 10470 | return -1; |
| 10471 | } |
| 10472 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10473 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10474 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10475 | } |
| 10476 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10477 | /* Return -1 on failure, 0 on success. */ |
| 10478 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10479 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10480 | { |
| 10481 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10482 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10483 | sizeof(posix_constants_pathconf) |
| 10484 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10485 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10486 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10487 | #endif |
| 10488 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10489 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10490 | sizeof(posix_constants_confstr) |
| 10491 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10492 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10493 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10494 | #endif |
| 10495 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10496 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10497 | sizeof(posix_constants_sysconf) |
| 10498 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10499 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10500 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10501 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10502 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10503 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10504 | |
| 10505 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10506 | /*[clinic input] |
| 10507 | os.abort |
| 10508 | |
| 10509 | Abort the interpreter immediately. |
| 10510 | |
| 10511 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 10512 | on the hosting operating system. This function never returns. |
| 10513 | [clinic start generated code]*/ |
| 10514 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10515 | static PyObject * |
| 10516 | os_abort_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10517 | /*[clinic end generated code: output=486bb96647c299b3 input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10518 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10519 | abort(); |
| 10520 | /*NOTREACHED*/ |
| 10521 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 10522 | return NULL; |
| 10523 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10524 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10525 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10526 | /* AC 3.5: change to path_t? but that might change exceptions */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10527 | PyDoc_STRVAR(win32_startfile__doc__, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10528 | "startfile(filepath [, operation])\n\ |
| 10529 | \n\ |
| 10530 | Start a file with its associated application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10531 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 10532 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 10533 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 10534 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 10535 | application (if any) its extension is associated.\n\ |
| 10536 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 10537 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10538 | \n\ |
| 10539 | startfile returns as soon as the associated application is launched.\n\ |
| 10540 | There is no option to wait for the application to close, and no way\n\ |
| 10541 | to retrieve the application's exit status.\n\ |
| 10542 | \n\ |
| 10543 | The filepath is relative to the current directory. If you want to use\n\ |
| 10544 | an absolute path, make sure the first character is not a slash (\"/\");\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10545 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10546 | |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10547 | /* Grab ShellExecute dynamically from shell32 */ |
| 10548 | static int has_ShellExecute = -1; |
| 10549 | static HINSTANCE (CALLBACK *Py_ShellExecuteA)(HWND, LPCSTR, LPCSTR, LPCSTR, |
| 10550 | LPCSTR, INT); |
| 10551 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 10552 | LPCWSTR, INT); |
| 10553 | static int |
| 10554 | check_ShellExecute() |
| 10555 | { |
| 10556 | HINSTANCE hShell32; |
| 10557 | |
| 10558 | /* only recheck */ |
| 10559 | if (-1 == has_ShellExecute) { |
| 10560 | Py_BEGIN_ALLOW_THREADS |
| 10561 | hShell32 = LoadLibraryW(L"SHELL32"); |
| 10562 | Py_END_ALLOW_THREADS |
| 10563 | if (hShell32) { |
| 10564 | *(FARPROC*)&Py_ShellExecuteA = GetProcAddress(hShell32, |
| 10565 | "ShellExecuteA"); |
| 10566 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 10567 | "ShellExecuteW"); |
| 10568 | has_ShellExecute = Py_ShellExecuteA && |
| 10569 | Py_ShellExecuteW; |
| 10570 | } else { |
| 10571 | has_ShellExecute = 0; |
| 10572 | } |
| 10573 | } |
| 10574 | return has_ShellExecute; |
| 10575 | } |
| 10576 | |
| 10577 | |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10578 | static PyObject * |
| 10579 | win32_startfile(PyObject *self, PyObject *args) |
| 10580 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10581 | PyObject *ofilepath; |
| 10582 | char *filepath; |
| 10583 | char *operation = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10584 | wchar_t *wpath, *woperation; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10585 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 10586 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10587 | PyObject *unipath, *uoperation = NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10588 | |
| 10589 | if(!check_ShellExecute()) { |
| 10590 | /* If the OS doesn't have ShellExecute, return a |
| 10591 | NotImplementedError. */ |
| 10592 | return PyErr_Format(PyExc_NotImplementedError, |
| 10593 | "startfile not available on this platform"); |
| 10594 | } |
| 10595 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10596 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 10597 | &unipath, &operation)) { |
| 10598 | PyErr_Clear(); |
| 10599 | goto normal; |
| 10600 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10601 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10602 | if (operation) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10603 | uoperation = PyUnicode_DecodeASCII(operation, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10604 | strlen(operation), NULL); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10605 | if (!uoperation) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10606 | PyErr_Clear(); |
| 10607 | operation = NULL; |
| 10608 | goto normal; |
| 10609 | } |
| 10610 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10611 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10612 | wpath = PyUnicode_AsUnicode(unipath); |
| 10613 | if (wpath == NULL) |
| 10614 | goto normal; |
| 10615 | if (uoperation) { |
| 10616 | woperation = PyUnicode_AsUnicode(uoperation); |
| 10617 | if (woperation == NULL) |
| 10618 | goto normal; |
| 10619 | } |
| 10620 | else |
| 10621 | woperation = NULL; |
| 10622 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10623 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10624 | rc = Py_ShellExecuteW((HWND)0, woperation, wpath, |
| 10625 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10626 | Py_END_ALLOW_THREADS |
| 10627 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10628 | Py_XDECREF(uoperation); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10629 | if (rc <= (HINSTANCE)32) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10630 | win32_error_object("startfile", unipath); |
| 10631 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10632 | } |
| 10633 | Py_INCREF(Py_None); |
| 10634 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10635 | |
| 10636 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10637 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 10638 | PyUnicode_FSConverter, &ofilepath, |
| 10639 | &operation)) |
| 10640 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 10641 | if (win32_warn_bytes_api()) { |
| 10642 | Py_DECREF(ofilepath); |
| 10643 | return NULL; |
| 10644 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10645 | filepath = PyBytes_AsString(ofilepath); |
| 10646 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10647 | rc = Py_ShellExecuteA((HWND)0, operation, filepath, |
| 10648 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10649 | Py_END_ALLOW_THREADS |
| 10650 | if (rc <= (HINSTANCE)32) { |
| 10651 | PyObject *errval = win32_error("startfile", filepath); |
| 10652 | Py_DECREF(ofilepath); |
| 10653 | return errval; |
| 10654 | } |
| 10655 | Py_DECREF(ofilepath); |
| 10656 | Py_INCREF(Py_None); |
| 10657 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10658 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10659 | #endif /* MS_WINDOWS */ |
| 10660 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10661 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10662 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10663 | /*[clinic input] |
| 10664 | os.getloadavg |
| 10665 | |
| 10666 | Return average recent system load information. |
| 10667 | |
| 10668 | Return the number of processes in the system run queue averaged over |
| 10669 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 10670 | Raises OSError if the load average was unobtainable. |
| 10671 | [clinic start generated code]*/ |
| 10672 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10673 | static PyObject * |
| 10674 | os_getloadavg_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10675 | /*[clinic end generated code: output=2b64c5b675d74c14 input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10676 | { |
| 10677 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10678 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10679 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 10680 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10681 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10682 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10683 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10684 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10685 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10686 | |
| 10687 | /*[clinic input] |
| 10688 | os.device_encoding |
| 10689 | fd: int |
| 10690 | |
| 10691 | Return a string describing the encoding of a terminal's file descriptor. |
| 10692 | |
| 10693 | The file descriptor must be attached to a terminal. |
| 10694 | If the device is not a terminal, return None. |
| 10695 | [clinic start generated code]*/ |
| 10696 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10697 | static PyObject * |
| 10698 | os_device_encoding_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10699 | /*[clinic end generated code: output=34f14e33468419c1 input=9e1d4a42b66df312]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10700 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10701 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 10702 | } |
| 10703 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10704 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10705 | #ifdef HAVE_SETRESUID |
| 10706 | /*[clinic input] |
| 10707 | os.setresuid |
| 10708 | |
| 10709 | ruid: uid_t |
| 10710 | euid: uid_t |
| 10711 | suid: uid_t |
| 10712 | / |
| 10713 | |
| 10714 | Set the current process's real, effective, and saved user ids. |
| 10715 | [clinic start generated code]*/ |
| 10716 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10717 | static PyObject * |
| 10718 | os_setresuid_impl(PyModuleDef *module, uid_t ruid, uid_t euid, uid_t suid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10719 | /*[clinic end generated code: output=92cc330812c6ed0f input=9e33cb79a82792f3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10720 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10721 | if (setresuid(ruid, euid, suid) < 0) |
| 10722 | return posix_error(); |
| 10723 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10724 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10725 | #endif /* HAVE_SETRESUID */ |
| 10726 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10727 | |
| 10728 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10729 | /*[clinic input] |
| 10730 | os.setresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10731 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10732 | rgid: gid_t |
| 10733 | egid: gid_t |
| 10734 | sgid: gid_t |
| 10735 | / |
| 10736 | |
| 10737 | Set the current process's real, effective, and saved group ids. |
| 10738 | [clinic start generated code]*/ |
| 10739 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10740 | static PyObject * |
| 10741 | os_setresgid_impl(PyModuleDef *module, gid_t rgid, gid_t egid, gid_t sgid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10742 | /*[clinic end generated code: output=e91dc4842a604429 input=33e9e0785ef426b1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10743 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10744 | if (setresgid(rgid, egid, sgid) < 0) |
| 10745 | return posix_error(); |
| 10746 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10747 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10748 | #endif /* HAVE_SETRESGID */ |
| 10749 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10750 | |
| 10751 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10752 | /*[clinic input] |
| 10753 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10754 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10755 | Return a tuple of the current process's real, effective, and saved user ids. |
| 10756 | [clinic start generated code]*/ |
| 10757 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10758 | static PyObject * |
| 10759 | os_getresuid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10760 | /*[clinic end generated code: output=9ddef62faae8e477 input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10761 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10762 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10763 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 10764 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10765 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 10766 | _PyLong_FromUid(euid), |
| 10767 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10768 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10769 | #endif /* HAVE_GETRESUID */ |
| 10770 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10771 | |
| 10772 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10773 | /*[clinic input] |
| 10774 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10775 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10776 | Return a tuple of the current process's real, effective, and saved group ids. |
| 10777 | [clinic start generated code]*/ |
| 10778 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10779 | static PyObject * |
| 10780 | os_getresgid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10781 | /*[clinic end generated code: output=e1a553cbcf16234c input=517e68db9ca32df6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10782 | { |
| 10783 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10784 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 10785 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10786 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 10787 | _PyLong_FromGid(egid), |
| 10788 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10789 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10790 | #endif /* HAVE_GETRESGID */ |
| 10791 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10792 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10793 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10794 | /*[clinic input] |
| 10795 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10796 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10797 | path: path_t(allow_fd=True) |
| 10798 | attribute: path_t |
| 10799 | * |
| 10800 | follow_symlinks: bool = True |
| 10801 | |
| 10802 | Return the value of extended attribute attribute on path. |
| 10803 | |
| 10804 | path may be either a string or an open file descriptor. |
| 10805 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10806 | link, getxattr will examine the symbolic link itself instead of the file |
| 10807 | the link points to. |
| 10808 | |
| 10809 | [clinic start generated code]*/ |
| 10810 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10811 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10812 | os_getxattr_impl(PyModuleDef *module, path_t *path, path_t *attribute, |
| 10813 | int follow_symlinks) |
| 10814 | /*[clinic end generated code: output=cf2cede74bd5d412 input=8c8ea3bab78d89c2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10815 | { |
| 10816 | Py_ssize_t i; |
| 10817 | PyObject *buffer = NULL; |
| 10818 | |
| 10819 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 10820 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10821 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10822 | for (i = 0; ; i++) { |
| 10823 | void *ptr; |
| 10824 | ssize_t result; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame^] | 10825 | static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10826 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10827 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10828 | path_error(path); |
| 10829 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10830 | } |
| 10831 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 10832 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10833 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10834 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10835 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10836 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10837 | if (path->fd >= 0) |
| 10838 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10839 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10840 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10841 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10842 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10843 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10844 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10845 | if (result < 0) { |
| 10846 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10847 | if (errno == ERANGE) |
| 10848 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10849 | path_error(path); |
| 10850 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10851 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10852 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10853 | if (result != buffer_size) { |
| 10854 | /* Can only shrink. */ |
| 10855 | _PyBytes_Resize(&buffer, result); |
| 10856 | } |
| 10857 | break; |
| 10858 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10859 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10860 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10861 | } |
| 10862 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10863 | |
| 10864 | /*[clinic input] |
| 10865 | os.setxattr |
| 10866 | |
| 10867 | path: path_t(allow_fd=True) |
| 10868 | attribute: path_t |
| 10869 | value: Py_buffer |
| 10870 | flags: int = 0 |
| 10871 | * |
| 10872 | follow_symlinks: bool = True |
| 10873 | |
| 10874 | Set extended attribute attribute on path to value. |
| 10875 | |
| 10876 | path may be either a string or an open file descriptor. |
| 10877 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10878 | link, setxattr will modify the symbolic link itself instead of the file |
| 10879 | the link points to. |
| 10880 | |
| 10881 | [clinic start generated code]*/ |
| 10882 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10883 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10884 | os_setxattr_impl(PyModuleDef *module, path_t *path, path_t *attribute, |
| 10885 | Py_buffer *value, int flags, int follow_symlinks) |
| 10886 | /*[clinic end generated code: output=1b395ef82880fea0 input=f0d26833992015c2]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10887 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10888 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10889 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10890 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10891 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10892 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10893 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10894 | if (path->fd > -1) |
| 10895 | result = fsetxattr(path->fd, attribute->narrow, |
| 10896 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10897 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10898 | result = setxattr(path->narrow, attribute->narrow, |
| 10899 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10900 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10901 | result = lsetxattr(path->narrow, attribute->narrow, |
| 10902 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10903 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10904 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10905 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10906 | path_error(path); |
| 10907 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10908 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10909 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10910 | Py_RETURN_NONE; |
| 10911 | } |
| 10912 | |
| 10913 | |
| 10914 | /*[clinic input] |
| 10915 | os.removexattr |
| 10916 | |
| 10917 | path: path_t(allow_fd=True) |
| 10918 | attribute: path_t |
| 10919 | * |
| 10920 | follow_symlinks: bool = True |
| 10921 | |
| 10922 | Remove extended attribute attribute on path. |
| 10923 | |
| 10924 | path may be either a string or an open file descriptor. |
| 10925 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10926 | link, removexattr will modify the symbolic link itself instead of the file |
| 10927 | the link points to. |
| 10928 | |
| 10929 | [clinic start generated code]*/ |
| 10930 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10931 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10932 | os_removexattr_impl(PyModuleDef *module, path_t *path, path_t *attribute, |
| 10933 | int follow_symlinks) |
| 10934 | /*[clinic end generated code: output=f92bb39ab992650d input=cdb54834161e3329]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10935 | { |
| 10936 | ssize_t result; |
| 10937 | |
| 10938 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 10939 | return NULL; |
| 10940 | |
| 10941 | Py_BEGIN_ALLOW_THREADS; |
| 10942 | if (path->fd > -1) |
| 10943 | result = fremovexattr(path->fd, attribute->narrow); |
| 10944 | else if (follow_symlinks) |
| 10945 | result = removexattr(path->narrow, attribute->narrow); |
| 10946 | else |
| 10947 | result = lremovexattr(path->narrow, attribute->narrow); |
| 10948 | Py_END_ALLOW_THREADS; |
| 10949 | |
| 10950 | if (result) { |
| 10951 | return path_error(path); |
| 10952 | } |
| 10953 | |
| 10954 | Py_RETURN_NONE; |
| 10955 | } |
| 10956 | |
| 10957 | |
| 10958 | /*[clinic input] |
| 10959 | os.listxattr |
| 10960 | |
| 10961 | path: path_t(allow_fd=True, nullable=True) = None |
| 10962 | * |
| 10963 | follow_symlinks: bool = True |
| 10964 | |
| 10965 | Return a list of extended attributes on path. |
| 10966 | |
| 10967 | path may be either None, a string, or an open file descriptor. |
| 10968 | if path is None, listxattr will examine the current directory. |
| 10969 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10970 | link, listxattr will examine the symbolic link itself instead of the file |
| 10971 | the link points to. |
| 10972 | [clinic start generated code]*/ |
| 10973 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10974 | static PyObject * |
| 10975 | os_listxattr_impl(PyModuleDef *module, path_t *path, int follow_symlinks) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10976 | /*[clinic end generated code: output=a87ad6ce56e42a4f input=08cca53ac0b07c13]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10977 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10978 | Py_ssize_t i; |
| 10979 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10980 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10981 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10982 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10983 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10984 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10985 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10986 | name = path->narrow ? path->narrow : "."; |
| 10987 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10988 | for (i = 0; ; i++) { |
| 10989 | char *start, *trace, *end; |
| 10990 | ssize_t length; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame^] | 10991 | static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10992 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10993 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 10994 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10995 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10996 | break; |
| 10997 | } |
| 10998 | buffer = PyMem_MALLOC(buffer_size); |
| 10999 | if (!buffer) { |
| 11000 | PyErr_NoMemory(); |
| 11001 | break; |
| 11002 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11003 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11004 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11005 | if (path->fd > -1) |
| 11006 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11007 | else if (follow_symlinks) |
| 11008 | length = listxattr(name, buffer, buffer_size); |
| 11009 | else |
| 11010 | length = llistxattr(name, buffer, buffer_size); |
| 11011 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11012 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11013 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 11014 | if (errno == ERANGE) { |
| 11015 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 11016 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11017 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 11018 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11019 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11020 | break; |
| 11021 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11022 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11023 | result = PyList_New(0); |
| 11024 | if (!result) { |
| 11025 | goto exit; |
| 11026 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11027 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11028 | end = buffer + length; |
| 11029 | for (trace = start = buffer; trace != end; trace++) { |
| 11030 | if (!*trace) { |
| 11031 | int error; |
| 11032 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 11033 | trace - start); |
| 11034 | if (!attribute) { |
| 11035 | Py_DECREF(result); |
| 11036 | result = NULL; |
| 11037 | goto exit; |
| 11038 | } |
| 11039 | error = PyList_Append(result, attribute); |
| 11040 | Py_DECREF(attribute); |
| 11041 | if (error) { |
| 11042 | Py_DECREF(result); |
| 11043 | result = NULL; |
| 11044 | goto exit; |
| 11045 | } |
| 11046 | start = trace + 1; |
| 11047 | } |
| 11048 | } |
| 11049 | break; |
| 11050 | } |
| 11051 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11052 | if (buffer) |
| 11053 | PyMem_FREE(buffer); |
| 11054 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11055 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11056 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11057 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11058 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11059 | /*[clinic input] |
| 11060 | os.urandom |
| 11061 | |
| 11062 | size: Py_ssize_t |
| 11063 | / |
| 11064 | |
| 11065 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 11066 | [clinic start generated code]*/ |
| 11067 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11068 | static PyObject * |
| 11069 | os_urandom_impl(PyModuleDef *module, Py_ssize_t size) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 11070 | /*[clinic end generated code: output=e0011f021501f03b input=4067cdb1b6776c29]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11071 | { |
| 11072 | PyObject *bytes; |
| 11073 | int result; |
| 11074 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11075 | if (size < 0) |
| 11076 | return PyErr_Format(PyExc_ValueError, |
| 11077 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11078 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 11079 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11080 | return NULL; |
| 11081 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11082 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), |
| 11083 | PyBytes_GET_SIZE(bytes)); |
| 11084 | if (result == -1) { |
| 11085 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11086 | return NULL; |
| 11087 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11088 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11089 | } |
| 11090 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11091 | /* Terminal size querying */ |
| 11092 | |
| 11093 | static PyTypeObject TerminalSizeType; |
| 11094 | |
| 11095 | PyDoc_STRVAR(TerminalSize_docstring, |
| 11096 | "A tuple of (columns, lines) for holding terminal window size"); |
| 11097 | |
| 11098 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 11099 | {"columns", "width of the terminal window in characters"}, |
| 11100 | {"lines", "height of the terminal window in characters"}, |
| 11101 | {NULL, NULL} |
| 11102 | }; |
| 11103 | |
| 11104 | static PyStructSequence_Desc TerminalSize_desc = { |
| 11105 | "os.terminal_size", |
| 11106 | TerminalSize_docstring, |
| 11107 | TerminalSize_fields, |
| 11108 | 2, |
| 11109 | }; |
| 11110 | |
| 11111 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11112 | /* AC 3.5: fd should accept None */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11113 | PyDoc_STRVAR(termsize__doc__, |
| 11114 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 11115 | "\n" \ |
| 11116 | "The optional argument fd (default standard output) specifies\n" \ |
| 11117 | "which file descriptor should be queried.\n" \ |
| 11118 | "\n" \ |
| 11119 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 11120 | "is thrown.\n" \ |
| 11121 | "\n" \ |
| 11122 | "This function will only be defined if an implementation is\n" \ |
| 11123 | "available for this system.\n" \ |
| 11124 | "\n" \ |
| 11125 | "shutil.get_terminal_size is the high-level function which should \n" \ |
| 11126 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 11127 | |
| 11128 | static PyObject* |
| 11129 | get_terminal_size(PyObject *self, PyObject *args) |
| 11130 | { |
| 11131 | int columns, lines; |
| 11132 | PyObject *termsize; |
| 11133 | |
| 11134 | int fd = fileno(stdout); |
| 11135 | /* Under some conditions stdout may not be connected and |
| 11136 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 11137 | * GUI apps don't have valid standard streams by default. |
| 11138 | * |
| 11139 | * If this happens, and the optional fd argument is not present, |
| 11140 | * the ioctl below will fail returning EBADF. This is what we want. |
| 11141 | */ |
| 11142 | |
| 11143 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 11144 | return NULL; |
| 11145 | |
| 11146 | #ifdef TERMSIZE_USE_IOCTL |
| 11147 | { |
| 11148 | struct winsize w; |
| 11149 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 11150 | return PyErr_SetFromErrno(PyExc_OSError); |
| 11151 | columns = w.ws_col; |
| 11152 | lines = w.ws_row; |
| 11153 | } |
| 11154 | #endif /* TERMSIZE_USE_IOCTL */ |
| 11155 | |
| 11156 | #ifdef TERMSIZE_USE_CONIO |
| 11157 | { |
| 11158 | DWORD nhandle; |
| 11159 | HANDLE handle; |
| 11160 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 11161 | switch (fd) { |
| 11162 | case 0: nhandle = STD_INPUT_HANDLE; |
| 11163 | break; |
| 11164 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 11165 | break; |
| 11166 | case 2: nhandle = STD_ERROR_HANDLE; |
| 11167 | break; |
| 11168 | default: |
| 11169 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 11170 | } |
| 11171 | handle = GetStdHandle(nhandle); |
| 11172 | if (handle == NULL) |
| 11173 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 11174 | if (handle == INVALID_HANDLE_VALUE) |
| 11175 | return PyErr_SetFromWindowsErr(0); |
| 11176 | |
| 11177 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 11178 | return PyErr_SetFromWindowsErr(0); |
| 11179 | |
| 11180 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 11181 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 11182 | } |
| 11183 | #endif /* TERMSIZE_USE_CONIO */ |
| 11184 | |
| 11185 | termsize = PyStructSequence_New(&TerminalSizeType); |
| 11186 | if (termsize == NULL) |
| 11187 | return NULL; |
| 11188 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 11189 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 11190 | if (PyErr_Occurred()) { |
| 11191 | Py_DECREF(termsize); |
| 11192 | return NULL; |
| 11193 | } |
| 11194 | return termsize; |
| 11195 | } |
| 11196 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 11197 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11198 | |
| 11199 | /*[clinic input] |
| 11200 | os.cpu_count |
| 11201 | |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 11202 | Return the number of CPUs in the system; return None if indeterminable. |
| 11203 | |
| 11204 | This number is not equivalent to the number of CPUs the current process can |
| 11205 | use. The number of usable CPUs can be obtained with |
| 11206 | ``len(os.sched_getaffinity(0))`` |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11207 | [clinic start generated code]*/ |
| 11208 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11209 | static PyObject * |
| 11210 | os_cpu_count_impl(PyModuleDef *module) |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 11211 | /*[clinic end generated code: output=c59ee7f6bce832b8 input=e7c8f4ba6dbbadd3]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11212 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11213 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11214 | #ifdef MS_WINDOWS |
| 11215 | SYSTEM_INFO sysinfo; |
| 11216 | GetSystemInfo(&sysinfo); |
| 11217 | ncpu = sysinfo.dwNumberOfProcessors; |
| 11218 | #elif defined(__hpux) |
| 11219 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 11220 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 11221 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11222 | #elif defined(__DragonFly__) || \ |
| 11223 | defined(__OpenBSD__) || \ |
| 11224 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11225 | defined(__NetBSD__) || \ |
| 11226 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 11227 | int mib[2]; |
| 11228 | size_t len = sizeof(ncpu); |
| 11229 | mib[0] = CTL_HW; |
| 11230 | mib[1] = HW_NCPU; |
| 11231 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 11232 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11233 | #endif |
| 11234 | if (ncpu >= 1) |
| 11235 | return PyLong_FromLong(ncpu); |
| 11236 | else |
| 11237 | Py_RETURN_NONE; |
| 11238 | } |
| 11239 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11240 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11241 | /*[clinic input] |
| 11242 | os.get_inheritable -> bool |
| 11243 | |
| 11244 | fd: int |
| 11245 | / |
| 11246 | |
| 11247 | Get the close-on-exe flag of the specified file descriptor. |
| 11248 | [clinic start generated code]*/ |
| 11249 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11250 | static int |
| 11251 | os_get_inheritable_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 11252 | /*[clinic end generated code: output=36110bb36efaa21e input=89ac008dc9ab6b95]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11253 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11254 | int return_value; |
| 11255 | if (!_PyVerify_fd(fd)) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11256 | posix_error(); |
| 11257 | return -1; |
| 11258 | } |
| 11259 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11260 | _Py_BEGIN_SUPPRESS_IPH |
| 11261 | return_value = _Py_get_inheritable(fd); |
| 11262 | _Py_END_SUPPRESS_IPH |
| 11263 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11264 | } |
| 11265 | |
| 11266 | |
| 11267 | /*[clinic input] |
| 11268 | os.set_inheritable |
| 11269 | fd: int |
| 11270 | inheritable: int |
| 11271 | / |
| 11272 | |
| 11273 | Set the inheritable flag of the specified file descriptor. |
| 11274 | [clinic start generated code]*/ |
| 11275 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11276 | static PyObject * |
| 11277 | os_set_inheritable_impl(PyModuleDef *module, int fd, int inheritable) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 11278 | /*[clinic end generated code: output=2ac5c6ce8623f045 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11279 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11280 | int result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11281 | if (!_PyVerify_fd(fd)) |
| 11282 | return posix_error(); |
| 11283 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11284 | _Py_BEGIN_SUPPRESS_IPH |
| 11285 | result = _Py_set_inheritable(fd, inheritable, NULL); |
| 11286 | _Py_END_SUPPRESS_IPH |
| 11287 | if (result < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11288 | return NULL; |
| 11289 | Py_RETURN_NONE; |
| 11290 | } |
| 11291 | |
| 11292 | |
| 11293 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11294 | /*[clinic input] |
| 11295 | os.get_handle_inheritable -> bool |
| 11296 | handle: Py_intptr_t |
| 11297 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11298 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11299 | Get the close-on-exe flag of the specified file descriptor. |
| 11300 | [clinic start generated code]*/ |
| 11301 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11302 | static int |
| 11303 | os_get_handle_inheritable_impl(PyModuleDef *module, Py_intptr_t handle) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 11304 | /*[clinic end generated code: output=3b7b3e1b43f312b6 input=5f7759443aae3dc5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11305 | { |
| 11306 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11307 | |
| 11308 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 11309 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11310 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11311 | } |
| 11312 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11313 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11314 | } |
| 11315 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11316 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11317 | /*[clinic input] |
| 11318 | os.set_handle_inheritable |
| 11319 | handle: Py_intptr_t |
| 11320 | inheritable: bool |
| 11321 | / |
| 11322 | |
| 11323 | Set the inheritable flag of the specified handle. |
| 11324 | [clinic start generated code]*/ |
| 11325 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11326 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11327 | os_set_handle_inheritable_impl(PyModuleDef *module, Py_intptr_t handle, |
| 11328 | int inheritable) |
| 11329 | /*[clinic end generated code: output=d2e111a96c9eb296 input=e64b2b2730469def]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11330 | { |
| 11331 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11332 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 11333 | PyErr_SetFromWindowsErr(0); |
| 11334 | return NULL; |
| 11335 | } |
| 11336 | Py_RETURN_NONE; |
| 11337 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11338 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11339 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11340 | #ifndef MS_WINDOWS |
| 11341 | PyDoc_STRVAR(get_blocking__doc__, |
| 11342 | "get_blocking(fd) -> bool\n" \ |
| 11343 | "\n" \ |
| 11344 | "Get the blocking mode of the file descriptor:\n" \ |
| 11345 | "False if the O_NONBLOCK flag is set, True if the flag is cleared."); |
| 11346 | |
| 11347 | static PyObject* |
| 11348 | posix_get_blocking(PyObject *self, PyObject *args) |
| 11349 | { |
| 11350 | int fd; |
| 11351 | int blocking; |
| 11352 | |
| 11353 | if (!PyArg_ParseTuple(args, "i:get_blocking", &fd)) |
| 11354 | return NULL; |
| 11355 | |
| 11356 | if (!_PyVerify_fd(fd)) |
| 11357 | return posix_error(); |
| 11358 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11359 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11360 | blocking = _Py_get_blocking(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11361 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11362 | if (blocking < 0) |
| 11363 | return NULL; |
| 11364 | return PyBool_FromLong(blocking); |
| 11365 | } |
| 11366 | |
| 11367 | PyDoc_STRVAR(set_blocking__doc__, |
| 11368 | "set_blocking(fd, blocking)\n" \ |
| 11369 | "\n" \ |
| 11370 | "Set the blocking mode of the specified file descriptor.\n" \ |
| 11371 | "Set the O_NONBLOCK flag if blocking is False,\n" \ |
| 11372 | "clear the O_NONBLOCK flag otherwise."); |
| 11373 | |
| 11374 | static PyObject* |
| 11375 | posix_set_blocking(PyObject *self, PyObject *args) |
| 11376 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11377 | int fd, blocking, result; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11378 | |
| 11379 | if (!PyArg_ParseTuple(args, "ii:set_blocking", &fd, &blocking)) |
| 11380 | return NULL; |
| 11381 | |
| 11382 | if (!_PyVerify_fd(fd)) |
| 11383 | return posix_error(); |
| 11384 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11385 | _Py_BEGIN_SUPPRESS_IPH |
| 11386 | result = _Py_set_blocking(fd, blocking); |
| 11387 | _Py_END_SUPPRESS_IPH |
| 11388 | if (result < 0) |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11389 | return NULL; |
| 11390 | Py_RETURN_NONE; |
| 11391 | } |
| 11392 | #endif /* !MS_WINDOWS */ |
| 11393 | |
| 11394 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11395 | PyDoc_STRVAR(posix_scandir__doc__, |
| 11396 | "scandir(path='.') -> iterator of DirEntry objects for given path"); |
| 11397 | |
| 11398 | static char *follow_symlinks_keywords[] = {"follow_symlinks", NULL}; |
| 11399 | |
| 11400 | typedef struct { |
| 11401 | PyObject_HEAD |
| 11402 | PyObject *name; |
| 11403 | PyObject *path; |
| 11404 | PyObject *stat; |
| 11405 | PyObject *lstat; |
| 11406 | #ifdef MS_WINDOWS |
| 11407 | struct _Py_stat_struct win32_lstat; |
| 11408 | __int64 win32_file_index; |
| 11409 | int got_file_index; |
| 11410 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11411 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11412 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11413 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11414 | ino_t d_ino; |
| 11415 | #endif |
| 11416 | } DirEntry; |
| 11417 | |
| 11418 | static void |
| 11419 | DirEntry_dealloc(DirEntry *entry) |
| 11420 | { |
| 11421 | Py_XDECREF(entry->name); |
| 11422 | Py_XDECREF(entry->path); |
| 11423 | Py_XDECREF(entry->stat); |
| 11424 | Py_XDECREF(entry->lstat); |
| 11425 | Py_TYPE(entry)->tp_free((PyObject *)entry); |
| 11426 | } |
| 11427 | |
| 11428 | /* Forward reference */ |
| 11429 | static int |
| 11430 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits); |
| 11431 | |
| 11432 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 11433 | static int |
| 11434 | DirEntry_is_symlink(DirEntry *self) |
| 11435 | { |
| 11436 | #ifdef MS_WINDOWS |
| 11437 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11438 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 11439 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11440 | if (self->d_type != DT_UNKNOWN) |
| 11441 | return self->d_type == DT_LNK; |
| 11442 | else |
| 11443 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11444 | #else |
| 11445 | /* POSIX without d_type */ |
| 11446 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11447 | #endif |
| 11448 | } |
| 11449 | |
| 11450 | static PyObject * |
| 11451 | DirEntry_py_is_symlink(DirEntry *self) |
| 11452 | { |
| 11453 | int result; |
| 11454 | |
| 11455 | result = DirEntry_is_symlink(self); |
| 11456 | if (result == -1) |
| 11457 | return NULL; |
| 11458 | return PyBool_FromLong(result); |
| 11459 | } |
| 11460 | |
| 11461 | static PyObject * |
| 11462 | DirEntry_fetch_stat(DirEntry *self, int follow_symlinks) |
| 11463 | { |
| 11464 | int result; |
| 11465 | struct _Py_stat_struct st; |
| 11466 | |
| 11467 | #ifdef MS_WINDOWS |
| 11468 | wchar_t *path; |
| 11469 | |
| 11470 | path = PyUnicode_AsUnicode(self->path); |
| 11471 | if (!path) |
| 11472 | return NULL; |
| 11473 | |
| 11474 | if (follow_symlinks) |
| 11475 | result = win32_stat_w(path, &st); |
| 11476 | else |
| 11477 | result = win32_lstat_w(path, &st); |
| 11478 | |
| 11479 | if (result != 0) { |
| 11480 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 11481 | 0, self->path); |
| 11482 | } |
| 11483 | #else /* POSIX */ |
| 11484 | PyObject *bytes; |
| 11485 | char *path; |
| 11486 | |
| 11487 | if (!PyUnicode_FSConverter(self->path, &bytes)) |
| 11488 | return NULL; |
| 11489 | path = PyBytes_AS_STRING(bytes); |
| 11490 | |
| 11491 | if (follow_symlinks) |
| 11492 | result = STAT(path, &st); |
| 11493 | else |
| 11494 | result = LSTAT(path, &st); |
| 11495 | Py_DECREF(bytes); |
| 11496 | |
| 11497 | if (result != 0) |
| 11498 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, self->path); |
| 11499 | #endif |
| 11500 | |
| 11501 | return _pystat_fromstructstat(&st); |
| 11502 | } |
| 11503 | |
| 11504 | static PyObject * |
| 11505 | DirEntry_get_lstat(DirEntry *self) |
| 11506 | { |
| 11507 | if (!self->lstat) { |
| 11508 | #ifdef MS_WINDOWS |
| 11509 | self->lstat = _pystat_fromstructstat(&self->win32_lstat); |
| 11510 | #else /* POSIX */ |
| 11511 | self->lstat = DirEntry_fetch_stat(self, 0); |
| 11512 | #endif |
| 11513 | } |
| 11514 | Py_XINCREF(self->lstat); |
| 11515 | return self->lstat; |
| 11516 | } |
| 11517 | |
| 11518 | static PyObject * |
| 11519 | DirEntry_get_stat(DirEntry *self, int follow_symlinks) |
| 11520 | { |
| 11521 | if (!follow_symlinks) |
| 11522 | return DirEntry_get_lstat(self); |
| 11523 | |
| 11524 | if (!self->stat) { |
| 11525 | int result = DirEntry_is_symlink(self); |
| 11526 | if (result == -1) |
| 11527 | return NULL; |
| 11528 | else if (result) |
| 11529 | self->stat = DirEntry_fetch_stat(self, 1); |
| 11530 | else |
| 11531 | self->stat = DirEntry_get_lstat(self); |
| 11532 | } |
| 11533 | |
| 11534 | Py_XINCREF(self->stat); |
| 11535 | return self->stat; |
| 11536 | } |
| 11537 | |
| 11538 | static PyObject * |
| 11539 | DirEntry_stat(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 11540 | { |
| 11541 | int follow_symlinks = 1; |
| 11542 | |
| 11543 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.stat", |
| 11544 | follow_symlinks_keywords, &follow_symlinks)) |
| 11545 | return NULL; |
| 11546 | |
| 11547 | return DirEntry_get_stat(self, follow_symlinks); |
| 11548 | } |
| 11549 | |
| 11550 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 11551 | static int |
| 11552 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 11553 | { |
| 11554 | PyObject *stat = NULL; |
| 11555 | PyObject *st_mode = NULL; |
| 11556 | long mode; |
| 11557 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11558 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11559 | int is_symlink; |
| 11560 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11561 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11562 | #ifdef MS_WINDOWS |
| 11563 | unsigned long dir_bits; |
| 11564 | #endif |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11565 | _Py_IDENTIFIER(st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11566 | |
| 11567 | #ifdef MS_WINDOWS |
| 11568 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 11569 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11570 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11571 | is_symlink = self->d_type == DT_LNK; |
| 11572 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 11573 | #endif |
| 11574 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11575 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11576 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11577 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11578 | stat = DirEntry_get_stat(self, follow_symlinks); |
| 11579 | if (!stat) { |
| 11580 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 11581 | /* If file doesn't exist (anymore), then return False |
| 11582 | (i.e., say it's not a file/directory) */ |
| 11583 | PyErr_Clear(); |
| 11584 | return 0; |
| 11585 | } |
| 11586 | goto error; |
| 11587 | } |
| 11588 | st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode); |
| 11589 | if (!st_mode) |
| 11590 | goto error; |
| 11591 | |
| 11592 | mode = PyLong_AsLong(st_mode); |
| 11593 | if (mode == -1 && PyErr_Occurred()) |
| 11594 | goto error; |
| 11595 | Py_CLEAR(st_mode); |
| 11596 | Py_CLEAR(stat); |
| 11597 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11598 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11599 | } |
| 11600 | else if (is_symlink) { |
| 11601 | assert(mode_bits != S_IFLNK); |
| 11602 | result = 0; |
| 11603 | } |
| 11604 | else { |
| 11605 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 11606 | #ifdef MS_WINDOWS |
| 11607 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 11608 | if (mode_bits == S_IFDIR) |
| 11609 | result = dir_bits != 0; |
| 11610 | else |
| 11611 | result = dir_bits == 0; |
| 11612 | #else /* POSIX */ |
| 11613 | if (mode_bits == S_IFDIR) |
| 11614 | result = self->d_type == DT_DIR; |
| 11615 | else |
| 11616 | result = self->d_type == DT_REG; |
| 11617 | #endif |
| 11618 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11619 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11620 | |
| 11621 | return result; |
| 11622 | |
| 11623 | error: |
| 11624 | Py_XDECREF(st_mode); |
| 11625 | Py_XDECREF(stat); |
| 11626 | return -1; |
| 11627 | } |
| 11628 | |
| 11629 | static PyObject * |
| 11630 | DirEntry_py_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 11631 | { |
| 11632 | int result; |
| 11633 | |
| 11634 | result = DirEntry_test_mode(self, follow_symlinks, mode_bits); |
| 11635 | if (result == -1) |
| 11636 | return NULL; |
| 11637 | return PyBool_FromLong(result); |
| 11638 | } |
| 11639 | |
| 11640 | static PyObject * |
| 11641 | DirEntry_is_dir(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 11642 | { |
| 11643 | int follow_symlinks = 1; |
| 11644 | |
| 11645 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_dir", |
| 11646 | follow_symlinks_keywords, &follow_symlinks)) |
| 11647 | return NULL; |
| 11648 | |
| 11649 | return DirEntry_py_test_mode(self, follow_symlinks, S_IFDIR); |
| 11650 | } |
| 11651 | |
| 11652 | static PyObject * |
| 11653 | DirEntry_is_file(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 11654 | { |
| 11655 | int follow_symlinks = 1; |
| 11656 | |
| 11657 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_file", |
| 11658 | follow_symlinks_keywords, &follow_symlinks)) |
| 11659 | return NULL; |
| 11660 | |
| 11661 | return DirEntry_py_test_mode(self, follow_symlinks, S_IFREG); |
| 11662 | } |
| 11663 | |
| 11664 | static PyObject * |
| 11665 | DirEntry_inode(DirEntry *self) |
| 11666 | { |
| 11667 | #ifdef MS_WINDOWS |
| 11668 | if (!self->got_file_index) { |
| 11669 | wchar_t *path; |
| 11670 | struct _Py_stat_struct stat; |
| 11671 | |
| 11672 | path = PyUnicode_AsUnicode(self->path); |
| 11673 | if (!path) |
| 11674 | return NULL; |
| 11675 | |
| 11676 | if (win32_lstat_w(path, &stat) != 0) { |
| 11677 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 11678 | 0, self->path); |
| 11679 | } |
| 11680 | |
| 11681 | self->win32_file_index = stat.st_ino; |
| 11682 | self->got_file_index = 1; |
| 11683 | } |
| 11684 | return PyLong_FromLongLong((PY_LONG_LONG)self->win32_file_index); |
| 11685 | #else /* POSIX */ |
| 11686 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 11687 | return PyLong_FromLongLong((PY_LONG_LONG)self->d_ino); |
| 11688 | #else |
| 11689 | return PyLong_FromLong((long)self->d_ino); |
| 11690 | #endif |
| 11691 | #endif |
| 11692 | } |
| 11693 | |
| 11694 | static PyObject * |
| 11695 | DirEntry_repr(DirEntry *self) |
| 11696 | { |
| 11697 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 11698 | } |
| 11699 | |
| 11700 | static PyMemberDef DirEntry_members[] = { |
| 11701 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 11702 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 11703 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 11704 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 11705 | {NULL} |
| 11706 | }; |
| 11707 | |
| 11708 | static PyMethodDef DirEntry_methods[] = { |
| 11709 | {"is_dir", (PyCFunction)DirEntry_is_dir, METH_VARARGS | METH_KEYWORDS, |
| 11710 | "return True if the entry is a directory; cached per entry" |
| 11711 | }, |
| 11712 | {"is_file", (PyCFunction)DirEntry_is_file, METH_VARARGS | METH_KEYWORDS, |
| 11713 | "return True if the entry is a file; cached per entry" |
| 11714 | }, |
| 11715 | {"is_symlink", (PyCFunction)DirEntry_py_is_symlink, METH_NOARGS, |
| 11716 | "return True if the entry is a symbolic link; cached per entry" |
| 11717 | }, |
| 11718 | {"stat", (PyCFunction)DirEntry_stat, METH_VARARGS | METH_KEYWORDS, |
| 11719 | "return stat_result object for the entry; cached per entry" |
| 11720 | }, |
| 11721 | {"inode", (PyCFunction)DirEntry_inode, METH_NOARGS, |
| 11722 | "return inode of the entry; cached per entry", |
| 11723 | }, |
| 11724 | {NULL} |
| 11725 | }; |
| 11726 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 11727 | static PyTypeObject DirEntryType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11728 | PyVarObject_HEAD_INIT(NULL, 0) |
| 11729 | MODNAME ".DirEntry", /* tp_name */ |
| 11730 | sizeof(DirEntry), /* tp_basicsize */ |
| 11731 | 0, /* tp_itemsize */ |
| 11732 | /* methods */ |
| 11733 | (destructor)DirEntry_dealloc, /* tp_dealloc */ |
| 11734 | 0, /* tp_print */ |
| 11735 | 0, /* tp_getattr */ |
| 11736 | 0, /* tp_setattr */ |
| 11737 | 0, /* tp_compare */ |
| 11738 | (reprfunc)DirEntry_repr, /* tp_repr */ |
| 11739 | 0, /* tp_as_number */ |
| 11740 | 0, /* tp_as_sequence */ |
| 11741 | 0, /* tp_as_mapping */ |
| 11742 | 0, /* tp_hash */ |
| 11743 | 0, /* tp_call */ |
| 11744 | 0, /* tp_str */ |
| 11745 | 0, /* tp_getattro */ |
| 11746 | 0, /* tp_setattro */ |
| 11747 | 0, /* tp_as_buffer */ |
| 11748 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 11749 | 0, /* tp_doc */ |
| 11750 | 0, /* tp_traverse */ |
| 11751 | 0, /* tp_clear */ |
| 11752 | 0, /* tp_richcompare */ |
| 11753 | 0, /* tp_weaklistoffset */ |
| 11754 | 0, /* tp_iter */ |
| 11755 | 0, /* tp_iternext */ |
| 11756 | DirEntry_methods, /* tp_methods */ |
| 11757 | DirEntry_members, /* tp_members */ |
| 11758 | }; |
| 11759 | |
| 11760 | #ifdef MS_WINDOWS |
| 11761 | |
| 11762 | static wchar_t * |
| 11763 | join_path_filenameW(wchar_t *path_wide, wchar_t* filename) |
| 11764 | { |
| 11765 | Py_ssize_t path_len; |
| 11766 | Py_ssize_t size; |
| 11767 | wchar_t *result; |
| 11768 | wchar_t ch; |
| 11769 | |
| 11770 | if (!path_wide) { /* Default arg: "." */ |
| 11771 | path_wide = L"."; |
| 11772 | path_len = 1; |
| 11773 | } |
| 11774 | else { |
| 11775 | path_len = wcslen(path_wide); |
| 11776 | } |
| 11777 | |
| 11778 | /* The +1's are for the path separator and the NUL */ |
| 11779 | size = path_len + 1 + wcslen(filename) + 1; |
| 11780 | result = PyMem_New(wchar_t, size); |
| 11781 | if (!result) { |
| 11782 | PyErr_NoMemory(); |
| 11783 | return NULL; |
| 11784 | } |
| 11785 | wcscpy(result, path_wide); |
| 11786 | if (path_len > 0) { |
| 11787 | ch = result[path_len - 1]; |
| 11788 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 11789 | result[path_len++] = SEP; |
| 11790 | wcscpy(result + path_len, filename); |
| 11791 | } |
| 11792 | return result; |
| 11793 | } |
| 11794 | |
| 11795 | static PyObject * |
| 11796 | DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW) |
| 11797 | { |
| 11798 | DirEntry *entry; |
| 11799 | BY_HANDLE_FILE_INFORMATION file_info; |
| 11800 | ULONG reparse_tag; |
| 11801 | wchar_t *joined_path; |
| 11802 | |
| 11803 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 11804 | if (!entry) |
| 11805 | return NULL; |
| 11806 | entry->name = NULL; |
| 11807 | entry->path = NULL; |
| 11808 | entry->stat = NULL; |
| 11809 | entry->lstat = NULL; |
| 11810 | entry->got_file_index = 0; |
| 11811 | |
| 11812 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 11813 | if (!entry->name) |
| 11814 | goto error; |
| 11815 | |
| 11816 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 11817 | if (!joined_path) |
| 11818 | goto error; |
| 11819 | |
| 11820 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 11821 | PyMem_Free(joined_path); |
| 11822 | if (!entry->path) |
| 11823 | goto error; |
| 11824 | |
| 11825 | find_data_to_file_info_w(dataW, &file_info, &reparse_tag); |
| 11826 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 11827 | |
| 11828 | return (PyObject *)entry; |
| 11829 | |
| 11830 | error: |
| 11831 | Py_DECREF(entry); |
| 11832 | return NULL; |
| 11833 | } |
| 11834 | |
| 11835 | #else /* POSIX */ |
| 11836 | |
| 11837 | static char * |
| 11838 | join_path_filename(char *path_narrow, char* filename, Py_ssize_t filename_len) |
| 11839 | { |
| 11840 | Py_ssize_t path_len; |
| 11841 | Py_ssize_t size; |
| 11842 | char *result; |
| 11843 | |
| 11844 | if (!path_narrow) { /* Default arg: "." */ |
| 11845 | path_narrow = "."; |
| 11846 | path_len = 1; |
| 11847 | } |
| 11848 | else { |
| 11849 | path_len = strlen(path_narrow); |
| 11850 | } |
| 11851 | |
| 11852 | if (filename_len == -1) |
| 11853 | filename_len = strlen(filename); |
| 11854 | |
| 11855 | /* The +1's are for the path separator and the NUL */ |
| 11856 | size = path_len + 1 + filename_len + 1; |
| 11857 | result = PyMem_New(char, size); |
| 11858 | if (!result) { |
| 11859 | PyErr_NoMemory(); |
| 11860 | return NULL; |
| 11861 | } |
| 11862 | strcpy(result, path_narrow); |
| 11863 | if (path_len > 0 && result[path_len - 1] != '/') |
| 11864 | result[path_len++] = '/'; |
| 11865 | strcpy(result + path_len, filename); |
| 11866 | return result; |
| 11867 | } |
| 11868 | |
| 11869 | static PyObject * |
| 11870 | DirEntry_from_posix_info(path_t *path, char *name, Py_ssize_t name_len, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11871 | ino_t d_ino |
| 11872 | #ifdef HAVE_DIRENT_D_TYPE |
| 11873 | , unsigned char d_type |
| 11874 | #endif |
| 11875 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11876 | { |
| 11877 | DirEntry *entry; |
| 11878 | char *joined_path; |
| 11879 | |
| 11880 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 11881 | if (!entry) |
| 11882 | return NULL; |
| 11883 | entry->name = NULL; |
| 11884 | entry->path = NULL; |
| 11885 | entry->stat = NULL; |
| 11886 | entry->lstat = NULL; |
| 11887 | |
| 11888 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 11889 | if (!joined_path) |
| 11890 | goto error; |
| 11891 | |
| 11892 | if (!path->narrow || !PyBytes_Check(path->object)) { |
| 11893 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
| 11894 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
| 11895 | } |
| 11896 | else { |
| 11897 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
| 11898 | entry->path = PyBytes_FromString(joined_path); |
| 11899 | } |
| 11900 | PyMem_Free(joined_path); |
| 11901 | if (!entry->name || !entry->path) |
| 11902 | goto error; |
| 11903 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11904 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11905 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11906 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11907 | entry->d_ino = d_ino; |
| 11908 | |
| 11909 | return (PyObject *)entry; |
| 11910 | |
| 11911 | error: |
| 11912 | Py_XDECREF(entry); |
| 11913 | return NULL; |
| 11914 | } |
| 11915 | |
| 11916 | #endif |
| 11917 | |
| 11918 | |
| 11919 | typedef struct { |
| 11920 | PyObject_HEAD |
| 11921 | path_t path; |
| 11922 | #ifdef MS_WINDOWS |
| 11923 | HANDLE handle; |
| 11924 | WIN32_FIND_DATAW file_data; |
| 11925 | int first_time; |
| 11926 | #else /* POSIX */ |
| 11927 | DIR *dirp; |
| 11928 | #endif |
| 11929 | } ScandirIterator; |
| 11930 | |
| 11931 | #ifdef MS_WINDOWS |
| 11932 | |
| 11933 | static void |
| 11934 | ScandirIterator_close(ScandirIterator *iterator) |
| 11935 | { |
| 11936 | if (iterator->handle == INVALID_HANDLE_VALUE) |
| 11937 | return; |
| 11938 | |
| 11939 | Py_BEGIN_ALLOW_THREADS |
| 11940 | FindClose(iterator->handle); |
| 11941 | Py_END_ALLOW_THREADS |
| 11942 | iterator->handle = INVALID_HANDLE_VALUE; |
| 11943 | } |
| 11944 | |
| 11945 | static PyObject * |
| 11946 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 11947 | { |
| 11948 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 11949 | BOOL success; |
| 11950 | |
| 11951 | /* Happens if the iterator is iterated twice */ |
| 11952 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 11953 | PyErr_SetNone(PyExc_StopIteration); |
| 11954 | return NULL; |
| 11955 | } |
| 11956 | |
| 11957 | while (1) { |
| 11958 | if (!iterator->first_time) { |
| 11959 | Py_BEGIN_ALLOW_THREADS |
| 11960 | success = FindNextFileW(iterator->handle, file_data); |
| 11961 | Py_END_ALLOW_THREADS |
| 11962 | if (!success) { |
| 11963 | if (GetLastError() != ERROR_NO_MORE_FILES) |
| 11964 | return path_error(&iterator->path); |
| 11965 | /* No more files found in directory, stop iterating */ |
| 11966 | break; |
| 11967 | } |
| 11968 | } |
| 11969 | iterator->first_time = 0; |
| 11970 | |
| 11971 | /* Skip over . and .. */ |
| 11972 | if (wcscmp(file_data->cFileName, L".") != 0 && |
| 11973 | wcscmp(file_data->cFileName, L"..") != 0) |
| 11974 | return DirEntry_from_find_data(&iterator->path, file_data); |
| 11975 | |
| 11976 | /* Loop till we get a non-dot directory or finish iterating */ |
| 11977 | } |
| 11978 | |
| 11979 | ScandirIterator_close(iterator); |
| 11980 | |
| 11981 | PyErr_SetNone(PyExc_StopIteration); |
| 11982 | return NULL; |
| 11983 | } |
| 11984 | |
| 11985 | #else /* POSIX */ |
| 11986 | |
| 11987 | static void |
| 11988 | ScandirIterator_close(ScandirIterator *iterator) |
| 11989 | { |
| 11990 | if (!iterator->dirp) |
| 11991 | return; |
| 11992 | |
| 11993 | Py_BEGIN_ALLOW_THREADS |
| 11994 | closedir(iterator->dirp); |
| 11995 | Py_END_ALLOW_THREADS |
| 11996 | iterator->dirp = NULL; |
| 11997 | return; |
| 11998 | } |
| 11999 | |
| 12000 | static PyObject * |
| 12001 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 12002 | { |
| 12003 | struct dirent *direntp; |
| 12004 | Py_ssize_t name_len; |
| 12005 | int is_dot; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12006 | |
| 12007 | /* Happens if the iterator is iterated twice */ |
| 12008 | if (!iterator->dirp) { |
| 12009 | PyErr_SetNone(PyExc_StopIteration); |
| 12010 | return NULL; |
| 12011 | } |
| 12012 | |
| 12013 | while (1) { |
| 12014 | errno = 0; |
| 12015 | Py_BEGIN_ALLOW_THREADS |
| 12016 | direntp = readdir(iterator->dirp); |
| 12017 | Py_END_ALLOW_THREADS |
| 12018 | |
| 12019 | if (!direntp) { |
| 12020 | if (errno != 0) |
| 12021 | return path_error(&iterator->path); |
| 12022 | /* No more files found in directory, stop iterating */ |
| 12023 | break; |
| 12024 | } |
| 12025 | |
| 12026 | /* Skip over . and .. */ |
| 12027 | name_len = NAMLEN(direntp); |
| 12028 | is_dot = direntp->d_name[0] == '.' && |
| 12029 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 12030 | if (!is_dot) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12031 | return DirEntry_from_posix_info(&iterator->path, direntp->d_name, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12032 | name_len, direntp->d_ino |
| 12033 | #ifdef HAVE_DIRENT_D_TYPE |
| 12034 | , direntp->d_type |
| 12035 | #endif |
| 12036 | ); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12037 | } |
| 12038 | |
| 12039 | /* Loop till we get a non-dot directory or finish iterating */ |
| 12040 | } |
| 12041 | |
| 12042 | ScandirIterator_close(iterator); |
| 12043 | |
| 12044 | PyErr_SetNone(PyExc_StopIteration); |
| 12045 | return NULL; |
| 12046 | } |
| 12047 | |
| 12048 | #endif |
| 12049 | |
| 12050 | static void |
| 12051 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 12052 | { |
| 12053 | ScandirIterator_close(iterator); |
| 12054 | Py_XDECREF(iterator->path.object); |
| 12055 | path_cleanup(&iterator->path); |
| 12056 | Py_TYPE(iterator)->tp_free((PyObject *)iterator); |
| 12057 | } |
| 12058 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 12059 | static PyTypeObject ScandirIteratorType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12060 | PyVarObject_HEAD_INIT(NULL, 0) |
| 12061 | MODNAME ".ScandirIterator", /* tp_name */ |
| 12062 | sizeof(ScandirIterator), /* tp_basicsize */ |
| 12063 | 0, /* tp_itemsize */ |
| 12064 | /* methods */ |
| 12065 | (destructor)ScandirIterator_dealloc, /* tp_dealloc */ |
| 12066 | 0, /* tp_print */ |
| 12067 | 0, /* tp_getattr */ |
| 12068 | 0, /* tp_setattr */ |
| 12069 | 0, /* tp_compare */ |
| 12070 | 0, /* tp_repr */ |
| 12071 | 0, /* tp_as_number */ |
| 12072 | 0, /* tp_as_sequence */ |
| 12073 | 0, /* tp_as_mapping */ |
| 12074 | 0, /* tp_hash */ |
| 12075 | 0, /* tp_call */ |
| 12076 | 0, /* tp_str */ |
| 12077 | 0, /* tp_getattro */ |
| 12078 | 0, /* tp_setattro */ |
| 12079 | 0, /* tp_as_buffer */ |
| 12080 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 12081 | 0, /* tp_doc */ |
| 12082 | 0, /* tp_traverse */ |
| 12083 | 0, /* tp_clear */ |
| 12084 | 0, /* tp_richcompare */ |
| 12085 | 0, /* tp_weaklistoffset */ |
| 12086 | PyObject_SelfIter, /* tp_iter */ |
| 12087 | (iternextfunc)ScandirIterator_iternext, /* tp_iternext */ |
| 12088 | }; |
| 12089 | |
| 12090 | static PyObject * |
| 12091 | posix_scandir(PyObject *self, PyObject *args, PyObject *kwargs) |
| 12092 | { |
| 12093 | ScandirIterator *iterator; |
| 12094 | static char *keywords[] = {"path", NULL}; |
| 12095 | #ifdef MS_WINDOWS |
| 12096 | wchar_t *path_strW; |
| 12097 | #else |
| 12098 | char *path; |
| 12099 | #endif |
| 12100 | |
| 12101 | iterator = PyObject_New(ScandirIterator, &ScandirIteratorType); |
| 12102 | if (!iterator) |
| 12103 | return NULL; |
| 12104 | memset(&iterator->path, 0, sizeof(path_t)); |
| 12105 | iterator->path.function_name = "scandir"; |
| 12106 | iterator->path.nullable = 1; |
| 12107 | |
| 12108 | #ifdef MS_WINDOWS |
| 12109 | iterator->handle = INVALID_HANDLE_VALUE; |
| 12110 | #else |
| 12111 | iterator->dirp = NULL; |
| 12112 | #endif |
| 12113 | |
| 12114 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:scandir", keywords, |
| 12115 | path_converter, &iterator->path)) |
| 12116 | goto error; |
| 12117 | |
| 12118 | /* path_converter doesn't keep path.object around, so do it |
| 12119 | manually for the lifetime of the iterator here (the refcount |
| 12120 | is decremented in ScandirIterator_dealloc) |
| 12121 | */ |
| 12122 | Py_XINCREF(iterator->path.object); |
| 12123 | |
| 12124 | #ifdef MS_WINDOWS |
| 12125 | if (iterator->path.narrow) { |
| 12126 | PyErr_SetString(PyExc_TypeError, |
| 12127 | "os.scandir() doesn't support bytes path on Windows, use Unicode instead"); |
| 12128 | goto error; |
| 12129 | } |
| 12130 | iterator->first_time = 1; |
| 12131 | |
| 12132 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 12133 | if (!path_strW) |
| 12134 | goto error; |
| 12135 | |
| 12136 | Py_BEGIN_ALLOW_THREADS |
| 12137 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 12138 | Py_END_ALLOW_THREADS |
| 12139 | |
| 12140 | PyMem_Free(path_strW); |
| 12141 | |
| 12142 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 12143 | path_error(&iterator->path); |
| 12144 | goto error; |
| 12145 | } |
| 12146 | #else /* POSIX */ |
| 12147 | if (iterator->path.narrow) |
| 12148 | path = iterator->path.narrow; |
| 12149 | else |
| 12150 | path = "."; |
| 12151 | |
| 12152 | errno = 0; |
| 12153 | Py_BEGIN_ALLOW_THREADS |
| 12154 | iterator->dirp = opendir(path); |
| 12155 | Py_END_ALLOW_THREADS |
| 12156 | |
| 12157 | if (!iterator->dirp) { |
| 12158 | path_error(&iterator->path); |
| 12159 | goto error; |
| 12160 | } |
| 12161 | #endif |
| 12162 | |
| 12163 | return (PyObject *)iterator; |
| 12164 | |
| 12165 | error: |
| 12166 | Py_DECREF(iterator); |
| 12167 | return NULL; |
| 12168 | } |
| 12169 | |
| 12170 | |
Serhiy Storchaka | a4c6bad | 2015-04-04 23:35:52 +0300 | [diff] [blame] | 12171 | #include "clinic/posixmodule.c.h" |
| 12172 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 12173 | /*[clinic input] |
| 12174 | dump buffer |
| 12175 | [clinic start generated code]*/ |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 12176 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/ |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 12177 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12178 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12179 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12180 | |
| 12181 | OS_STAT_METHODDEF |
| 12182 | OS_ACCESS_METHODDEF |
| 12183 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12184 | OS_CHDIR_METHODDEF |
| 12185 | OS_CHFLAGS_METHODDEF |
| 12186 | OS_CHMOD_METHODDEF |
| 12187 | OS_FCHMOD_METHODDEF |
| 12188 | OS_LCHMOD_METHODDEF |
| 12189 | OS_CHOWN_METHODDEF |
| 12190 | OS_FCHOWN_METHODDEF |
| 12191 | OS_LCHOWN_METHODDEF |
| 12192 | OS_LCHFLAGS_METHODDEF |
| 12193 | OS_CHROOT_METHODDEF |
| 12194 | OS_CTERMID_METHODDEF |
| 12195 | OS_GETCWD_METHODDEF |
| 12196 | OS_GETCWDB_METHODDEF |
| 12197 | OS_LINK_METHODDEF |
| 12198 | OS_LISTDIR_METHODDEF |
| 12199 | OS_LSTAT_METHODDEF |
| 12200 | OS_MKDIR_METHODDEF |
| 12201 | OS_NICE_METHODDEF |
| 12202 | OS_GETPRIORITY_METHODDEF |
| 12203 | OS_SETPRIORITY_METHODDEF |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12204 | #ifdef HAVE_READLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12205 | {"readlink", (PyCFunction)posix_readlink, |
| 12206 | METH_VARARGS | METH_KEYWORDS, |
| 12207 | readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 12208 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 12209 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12210 | {"readlink", (PyCFunction)win_readlink, |
| 12211 | METH_VARARGS | METH_KEYWORDS, |
| 12212 | readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 12213 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12214 | OS_RENAME_METHODDEF |
| 12215 | OS_REPLACE_METHODDEF |
| 12216 | OS_RMDIR_METHODDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12217 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12218 | OS_SYMLINK_METHODDEF |
| 12219 | OS_SYSTEM_METHODDEF |
| 12220 | OS_UMASK_METHODDEF |
| 12221 | OS_UNAME_METHODDEF |
| 12222 | OS_UNLINK_METHODDEF |
| 12223 | OS_REMOVE_METHODDEF |
| 12224 | OS_UTIME_METHODDEF |
| 12225 | OS_TIMES_METHODDEF |
| 12226 | OS__EXIT_METHODDEF |
| 12227 | OS_EXECV_METHODDEF |
| 12228 | OS_EXECVE_METHODDEF |
| 12229 | OS_SPAWNV_METHODDEF |
| 12230 | OS_SPAWNVE_METHODDEF |
| 12231 | OS_FORK1_METHODDEF |
| 12232 | OS_FORK_METHODDEF |
| 12233 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 12234 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 12235 | OS_SCHED_GETPARAM_METHODDEF |
| 12236 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 12237 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 12238 | OS_SCHED_SETPARAM_METHODDEF |
| 12239 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 12240 | OS_SCHED_YIELD_METHODDEF |
| 12241 | OS_SCHED_SETAFFINITY_METHODDEF |
| 12242 | OS_SCHED_GETAFFINITY_METHODDEF |
| 12243 | OS_OPENPTY_METHODDEF |
| 12244 | OS_FORKPTY_METHODDEF |
| 12245 | OS_GETEGID_METHODDEF |
| 12246 | OS_GETEUID_METHODDEF |
| 12247 | OS_GETGID_METHODDEF |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 12248 | #ifdef HAVE_GETGROUPLIST |
| 12249 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 12250 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12251 | OS_GETGROUPS_METHODDEF |
| 12252 | OS_GETPID_METHODDEF |
| 12253 | OS_GETPGRP_METHODDEF |
| 12254 | OS_GETPPID_METHODDEF |
| 12255 | OS_GETUID_METHODDEF |
| 12256 | OS_GETLOGIN_METHODDEF |
| 12257 | OS_KILL_METHODDEF |
| 12258 | OS_KILLPG_METHODDEF |
| 12259 | OS_PLOCK_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 12260 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12261 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 12262 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12263 | OS_SETUID_METHODDEF |
| 12264 | OS_SETEUID_METHODDEF |
| 12265 | OS_SETREUID_METHODDEF |
| 12266 | OS_SETGID_METHODDEF |
| 12267 | OS_SETEGID_METHODDEF |
| 12268 | OS_SETREGID_METHODDEF |
| 12269 | OS_SETGROUPS_METHODDEF |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 12270 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12271 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 12272 | #endif /* HAVE_INITGROUPS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12273 | OS_GETPGID_METHODDEF |
| 12274 | OS_SETPGRP_METHODDEF |
| 12275 | OS_WAIT_METHODDEF |
| 12276 | OS_WAIT3_METHODDEF |
| 12277 | OS_WAIT4_METHODDEF |
| 12278 | OS_WAITID_METHODDEF |
| 12279 | OS_WAITPID_METHODDEF |
| 12280 | OS_GETSID_METHODDEF |
| 12281 | OS_SETSID_METHODDEF |
| 12282 | OS_SETPGID_METHODDEF |
| 12283 | OS_TCGETPGRP_METHODDEF |
| 12284 | OS_TCSETPGRP_METHODDEF |
| 12285 | OS_OPEN_METHODDEF |
| 12286 | OS_CLOSE_METHODDEF |
| 12287 | OS_CLOSERANGE_METHODDEF |
| 12288 | OS_DEVICE_ENCODING_METHODDEF |
| 12289 | OS_DUP_METHODDEF |
| 12290 | OS_DUP2_METHODDEF |
| 12291 | OS_LOCKF_METHODDEF |
| 12292 | OS_LSEEK_METHODDEF |
| 12293 | OS_READ_METHODDEF |
| 12294 | OS_READV_METHODDEF |
| 12295 | OS_PREAD_METHODDEF |
| 12296 | OS_WRITE_METHODDEF |
| 12297 | OS_WRITEV_METHODDEF |
| 12298 | OS_PWRITE_METHODDEF |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12299 | #ifdef HAVE_SENDFILE |
| 12300 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 12301 | posix_sendfile__doc__}, |
| 12302 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12303 | OS_FSTAT_METHODDEF |
| 12304 | OS_ISATTY_METHODDEF |
| 12305 | OS_PIPE_METHODDEF |
| 12306 | OS_PIPE2_METHODDEF |
| 12307 | OS_MKFIFO_METHODDEF |
| 12308 | OS_MKNOD_METHODDEF |
| 12309 | OS_MAJOR_METHODDEF |
| 12310 | OS_MINOR_METHODDEF |
| 12311 | OS_MAKEDEV_METHODDEF |
| 12312 | OS_FTRUNCATE_METHODDEF |
| 12313 | OS_TRUNCATE_METHODDEF |
| 12314 | OS_POSIX_FALLOCATE_METHODDEF |
| 12315 | OS_POSIX_FADVISE_METHODDEF |
| 12316 | OS_PUTENV_METHODDEF |
| 12317 | OS_UNSETENV_METHODDEF |
| 12318 | OS_STRERROR_METHODDEF |
| 12319 | OS_FCHDIR_METHODDEF |
| 12320 | OS_FSYNC_METHODDEF |
| 12321 | OS_SYNC_METHODDEF |
| 12322 | OS_FDATASYNC_METHODDEF |
| 12323 | OS_WCOREDUMP_METHODDEF |
| 12324 | OS_WIFCONTINUED_METHODDEF |
| 12325 | OS_WIFSTOPPED_METHODDEF |
| 12326 | OS_WIFSIGNALED_METHODDEF |
| 12327 | OS_WIFEXITED_METHODDEF |
| 12328 | OS_WEXITSTATUS_METHODDEF |
| 12329 | OS_WTERMSIG_METHODDEF |
| 12330 | OS_WSTOPSIG_METHODDEF |
| 12331 | OS_FSTATVFS_METHODDEF |
| 12332 | OS_STATVFS_METHODDEF |
| 12333 | OS_CONFSTR_METHODDEF |
| 12334 | OS_SYSCONF_METHODDEF |
| 12335 | OS_FPATHCONF_METHODDEF |
| 12336 | OS_PATHCONF_METHODDEF |
| 12337 | OS_ABORT_METHODDEF |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 12338 | OS__GETFULLPATHNAME_METHODDEF |
| 12339 | OS__ISDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12340 | OS__GETDISKUSAGE_METHODDEF |
| 12341 | OS__GETFINALPATHNAME_METHODDEF |
| 12342 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 12343 | OS_GETLOADAVG_METHODDEF |
| 12344 | OS_URANDOM_METHODDEF |
| 12345 | OS_SETRESUID_METHODDEF |
| 12346 | OS_SETRESGID_METHODDEF |
| 12347 | OS_GETRESUID_METHODDEF |
| 12348 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12349 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12350 | OS_GETXATTR_METHODDEF |
| 12351 | OS_SETXATTR_METHODDEF |
| 12352 | OS_REMOVEXATTR_METHODDEF |
| 12353 | OS_LISTXATTR_METHODDEF |
| 12354 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12355 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 12356 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 12357 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12358 | OS_CPU_COUNT_METHODDEF |
| 12359 | OS_GET_INHERITABLE_METHODDEF |
| 12360 | OS_SET_INHERITABLE_METHODDEF |
| 12361 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 12362 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12363 | #ifndef MS_WINDOWS |
| 12364 | {"get_blocking", posix_get_blocking, METH_VARARGS, get_blocking__doc__}, |
| 12365 | {"set_blocking", posix_set_blocking, METH_VARARGS, set_blocking__doc__}, |
| 12366 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12367 | {"scandir", (PyCFunction)posix_scandir, |
| 12368 | METH_VARARGS | METH_KEYWORDS, |
| 12369 | posix_scandir__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12370 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 12371 | }; |
| 12372 | |
| 12373 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12374 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12375 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12376 | enable_symlink() |
| 12377 | { |
| 12378 | HANDLE tok; |
| 12379 | TOKEN_PRIVILEGES tok_priv; |
| 12380 | LUID luid; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12381 | |
| 12382 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12383 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12384 | |
| 12385 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12386 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12387 | |
| 12388 | tok_priv.PrivilegeCount = 1; |
| 12389 | tok_priv.Privileges[0].Luid = luid; |
| 12390 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 12391 | |
| 12392 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 12393 | sizeof(TOKEN_PRIVILEGES), |
| 12394 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12395 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12396 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12397 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 12398 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12399 | } |
| 12400 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 12401 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12402 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12403 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12404 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12405 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12406 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12407 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12408 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12409 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12410 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12411 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12412 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12413 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12414 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12415 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12416 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12417 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12418 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12419 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12420 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12421 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12422 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12423 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12424 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12425 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12426 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12427 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12428 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12429 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12430 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12431 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12432 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12433 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12434 | #endif |
| 12435 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12436 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12437 | #endif |
| 12438 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12439 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12440 | #endif |
| 12441 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12442 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12443 | #endif |
| 12444 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12445 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12446 | #endif |
| 12447 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12448 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12449 | #endif |
| 12450 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12451 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12452 | #endif |
| 12453 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12454 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12455 | #endif |
| 12456 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12457 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12458 | #endif |
| 12459 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12460 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12461 | #endif |
| 12462 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12463 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12464 | #endif |
| 12465 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12466 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12467 | #endif |
| 12468 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12469 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12470 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 12471 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12472 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 12473 | #endif |
| 12474 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12475 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 12476 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12477 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12478 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12479 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12480 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12481 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12482 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 12483 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12484 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 12485 | #endif |
| 12486 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12487 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 12488 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12489 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12490 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12491 | #endif |
| 12492 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12493 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12494 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 12495 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12496 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 12497 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12498 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12499 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12500 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 12501 | #ifdef O_TMPFILE |
| 12502 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 12503 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12504 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12505 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12506 | #endif |
| 12507 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12508 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12509 | #endif |
| 12510 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12511 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12512 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 12513 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12514 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 12515 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12516 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12517 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12518 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12519 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12520 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 12521 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12522 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 12523 | #endif |
| 12524 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12525 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 12526 | #endif |
| 12527 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12528 | /* MS Windows */ |
| 12529 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12530 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12531 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12532 | #endif |
| 12533 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12534 | /* Optimize for short life (keep in memory). */ |
| 12535 | /* 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] | 12536 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12537 | #endif |
| 12538 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12539 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12540 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12541 | #endif |
| 12542 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12543 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12544 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12545 | #endif |
| 12546 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12547 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12548 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12549 | #endif |
| 12550 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12551 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 12552 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12553 | /* Send a SIGIO signal whenever input or output |
| 12554 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12555 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 12556 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12557 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12558 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12559 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12560 | #endif |
| 12561 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12562 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12563 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12564 | #endif |
| 12565 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12566 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12567 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12568 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12569 | #ifdef O_NOLINKS |
| 12570 | /* 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] | 12571 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12572 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 12573 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12574 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12575 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 12576 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 12577 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12578 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12579 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12580 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12581 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12582 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12583 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12584 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12585 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12586 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12587 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12588 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12589 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12590 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12591 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12592 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12593 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12594 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12595 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12596 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12597 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12598 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12599 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12600 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12601 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12602 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12603 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12604 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12605 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12606 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12607 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12608 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12609 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12610 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12611 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12612 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12613 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12614 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12615 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12616 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12617 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12618 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12619 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12620 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12621 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12622 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12623 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12624 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12625 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12626 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12627 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12628 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12629 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12630 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 12631 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 12632 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12633 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 12634 | #endif /* ST_RDONLY */ |
| 12635 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12636 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 12637 | #endif /* ST_NOSUID */ |
| 12638 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 12639 | /* GNU extensions */ |
| 12640 | #ifdef ST_NODEV |
| 12641 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 12642 | #endif /* ST_NODEV */ |
| 12643 | #ifdef ST_NOEXEC |
| 12644 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 12645 | #endif /* ST_NOEXEC */ |
| 12646 | #ifdef ST_SYNCHRONOUS |
| 12647 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 12648 | #endif /* ST_SYNCHRONOUS */ |
| 12649 | #ifdef ST_MANDLOCK |
| 12650 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 12651 | #endif /* ST_MANDLOCK */ |
| 12652 | #ifdef ST_WRITE |
| 12653 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 12654 | #endif /* ST_WRITE */ |
| 12655 | #ifdef ST_APPEND |
| 12656 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 12657 | #endif /* ST_APPEND */ |
| 12658 | #ifdef ST_NOATIME |
| 12659 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 12660 | #endif /* ST_NOATIME */ |
| 12661 | #ifdef ST_NODIRATIME |
| 12662 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 12663 | #endif /* ST_NODIRATIME */ |
| 12664 | #ifdef ST_RELATIME |
| 12665 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 12666 | #endif /* ST_RELATIME */ |
| 12667 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12668 | /* FreeBSD sendfile() constants */ |
| 12669 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12670 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12671 | #endif |
| 12672 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12673 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12674 | #endif |
| 12675 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12676 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12677 | #endif |
| 12678 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12679 | /* constants for posix_fadvise */ |
| 12680 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12681 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12682 | #endif |
| 12683 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12684 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12685 | #endif |
| 12686 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12687 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12688 | #endif |
| 12689 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12690 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12691 | #endif |
| 12692 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12693 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12694 | #endif |
| 12695 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12696 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12697 | #endif |
| 12698 | |
| 12699 | /* constants for waitid */ |
| 12700 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12701 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 12702 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 12703 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12704 | #endif |
| 12705 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12706 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12707 | #endif |
| 12708 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12709 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12710 | #endif |
| 12711 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12712 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12713 | #endif |
| 12714 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12715 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12716 | #endif |
| 12717 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12718 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12719 | #endif |
| 12720 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12721 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12722 | #endif |
| 12723 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12724 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12725 | #endif |
| 12726 | |
| 12727 | /* constants for lockf */ |
| 12728 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12729 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12730 | #endif |
| 12731 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12732 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12733 | #endif |
| 12734 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12735 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12736 | #endif |
| 12737 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12738 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12739 | #endif |
| 12740 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 12741 | #ifdef HAVE_SPAWNV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12742 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 12743 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
| 12744 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
| 12745 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
| 12746 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 12747 | #endif |
| 12748 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12749 | #ifdef HAVE_SCHED_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12750 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
| 12751 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
| 12752 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12753 | #ifdef SCHED_SPORADIC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12754 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12755 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12756 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12757 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12758 | #endif |
| 12759 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12760 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12761 | #endif |
| 12762 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12763 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12764 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12765 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12766 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12767 | #endif |
| 12768 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12769 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12770 | #endif |
| 12771 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12772 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12773 | #endif |
| 12774 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12775 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12776 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12777 | #endif |
| 12778 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12779 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12780 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 12781 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 12782 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12783 | #endif |
| 12784 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12785 | #ifdef RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12786 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12787 | #endif |
| 12788 | #ifdef RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12789 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12790 | #endif |
| 12791 | #ifdef RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12792 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12793 | #endif |
| 12794 | #ifdef RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12795 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12796 | #endif |
| 12797 | #ifdef RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12798 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12799 | #endif |
| 12800 | #ifdef RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12801 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12802 | #endif |
| 12803 | #ifdef RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12804 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12805 | #endif |
| 12806 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12807 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12808 | } |
| 12809 | |
| 12810 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12811 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12812 | PyModuleDef_HEAD_INIT, |
| 12813 | MODNAME, |
| 12814 | posix__doc__, |
| 12815 | -1, |
| 12816 | posix_methods, |
| 12817 | NULL, |
| 12818 | NULL, |
| 12819 | NULL, |
| 12820 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12821 | }; |
| 12822 | |
| 12823 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame^] | 12824 | static const char * const have_functions[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12825 | |
| 12826 | #ifdef HAVE_FACCESSAT |
| 12827 | "HAVE_FACCESSAT", |
| 12828 | #endif |
| 12829 | |
| 12830 | #ifdef HAVE_FCHDIR |
| 12831 | "HAVE_FCHDIR", |
| 12832 | #endif |
| 12833 | |
| 12834 | #ifdef HAVE_FCHMOD |
| 12835 | "HAVE_FCHMOD", |
| 12836 | #endif |
| 12837 | |
| 12838 | #ifdef HAVE_FCHMODAT |
| 12839 | "HAVE_FCHMODAT", |
| 12840 | #endif |
| 12841 | |
| 12842 | #ifdef HAVE_FCHOWN |
| 12843 | "HAVE_FCHOWN", |
| 12844 | #endif |
| 12845 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 12846 | #ifdef HAVE_FCHOWNAT |
| 12847 | "HAVE_FCHOWNAT", |
| 12848 | #endif |
| 12849 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12850 | #ifdef HAVE_FEXECVE |
| 12851 | "HAVE_FEXECVE", |
| 12852 | #endif |
| 12853 | |
| 12854 | #ifdef HAVE_FDOPENDIR |
| 12855 | "HAVE_FDOPENDIR", |
| 12856 | #endif |
| 12857 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12858 | #ifdef HAVE_FPATHCONF |
| 12859 | "HAVE_FPATHCONF", |
| 12860 | #endif |
| 12861 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12862 | #ifdef HAVE_FSTATAT |
| 12863 | "HAVE_FSTATAT", |
| 12864 | #endif |
| 12865 | |
| 12866 | #ifdef HAVE_FSTATVFS |
| 12867 | "HAVE_FSTATVFS", |
| 12868 | #endif |
| 12869 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 12870 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12871 | "HAVE_FTRUNCATE", |
| 12872 | #endif |
| 12873 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12874 | #ifdef HAVE_FUTIMENS |
| 12875 | "HAVE_FUTIMENS", |
| 12876 | #endif |
| 12877 | |
| 12878 | #ifdef HAVE_FUTIMES |
| 12879 | "HAVE_FUTIMES", |
| 12880 | #endif |
| 12881 | |
| 12882 | #ifdef HAVE_FUTIMESAT |
| 12883 | "HAVE_FUTIMESAT", |
| 12884 | #endif |
| 12885 | |
| 12886 | #ifdef HAVE_LINKAT |
| 12887 | "HAVE_LINKAT", |
| 12888 | #endif |
| 12889 | |
| 12890 | #ifdef HAVE_LCHFLAGS |
| 12891 | "HAVE_LCHFLAGS", |
| 12892 | #endif |
| 12893 | |
| 12894 | #ifdef HAVE_LCHMOD |
| 12895 | "HAVE_LCHMOD", |
| 12896 | #endif |
| 12897 | |
| 12898 | #ifdef HAVE_LCHOWN |
| 12899 | "HAVE_LCHOWN", |
| 12900 | #endif |
| 12901 | |
| 12902 | #ifdef HAVE_LSTAT |
| 12903 | "HAVE_LSTAT", |
| 12904 | #endif |
| 12905 | |
| 12906 | #ifdef HAVE_LUTIMES |
| 12907 | "HAVE_LUTIMES", |
| 12908 | #endif |
| 12909 | |
| 12910 | #ifdef HAVE_MKDIRAT |
| 12911 | "HAVE_MKDIRAT", |
| 12912 | #endif |
| 12913 | |
| 12914 | #ifdef HAVE_MKFIFOAT |
| 12915 | "HAVE_MKFIFOAT", |
| 12916 | #endif |
| 12917 | |
| 12918 | #ifdef HAVE_MKNODAT |
| 12919 | "HAVE_MKNODAT", |
| 12920 | #endif |
| 12921 | |
| 12922 | #ifdef HAVE_OPENAT |
| 12923 | "HAVE_OPENAT", |
| 12924 | #endif |
| 12925 | |
| 12926 | #ifdef HAVE_READLINKAT |
| 12927 | "HAVE_READLINKAT", |
| 12928 | #endif |
| 12929 | |
| 12930 | #ifdef HAVE_RENAMEAT |
| 12931 | "HAVE_RENAMEAT", |
| 12932 | #endif |
| 12933 | |
| 12934 | #ifdef HAVE_SYMLINKAT |
| 12935 | "HAVE_SYMLINKAT", |
| 12936 | #endif |
| 12937 | |
| 12938 | #ifdef HAVE_UNLINKAT |
| 12939 | "HAVE_UNLINKAT", |
| 12940 | #endif |
| 12941 | |
| 12942 | #ifdef HAVE_UTIMENSAT |
| 12943 | "HAVE_UTIMENSAT", |
| 12944 | #endif |
| 12945 | |
| 12946 | #ifdef MS_WINDOWS |
| 12947 | "MS_WINDOWS", |
| 12948 | #endif |
| 12949 | |
| 12950 | NULL |
| 12951 | }; |
| 12952 | |
| 12953 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 12954 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 12955 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12956 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12957 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12958 | PyObject *list; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame^] | 12959 | const char * const *trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12960 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12961 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12962 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12963 | #endif |
| 12964 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12965 | m = PyModule_Create(&posixmodule); |
| 12966 | if (m == NULL) |
| 12967 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12968 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12969 | /* Initialize environ dictionary */ |
| 12970 | v = convertenviron(); |
| 12971 | Py_XINCREF(v); |
| 12972 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 12973 | return NULL; |
| 12974 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12975 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12976 | if (all_ins(m)) |
| 12977 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12978 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12979 | if (setup_confname_tables(m)) |
| 12980 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12981 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12982 | Py_INCREF(PyExc_OSError); |
| 12983 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 12984 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12985 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12986 | if (posix_putenv_garbage == NULL) |
| 12987 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12988 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 12989 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12990 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12991 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 12992 | waitid_result_desc.name = MODNAME ".waitid_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12993 | if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0) |
| 12994 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12995 | #endif |
| 12996 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 12997 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12998 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 12999 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 13000 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13001 | if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0) |
| 13002 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13003 | structseq_new = StatResultType.tp_new; |
| 13004 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13005 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 13006 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13007 | if (PyStructSequence_InitType2(&StatVFSResultType, |
| 13008 | &statvfs_result_desc) < 0) |
| 13009 | return NULL; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 13010 | #ifdef NEED_TICKS_PER_SECOND |
| 13011 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13012 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 13013 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13014 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 13015 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13016 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 13017 | # endif |
| 13018 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13019 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 13020 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13021 | sched_param_desc.name = MODNAME ".sched_param"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13022 | if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0) |
| 13023 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13024 | SchedParamType.tp_new = os_sched_param; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13025 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13026 | |
| 13027 | /* initialize TerminalSize_info */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13028 | if (PyStructSequence_InitType2(&TerminalSizeType, |
| 13029 | &TerminalSize_desc) < 0) |
| 13030 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13031 | |
| 13032 | /* initialize scandir types */ |
| 13033 | if (PyType_Ready(&ScandirIteratorType) < 0) |
| 13034 | return NULL; |
| 13035 | if (PyType_Ready(&DirEntryType) < 0) |
| 13036 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13037 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13038 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 13039 | Py_INCREF((PyObject*) &WaitidResultType); |
| 13040 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 13041 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13042 | Py_INCREF((PyObject*) &StatResultType); |
| 13043 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 13044 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 13045 | PyModule_AddObject(m, "statvfs_result", |
| 13046 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 13047 | |
| 13048 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13049 | Py_INCREF(&SchedParamType); |
| 13050 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 13051 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13052 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 13053 | times_result_desc.name = MODNAME ".times_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13054 | if (PyStructSequence_InitType2(&TimesResultType, ×_result_desc) < 0) |
| 13055 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 13056 | PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType); |
| 13057 | |
| 13058 | uname_result_desc.name = MODNAME ".uname_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13059 | if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0) |
| 13060 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 13061 | PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType); |
| 13062 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13063 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13064 | /* |
| 13065 | * Step 2 of weak-linking support on Mac OS X. |
| 13066 | * |
| 13067 | * The code below removes functions that are not available on the |
| 13068 | * currently active platform. |
| 13069 | * |
| 13070 | * 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] | 13071 | * 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] | 13072 | * OSX 10.4. |
| 13073 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13074 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13075 | if (fstatvfs == NULL) { |
| 13076 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 13077 | return NULL; |
| 13078 | } |
| 13079 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13080 | #endif /* HAVE_FSTATVFS */ |
| 13081 | |
| 13082 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13083 | if (statvfs == NULL) { |
| 13084 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 13085 | return NULL; |
| 13086 | } |
| 13087 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13088 | #endif /* HAVE_STATVFS */ |
| 13089 | |
| 13090 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13091 | if (lchown == NULL) { |
| 13092 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 13093 | return NULL; |
| 13094 | } |
| 13095 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13096 | #endif /* HAVE_LCHOWN */ |
| 13097 | |
| 13098 | |
| 13099 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13100 | |
Antoine Pitrou | 9d20e0e | 2012-09-12 18:01:36 +0200 | [diff] [blame] | 13101 | Py_INCREF(&TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13102 | PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); |
| 13103 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 13104 | billion = PyLong_FromLong(1000000000); |
| 13105 | if (!billion) |
| 13106 | return NULL; |
| 13107 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13108 | /* suppress "function not used" warnings */ |
| 13109 | { |
| 13110 | int ignored; |
| 13111 | fd_specified("", -1); |
| 13112 | follow_symlinks_specified("", 1); |
| 13113 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 13114 | dir_fd_converter(Py_None, &ignored); |
| 13115 | dir_fd_unavailable(Py_None, &ignored); |
| 13116 | } |
| 13117 | |
| 13118 | /* |
| 13119 | * provide list of locally available functions |
| 13120 | * so os.py can populate support_* lists |
| 13121 | */ |
| 13122 | list = PyList_New(0); |
| 13123 | if (!list) |
| 13124 | return NULL; |
| 13125 | for (trace = have_functions; *trace; trace++) { |
| 13126 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 13127 | if (!unicode) |
| 13128 | return NULL; |
| 13129 | if (PyList_Append(list, unicode)) |
| 13130 | return NULL; |
| 13131 | Py_DECREF(unicode); |
| 13132 | } |
| 13133 | PyModule_AddObject(m, "_have_functions", list); |
| 13134 | |
| 13135 | initialized = 1; |
| 13136 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13137 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 13138 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13139 | |
| 13140 | #ifdef __cplusplus |
| 13141 | } |
| 13142 | #endif |