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 |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 952 | argument_unavailable_error(const char *function_name, const char *argument_name) |
| 953 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 954 | PyErr_Format(PyExc_NotImplementedError, |
| 955 | "%s%s%s unavailable on this platform", |
| 956 | (function_name != NULL) ? function_name : "", |
| 957 | (function_name != NULL) ? ": ": "", |
| 958 | argument_name); |
| 959 | } |
| 960 | |
| 961 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 962 | dir_fd_unavailable(PyObject *o, void *p) |
| 963 | { |
| 964 | int dir_fd; |
| 965 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 966 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 967 | if (dir_fd != DEFAULT_DIR_FD) { |
| 968 | argument_unavailable_error(NULL, "dir_fd"); |
| 969 | return 0; |
| 970 | } |
| 971 | *(int *)p = dir_fd; |
| 972 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 976 | fd_specified(const char *function_name, int fd) |
| 977 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 978 | if (fd == -1) |
| 979 | return 0; |
| 980 | |
| 981 | argument_unavailable_error(function_name, "fd"); |
| 982 | return 1; |
| 983 | } |
| 984 | |
| 985 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 986 | follow_symlinks_specified(const char *function_name, int follow_symlinks) |
| 987 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 988 | if (follow_symlinks) |
| 989 | return 0; |
| 990 | |
| 991 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 992 | return 1; |
| 993 | } |
| 994 | |
| 995 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 996 | path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd) |
| 997 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 998 | if (!path->narrow && !path->wide && (dir_fd != DEFAULT_DIR_FD)) { |
| 999 | PyErr_Format(PyExc_ValueError, |
| 1000 | "%s: can't specify dir_fd without matching path", |
| 1001 | function_name); |
| 1002 | return 1; |
| 1003 | } |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
| 1007 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1008 | dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd) |
| 1009 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1010 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 1011 | PyErr_Format(PyExc_ValueError, |
| 1012 | "%s: can't specify both dir_fd and fd", |
| 1013 | function_name); |
| 1014 | return 1; |
| 1015 | } |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
| 1019 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1020 | fd_and_follow_symlinks_invalid(const char *function_name, int fd, |
| 1021 | int follow_symlinks) |
| 1022 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1023 | if ((fd > 0) && (!follow_symlinks)) { |
| 1024 | PyErr_Format(PyExc_ValueError, |
| 1025 | "%s: cannot use fd and follow_symlinks together", |
| 1026 | function_name); |
| 1027 | return 1; |
| 1028 | } |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
| 1032 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1033 | dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd, |
| 1034 | int follow_symlinks) |
| 1035 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1036 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 1037 | PyErr_Format(PyExc_ValueError, |
| 1038 | "%s: cannot use dir_fd and follow_symlinks together", |
| 1039 | function_name); |
| 1040 | return 1; |
| 1041 | } |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1045 | #ifdef MS_WINDOWS |
| 1046 | typedef PY_LONG_LONG Py_off_t; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1047 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1048 | typedef off_t Py_off_t; |
| 1049 | #endif |
| 1050 | |
| 1051 | static int |
| 1052 | Py_off_t_converter(PyObject *arg, void *addr) |
| 1053 | { |
| 1054 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1055 | *((Py_off_t *)addr) = PyLong_AsLongLong(arg); |
| 1056 | #else |
| 1057 | *((Py_off_t *)addr) = PyLong_AsLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1058 | #endif |
| 1059 | if (PyErr_Occurred()) |
| 1060 | return 0; |
| 1061 | return 1; |
| 1062 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1063 | |
| 1064 | static PyObject * |
| 1065 | PyLong_FromPy_off_t(Py_off_t offset) |
| 1066 | { |
| 1067 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1068 | return PyLong_FromLongLong(offset); |
| 1069 | #else |
| 1070 | return PyLong_FromLong(offset); |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1071 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1072 | } |
| 1073 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1074 | |
Steve Dower | d81431f | 2015-03-06 14:47:02 -0800 | [diff] [blame] | 1075 | #if defined _MSC_VER && _MSC_VER >= 1400 && _MSC_VER < 1900 |
| 1076 | /* Legacy implementation of _PyVerify_fd_dup2 while transitioning to |
| 1077 | * MSVC 14.0. This should eventually be removed. (issue23524) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1078 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1079 | #define IOINFO_L2E 5 |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1080 | #define IOINFO_ARRAYS 64 |
Steve Dower | 65e4cb1 | 2014-11-22 12:54:57 -0800 | [diff] [blame] | 1081 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1082 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1083 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 1084 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1085 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 1086 | static int |
| 1087 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 1088 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1089 | if (!_PyVerify_fd(fd1)) |
| 1090 | return 0; |
| 1091 | if (fd2 == _NO_CONSOLE_FILENO) |
| 1092 | return 0; |
| 1093 | if ((unsigned)fd2 < _NHANDLE_) |
| 1094 | return 1; |
| 1095 | else |
| 1096 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1097 | } |
| 1098 | #else |
Steve Dower | d81431f | 2015-03-06 14:47:02 -0800 | [diff] [blame] | 1099 | #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] | 1100 | #endif |
| 1101 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1102 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1103 | |
| 1104 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1105 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1106 | { |
| 1107 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1108 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 1109 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1110 | |
| 1111 | if (0 == DeviceIoControl( |
| 1112 | reparse_point_handle, |
| 1113 | FSCTL_GET_REPARSE_POINT, |
| 1114 | NULL, 0, /* in buffer */ |
| 1115 | target_buffer, sizeof(target_buffer), |
| 1116 | &n_bytes_returned, |
| 1117 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1118 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1119 | |
| 1120 | if (reparse_tag) |
| 1121 | *reparse_tag = rdb->ReparseTag; |
| 1122 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1123 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1124 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1125 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1126 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1127 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1128 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1129 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1130 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1131 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1132 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1133 | */ |
| 1134 | #include <crt_externs.h> |
| 1135 | static char **environ; |
| 1136 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1137 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1138 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1139 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1140 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1141 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1142 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1143 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1144 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1145 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1146 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1147 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1148 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1149 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1150 | d = PyDict_New(); |
| 1151 | if (d == NULL) |
| 1152 | return NULL; |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1153 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1154 | if (environ == NULL) |
| 1155 | environ = *_NSGetEnviron(); |
| 1156 | #endif |
| 1157 | #ifdef MS_WINDOWS |
| 1158 | /* _wenviron must be initialized in this way if the program is started |
| 1159 | through main() instead of wmain(). */ |
| 1160 | _wgetenv(L""); |
| 1161 | if (_wenviron == NULL) |
| 1162 | return d; |
| 1163 | /* This part ignores errors */ |
| 1164 | for (e = _wenviron; *e != NULL; e++) { |
| 1165 | PyObject *k; |
| 1166 | PyObject *v; |
| 1167 | wchar_t *p = wcschr(*e, L'='); |
| 1168 | if (p == NULL) |
| 1169 | continue; |
| 1170 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1171 | if (k == NULL) { |
| 1172 | PyErr_Clear(); |
| 1173 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1174 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1175 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1176 | if (v == NULL) { |
| 1177 | PyErr_Clear(); |
| 1178 | Py_DECREF(k); |
| 1179 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1180 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1181 | if (PyDict_GetItem(d, k) == NULL) { |
| 1182 | if (PyDict_SetItem(d, k, v) != 0) |
| 1183 | PyErr_Clear(); |
| 1184 | } |
| 1185 | Py_DECREF(k); |
| 1186 | Py_DECREF(v); |
| 1187 | } |
| 1188 | #else |
| 1189 | if (environ == NULL) |
| 1190 | return d; |
| 1191 | /* This part ignores errors */ |
| 1192 | for (e = environ; *e != NULL; e++) { |
| 1193 | PyObject *k; |
| 1194 | PyObject *v; |
| 1195 | char *p = strchr(*e, '='); |
| 1196 | if (p == NULL) |
| 1197 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1198 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1199 | if (k == NULL) { |
| 1200 | PyErr_Clear(); |
| 1201 | continue; |
| 1202 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1203 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1204 | if (v == NULL) { |
| 1205 | PyErr_Clear(); |
| 1206 | Py_DECREF(k); |
| 1207 | continue; |
| 1208 | } |
| 1209 | if (PyDict_GetItem(d, k) == NULL) { |
| 1210 | if (PyDict_SetItem(d, k, v) != 0) |
| 1211 | PyErr_Clear(); |
| 1212 | } |
| 1213 | Py_DECREF(k); |
| 1214 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1215 | } |
| 1216 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1217 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1220 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1221 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1222 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1223 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1224 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1225 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1226 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1227 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1228 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1229 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1230 | win32_error(const char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1231 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1232 | /* XXX We should pass the function name along in the future. |
| 1233 | (winreg.c also wants to pass the function name.) |
| 1234 | This would however require an additional param to the |
| 1235 | Windows error object, which is non-trivial. |
| 1236 | */ |
| 1237 | errno = GetLastError(); |
| 1238 | if (filename) |
| 1239 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1240 | else |
| 1241 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1242 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1243 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1244 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1245 | win32_error_object(const char* function, PyObject* filename) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1246 | { |
| 1247 | /* XXX - see win32_error for comments on 'function' */ |
| 1248 | errno = GetLastError(); |
| 1249 | if (filename) |
| 1250 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1251 | PyExc_OSError, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1252 | errno, |
| 1253 | filename); |
| 1254 | else |
| 1255 | return PyErr_SetFromWindowsErr(errno); |
| 1256 | } |
| 1257 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1258 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1259 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1260 | static PyObject * |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1261 | path_error(path_t *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1262 | { |
| 1263 | #ifdef MS_WINDOWS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1264 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 1265 | 0, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1266 | #else |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1267 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1268 | #endif |
| 1269 | } |
| 1270 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1271 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1272 | static PyObject * |
| 1273 | path_error2(path_t *path, path_t *path2) |
| 1274 | { |
| 1275 | #ifdef MS_WINDOWS |
| 1276 | return PyErr_SetExcFromWindowsErrWithFilenameObjects(PyExc_OSError, |
| 1277 | 0, path->object, path2->object); |
| 1278 | #else |
| 1279 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, |
| 1280 | path->object, path2->object); |
| 1281 | #endif |
| 1282 | } |
| 1283 | |
| 1284 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1285 | /* POSIX generic methods */ |
| 1286 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1287 | static int |
| 1288 | fildes_converter(PyObject *o, void *p) |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1289 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1290 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1291 | int *pointer = (int *)p; |
| 1292 | fd = PyObject_AsFileDescriptor(o); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1293 | if (fd < 0) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1294 | return 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1295 | *pointer = fd; |
| 1296 | return 1; |
| 1297 | } |
| 1298 | |
| 1299 | static PyObject * |
| 1300 | posix_fildes_fd(int fd, int (*func)(int)) |
| 1301 | { |
| 1302 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1303 | int async_err = 0; |
| 1304 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1305 | if (!_PyVerify_fd(fd)) |
| 1306 | return posix_error(); |
| 1307 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1308 | do { |
| 1309 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1310 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1311 | res = (*func)(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1312 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1313 | Py_END_ALLOW_THREADS |
| 1314 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 1315 | if (res != 0) |
| 1316 | return (!async_err) ? posix_error() : NULL; |
| 1317 | Py_RETURN_NONE; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1318 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1319 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1320 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1321 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1322 | /* This is a reimplementation of the C library's chdir function, |
| 1323 | but one that produces Win32 errors instead of DOS error codes. |
| 1324 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1325 | it also needs to set "magic" environment variables indicating |
| 1326 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1327 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1328 | win32_chdir(LPCSTR path) |
| 1329 | { |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1330 | char new_path[MAX_PATH]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1331 | int result; |
| 1332 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1333 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1334 | if(!SetCurrentDirectoryA(path)) |
| 1335 | return FALSE; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1336 | result = GetCurrentDirectoryA(Py_ARRAY_LENGTH(new_path), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1337 | if (!result) |
| 1338 | return FALSE; |
| 1339 | /* In the ANSI API, there should not be any paths longer |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1340 | than MAX_PATH-1 (not including the final null character). */ |
| 1341 | assert(result < Py_ARRAY_LENGTH(new_path)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1342 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 1343 | strncmp(new_path, "//", 2) == 0) |
| 1344 | /* UNC path, nothing to do. */ |
| 1345 | return TRUE; |
| 1346 | env[1] = new_path[0]; |
| 1347 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | /* The Unicode version differs from the ANSI version |
| 1351 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1352 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1353 | win32_wchdir(LPCWSTR path) |
| 1354 | { |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1355 | wchar_t path_buf[MAX_PATH], *new_path = path_buf; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1356 | int result; |
| 1357 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1358 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1359 | if(!SetCurrentDirectoryW(path)) |
| 1360 | return FALSE; |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1361 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1362 | if (!result) |
| 1363 | return FALSE; |
Victor Stinner | e847d71 | 2015-12-14 00:21:50 +0100 | [diff] [blame] | 1364 | if (result > Py_ARRAY_LENGTH(path_buf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1365 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1366 | if (!new_path) { |
| 1367 | SetLastError(ERROR_OUTOFMEMORY); |
| 1368 | return FALSE; |
| 1369 | } |
| 1370 | result = GetCurrentDirectoryW(result, new_path); |
| 1371 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1372 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1373 | return FALSE; |
| 1374 | } |
| 1375 | } |
| 1376 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1377 | wcsncmp(new_path, L"//", 2) == 0) |
| 1378 | /* UNC path, nothing to do. */ |
| 1379 | return TRUE; |
| 1380 | env[1] = new_path[0]; |
| 1381 | result = SetEnvironmentVariableW(env, new_path); |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1382 | if (new_path != path_buf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1383 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1384 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1385 | } |
| 1386 | #endif |
| 1387 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1388 | #ifdef MS_WINDOWS |
| 1389 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1390 | - time stamps are restricted to second resolution |
| 1391 | - file modification times suffer from forth-and-back conversions between |
| 1392 | UTC and local time |
| 1393 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1394 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1395 | #define HAVE_STAT_NSEC 1 |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1396 | #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1397 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1398 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1399 | 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] | 1400 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1401 | HANDLE hFindFile; |
| 1402 | WIN32_FIND_DATAA FileData; |
| 1403 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1404 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1405 | return FALSE; |
| 1406 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1407 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1408 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1409 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1410 | info->ftCreationTime = FileData.ftCreationTime; |
| 1411 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1412 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1413 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1414 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1415 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1416 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1417 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1418 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1421 | static void |
| 1422 | find_data_to_file_info_w(WIN32_FIND_DATAW *pFileData, |
| 1423 | BY_HANDLE_FILE_INFORMATION *info, |
| 1424 | ULONG *reparse_tag) |
| 1425 | { |
| 1426 | memset(info, 0, sizeof(*info)); |
| 1427 | info->dwFileAttributes = pFileData->dwFileAttributes; |
| 1428 | info->ftCreationTime = pFileData->ftCreationTime; |
| 1429 | info->ftLastAccessTime = pFileData->ftLastAccessTime; |
| 1430 | info->ftLastWriteTime = pFileData->ftLastWriteTime; |
| 1431 | info->nFileSizeHigh = pFileData->nFileSizeHigh; |
| 1432 | info->nFileSizeLow = pFileData->nFileSizeLow; |
| 1433 | /* info->nNumberOfLinks = 1; */ |
| 1434 | if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1435 | *reparse_tag = pFileData->dwReserved0; |
| 1436 | else |
| 1437 | *reparse_tag = 0; |
| 1438 | } |
| 1439 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1440 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1441 | 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] | 1442 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1443 | HANDLE hFindFile; |
| 1444 | WIN32_FIND_DATAW FileData; |
| 1445 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1446 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1447 | return FALSE; |
| 1448 | FindClose(hFindFile); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1449 | find_data_to_file_info_w(&FileData, info, reparse_tag); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1450 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1453 | static BOOL |
| 1454 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1455 | { |
| 1456 | int buf_size, result_length; |
| 1457 | wchar_t *buf; |
| 1458 | |
| 1459 | /* We have a good handle to the target, use it to determine |
| 1460 | the target path name (then we'll call lstat on it). */ |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1461 | buf_size = GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1462 | VOLUME_NAME_DOS); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1463 | if(!buf_size) |
| 1464 | return FALSE; |
| 1465 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 1466 | buf = PyMem_New(wchar_t, buf_size+1); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1467 | if (!buf) { |
| 1468 | SetLastError(ERROR_OUTOFMEMORY); |
| 1469 | return FALSE; |
| 1470 | } |
| 1471 | |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1472 | result_length = GetFinalPathNameByHandleW(hdl, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1473 | buf, buf_size, VOLUME_NAME_DOS); |
| 1474 | |
| 1475 | if(!result_length) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1476 | PyMem_Free(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1477 | return FALSE; |
| 1478 | } |
| 1479 | |
| 1480 | if(!CloseHandle(hdl)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1481 | PyMem_Free(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1482 | return FALSE; |
| 1483 | } |
| 1484 | |
| 1485 | buf[result_length] = 0; |
| 1486 | |
| 1487 | *target_path = buf; |
| 1488 | return TRUE; |
| 1489 | } |
| 1490 | |
| 1491 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1492 | 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] | 1493 | BOOL traverse); |
| 1494 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1495 | win32_xstat_impl(const char *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1496 | BOOL traverse) |
| 1497 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1498 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1499 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1500 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1501 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1502 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1503 | const char *dot; |
| 1504 | |
| 1505 | hFile = CreateFileA( |
| 1506 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1507 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1508 | 0, /* share mode */ |
| 1509 | NULL, /* security attributes */ |
| 1510 | OPEN_EXISTING, |
| 1511 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1512 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1513 | Because of this, calls like GetFinalPathNameByHandle will return |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 1514 | the symlink path again and not the actual final path. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1515 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1516 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1517 | NULL); |
| 1518 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1519 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1520 | /* Either the target doesn't exist, or we don't have access to |
| 1521 | get a handle to it. If the former, we need to return an error. |
| 1522 | If the latter, we can use attributes_from_dir. */ |
| 1523 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1524 | return -1; |
| 1525 | /* Could not get attributes on open file. Fall back to |
| 1526 | reading the directory. */ |
| 1527 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1528 | /* Very strange. This should not fail now */ |
| 1529 | return -1; |
| 1530 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1531 | if (traverse) { |
| 1532 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1533 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1534 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1535 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1536 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1537 | } else { |
| 1538 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1539 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1540 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1541 | } |
| 1542 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1543 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1544 | return -1; |
| 1545 | |
| 1546 | /* Close the outer open file handle now that we're about to |
| 1547 | reopen it with different flags. */ |
| 1548 | if (!CloseHandle(hFile)) |
| 1549 | return -1; |
| 1550 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1551 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1552 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1553 | the file without the reparse handling flag set. */ |
| 1554 | hFile2 = CreateFileA( |
| 1555 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1556 | NULL, OPEN_EXISTING, |
| 1557 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1558 | NULL); |
| 1559 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1560 | return -1; |
| 1561 | |
| 1562 | if (!get_target_path(hFile2, &target_path)) |
| 1563 | return -1; |
| 1564 | |
| 1565 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1566 | PyMem_Free(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1567 | return code; |
| 1568 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1569 | } else |
| 1570 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1571 | } |
Steve Dower | a2af1a5 | 2015-02-21 10:04:10 -0800 | [diff] [blame] | 1572 | _Py_attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1573 | |
| 1574 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1575 | dot = strrchr(path, '.'); |
| 1576 | if (dot) { |
| 1577 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1578 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1579 | result->st_mode |= 0111; |
| 1580 | } |
| 1581 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1585 | 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] | 1586 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1587 | { |
| 1588 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1589 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1590 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1591 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1592 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1593 | const wchar_t *dot; |
| 1594 | |
| 1595 | hFile = CreateFileW( |
| 1596 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1597 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1598 | 0, /* share mode */ |
| 1599 | NULL, /* security attributes */ |
| 1600 | OPEN_EXISTING, |
| 1601 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1602 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1603 | Because of this, calls like GetFinalPathNameByHandle will return |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 1604 | the symlink path again and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1605 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1606 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1607 | NULL); |
| 1608 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1609 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1610 | /* Either the target doesn't exist, or we don't have access to |
| 1611 | get a handle to it. If the former, we need to return an error. |
| 1612 | If the latter, we can use attributes_from_dir. */ |
| 1613 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1614 | return -1; |
| 1615 | /* Could not get attributes on open file. Fall back to |
| 1616 | reading the directory. */ |
| 1617 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1618 | /* Very strange. This should not fail now */ |
| 1619 | return -1; |
| 1620 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1621 | if (traverse) { |
| 1622 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1623 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1624 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1625 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1626 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1627 | } else { |
| 1628 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1629 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1630 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1631 | } |
| 1632 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1633 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1634 | return -1; |
| 1635 | |
| 1636 | /* Close the outer open file handle now that we're about to |
| 1637 | reopen it with different flags. */ |
| 1638 | if (!CloseHandle(hFile)) |
| 1639 | return -1; |
| 1640 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1641 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1642 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1643 | the file without the reparse handling flag set. */ |
| 1644 | hFile2 = CreateFileW( |
| 1645 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1646 | NULL, OPEN_EXISTING, |
| 1647 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1648 | NULL); |
| 1649 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1650 | return -1; |
| 1651 | |
| 1652 | if (!get_target_path(hFile2, &target_path)) |
| 1653 | return -1; |
| 1654 | |
| 1655 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1656 | PyMem_Free(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1657 | return code; |
| 1658 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1659 | } else |
| 1660 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1661 | } |
Steve Dower | a2af1a5 | 2015-02-21 10:04:10 -0800 | [diff] [blame] | 1662 | _Py_attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1663 | |
| 1664 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1665 | dot = wcsrchr(path, '.'); |
| 1666 | if (dot) { |
| 1667 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1668 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1669 | result->st_mode |= 0111; |
| 1670 | } |
| 1671 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1675 | win32_xstat(const char *path, struct _Py_stat_struct *result, BOOL traverse) |
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 | /* Protocol violation: we explicitly clear errno, instead of |
| 1678 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1679 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1680 | errno = 0; |
| 1681 | return code; |
| 1682 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1683 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1684 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1685 | 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] | 1686 | { |
| 1687 | /* Protocol violation: we explicitly clear errno, instead of |
| 1688 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1689 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1690 | errno = 0; |
| 1691 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1692 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1693 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1694 | |
| 1695 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1696 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1697 | default does not traverse symlinks and instead returns attributes for |
| 1698 | the symlink. |
| 1699 | |
| 1700 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1701 | win32_stat will first explicitly resolve the symlink target and then will |
| 1702 | call win32_lstat on that result. |
| 1703 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1704 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1705 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1706 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1707 | win32_lstat(const char* path, struct _Py_stat_struct *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1708 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1709 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1712 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1713 | 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] | 1714 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1715 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1719 | win32_stat(const char* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1720 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1721 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1724 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1725 | win32_stat_w(const wchar_t* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1726 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1727 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1730 | #endif /* MS_WINDOWS */ |
| 1731 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1732 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1733 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1734 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1735 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1736 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1737 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1738 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1739 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1740 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1741 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1742 | |
| 1743 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1744 | {"st_mode", "protection bits"}, |
| 1745 | {"st_ino", "inode"}, |
| 1746 | {"st_dev", "device"}, |
| 1747 | {"st_nlink", "number of hard links"}, |
| 1748 | {"st_uid", "user ID of owner"}, |
| 1749 | {"st_gid", "group ID of owner"}, |
| 1750 | {"st_size", "total size, in bytes"}, |
| 1751 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1752 | {NULL, "integer time of last access"}, |
| 1753 | {NULL, "integer time of last modification"}, |
| 1754 | {NULL, "integer time of last change"}, |
| 1755 | {"st_atime", "time of last access"}, |
| 1756 | {"st_mtime", "time of last modification"}, |
| 1757 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1758 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1759 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1760 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1761 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1762 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1763 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1764 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1765 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1766 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1767 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1768 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1769 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1770 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1771 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1772 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1773 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1774 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1775 | #endif |
| 1776 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1777 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1778 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1779 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1780 | {"st_file_attributes", "Windows file attribute bits"}, |
| 1781 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1782 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1783 | }; |
| 1784 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1785 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1786 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1787 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1788 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1789 | #endif |
| 1790 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1791 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1792 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1793 | #else |
| 1794 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1795 | #endif |
| 1796 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1797 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1798 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1799 | #else |
| 1800 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1801 | #endif |
| 1802 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1803 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1804 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1805 | #else |
| 1806 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1807 | #endif |
| 1808 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1809 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1810 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1811 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1812 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1813 | #endif |
| 1814 | |
| 1815 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1816 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1817 | #else |
| 1818 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1819 | #endif |
| 1820 | |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1821 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1822 | #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1) |
| 1823 | #else |
| 1824 | #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX |
| 1825 | #endif |
| 1826 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1827 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1828 | "stat_result", /* name */ |
| 1829 | stat_result__doc__, /* doc */ |
| 1830 | stat_result_fields, |
| 1831 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1832 | }; |
| 1833 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1834 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1835 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1836 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1837 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1838 | 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] | 1839 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1840 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1841 | |
| 1842 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1843 | {"f_bsize", }, |
| 1844 | {"f_frsize", }, |
| 1845 | {"f_blocks", }, |
| 1846 | {"f_bfree", }, |
| 1847 | {"f_bavail", }, |
| 1848 | {"f_files", }, |
| 1849 | {"f_ffree", }, |
| 1850 | {"f_favail", }, |
| 1851 | {"f_flag", }, |
| 1852 | {"f_namemax",}, |
| 1853 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1854 | }; |
| 1855 | |
| 1856 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1857 | "statvfs_result", /* name */ |
| 1858 | statvfs_result__doc__, /* doc */ |
| 1859 | statvfs_result_fields, |
| 1860 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1861 | }; |
| 1862 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1863 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1864 | PyDoc_STRVAR(waitid_result__doc__, |
| 1865 | "waitid_result: Result from waitid.\n\n\ |
| 1866 | This object may be accessed either as a tuple of\n\ |
| 1867 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1868 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1869 | \n\ |
| 1870 | See os.waitid for more information."); |
| 1871 | |
| 1872 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1873 | {"si_pid", }, |
| 1874 | {"si_uid", }, |
| 1875 | {"si_signo", }, |
| 1876 | {"si_status", }, |
| 1877 | {"si_code", }, |
| 1878 | {0} |
| 1879 | }; |
| 1880 | |
| 1881 | static PyStructSequence_Desc waitid_result_desc = { |
| 1882 | "waitid_result", /* name */ |
| 1883 | waitid_result__doc__, /* doc */ |
| 1884 | waitid_result_fields, |
| 1885 | 5 |
| 1886 | }; |
| 1887 | static PyTypeObject WaitidResultType; |
| 1888 | #endif |
| 1889 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1890 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1891 | static PyTypeObject StatResultType; |
| 1892 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1893 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 1894 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1895 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1896 | static newfunc structseq_new; |
| 1897 | |
| 1898 | static PyObject * |
| 1899 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1900 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1901 | PyStructSequence *result; |
| 1902 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1903 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1904 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1905 | if (!result) |
| 1906 | return NULL; |
| 1907 | /* If we have been initialized from a tuple, |
| 1908 | st_?time might be set to None. Initialize it |
| 1909 | from the int slots. */ |
| 1910 | for (i = 7; i <= 9; i++) { |
| 1911 | if (result->ob_item[i+3] == Py_None) { |
| 1912 | Py_DECREF(Py_None); |
| 1913 | Py_INCREF(result->ob_item[i]); |
| 1914 | result->ob_item[i+3] = result->ob_item[i]; |
| 1915 | } |
| 1916 | } |
| 1917 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | |
| 1921 | |
| 1922 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1923 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1924 | |
| 1925 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1926 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1927 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1928 | \n\ |
| 1929 | If value is True, future calls to stat() return floats; if it is False,\n\ |
| 1930 | future calls return ints.\n\ |
| 1931 | If value is omitted, return the current setting.\n"); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1932 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1933 | /* 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] | 1934 | static PyObject* |
| 1935 | stat_float_times(PyObject* self, PyObject *args) |
| 1936 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1937 | int newval = -1; |
| 1938 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1939 | return NULL; |
Victor Stinner | 034d0aa | 2012-06-05 01:22:15 +0200 | [diff] [blame] | 1940 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 1941 | "stat_float_times() is deprecated", |
| 1942 | 1)) |
| 1943 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1944 | if (newval == -1) |
| 1945 | /* Return old value */ |
| 1946 | return PyBool_FromLong(_stat_float_times); |
| 1947 | _stat_float_times = newval; |
| 1948 | Py_INCREF(Py_None); |
| 1949 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1950 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1951 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1952 | static PyObject *billion = NULL; |
| 1953 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1954 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1955 | 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] | 1956 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1957 | PyObject *s = _PyLong_FromTime_t(sec); |
| 1958 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 1959 | PyObject *s_in_ns = NULL; |
| 1960 | PyObject *ns_total = NULL; |
| 1961 | PyObject *float_s = NULL; |
| 1962 | |
| 1963 | if (!(s && ns_fractional)) |
| 1964 | goto exit; |
| 1965 | |
| 1966 | s_in_ns = PyNumber_Multiply(s, billion); |
| 1967 | if (!s_in_ns) |
| 1968 | goto exit; |
| 1969 | |
| 1970 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 1971 | if (!ns_total) |
| 1972 | goto exit; |
| 1973 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1974 | if (_stat_float_times) { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1975 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1976 | if (!float_s) |
| 1977 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1978 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1979 | else { |
| 1980 | float_s = s; |
| 1981 | Py_INCREF(float_s); |
| 1982 | } |
| 1983 | |
| 1984 | PyStructSequence_SET_ITEM(v, index, s); |
| 1985 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 1986 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 1987 | s = NULL; |
| 1988 | float_s = NULL; |
| 1989 | ns_total = NULL; |
| 1990 | exit: |
| 1991 | Py_XDECREF(s); |
| 1992 | Py_XDECREF(ns_fractional); |
| 1993 | Py_XDECREF(s_in_ns); |
| 1994 | Py_XDECREF(ns_total); |
| 1995 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1998 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1999 | (used by posix_stat() and posix_fstat()) */ |
| 2000 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2001 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2002 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2003 | unsigned long ansec, mnsec, cnsec; |
| 2004 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 2005 | if (v == NULL) |
| 2006 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2007 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2008 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2009 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2010 | PyStructSequence_SET_ITEM(v, 1, |
| 2011 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2012 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2013 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2014 | #endif |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2015 | #ifdef MS_WINDOWS |
| 2016 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2017 | #else |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 2018 | PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2019 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2020 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2021 | #if defined(MS_WINDOWS) |
| 2022 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2023 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2024 | #else |
| 2025 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2026 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2027 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2028 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2029 | PyStructSequence_SET_ITEM(v, 6, |
| 2030 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2031 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2032 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2033 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2034 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2035 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2036 | ansec = st->st_atim.tv_nsec; |
| 2037 | mnsec = st->st_mtim.tv_nsec; |
| 2038 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2039 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2040 | ansec = st->st_atimespec.tv_nsec; |
| 2041 | mnsec = st->st_mtimespec.tv_nsec; |
| 2042 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2043 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2044 | ansec = st->st_atime_nsec; |
| 2045 | mnsec = st->st_mtime_nsec; |
| 2046 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2047 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2048 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2049 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2050 | fill_time(v, 7, st->st_atime, ansec); |
| 2051 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2052 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2053 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2054 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2055 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2056 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2057 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2058 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2059 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2060 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2061 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2062 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2063 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2064 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2065 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2066 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2067 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2068 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2069 | #endif |
| 2070 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2071 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2072 | PyObject *val; |
| 2073 | unsigned long bsec,bnsec; |
| 2074 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2075 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2076 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2077 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2078 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2079 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2080 | if (_stat_float_times) { |
| 2081 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 2082 | } else { |
| 2083 | val = PyLong_FromLong((long)bsec); |
| 2084 | } |
| 2085 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2086 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2087 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2088 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2089 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2090 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2091 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2092 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2093 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2094 | PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX, |
| 2095 | PyLong_FromUnsignedLong(st->st_file_attributes)); |
| 2096 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2097 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2098 | if (PyErr_Occurred()) { |
| 2099 | Py_DECREF(v); |
| 2100 | return NULL; |
| 2101 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2102 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2103 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2106 | /* POSIX methods */ |
| 2107 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2108 | |
| 2109 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2110 | posix_do_stat(const char *function_name, path_t *path, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2111 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2112 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2113 | STRUCT_STAT st; |
| 2114 | int result; |
| 2115 | |
| 2116 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2117 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2118 | return NULL; |
| 2119 | #endif |
| 2120 | |
| 2121 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2122 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2123 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2124 | return NULL; |
| 2125 | |
| 2126 | Py_BEGIN_ALLOW_THREADS |
| 2127 | if (path->fd != -1) |
| 2128 | result = FSTAT(path->fd, &st); |
| 2129 | else |
| 2130 | #ifdef MS_WINDOWS |
| 2131 | if (path->wide) { |
| 2132 | if (follow_symlinks) |
| 2133 | result = win32_stat_w(path->wide, &st); |
| 2134 | else |
| 2135 | result = win32_lstat_w(path->wide, &st); |
| 2136 | } |
| 2137 | else |
| 2138 | #endif |
| 2139 | #if defined(HAVE_LSTAT) || defined(MS_WINDOWS) |
| 2140 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2141 | result = LSTAT(path->narrow, &st); |
| 2142 | else |
| 2143 | #endif |
| 2144 | #ifdef HAVE_FSTATAT |
| 2145 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2146 | result = fstatat(dir_fd, path->narrow, &st, |
| 2147 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2148 | else |
| 2149 | #endif |
| 2150 | result = STAT(path->narrow, &st); |
| 2151 | Py_END_ALLOW_THREADS |
| 2152 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2153 | if (result != 0) { |
| 2154 | return path_error(path); |
| 2155 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2156 | |
| 2157 | return _pystat_fromstructstat(&st); |
| 2158 | } |
| 2159 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2160 | /*[python input] |
| 2161 | |
| 2162 | for s in """ |
| 2163 | |
| 2164 | FACCESSAT |
| 2165 | FCHMODAT |
| 2166 | FCHOWNAT |
| 2167 | FSTATAT |
| 2168 | LINKAT |
| 2169 | MKDIRAT |
| 2170 | MKFIFOAT |
| 2171 | MKNODAT |
| 2172 | OPENAT |
| 2173 | READLINKAT |
| 2174 | SYMLINKAT |
| 2175 | UNLINKAT |
| 2176 | |
| 2177 | """.strip().split(): |
| 2178 | s = s.strip() |
| 2179 | print(""" |
| 2180 | #ifdef HAVE_{s} |
| 2181 | #define {s}_DIR_FD_CONVERTER dir_fd_converter |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2182 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2183 | #define {s}_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2184 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2185 | """.rstrip().format(s=s)) |
| 2186 | |
| 2187 | for s in """ |
| 2188 | |
| 2189 | FCHDIR |
| 2190 | FCHMOD |
| 2191 | FCHOWN |
| 2192 | FDOPENDIR |
| 2193 | FEXECVE |
| 2194 | FPATHCONF |
| 2195 | FSTATVFS |
| 2196 | FTRUNCATE |
| 2197 | |
| 2198 | """.strip().split(): |
| 2199 | s = s.strip() |
| 2200 | print(""" |
| 2201 | #ifdef HAVE_{s} |
| 2202 | #define PATH_HAVE_{s} 1 |
| 2203 | #else |
| 2204 | #define PATH_HAVE_{s} 0 |
| 2205 | #endif |
| 2206 | |
| 2207 | """.rstrip().format(s=s)) |
| 2208 | [python start generated code]*/ |
| 2209 | |
| 2210 | #ifdef HAVE_FACCESSAT |
| 2211 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter |
| 2212 | #else |
| 2213 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2214 | #endif |
| 2215 | |
| 2216 | #ifdef HAVE_FCHMODAT |
| 2217 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2218 | #else |
| 2219 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2220 | #endif |
| 2221 | |
| 2222 | #ifdef HAVE_FCHOWNAT |
| 2223 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter |
| 2224 | #else |
| 2225 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2226 | #endif |
| 2227 | |
| 2228 | #ifdef HAVE_FSTATAT |
| 2229 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter |
| 2230 | #else |
| 2231 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2232 | #endif |
| 2233 | |
| 2234 | #ifdef HAVE_LINKAT |
| 2235 | #define LINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2236 | #else |
| 2237 | #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2238 | #endif |
| 2239 | |
| 2240 | #ifdef HAVE_MKDIRAT |
| 2241 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter |
| 2242 | #else |
| 2243 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2244 | #endif |
| 2245 | |
| 2246 | #ifdef HAVE_MKFIFOAT |
| 2247 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter |
| 2248 | #else |
| 2249 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2250 | #endif |
| 2251 | |
| 2252 | #ifdef HAVE_MKNODAT |
| 2253 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2254 | #else |
| 2255 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2256 | #endif |
| 2257 | |
| 2258 | #ifdef HAVE_OPENAT |
| 2259 | #define OPENAT_DIR_FD_CONVERTER dir_fd_converter |
| 2260 | #else |
| 2261 | #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2262 | #endif |
| 2263 | |
| 2264 | #ifdef HAVE_READLINKAT |
| 2265 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2266 | #else |
| 2267 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2268 | #endif |
| 2269 | |
| 2270 | #ifdef HAVE_SYMLINKAT |
| 2271 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2272 | #else |
| 2273 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2274 | #endif |
| 2275 | |
| 2276 | #ifdef HAVE_UNLINKAT |
| 2277 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2278 | #else |
| 2279 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2280 | #endif |
| 2281 | |
| 2282 | #ifdef HAVE_FCHDIR |
| 2283 | #define PATH_HAVE_FCHDIR 1 |
| 2284 | #else |
| 2285 | #define PATH_HAVE_FCHDIR 0 |
| 2286 | #endif |
| 2287 | |
| 2288 | #ifdef HAVE_FCHMOD |
| 2289 | #define PATH_HAVE_FCHMOD 1 |
| 2290 | #else |
| 2291 | #define PATH_HAVE_FCHMOD 0 |
| 2292 | #endif |
| 2293 | |
| 2294 | #ifdef HAVE_FCHOWN |
| 2295 | #define PATH_HAVE_FCHOWN 1 |
| 2296 | #else |
| 2297 | #define PATH_HAVE_FCHOWN 0 |
| 2298 | #endif |
| 2299 | |
| 2300 | #ifdef HAVE_FDOPENDIR |
| 2301 | #define PATH_HAVE_FDOPENDIR 1 |
| 2302 | #else |
| 2303 | #define PATH_HAVE_FDOPENDIR 0 |
| 2304 | #endif |
| 2305 | |
| 2306 | #ifdef HAVE_FEXECVE |
| 2307 | #define PATH_HAVE_FEXECVE 1 |
| 2308 | #else |
| 2309 | #define PATH_HAVE_FEXECVE 0 |
| 2310 | #endif |
| 2311 | |
| 2312 | #ifdef HAVE_FPATHCONF |
| 2313 | #define PATH_HAVE_FPATHCONF 1 |
| 2314 | #else |
| 2315 | #define PATH_HAVE_FPATHCONF 0 |
| 2316 | #endif |
| 2317 | |
| 2318 | #ifdef HAVE_FSTATVFS |
| 2319 | #define PATH_HAVE_FSTATVFS 1 |
| 2320 | #else |
| 2321 | #define PATH_HAVE_FSTATVFS 0 |
| 2322 | #endif |
| 2323 | |
| 2324 | #ifdef HAVE_FTRUNCATE |
| 2325 | #define PATH_HAVE_FTRUNCATE 1 |
| 2326 | #else |
| 2327 | #define PATH_HAVE_FTRUNCATE 0 |
| 2328 | #endif |
| 2329 | /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2330 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 2331 | #ifdef MS_WINDOWS |
| 2332 | #undef PATH_HAVE_FTRUNCATE |
| 2333 | #define PATH_HAVE_FTRUNCATE 1 |
| 2334 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2335 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2336 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2337 | |
| 2338 | class path_t_converter(CConverter): |
| 2339 | |
| 2340 | type = "path_t" |
| 2341 | impl_by_reference = True |
| 2342 | parse_by_reference = True |
| 2343 | |
| 2344 | converter = 'path_converter' |
| 2345 | |
| 2346 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2347 | # right now path_t doesn't support default values. |
| 2348 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2349 | if self.default not in (unspecified, None): |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2350 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2351 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2352 | if self.c_default not in (None, 'Py_None'): |
| 2353 | 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] | 2354 | |
| 2355 | self.nullable = nullable |
| 2356 | self.allow_fd = allow_fd |
| 2357 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2358 | def pre_render(self): |
| 2359 | def strify(value): |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2360 | if isinstance(value, str): |
| 2361 | return value |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2362 | return str(int(bool(value))) |
| 2363 | |
| 2364 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2365 | self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format( |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2366 | self.function.name, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2367 | self.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2368 | strify(self.nullable), |
| 2369 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2370 | ) |
| 2371 | |
| 2372 | def cleanup(self): |
| 2373 | return "path_cleanup(&" + self.name + ");\n" |
| 2374 | |
| 2375 | |
| 2376 | class dir_fd_converter(CConverter): |
| 2377 | type = 'int' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2378 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2379 | def converter_init(self, requires=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2380 | if self.default in (unspecified, None): |
| 2381 | self.c_default = 'DEFAULT_DIR_FD' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2382 | if isinstance(requires, str): |
| 2383 | self.converter = requires.upper() + '_DIR_FD_CONVERTER' |
| 2384 | else: |
| 2385 | self.converter = 'dir_fd_converter' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2386 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2387 | class fildes_converter(CConverter): |
| 2388 | type = 'int' |
| 2389 | converter = 'fildes_converter' |
| 2390 | |
| 2391 | class uid_t_converter(CConverter): |
| 2392 | type = "uid_t" |
| 2393 | converter = '_Py_Uid_Converter' |
| 2394 | |
| 2395 | class gid_t_converter(CConverter): |
| 2396 | type = "gid_t" |
| 2397 | converter = '_Py_Gid_Converter' |
| 2398 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2399 | class dev_t_converter(CConverter): |
| 2400 | type = 'dev_t' |
| 2401 | converter = '_Py_Dev_Converter' |
| 2402 | |
| 2403 | class dev_t_return_converter(unsigned_long_return_converter): |
| 2404 | type = 'dev_t' |
| 2405 | conversion_fn = '_PyLong_FromDev' |
| 2406 | unsigned_cast = '(dev_t)' |
| 2407 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2408 | class FSConverter_converter(CConverter): |
| 2409 | type = 'PyObject *' |
| 2410 | converter = 'PyUnicode_FSConverter' |
| 2411 | def converter_init(self): |
| 2412 | if self.default is not unspecified: |
| 2413 | fail("FSConverter_converter does not support default values") |
| 2414 | self.c_default = 'NULL' |
| 2415 | |
| 2416 | def cleanup(self): |
| 2417 | return "Py_XDECREF(" + self.name + ");\n" |
| 2418 | |
| 2419 | class pid_t_converter(CConverter): |
| 2420 | type = 'pid_t' |
| 2421 | format_unit = '" _Py_PARSE_PID "' |
| 2422 | |
| 2423 | class idtype_t_converter(int_converter): |
| 2424 | type = 'idtype_t' |
| 2425 | |
| 2426 | class id_t_converter(CConverter): |
| 2427 | type = 'id_t' |
| 2428 | format_unit = '" _Py_PARSE_PID "' |
| 2429 | |
| 2430 | class Py_intptr_t_converter(CConverter): |
| 2431 | type = 'Py_intptr_t' |
| 2432 | format_unit = '" _Py_PARSE_INTPTR "' |
| 2433 | |
| 2434 | class Py_off_t_converter(CConverter): |
| 2435 | type = 'Py_off_t' |
| 2436 | converter = 'Py_off_t_converter' |
| 2437 | |
| 2438 | class Py_off_t_return_converter(long_return_converter): |
| 2439 | type = 'Py_off_t' |
| 2440 | conversion_fn = 'PyLong_FromPy_off_t' |
| 2441 | |
| 2442 | class path_confname_converter(CConverter): |
| 2443 | type="int" |
| 2444 | converter="conv_path_confname" |
| 2445 | |
| 2446 | class confstr_confname_converter(path_confname_converter): |
| 2447 | converter='conv_confstr_confname' |
| 2448 | |
| 2449 | class sysconf_confname_converter(path_confname_converter): |
| 2450 | converter="conv_sysconf_confname" |
| 2451 | |
| 2452 | class sched_param_converter(CConverter): |
| 2453 | type = 'struct sched_param' |
| 2454 | converter = 'convert_sched_param' |
| 2455 | impl_by_reference = True; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2456 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2457 | [python start generated code]*/ |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2458 | /*[python end generated code: output=da39a3ee5e6b4b0d input=affe68316f160401]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2459 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2460 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2461 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2462 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2463 | |
| 2464 | path : path_t(allow_fd=True) |
| 2465 | Path to be examined; can be string, bytes, or open-file-descriptor int. |
| 2466 | |
| 2467 | * |
| 2468 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2469 | dir_fd : dir_fd(requires='fstatat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2470 | If not None, it should be a file descriptor open to a directory, |
| 2471 | and path should be a relative string; path will then be relative to |
| 2472 | that directory. |
| 2473 | |
| 2474 | follow_symlinks: bool = True |
| 2475 | If False, and the last element of the path is a symbolic link, |
| 2476 | stat will examine the symbolic link itself instead of the file |
| 2477 | the link points to. |
| 2478 | |
| 2479 | Perform a stat system call on the given path. |
| 2480 | |
| 2481 | dir_fd and follow_symlinks may not be implemented |
| 2482 | on your platform. If they are unavailable, using them will raise a |
| 2483 | NotImplementedError. |
| 2484 | |
| 2485 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2486 | an open file descriptor. |
| 2487 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2488 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2489 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2490 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2491 | os_stat_impl(PyModuleDef *module, path_t *path, int dir_fd, |
| 2492 | int follow_symlinks) |
| 2493 | /*[clinic end generated code: output=e4f7569f95d523ca input=099d356c306fa24a]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2494 | { |
| 2495 | return posix_do_stat("stat", path, dir_fd, follow_symlinks); |
| 2496 | } |
| 2497 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2498 | |
| 2499 | /*[clinic input] |
| 2500 | os.lstat |
| 2501 | |
| 2502 | path : path_t |
| 2503 | |
| 2504 | * |
| 2505 | |
| 2506 | dir_fd : dir_fd(requires='fstatat') = None |
| 2507 | |
| 2508 | Perform a stat system call on the given path, without following symbolic links. |
| 2509 | |
| 2510 | Like stat(), but do not follow symbolic links. |
| 2511 | Equivalent to stat(path, follow_symlinks=False). |
| 2512 | [clinic start generated code]*/ |
| 2513 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2514 | static PyObject * |
| 2515 | os_lstat_impl(PyModuleDef *module, path_t *path, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2516 | /*[clinic end generated code: output=7a748e333fcb39bd input=0b7474765927b925]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2517 | { |
| 2518 | int follow_symlinks = 0; |
| 2519 | return posix_do_stat("lstat", path, dir_fd, follow_symlinks); |
| 2520 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2521 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2522 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2523 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2524 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2525 | |
| 2526 | path: path_t(allow_fd=True) |
| 2527 | Path to be tested; can be string, bytes, or open-file-descriptor int. |
| 2528 | |
| 2529 | mode: int |
| 2530 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2531 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2532 | |
| 2533 | * |
| 2534 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2535 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2536 | If not None, it should be a file descriptor open to a directory, |
| 2537 | and path should be relative; path will then be relative to that |
| 2538 | directory. |
| 2539 | |
| 2540 | effective_ids: bool = False |
| 2541 | If True, access will use the effective uid/gid instead of |
| 2542 | the real uid/gid. |
| 2543 | |
| 2544 | follow_symlinks: bool = True |
| 2545 | If False, and the last element of the path is a symbolic link, |
| 2546 | access will examine the symbolic link itself instead of the file |
| 2547 | the link points to. |
| 2548 | |
| 2549 | Use the real uid/gid to test for access to a path. |
| 2550 | |
| 2551 | {parameters} |
| 2552 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2553 | on your platform. If they are unavailable, using them will raise a |
| 2554 | NotImplementedError. |
| 2555 | |
| 2556 | Note that most operations will use the effective uid/gid, therefore this |
| 2557 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2558 | has the specified access to the path. |
| 2559 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2560 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2561 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2562 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2563 | os_access_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, |
| 2564 | int effective_ids, int follow_symlinks) |
| 2565 | /*[clinic end generated code: output=abaa53340210088d input=b75a756797af45ec]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2566 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2567 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2568 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2569 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2570 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2571 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2572 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2573 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2574 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2575 | #ifndef HAVE_FACCESSAT |
| 2576 | if (follow_symlinks_specified("access", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2577 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2578 | |
| 2579 | if (effective_ids) { |
| 2580 | argument_unavailable_error("access", "effective_ids"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2581 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2582 | } |
| 2583 | #endif |
| 2584 | |
| 2585 | #ifdef MS_WINDOWS |
| 2586 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | ebe83f9 | 2013-10-19 22:36:17 +0200 | [diff] [blame] | 2587 | if (path->wide != NULL) |
| 2588 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2589 | else |
Christian Heimes | ebe83f9 | 2013-10-19 22:36:17 +0200 | [diff] [blame] | 2590 | attr = GetFileAttributesA(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2591 | Py_END_ALLOW_THREADS |
| 2592 | |
| 2593 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2594 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2595 | * * we didn't get a -1, and |
| 2596 | * * write access wasn't requested, |
| 2597 | * * or the file isn't read-only, |
| 2598 | * * or it's a directory. |
| 2599 | * (Directories cannot be read-only on Windows.) |
| 2600 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2601 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2602 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2603 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2604 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2605 | #else |
| 2606 | |
| 2607 | Py_BEGIN_ALLOW_THREADS |
| 2608 | #ifdef HAVE_FACCESSAT |
| 2609 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2610 | effective_ids || |
| 2611 | !follow_symlinks) { |
| 2612 | int flags = 0; |
| 2613 | if (!follow_symlinks) |
| 2614 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2615 | if (effective_ids) |
| 2616 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2617 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2618 | } |
| 2619 | else |
| 2620 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2621 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2622 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2623 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2624 | #endif |
| 2625 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2626 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2627 | } |
| 2628 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2629 | #ifndef F_OK |
| 2630 | #define F_OK 0 |
| 2631 | #endif |
| 2632 | #ifndef R_OK |
| 2633 | #define R_OK 4 |
| 2634 | #endif |
| 2635 | #ifndef W_OK |
| 2636 | #define W_OK 2 |
| 2637 | #endif |
| 2638 | #ifndef X_OK |
| 2639 | #define X_OK 1 |
| 2640 | #endif |
| 2641 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2642 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2643 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2644 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2645 | os.ttyname -> DecodeFSDefault |
| 2646 | |
| 2647 | fd: int |
| 2648 | Integer file descriptor handle. |
| 2649 | |
| 2650 | / |
| 2651 | |
| 2652 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2653 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2654 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2655 | static char * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2656 | os_ttyname_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2657 | /*[clinic end generated code: output=03ad3d5ccaef75c3 input=5f72ca83e76b3b45]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2658 | { |
| 2659 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2660 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2661 | ret = ttyname(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2662 | if (ret == NULL) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2663 | posix_error(); |
| 2664 | return ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2665 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2666 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2667 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2668 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2669 | /*[clinic input] |
| 2670 | os.ctermid |
| 2671 | |
| 2672 | Return the name of the controlling terminal for this process. |
| 2673 | [clinic start generated code]*/ |
| 2674 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2675 | static PyObject * |
| 2676 | os_ctermid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2677 | /*[clinic end generated code: output=1b73788201e0aebd input=3b87fdd52556382d]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2678 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2679 | char *ret; |
| 2680 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2681 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2682 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2683 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2684 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2685 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2686 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2687 | if (ret == NULL) |
| 2688 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2689 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2690 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2691 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2692 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2693 | |
| 2694 | /*[clinic input] |
| 2695 | os.chdir |
| 2696 | |
| 2697 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') |
| 2698 | |
| 2699 | Change the current working directory to the specified path. |
| 2700 | |
| 2701 | path may always be specified as a string. |
| 2702 | On some platforms, path may also be specified as an open file descriptor. |
| 2703 | If this functionality is unavailable, using it raises an exception. |
| 2704 | [clinic start generated code]*/ |
| 2705 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2706 | static PyObject * |
| 2707 | os_chdir_impl(PyModuleDef *module, path_t *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2708 | /*[clinic end generated code: output=7358e3a20fb5aa93 input=1a4a15b4d12cb15d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2709 | { |
| 2710 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2711 | |
| 2712 | Py_BEGIN_ALLOW_THREADS |
| 2713 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2714 | if (path->wide) |
| 2715 | result = win32_wchdir(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2716 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2717 | result = win32_chdir(path->narrow); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 2718 | result = !result; /* on unix, success = 0, on windows, success = !0 */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2719 | #else |
| 2720 | #ifdef HAVE_FCHDIR |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2721 | if (path->fd != -1) |
| 2722 | result = fchdir(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2723 | else |
| 2724 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2725 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2726 | #endif |
| 2727 | Py_END_ALLOW_THREADS |
| 2728 | |
| 2729 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2730 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2731 | } |
| 2732 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2733 | Py_RETURN_NONE; |
| 2734 | } |
| 2735 | |
| 2736 | |
| 2737 | #ifdef HAVE_FCHDIR |
| 2738 | /*[clinic input] |
| 2739 | os.fchdir |
| 2740 | |
| 2741 | fd: fildes |
| 2742 | |
| 2743 | Change to the directory of the given file descriptor. |
| 2744 | |
| 2745 | fd must be opened on a directory, not a file. |
| 2746 | Equivalent to os.chdir(fd). |
| 2747 | |
| 2748 | [clinic start generated code]*/ |
| 2749 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2750 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2751 | os_fchdir_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2752 | /*[clinic end generated code: output=361d30df6b2d3418 input=18e816479a2fa985]*/ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2753 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2754 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2755 | } |
| 2756 | #endif /* HAVE_FCHDIR */ |
| 2757 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2758 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2759 | /*[clinic input] |
| 2760 | os.chmod |
| 2761 | |
| 2762 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
| 2763 | Path to be modified. May always be specified as a str or bytes. |
| 2764 | On some platforms, path may also be specified as an open file descriptor. |
| 2765 | If this functionality is unavailable, using it raises an exception. |
| 2766 | |
| 2767 | mode: int |
| 2768 | Operating-system mode bitfield. |
| 2769 | |
| 2770 | * |
| 2771 | |
| 2772 | dir_fd : dir_fd(requires='fchmodat') = None |
| 2773 | If not None, it should be a file descriptor open to a directory, |
| 2774 | and path should be relative; path will then be relative to that |
| 2775 | directory. |
| 2776 | |
| 2777 | follow_symlinks: bool = True |
| 2778 | If False, and the last element of the path is a symbolic link, |
| 2779 | chmod will modify the symbolic link itself instead of the file |
| 2780 | the link points to. |
| 2781 | |
| 2782 | Change the access permissions of a file. |
| 2783 | |
| 2784 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 2785 | an open file descriptor. |
| 2786 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 2787 | If they are unavailable, using them will raise a NotImplementedError. |
| 2788 | |
| 2789 | [clinic start generated code]*/ |
| 2790 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2791 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2792 | os_chmod_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, |
| 2793 | int follow_symlinks) |
| 2794 | /*[clinic end generated code: output=05e7f73b1a843ba2 input=7f1618e5e15cc196]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2795 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2796 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2797 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2798 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2799 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2800 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2801 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2802 | #ifdef HAVE_FCHMODAT |
| 2803 | int fchmodat_nofollow_unsupported = 0; |
| 2804 | #endif |
| 2805 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2806 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2807 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2808 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2809 | #endif |
| 2810 | |
| 2811 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2812 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2813 | if (path->wide) |
| 2814 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2815 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2816 | attr = GetFileAttributesA(path->narrow); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 2817 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2818 | result = 0; |
| 2819 | else { |
| 2820 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2821 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2822 | else |
| 2823 | attr |= FILE_ATTRIBUTE_READONLY; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2824 | if (path->wide) |
| 2825 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2826 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2827 | result = SetFileAttributesA(path->narrow, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2828 | } |
| 2829 | Py_END_ALLOW_THREADS |
| 2830 | |
| 2831 | if (!result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2832 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2833 | } |
| 2834 | #else /* MS_WINDOWS */ |
| 2835 | Py_BEGIN_ALLOW_THREADS |
| 2836 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2837 | if (path->fd != -1) |
| 2838 | result = fchmod(path->fd, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2839 | else |
| 2840 | #endif |
| 2841 | #ifdef HAVE_LCHMOD |
| 2842 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2843 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2844 | else |
| 2845 | #endif |
| 2846 | #ifdef HAVE_FCHMODAT |
| 2847 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2848 | /* |
| 2849 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2850 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2851 | * and then says it isn't implemented yet. |
| 2852 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2853 | * |
| 2854 | * Once it is supported, os.chmod will automatically |
| 2855 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2856 | * Until then, we need to be careful what exception we raise. |
| 2857 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2858 | result = fchmodat(dir_fd, path->narrow, mode, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2859 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2860 | /* |
| 2861 | * But wait! We can't throw the exception without allowing threads, |
| 2862 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2863 | */ |
| 2864 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2865 | result && |
| 2866 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2867 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2868 | } |
| 2869 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2870 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2871 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2872 | Py_END_ALLOW_THREADS |
| 2873 | |
| 2874 | if (result) { |
| 2875 | #ifdef HAVE_FCHMODAT |
| 2876 | if (fchmodat_nofollow_unsupported) { |
| 2877 | if (dir_fd != DEFAULT_DIR_FD) |
| 2878 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 2879 | dir_fd, follow_symlinks); |
| 2880 | else |
| 2881 | follow_symlinks_specified("chmod", follow_symlinks); |
| 2882 | } |
| 2883 | else |
| 2884 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2885 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2886 | } |
| 2887 | #endif |
| 2888 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2889 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2890 | } |
| 2891 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2892 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2893 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2894 | /*[clinic input] |
| 2895 | os.fchmod |
| 2896 | |
| 2897 | fd: int |
| 2898 | mode: int |
| 2899 | |
| 2900 | Change the access permissions of the file given by file descriptor fd. |
| 2901 | |
| 2902 | Equivalent to os.chmod(fd, mode). |
| 2903 | [clinic start generated code]*/ |
| 2904 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2905 | static PyObject * |
| 2906 | os_fchmod_impl(PyModuleDef *module, int fd, int mode) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2907 | /*[clinic end generated code: output=2ee31ca226d1ed33 input=8ab11975ca01ee5b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2908 | { |
| 2909 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2910 | int async_err = 0; |
| 2911 | |
| 2912 | do { |
| 2913 | Py_BEGIN_ALLOW_THREADS |
| 2914 | res = fchmod(fd, mode); |
| 2915 | Py_END_ALLOW_THREADS |
| 2916 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 2917 | if (res != 0) |
| 2918 | return (!async_err) ? posix_error() : NULL; |
| 2919 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2920 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2921 | } |
| 2922 | #endif /* HAVE_FCHMOD */ |
| 2923 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2924 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2925 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2926 | /*[clinic input] |
| 2927 | os.lchmod |
| 2928 | |
| 2929 | path: path_t |
| 2930 | mode: int |
| 2931 | |
| 2932 | Change the access permissions of a file, without following symbolic links. |
| 2933 | |
| 2934 | If path is a symlink, this affects the link itself rather than the target. |
| 2935 | Equivalent to chmod(path, mode, follow_symlinks=False)." |
| 2936 | [clinic start generated code]*/ |
| 2937 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2938 | static PyObject * |
| 2939 | os_lchmod_impl(PyModuleDef *module, path_t *path, int mode) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2940 | /*[clinic end generated code: output=7c0cc46588d89e46 input=90c5663c7465d24f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2941 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2942 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2943 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 2944 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2945 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2946 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2947 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2948 | return NULL; |
| 2949 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2950 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2951 | } |
| 2952 | #endif /* HAVE_LCHMOD */ |
| 2953 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2954 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2955 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2956 | /*[clinic input] |
| 2957 | os.chflags |
| 2958 | |
| 2959 | path: path_t |
| 2960 | flags: unsigned_long(bitwise=True) |
| 2961 | follow_symlinks: bool=True |
| 2962 | |
| 2963 | Set file flags. |
| 2964 | |
| 2965 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 2966 | link, chflags will change flags on the symbolic link itself instead of the |
| 2967 | file the link points to. |
| 2968 | follow_symlinks may not be implemented on your platform. If it is |
| 2969 | unavailable, using it will raise a NotImplementedError. |
| 2970 | |
| 2971 | [clinic start generated code]*/ |
| 2972 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2973 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2974 | os_chflags_impl(PyModuleDef *module, path_t *path, unsigned long flags, |
| 2975 | int follow_symlinks) |
| 2976 | /*[clinic end generated code: output=ff2d6e73534a95b9 input=0327e29feb876236]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2977 | { |
| 2978 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2979 | |
| 2980 | #ifndef HAVE_LCHFLAGS |
| 2981 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2982 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2983 | #endif |
| 2984 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2985 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2986 | #ifdef HAVE_LCHFLAGS |
| 2987 | if (!follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2988 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2989 | else |
| 2990 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2991 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2992 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2993 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2994 | if (result) |
| 2995 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2996 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2997 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2998 | } |
| 2999 | #endif /* HAVE_CHFLAGS */ |
| 3000 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3001 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3002 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3003 | /*[clinic input] |
| 3004 | os.lchflags |
| 3005 | |
| 3006 | path: path_t |
| 3007 | flags: unsigned_long(bitwise=True) |
| 3008 | |
| 3009 | Set file flags. |
| 3010 | |
| 3011 | This function will not follow symbolic links. |
| 3012 | Equivalent to chflags(path, flags, follow_symlinks=False). |
| 3013 | [clinic start generated code]*/ |
| 3014 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3015 | static PyObject * |
| 3016 | os_lchflags_impl(PyModuleDef *module, path_t *path, unsigned long flags) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3017 | /*[clinic end generated code: output=6741322fb949661b input=f9f82ea8b585ca9d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3018 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3019 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3020 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3021 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3022 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3023 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3024 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3025 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3026 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3027 | } |
| 3028 | #endif /* HAVE_LCHFLAGS */ |
| 3029 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3030 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3031 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3032 | /*[clinic input] |
| 3033 | os.chroot |
| 3034 | path: path_t |
| 3035 | |
| 3036 | Change root directory to path. |
| 3037 | |
| 3038 | [clinic start generated code]*/ |
| 3039 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3040 | static PyObject * |
| 3041 | os_chroot_impl(PyModuleDef *module, path_t *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3042 | /*[clinic end generated code: output=b6dbfabe74ecaa9d input=14822965652c3dc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3043 | { |
| 3044 | int res; |
| 3045 | Py_BEGIN_ALLOW_THREADS |
| 3046 | res = chroot(path->narrow); |
| 3047 | Py_END_ALLOW_THREADS |
| 3048 | if (res < 0) |
| 3049 | return path_error(path); |
| 3050 | Py_RETURN_NONE; |
| 3051 | } |
| 3052 | #endif /* HAVE_CHROOT */ |
| 3053 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3054 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3055 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3056 | /*[clinic input] |
| 3057 | os.fsync |
| 3058 | |
| 3059 | fd: fildes |
| 3060 | |
| 3061 | Force write of fd to disk. |
| 3062 | [clinic start generated code]*/ |
| 3063 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3064 | static PyObject * |
| 3065 | os_fsync_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3066 | /*[clinic end generated code: output=83a350851064aea7 input=21c3645c056967f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3067 | { |
| 3068 | return posix_fildes_fd(fd, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3069 | } |
| 3070 | #endif /* HAVE_FSYNC */ |
| 3071 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3072 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3073 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3074 | /*[clinic input] |
| 3075 | os.sync |
| 3076 | |
| 3077 | Force write of everything to disk. |
| 3078 | [clinic start generated code]*/ |
| 3079 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3080 | static PyObject * |
| 3081 | os_sync_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3082 | /*[clinic end generated code: output=ba524f656c201c40 input=84749fe5e9b404ff]*/ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3083 | { |
| 3084 | Py_BEGIN_ALLOW_THREADS |
| 3085 | sync(); |
| 3086 | Py_END_ALLOW_THREADS |
| 3087 | Py_RETURN_NONE; |
| 3088 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3089 | #endif /* HAVE_SYNC */ |
| 3090 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3091 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3092 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3093 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3094 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3095 | #endif |
| 3096 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3097 | /*[clinic input] |
| 3098 | os.fdatasync |
| 3099 | |
| 3100 | fd: fildes |
| 3101 | |
| 3102 | Force write of fd to disk without forcing update of metadata. |
| 3103 | [clinic start generated code]*/ |
| 3104 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3105 | static PyObject * |
| 3106 | os_fdatasync_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3107 | /*[clinic end generated code: output=e0f04a3aff515b75 input=bc74791ee54dd291]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3108 | { |
| 3109 | return posix_fildes_fd(fd, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3110 | } |
| 3111 | #endif /* HAVE_FDATASYNC */ |
| 3112 | |
| 3113 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3114 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3115 | /*[clinic input] |
| 3116 | os.chown |
| 3117 | |
| 3118 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') |
| 3119 | Path to be examined; can be string, bytes, or open-file-descriptor int. |
| 3120 | |
| 3121 | uid: uid_t |
| 3122 | |
| 3123 | gid: gid_t |
| 3124 | |
| 3125 | * |
| 3126 | |
| 3127 | dir_fd : dir_fd(requires='fchownat') = None |
| 3128 | If not None, it should be a file descriptor open to a directory, |
| 3129 | and path should be relative; path will then be relative to that |
| 3130 | directory. |
| 3131 | |
| 3132 | follow_symlinks: bool = True |
| 3133 | If False, and the last element of the path is a symbolic link, |
| 3134 | stat will examine the symbolic link itself instead of the file |
| 3135 | the link points to. |
| 3136 | |
| 3137 | Change the owner and group id of path to the numeric uid and gid.\ |
| 3138 | |
| 3139 | path may always be specified as a string. |
| 3140 | On some platforms, path may also be specified as an open file descriptor. |
| 3141 | If this functionality is unavailable, using it raises an exception. |
| 3142 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3143 | and path should be relative; path will then be relative to that directory. |
| 3144 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3145 | link, chown will modify the symbolic link itself instead of the file the |
| 3146 | link points to. |
| 3147 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3148 | an open file descriptor. |
| 3149 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3150 | If they are unavailable, using them will raise a NotImplementedError. |
| 3151 | |
| 3152 | [clinic start generated code]*/ |
| 3153 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3154 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3155 | os_chown_impl(PyModuleDef *module, path_t *path, uid_t uid, gid_t gid, |
| 3156 | int dir_fd, int follow_symlinks) |
| 3157 | /*[clinic end generated code: output=e0a4559f394dbd91 input=a61cc35574814d5d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3158 | { |
| 3159 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3160 | |
| 3161 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3162 | if (follow_symlinks_specified("chown", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3163 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3164 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3165 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
| 3166 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) |
| 3167 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3168 | |
| 3169 | #ifdef __APPLE__ |
| 3170 | /* |
| 3171 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3172 | * (But we still have an lchown symbol because of weak-linking.) |
| 3173 | * It doesn't have fchownat either. So there's no possibility |
| 3174 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3175 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3176 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3177 | follow_symlinks_specified("chown", follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3178 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3179 | } |
| 3180 | #endif |
| 3181 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3182 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3183 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3184 | if (path->fd != -1) |
| 3185 | result = fchown(path->fd, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3186 | else |
| 3187 | #endif |
| 3188 | #ifdef HAVE_LCHOWN |
| 3189 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3190 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3191 | else |
| 3192 | #endif |
| 3193 | #ifdef HAVE_FCHOWNAT |
| 3194 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3195 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3196 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3197 | else |
| 3198 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3199 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3200 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3201 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3202 | if (result) |
| 3203 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3204 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3205 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3206 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3207 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3208 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3209 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3210 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3211 | /*[clinic input] |
| 3212 | os.fchown |
| 3213 | |
| 3214 | fd: int |
| 3215 | uid: uid_t |
| 3216 | gid: gid_t |
| 3217 | |
| 3218 | Change the owner and group id of the file specified by file descriptor. |
| 3219 | |
| 3220 | Equivalent to os.chown(fd, uid, gid). |
| 3221 | |
| 3222 | [clinic start generated code]*/ |
| 3223 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3224 | static PyObject * |
| 3225 | 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] | 3226 | /*[clinic end generated code: output=7545abf8f6086d76 input=3af544ba1b13a0d7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3227 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3228 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3229 | int async_err = 0; |
| 3230 | |
| 3231 | do { |
| 3232 | Py_BEGIN_ALLOW_THREADS |
| 3233 | res = fchown(fd, uid, gid); |
| 3234 | Py_END_ALLOW_THREADS |
| 3235 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3236 | if (res != 0) |
| 3237 | return (!async_err) ? posix_error() : NULL; |
| 3238 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3239 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3240 | } |
| 3241 | #endif /* HAVE_FCHOWN */ |
| 3242 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3243 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3244 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3245 | /*[clinic input] |
| 3246 | os.lchown |
| 3247 | |
| 3248 | path : path_t |
| 3249 | uid: uid_t |
| 3250 | gid: gid_t |
| 3251 | |
| 3252 | Change the owner and group id of path to the numeric uid and gid. |
| 3253 | |
| 3254 | This function will not follow symbolic links. |
| 3255 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). |
| 3256 | [clinic start generated code]*/ |
| 3257 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3258 | static PyObject * |
| 3259 | 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] | 3260 | /*[clinic end generated code: output=bb0d2da1579ac275 input=b1c6014d563a7161]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3261 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3262 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3263 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3264 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3265 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3266 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3267 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3268 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3269 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3270 | } |
| 3271 | #endif /* HAVE_LCHOWN */ |
| 3272 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3273 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3274 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3275 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3276 | { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3277 | char *buf, *tmpbuf; |
| 3278 | char *cwd; |
| 3279 | const size_t chunk = 1024; |
| 3280 | size_t buflen = 0; |
| 3281 | PyObject *obj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3282 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3283 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3284 | if (!use_bytes) { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3285 | wchar_t wbuf[MAXPATHLEN]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3286 | wchar_t *wbuf2 = wbuf; |
| 3287 | PyObject *resobj; |
| 3288 | DWORD len; |
| 3289 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3290 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3291 | /* If the buffer is large enough, len does not include the |
| 3292 | terminating \0. If the buffer is too small, len includes |
| 3293 | the space needed for the terminator. */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3294 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3295 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3296 | if (wbuf2) |
| 3297 | len = GetCurrentDirectoryW(len, wbuf2); |
| 3298 | } |
| 3299 | Py_END_ALLOW_THREADS |
| 3300 | if (!wbuf2) { |
| 3301 | PyErr_NoMemory(); |
| 3302 | return NULL; |
| 3303 | } |
| 3304 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3305 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3306 | PyMem_RawFree(wbuf2); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3307 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3308 | } |
| 3309 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3310 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3311 | PyMem_RawFree(wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3312 | return resobj; |
| 3313 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3314 | |
| 3315 | if (win32_warn_bytes_api()) |
| 3316 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3317 | #endif |
| 3318 | |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3319 | buf = cwd = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3320 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3321 | do { |
| 3322 | buflen += chunk; |
| 3323 | tmpbuf = PyMem_RawRealloc(buf, buflen); |
| 3324 | if (tmpbuf == NULL) |
| 3325 | break; |
| 3326 | |
| 3327 | buf = tmpbuf; |
| 3328 | cwd = getcwd(buf, buflen); |
| 3329 | } while (cwd == NULL && errno == ERANGE); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3330 | Py_END_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3331 | |
| 3332 | if (cwd == NULL) { |
| 3333 | PyMem_RawFree(buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3334 | return posix_error(); |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3335 | } |
| 3336 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3337 | if (use_bytes) |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3338 | obj = PyBytes_FromStringAndSize(buf, strlen(buf)); |
| 3339 | else |
| 3340 | obj = PyUnicode_DecodeFSDefault(buf); |
| 3341 | PyMem_RawFree(buf); |
| 3342 | |
| 3343 | return obj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3344 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3345 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3346 | |
| 3347 | /*[clinic input] |
| 3348 | os.getcwd |
| 3349 | |
| 3350 | Return a unicode string representing the current working directory. |
| 3351 | [clinic start generated code]*/ |
| 3352 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3353 | static PyObject * |
| 3354 | os_getcwd_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3355 | /*[clinic end generated code: output=efe3a8c0121525ea input=f069211bb70e3d39]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3356 | { |
| 3357 | return posix_getcwd(0); |
| 3358 | } |
| 3359 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3360 | |
| 3361 | /*[clinic input] |
| 3362 | os.getcwdb |
| 3363 | |
| 3364 | Return a bytes string representing the current working directory. |
| 3365 | [clinic start generated code]*/ |
| 3366 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3367 | static PyObject * |
| 3368 | os_getcwdb_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3369 | /*[clinic end generated code: output=7fce42ee4b2a296a input=f6f6a378dad3d9cb]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3370 | { |
| 3371 | return posix_getcwd(1); |
| 3372 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3373 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3374 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3375 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3376 | #define HAVE_LINK 1 |
| 3377 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3378 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3379 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3380 | /*[clinic input] |
| 3381 | |
| 3382 | os.link |
| 3383 | |
| 3384 | src : path_t |
| 3385 | dst : path_t |
| 3386 | * |
| 3387 | src_dir_fd : dir_fd = None |
| 3388 | dst_dir_fd : dir_fd = None |
| 3389 | follow_symlinks: bool = True |
| 3390 | |
| 3391 | Create a hard link to a file. |
| 3392 | |
| 3393 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 3394 | descriptor open to a directory, and the respective path string (src or dst) |
| 3395 | should be relative; the path will then be relative to that directory. |
| 3396 | If follow_symlinks is False, and the last element of src is a symbolic |
| 3397 | link, link will create a link to the symbolic link itself instead of the |
| 3398 | file the link points to. |
| 3399 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 3400 | platform. If they are unavailable, using them will raise a |
| 3401 | NotImplementedError. |
| 3402 | [clinic start generated code]*/ |
| 3403 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3404 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3405 | os_link_impl(PyModuleDef *module, path_t *src, path_t *dst, int src_dir_fd, |
| 3406 | int dst_dir_fd, int follow_symlinks) |
| 3407 | /*[clinic end generated code: output=f47a7e88f7b391b6 input=b0095ebbcbaa7e04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3408 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3409 | #ifdef MS_WINDOWS |
| 3410 | BOOL result; |
| 3411 | #else |
| 3412 | int result; |
| 3413 | #endif |
| 3414 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3415 | #ifndef HAVE_LINKAT |
| 3416 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3417 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
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 | } |
| 3420 | #endif |
| 3421 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3422 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3423 | PyErr_SetString(PyExc_NotImplementedError, |
| 3424 | "link: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3425 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3426 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3427 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3428 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3429 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3430 | if (src->wide) |
| 3431 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3432 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3433 | result = CreateHardLinkA(dst->narrow, src->narrow, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3434 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3435 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3436 | if (!result) |
| 3437 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3438 | #else |
| 3439 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3440 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3441 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3442 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3443 | (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3444 | result = linkat(src_dir_fd, src->narrow, |
| 3445 | dst_dir_fd, dst->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3446 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3447 | else |
| 3448 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3449 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3450 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3451 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3452 | if (result) |
| 3453 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3454 | #endif |
| 3455 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3456 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3457 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3458 | #endif |
| 3459 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3460 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3461 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3462 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3463 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3464 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3465 | PyObject *v; |
| 3466 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3467 | BOOL result; |
| 3468 | WIN32_FIND_DATA FileData; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3469 | char namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3470 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3471 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3472 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3473 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3474 | if (!path->narrow) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3475 | WIN32_FIND_DATAW wFileData; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3476 | wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3477 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3478 | if (!path->wide) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3479 | po_wchars = L"."; |
| 3480 | len = 1; |
| 3481 | } else { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3482 | po_wchars = path->wide; |
| 3483 | len = wcslen(path->wide); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3484 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3485 | /* The +5 is so we can append "\\*.*\0" */ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3486 | wnamebuf = PyMem_New(wchar_t, len + 5); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3487 | if (!wnamebuf) { |
| 3488 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3489 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3490 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3491 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3492 | if (len > 0) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3493 | wchar_t wch = wnamebuf[len-1]; |
Tim Golden | 781bbeb | 2013-10-25 20:24:06 +0100 | [diff] [blame] | 3494 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3495 | wnamebuf[len++] = SEP; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3496 | wcscpy(wnamebuf + len, L"*.*"); |
| 3497 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3498 | if ((list = PyList_New(0)) == NULL) { |
| 3499 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3500 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3501 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3502 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3503 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3504 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3505 | int error = GetLastError(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3506 | if (error == ERROR_FILE_NOT_FOUND) |
| 3507 | goto exit; |
| 3508 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3509 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3510 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3511 | } |
| 3512 | do { |
| 3513 | /* Skip over . and .. */ |
| 3514 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3515 | wcscmp(wFileData.cFileName, L"..") != 0) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3516 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3517 | wcslen(wFileData.cFileName)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3518 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3519 | Py_DECREF(list); |
| 3520 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3521 | break; |
| 3522 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3523 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3524 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3525 | Py_DECREF(list); |
| 3526 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3527 | break; |
| 3528 | } |
| 3529 | Py_DECREF(v); |
| 3530 | } |
| 3531 | Py_BEGIN_ALLOW_THREADS |
| 3532 | result = FindNextFileW(hFindFile, &wFileData); |
| 3533 | Py_END_ALLOW_THREADS |
| 3534 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3535 | it got to the end of the directory. */ |
| 3536 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3537 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3538 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3539 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3540 | } |
| 3541 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3542 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3543 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3544 | } |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3545 | strcpy(namebuf, path->narrow); |
| 3546 | len = path->length; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3547 | if (len > 0) { |
| 3548 | char ch = namebuf[len-1]; |
Tim Golden | 781bbeb | 2013-10-25 20:24:06 +0100 | [diff] [blame] | 3549 | if (ch != '\\' && ch != '/' && ch != ':') |
| 3550 | namebuf[len++] = '\\'; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3551 | strcpy(namebuf + len, "*.*"); |
| 3552 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3553 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3554 | if ((list = PyList_New(0)) == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3555 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3556 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3557 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3558 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3559 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3560 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3561 | int error = GetLastError(); |
| 3562 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3563 | goto exit; |
| 3564 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3565 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3566 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3567 | } |
| 3568 | do { |
| 3569 | /* Skip over . and .. */ |
| 3570 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 3571 | strcmp(FileData.cFileName, "..") != 0) { |
| 3572 | v = PyBytes_FromString(FileData.cFileName); |
| 3573 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3574 | Py_DECREF(list); |
| 3575 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3576 | break; |
| 3577 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3578 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3579 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3580 | Py_DECREF(list); |
| 3581 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3582 | break; |
| 3583 | } |
| 3584 | Py_DECREF(v); |
| 3585 | } |
| 3586 | Py_BEGIN_ALLOW_THREADS |
| 3587 | result = FindNextFile(hFindFile, &FileData); |
| 3588 | Py_END_ALLOW_THREADS |
| 3589 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3590 | it got to the end of the directory. */ |
| 3591 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3592 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3593 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3594 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3595 | } |
| 3596 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3597 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3598 | exit: |
| 3599 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3600 | if (FindClose(hFindFile) == FALSE) { |
| 3601 | if (list != NULL) { |
| 3602 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3603 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3604 | } |
| 3605 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3606 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3607 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3608 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3609 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3610 | } /* end of _listdir_windows_no_opendir */ |
| 3611 | |
| 3612 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3613 | |
| 3614 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3615 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3616 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3617 | PyObject *v; |
| 3618 | DIR *dirp = NULL; |
| 3619 | struct dirent *ep; |
| 3620 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3621 | #ifdef HAVE_FDOPENDIR |
| 3622 | int fd = -1; |
| 3623 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3624 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3625 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3626 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3627 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3628 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3629 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3630 | if (fd == -1) |
| 3631 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3632 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3633 | return_str = 1; |
| 3634 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3635 | Py_BEGIN_ALLOW_THREADS |
| 3636 | dirp = fdopendir(fd); |
| 3637 | Py_END_ALLOW_THREADS |
| 3638 | } |
| 3639 | else |
| 3640 | #endif |
| 3641 | { |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3642 | char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3643 | if (path->narrow) { |
| 3644 | name = path->narrow; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3645 | /* only return bytes if they specified a bytes object */ |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3646 | return_str = !(PyBytes_Check(path->object)); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3647 | } |
| 3648 | else { |
| 3649 | name = "."; |
| 3650 | return_str = 1; |
| 3651 | } |
| 3652 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3653 | Py_BEGIN_ALLOW_THREADS |
| 3654 | dirp = opendir(name); |
| 3655 | Py_END_ALLOW_THREADS |
| 3656 | } |
| 3657 | |
| 3658 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3659 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3660 | #ifdef HAVE_FDOPENDIR |
| 3661 | if (fd != -1) { |
| 3662 | Py_BEGIN_ALLOW_THREADS |
| 3663 | close(fd); |
| 3664 | Py_END_ALLOW_THREADS |
| 3665 | } |
| 3666 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3667 | goto exit; |
| 3668 | } |
| 3669 | if ((list = PyList_New(0)) == NULL) { |
| 3670 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3671 | } |
| 3672 | for (;;) { |
| 3673 | errno = 0; |
| 3674 | Py_BEGIN_ALLOW_THREADS |
| 3675 | ep = readdir(dirp); |
| 3676 | Py_END_ALLOW_THREADS |
| 3677 | if (ep == NULL) { |
| 3678 | if (errno == 0) { |
| 3679 | break; |
| 3680 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3681 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3682 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3683 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3684 | } |
| 3685 | } |
| 3686 | if (ep->d_name[0] == '.' && |
| 3687 | (NAMLEN(ep) == 1 || |
| 3688 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3689 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3690 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3691 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3692 | else |
| 3693 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3694 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3695 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3696 | break; |
| 3697 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3698 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3699 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3700 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3701 | break; |
| 3702 | } |
| 3703 | Py_DECREF(v); |
| 3704 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3705 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3706 | exit: |
| 3707 | if (dirp != NULL) { |
| 3708 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3709 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3710 | if (fd > -1) |
| 3711 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3712 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3713 | closedir(dirp); |
| 3714 | Py_END_ALLOW_THREADS |
| 3715 | } |
| 3716 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3717 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3718 | } /* end of _posix_listdir */ |
| 3719 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3720 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3721 | |
| 3722 | /*[clinic input] |
| 3723 | os.listdir |
| 3724 | |
| 3725 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
| 3726 | |
| 3727 | Return a list containing the names of the files in the directory. |
| 3728 | |
| 3729 | path can be specified as either str or bytes. If path is bytes, |
| 3730 | the filenames returned will also be bytes; in all other circumstances |
| 3731 | the filenames returned will be str. |
| 3732 | If path is None, uses the path='.'. |
| 3733 | On some platforms, path may also be specified as an open file descriptor;\ |
| 3734 | the file descriptor must refer to a directory. |
| 3735 | If this functionality is unavailable, using it raises NotImplementedError. |
| 3736 | |
| 3737 | The list is in arbitrary order. It does not include the special |
| 3738 | entries '.' and '..' even if they are present in the directory. |
| 3739 | |
| 3740 | |
| 3741 | [clinic start generated code]*/ |
| 3742 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3743 | static PyObject * |
| 3744 | os_listdir_impl(PyModuleDef *module, path_t *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3745 | /*[clinic end generated code: output=1fbe67c1f780c8b7 input=09e300416e3cd729]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3746 | { |
| 3747 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3748 | return _listdir_windows_no_opendir(path, NULL); |
| 3749 | #else |
| 3750 | return _posix_listdir(path, NULL); |
| 3751 | #endif |
| 3752 | } |
| 3753 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3754 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3755 | /* A helper function for abspath on win32 */ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3756 | /*[clinic input] |
| 3757 | os._getfullpathname |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3758 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3759 | path: path_t |
| 3760 | / |
| 3761 | |
| 3762 | [clinic start generated code]*/ |
| 3763 | |
| 3764 | static PyObject * |
| 3765 | os__getfullpathname_impl(PyModuleDef *module, path_t *path) |
| 3766 | /*[clinic end generated code: output=b90b1f103b08773f input=332ed537c29d0a3e]*/ |
| 3767 | { |
| 3768 | if (!path->narrow) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3769 | { |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3770 | wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3771 | wchar_t *wtemp; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3772 | DWORD result; |
| 3773 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3774 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3775 | result = GetFullPathNameW(path->wide, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3776 | Py_ARRAY_LENGTH(woutbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3777 | woutbuf, &wtemp); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3778 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3779 | woutbufp = PyMem_New(wchar_t, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3780 | if (!woutbufp) |
| 3781 | return PyErr_NoMemory(); |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3782 | result = GetFullPathNameW(path->wide, result, woutbufp, &wtemp); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3783 | } |
| 3784 | if (result) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3785 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3786 | else |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3787 | v = win32_error_object("GetFullPathNameW", path->object); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3788 | if (woutbufp != woutbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3789 | PyMem_Free(woutbufp); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3790 | return v; |
| 3791 | } |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3792 | else { |
| 3793 | char outbuf[MAX_PATH]; |
| 3794 | char *temp; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3795 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3796 | if (!GetFullPathName(path->narrow, Py_ARRAY_LENGTH(outbuf), |
| 3797 | outbuf, &temp)) { |
| 3798 | win32_error_object("GetFullPathName", path->object); |
| 3799 | return NULL; |
| 3800 | } |
| 3801 | return PyBytes_FromString(outbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3802 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3803 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3804 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3805 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3806 | /*[clinic input] |
| 3807 | os._getfinalpathname |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3808 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3809 | path: unicode |
| 3810 | / |
| 3811 | |
| 3812 | A helper function for samepath on windows. |
| 3813 | [clinic start generated code]*/ |
| 3814 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3815 | static PyObject * |
| 3816 | os__getfinalpathname_impl(PyModuleDef *module, PyObject *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3817 | /*[clinic end generated code: output=8be81a5f51a34bcf input=71d5e89334891bf4]*/ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3818 | { |
| 3819 | HANDLE hFile; |
| 3820 | int buf_size; |
| 3821 | wchar_t *target_path; |
| 3822 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3823 | PyObject *result; |
| 3824 | wchar_t *path_wchar; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3825 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3826 | path_wchar = PyUnicode_AsUnicode(path); |
| 3827 | if (path_wchar == NULL) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3828 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3829 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3830 | hFile = CreateFileW( |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3831 | path_wchar, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3832 | 0, /* desired access */ |
| 3833 | 0, /* share mode */ |
| 3834 | NULL, /* security attributes */ |
| 3835 | OPEN_EXISTING, |
| 3836 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3837 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3838 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3839 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3840 | if(hFile == INVALID_HANDLE_VALUE) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3841 | return win32_error_object("CreateFileW", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3842 | |
| 3843 | /* We have a good handle to the target, use it to determine the |
| 3844 | target path name. */ |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 3845 | buf_size = GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3846 | |
| 3847 | if(!buf_size) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3848 | return win32_error_object("GetFinalPathNameByHandle", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3849 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3850 | target_path = PyMem_New(wchar_t, buf_size+1); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3851 | if(!target_path) |
| 3852 | return PyErr_NoMemory(); |
| 3853 | |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 3854 | result_length = GetFinalPathNameByHandleW(hFile, target_path, |
| 3855 | buf_size, VOLUME_NAME_DOS); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3856 | if(!result_length) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3857 | return win32_error_object("GetFinalPathNamyByHandle", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3858 | |
| 3859 | if(!CloseHandle(hFile)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3860 | return win32_error_object("CloseHandle", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3861 | |
| 3862 | target_path[result_length] = 0; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3863 | result = PyUnicode_FromWideChar(target_path, result_length); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3864 | PyMem_Free(target_path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3865 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3866 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3867 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3868 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3869 | "Return true if the pathname refers to an existing directory."); |
| 3870 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3871 | /*[clinic input] |
| 3872 | os._isdir |
| 3873 | |
| 3874 | path: path_t |
| 3875 | / |
| 3876 | |
| 3877 | [clinic start generated code]*/ |
| 3878 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3879 | static PyObject * |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3880 | os__isdir_impl(PyModuleDef *module, path_t *path) |
| 3881 | /*[clinic end generated code: output=f17b2d4e1994b0ff input=e794f12faab62a2a]*/ |
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 | DWORD attributes; |
| 3884 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3885 | if (!path->narrow) |
| 3886 | attributes = GetFileAttributesW(path->wide); |
| 3887 | else |
| 3888 | attributes = GetFileAttributesA(path->narrow); |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3889 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3890 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3891 | Py_RETURN_FALSE; |
| 3892 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3893 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3894 | Py_RETURN_TRUE; |
| 3895 | else |
| 3896 | Py_RETURN_FALSE; |
| 3897 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3898 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3899 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3900 | /*[clinic input] |
| 3901 | os._getvolumepathname |
| 3902 | |
| 3903 | path: unicode |
| 3904 | |
| 3905 | A helper function for ismount on Win32. |
| 3906 | [clinic start generated code]*/ |
| 3907 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3908 | static PyObject * |
| 3909 | os__getvolumepathname_impl(PyModuleDef *module, PyObject *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3910 | /*[clinic end generated code: output=79a0ba729f956dbe input=7eacadc40acbda6b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3911 | { |
| 3912 | PyObject *result; |
| 3913 | wchar_t *path_wchar, *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3914 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3915 | BOOL ret; |
| 3916 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3917 | path_wchar = PyUnicode_AsUnicodeAndSize(path, &buflen); |
| 3918 | if (path_wchar == NULL) |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3919 | return NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3920 | buflen += 1; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3921 | |
| 3922 | /* Volume path should be shorter than entire path */ |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3923 | buflen = Py_MAX(buflen, MAX_PATH); |
| 3924 | |
| 3925 | if (buflen > DWORD_MAX) { |
| 3926 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 3927 | return NULL; |
| 3928 | } |
| 3929 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3930 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3931 | if (mountpath == NULL) |
| 3932 | return PyErr_NoMemory(); |
| 3933 | |
| 3934 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3935 | ret = GetVolumePathNameW(path_wchar, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3936 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3937 | Py_END_ALLOW_THREADS |
| 3938 | |
| 3939 | if (!ret) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3940 | result = win32_error_object("_getvolumepathname", path); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3941 | goto exit; |
| 3942 | } |
| 3943 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
| 3944 | |
| 3945 | exit: |
| 3946 | PyMem_Free(mountpath); |
| 3947 | return result; |
| 3948 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3949 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3950 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3951 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3952 | |
| 3953 | /*[clinic input] |
| 3954 | os.mkdir |
| 3955 | |
| 3956 | path : path_t |
| 3957 | |
| 3958 | mode: int = 0o777 |
| 3959 | |
| 3960 | * |
| 3961 | |
| 3962 | dir_fd : dir_fd(requires='mkdirat') = None |
| 3963 | |
| 3964 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 3965 | |
| 3966 | Create a directory. |
| 3967 | |
| 3968 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3969 | and path should be relative; path will then be relative to that directory. |
| 3970 | dir_fd may not be implemented on your platform. |
| 3971 | If it is unavailable, using it will raise a NotImplementedError. |
| 3972 | |
| 3973 | The mode argument is ignored on Windows. |
| 3974 | [clinic start generated code]*/ |
| 3975 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3976 | static PyObject * |
| 3977 | 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] | 3978 | /*[clinic end generated code: output=8bf1f738873ef2c5 input=e965f68377e9b1ce]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3979 | { |
| 3980 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3981 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3982 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3983 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3984 | if (path->wide) |
| 3985 | result = CreateDirectoryW(path->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3986 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3987 | result = CreateDirectoryA(path->narrow, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3988 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3989 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3990 | if (!result) |
| 3991 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3992 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3993 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3994 | #if HAVE_MKDIRAT |
| 3995 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3996 | result = mkdirat(dir_fd, path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3997 | else |
| 3998 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3999 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4000 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4001 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4002 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4003 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4004 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4005 | if (result < 0) |
| 4006 | return path_error(path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4007 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4008 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4009 | } |
| 4010 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4011 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4012 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 4013 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4014 | #include <sys/resource.h> |
| 4015 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4016 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4017 | |
| 4018 | #ifdef HAVE_NICE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4019 | /*[clinic input] |
| 4020 | os.nice |
| 4021 | |
| 4022 | increment: int |
| 4023 | / |
| 4024 | |
| 4025 | Add increment to the priority of process and return the new priority. |
| 4026 | [clinic start generated code]*/ |
| 4027 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4028 | static PyObject * |
| 4029 | os_nice_impl(PyModuleDef *module, int increment) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4030 | /*[clinic end generated code: output=8870418a3fc07b51 input=864be2d402a21da2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4031 | { |
| 4032 | int value; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4033 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4034 | /* There are two flavours of 'nice': one that returns the new |
| 4035 | priority (as required by almost all standards out there) and the |
| 4036 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 4037 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4038 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4039 | If we are of the nice family that returns the new priority, we |
| 4040 | need to clear errno before the call, and check if errno is filled |
| 4041 | before calling posix_error() on a returnvalue of -1, because the |
| 4042 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4043 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4044 | errno = 0; |
| 4045 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4046 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4047 | if (value == 0) |
| 4048 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4049 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4050 | if (value == -1 && errno != 0) |
| 4051 | /* either nice() or getpriority() returned an error */ |
| 4052 | return posix_error(); |
| 4053 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4054 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4055 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4056 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4057 | |
| 4058 | #ifdef HAVE_GETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4059 | /*[clinic input] |
| 4060 | os.getpriority |
| 4061 | |
| 4062 | which: int |
| 4063 | who: int |
| 4064 | |
| 4065 | Return program scheduling priority. |
| 4066 | [clinic start generated code]*/ |
| 4067 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4068 | static PyObject * |
| 4069 | os_getpriority_impl(PyModuleDef *module, int which, int who) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4070 | /*[clinic end generated code: output=4759937aa5b67ed6 input=9be615d40e2544ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4071 | { |
| 4072 | int retval; |
| 4073 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4074 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4075 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4076 | if (errno != 0) |
| 4077 | return posix_error(); |
| 4078 | return PyLong_FromLong((long)retval); |
| 4079 | } |
| 4080 | #endif /* HAVE_GETPRIORITY */ |
| 4081 | |
| 4082 | |
| 4083 | #ifdef HAVE_SETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4084 | /*[clinic input] |
| 4085 | os.setpriority |
| 4086 | |
| 4087 | which: int |
| 4088 | who: int |
| 4089 | priority: int |
| 4090 | |
| 4091 | Set program scheduling priority. |
| 4092 | [clinic start generated code]*/ |
| 4093 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4094 | static PyObject * |
| 4095 | os_setpriority_impl(PyModuleDef *module, int which, int who, int priority) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4096 | /*[clinic end generated code: output=6497d3301547e7d5 input=710ccbf65b9dc513]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4097 | { |
| 4098 | int retval; |
| 4099 | |
| 4100 | retval = setpriority(which, who, priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4101 | if (retval == -1) |
| 4102 | return posix_error(); |
| 4103 | Py_RETURN_NONE; |
| 4104 | } |
| 4105 | #endif /* HAVE_SETPRIORITY */ |
| 4106 | |
| 4107 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4108 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4109 | 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] | 4110 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4111 | char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4112 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4113 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4114 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4115 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4116 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4117 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4118 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4119 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4120 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4121 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4122 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4123 | #ifndef HAVE_RENAMEAT |
| 4124 | if (dir_fd_specified) { |
| 4125 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
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 | #endif |
| 4129 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4130 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4131 | PyErr_Format(PyExc_ValueError, |
| 4132 | "%s: src and dst must be the same type", function_name); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4133 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4134 | } |
| 4135 | |
| 4136 | #ifdef MS_WINDOWS |
| 4137 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4138 | if (src->wide) |
| 4139 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4140 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4141 | result = MoveFileExA(src->narrow, dst->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4142 | Py_END_ALLOW_THREADS |
| 4143 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4144 | if (!result) |
| 4145 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4146 | |
| 4147 | #else |
| 4148 | Py_BEGIN_ALLOW_THREADS |
| 4149 | #ifdef HAVE_RENAMEAT |
| 4150 | if (dir_fd_specified) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4151 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4152 | else |
| 4153 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4154 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4155 | Py_END_ALLOW_THREADS |
| 4156 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4157 | if (result) |
| 4158 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4159 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4160 | Py_RETURN_NONE; |
| 4161 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4162 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4163 | |
| 4164 | /*[clinic input] |
| 4165 | os.rename |
| 4166 | |
| 4167 | src : path_t |
| 4168 | dst : path_t |
| 4169 | * |
| 4170 | src_dir_fd : dir_fd = None |
| 4171 | dst_dir_fd : dir_fd = None |
| 4172 | |
| 4173 | Rename a file or directory. |
| 4174 | |
| 4175 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4176 | descriptor open to a directory, and the respective path string (src or dst) |
| 4177 | should be relative; the path will then be relative to that directory. |
| 4178 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4179 | If they are unavailable, using them will raise a NotImplementedError. |
| 4180 | [clinic start generated code]*/ |
| 4181 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4182 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4183 | os_rename_impl(PyModuleDef *module, path_t *src, path_t *dst, int src_dir_fd, |
| 4184 | int dst_dir_fd) |
| 4185 | /*[clinic end generated code: output=08033bb2ec27fb5f input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4186 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4187 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4188 | } |
| 4189 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4190 | |
| 4191 | /*[clinic input] |
| 4192 | os.replace = os.rename |
| 4193 | |
| 4194 | Rename a file or directory, overwriting the destination. |
| 4195 | |
| 4196 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4197 | descriptor open to a directory, and the respective path string (src or dst) |
| 4198 | should be relative; the path will then be relative to that directory. |
| 4199 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4200 | If they are unavailable, using them will raise a NotImplementedError." |
| 4201 | [clinic start generated code]*/ |
| 4202 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4203 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4204 | os_replace_impl(PyModuleDef *module, path_t *src, path_t *dst, |
| 4205 | int src_dir_fd, int dst_dir_fd) |
| 4206 | /*[clinic end generated code: output=131d012eed8d3b8b input=25515dfb107c8421]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4207 | { |
| 4208 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); |
| 4209 | } |
| 4210 | |
| 4211 | |
| 4212 | /*[clinic input] |
| 4213 | os.rmdir |
| 4214 | |
| 4215 | path: path_t |
| 4216 | * |
| 4217 | dir_fd: dir_fd(requires='unlinkat') = None |
| 4218 | |
| 4219 | Remove a directory. |
| 4220 | |
| 4221 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4222 | and path should be relative; path will then be relative to that directory. |
| 4223 | dir_fd may not be implemented on your platform. |
| 4224 | If it is unavailable, using it will raise a NotImplementedError. |
| 4225 | [clinic start generated code]*/ |
| 4226 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4227 | static PyObject * |
| 4228 | os_rmdir_impl(PyModuleDef *module, path_t *path, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4229 | /*[clinic end generated code: output=cabadec80d5a77c7 input=38c8b375ca34a7e2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4230 | { |
| 4231 | int result; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4232 | |
| 4233 | Py_BEGIN_ALLOW_THREADS |
| 4234 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4235 | if (path->wide) |
| 4236 | result = RemoveDirectoryW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4237 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4238 | result = RemoveDirectoryA(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4239 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4240 | #else |
| 4241 | #ifdef HAVE_UNLINKAT |
| 4242 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4243 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4244 | else |
| 4245 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4246 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4247 | #endif |
| 4248 | Py_END_ALLOW_THREADS |
| 4249 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4250 | if (result) |
| 4251 | return path_error(path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4252 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4253 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4254 | } |
| 4255 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4256 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4257 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4258 | #ifdef MS_WINDOWS |
| 4259 | /*[clinic input] |
| 4260 | os.system -> long |
| 4261 | |
| 4262 | command: Py_UNICODE |
| 4263 | |
| 4264 | Execute the command in a subshell. |
| 4265 | [clinic start generated code]*/ |
| 4266 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4267 | static long |
| 4268 | os_system_impl(PyModuleDef *module, Py_UNICODE *command) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4269 | /*[clinic end generated code: output=4c3bd5abcd9c29e7 input=303f5ce97df606b0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4270 | { |
| 4271 | long result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4272 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4273 | result = _wsystem(command); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4274 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4275 | return result; |
| 4276 | } |
| 4277 | #else /* MS_WINDOWS */ |
| 4278 | /*[clinic input] |
| 4279 | os.system -> long |
| 4280 | |
| 4281 | command: FSConverter |
| 4282 | |
| 4283 | Execute the command in a subshell. |
| 4284 | [clinic start generated code]*/ |
| 4285 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4286 | static long |
| 4287 | os_system_impl(PyModuleDef *module, PyObject *command) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4288 | /*[clinic end generated code: output=800f775e10b7be55 input=86a58554ba6094af]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4289 | { |
| 4290 | long result; |
| 4291 | char *bytes = PyBytes_AsString(command); |
| 4292 | Py_BEGIN_ALLOW_THREADS |
| 4293 | result = system(bytes); |
| 4294 | Py_END_ALLOW_THREADS |
| 4295 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4296 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4297 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4298 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4299 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4300 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4301 | /*[clinic input] |
| 4302 | os.umask |
| 4303 | |
| 4304 | mask: int |
| 4305 | / |
| 4306 | |
| 4307 | Set the current numeric umask and return the previous umask. |
| 4308 | [clinic start generated code]*/ |
| 4309 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4310 | static PyObject * |
| 4311 | os_umask_impl(PyModuleDef *module, int mask) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4312 | /*[clinic end generated code: output=9e1fe3c9f14d6a05 input=ab6bfd9b24d8a7e8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4313 | { |
| 4314 | int i = (int)umask(mask); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4315 | if (i < 0) |
| 4316 | return posix_error(); |
| 4317 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4318 | } |
| 4319 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4320 | #ifdef MS_WINDOWS |
| 4321 | |
| 4322 | /* override the default DeleteFileW behavior so that directory |
| 4323 | symlinks can be removed with this function, the same as with |
| 4324 | Unix symlinks */ |
| 4325 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4326 | { |
| 4327 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4328 | WIN32_FIND_DATAW find_data; |
| 4329 | HANDLE find_data_handle; |
| 4330 | int is_directory = 0; |
| 4331 | int is_link = 0; |
| 4332 | |
| 4333 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4334 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4335 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4336 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4337 | it is a symlink */ |
| 4338 | if(is_directory && |
| 4339 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4340 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4341 | |
| 4342 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4343 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 4344 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 4345 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 4346 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4347 | FindClose(find_data_handle); |
| 4348 | } |
| 4349 | } |
| 4350 | } |
| 4351 | |
| 4352 | if (is_directory && is_link) |
| 4353 | return RemoveDirectoryW(lpFileName); |
| 4354 | |
| 4355 | return DeleteFileW(lpFileName); |
| 4356 | } |
| 4357 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4358 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4359 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4360 | /*[clinic input] |
| 4361 | os.unlink |
| 4362 | |
| 4363 | path: path_t |
| 4364 | * |
| 4365 | dir_fd: dir_fd(requires='unlinkat')=None |
| 4366 | |
| 4367 | Remove a file (same as remove()). |
| 4368 | |
| 4369 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4370 | and path should be relative; path will then be relative to that directory. |
| 4371 | dir_fd may not be implemented on your platform. |
| 4372 | If it is unavailable, using it will raise a NotImplementedError. |
| 4373 | |
| 4374 | [clinic start generated code]*/ |
| 4375 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4376 | static PyObject * |
| 4377 | os_unlink_impl(PyModuleDef *module, path_t *path, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4378 | /*[clinic end generated code: output=474afd5cd09b237e input=d7bcde2b1b2a2552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4379 | { |
| 4380 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4381 | |
| 4382 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4383 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4384 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4385 | if (path->wide) |
| 4386 | result = Py_DeleteFileW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4387 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4388 | result = DeleteFileA(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4389 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4390 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4391 | #ifdef HAVE_UNLINKAT |
| 4392 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4393 | result = unlinkat(dir_fd, path->narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4394 | else |
| 4395 | #endif /* HAVE_UNLINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4396 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4397 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4398 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4399 | Py_END_ALLOW_THREADS |
| 4400 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4401 | if (result) |
| 4402 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4403 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4404 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4405 | } |
| 4406 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4407 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4408 | /*[clinic input] |
| 4409 | os.remove = os.unlink |
| 4410 | |
| 4411 | Remove a file (same as unlink()). |
| 4412 | |
| 4413 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4414 | and path should be relative; path will then be relative to that directory. |
| 4415 | dir_fd may not be implemented on your platform. |
| 4416 | If it is unavailable, using it will raise a NotImplementedError. |
| 4417 | [clinic start generated code]*/ |
| 4418 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4419 | static PyObject * |
| 4420 | os_remove_impl(PyModuleDef *module, path_t *path, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4421 | /*[clinic end generated code: output=d0d5149e64832b9e input=e05c5ab55cd30983]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4422 | { |
| 4423 | return os_unlink_impl(module, path, dir_fd); |
| 4424 | } |
| 4425 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4426 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4427 | static PyStructSequence_Field uname_result_fields[] = { |
| 4428 | {"sysname", "operating system name"}, |
| 4429 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4430 | {"release", "operating system release"}, |
| 4431 | {"version", "operating system version"}, |
| 4432 | {"machine", "hardware identifier"}, |
| 4433 | {NULL} |
| 4434 | }; |
| 4435 | |
| 4436 | PyDoc_STRVAR(uname_result__doc__, |
| 4437 | "uname_result: Result from os.uname().\n\n\ |
| 4438 | This object may be accessed either as a tuple of\n\ |
| 4439 | (sysname, nodename, release, version, machine),\n\ |
| 4440 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4441 | \n\ |
| 4442 | See os.uname for more information."); |
| 4443 | |
| 4444 | static PyStructSequence_Desc uname_result_desc = { |
| 4445 | "uname_result", /* name */ |
| 4446 | uname_result__doc__, /* doc */ |
| 4447 | uname_result_fields, |
| 4448 | 5 |
| 4449 | }; |
| 4450 | |
| 4451 | static PyTypeObject UnameResultType; |
| 4452 | |
| 4453 | |
| 4454 | #ifdef HAVE_UNAME |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4455 | /*[clinic input] |
| 4456 | os.uname |
| 4457 | |
| 4458 | Return an object identifying the current operating system. |
| 4459 | |
| 4460 | The object behaves like a named tuple with the following fields: |
| 4461 | (sysname, nodename, release, version, machine) |
| 4462 | |
| 4463 | [clinic start generated code]*/ |
| 4464 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4465 | static PyObject * |
| 4466 | os_uname_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4467 | /*[clinic end generated code: output=01e1421b757e753f input=e68bd246db3043ed]*/ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4468 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4469 | struct utsname u; |
| 4470 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4471 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4472 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4473 | Py_BEGIN_ALLOW_THREADS |
| 4474 | res = uname(&u); |
| 4475 | Py_END_ALLOW_THREADS |
| 4476 | if (res < 0) |
| 4477 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4478 | |
| 4479 | value = PyStructSequence_New(&UnameResultType); |
| 4480 | if (value == NULL) |
| 4481 | return NULL; |
| 4482 | |
| 4483 | #define SET(i, field) \ |
| 4484 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4485 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4486 | if (!o) { \ |
| 4487 | Py_DECREF(value); \ |
| 4488 | return NULL; \ |
| 4489 | } \ |
| 4490 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4491 | } \ |
| 4492 | |
| 4493 | SET(0, u.sysname); |
| 4494 | SET(1, u.nodename); |
| 4495 | SET(2, u.release); |
| 4496 | SET(3, u.version); |
| 4497 | SET(4, u.machine); |
| 4498 | |
| 4499 | #undef SET |
| 4500 | |
| 4501 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4502 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4503 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4504 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4505 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4506 | |
| 4507 | typedef struct { |
| 4508 | int now; |
| 4509 | time_t atime_s; |
| 4510 | long atime_ns; |
| 4511 | time_t mtime_s; |
| 4512 | long mtime_ns; |
| 4513 | } utime_t; |
| 4514 | |
| 4515 | /* |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4516 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4517 | * they also intentionally leak the declaration of a pointer named "time" |
| 4518 | */ |
| 4519 | #define UTIME_TO_TIMESPEC \ |
| 4520 | struct timespec ts[2]; \ |
| 4521 | struct timespec *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4522 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4523 | time = NULL; \ |
| 4524 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4525 | ts[0].tv_sec = ut->atime_s; \ |
| 4526 | ts[0].tv_nsec = ut->atime_ns; \ |
| 4527 | ts[1].tv_sec = ut->mtime_s; \ |
| 4528 | ts[1].tv_nsec = ut->mtime_ns; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4529 | time = ts; \ |
| 4530 | } \ |
| 4531 | |
| 4532 | #define UTIME_TO_TIMEVAL \ |
| 4533 | struct timeval tv[2]; \ |
| 4534 | struct timeval *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4535 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4536 | time = NULL; \ |
| 4537 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4538 | tv[0].tv_sec = ut->atime_s; \ |
| 4539 | tv[0].tv_usec = ut->atime_ns / 1000; \ |
| 4540 | tv[1].tv_sec = ut->mtime_s; \ |
| 4541 | tv[1].tv_usec = ut->mtime_ns / 1000; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4542 | time = tv; \ |
| 4543 | } \ |
| 4544 | |
| 4545 | #define UTIME_TO_UTIMBUF \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4546 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4547 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4548 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4549 | time = NULL; \ |
| 4550 | else { \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4551 | u.actime = ut->atime_s; \ |
| 4552 | u.modtime = ut->mtime_s; \ |
| 4553 | time = &u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4554 | } |
| 4555 | |
| 4556 | #define UTIME_TO_TIME_T \ |
| 4557 | time_t timet[2]; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4558 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4559 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4560 | time = NULL; \ |
| 4561 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4562 | timet[0] = ut->atime_s; \ |
| 4563 | timet[1] = ut->mtime_s; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4564 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4565 | } \ |
| 4566 | |
| 4567 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4568 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4569 | |
| 4570 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4571 | utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4572 | { |
| 4573 | #ifdef HAVE_UTIMENSAT |
| 4574 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4575 | UTIME_TO_TIMESPEC; |
| 4576 | return utimensat(dir_fd, path, time, flags); |
| 4577 | #elif defined(HAVE_FUTIMESAT) |
| 4578 | UTIME_TO_TIMEVAL; |
| 4579 | /* |
| 4580 | * follow_symlinks will never be false here; |
| 4581 | * we only allow !follow_symlinks and dir_fd together |
| 4582 | * if we have utimensat() |
| 4583 | */ |
| 4584 | assert(follow_symlinks); |
| 4585 | return futimesat(dir_fd, path, time); |
| 4586 | #endif |
| 4587 | } |
| 4588 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4589 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
| 4590 | #else |
| 4591 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4592 | #endif |
| 4593 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4594 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4595 | |
| 4596 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4597 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4598 | { |
| 4599 | #ifdef HAVE_FUTIMENS |
| 4600 | UTIME_TO_TIMESPEC; |
| 4601 | return futimens(fd, time); |
| 4602 | #else |
| 4603 | UTIME_TO_TIMEVAL; |
| 4604 | return futimes(fd, time); |
| 4605 | #endif |
| 4606 | } |
| 4607 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4608 | #define PATH_UTIME_HAVE_FD 1 |
| 4609 | #else |
| 4610 | #define PATH_UTIME_HAVE_FD 0 |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4611 | #endif |
| 4612 | |
Victor Stinner | 5ebae87 | 2015-09-22 01:29:33 +0200 | [diff] [blame] | 4613 | #if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES) |
| 4614 | # define UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4615 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4616 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4617 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4618 | |
| 4619 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4620 | utime_nofollow_symlinks(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4621 | { |
| 4622 | #ifdef HAVE_UTIMENSAT |
| 4623 | UTIME_TO_TIMESPEC; |
| 4624 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4625 | #else |
| 4626 | UTIME_TO_TIMEVAL; |
| 4627 | return lutimes(path, time); |
| 4628 | #endif |
| 4629 | } |
| 4630 | |
| 4631 | #endif |
| 4632 | |
| 4633 | #ifndef MS_WINDOWS |
| 4634 | |
| 4635 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4636 | utime_default(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4637 | { |
| 4638 | #ifdef HAVE_UTIMENSAT |
| 4639 | UTIME_TO_TIMESPEC; |
| 4640 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4641 | #elif defined(HAVE_UTIMES) |
| 4642 | UTIME_TO_TIMEVAL; |
| 4643 | return utimes(path, time); |
| 4644 | #elif defined(HAVE_UTIME_H) |
| 4645 | UTIME_TO_UTIMBUF; |
| 4646 | return utime(path, time); |
| 4647 | #else |
| 4648 | UTIME_TO_TIME_T; |
| 4649 | return utime(path, time); |
| 4650 | #endif |
| 4651 | } |
| 4652 | |
| 4653 | #endif |
| 4654 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4655 | static int |
| 4656 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4657 | { |
| 4658 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4659 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4660 | divmod = PyNumber_Divmod(py_long, billion); |
| 4661 | if (!divmod) |
| 4662 | goto exit; |
| 4663 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4664 | if ((*s == -1) && PyErr_Occurred()) |
| 4665 | goto exit; |
| 4666 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4667 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4668 | goto exit; |
| 4669 | |
| 4670 | result = 1; |
| 4671 | exit: |
| 4672 | Py_XDECREF(divmod); |
| 4673 | return result; |
| 4674 | } |
| 4675 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4676 | |
| 4677 | /*[clinic input] |
| 4678 | os.utime |
| 4679 | |
| 4680 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') |
| 4681 | times: object = NULL |
| 4682 | * |
| 4683 | ns: object = NULL |
| 4684 | dir_fd: dir_fd(requires='futimensat') = None |
| 4685 | follow_symlinks: bool=True |
| 4686 | |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4687 | # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4688 | |
| 4689 | Set the access and modified time of path. |
| 4690 | |
| 4691 | path may always be specified as a string. |
| 4692 | On some platforms, path may also be specified as an open file descriptor. |
| 4693 | If this functionality is unavailable, using it raises an exception. |
| 4694 | |
| 4695 | If times is not None, it must be a tuple (atime, mtime); |
| 4696 | atime and mtime should be expressed as float seconds since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4697 | 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] | 4698 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 4699 | since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4700 | 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] | 4701 | Specifying tuples for both times and ns is an error. |
| 4702 | |
| 4703 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4704 | and path should be relative; path will then be relative to that directory. |
| 4705 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 4706 | link, utime will modify the symbolic link itself instead of the file the |
| 4707 | link points to. |
| 4708 | It is an error to use dir_fd or follow_symlinks when specifying path |
| 4709 | as an open file descriptor. |
| 4710 | dir_fd and follow_symlinks may not be available on your platform. |
| 4711 | If they are unavailable, using them will raise a NotImplementedError. |
| 4712 | |
| 4713 | [clinic start generated code]*/ |
| 4714 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4715 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4716 | os_utime_impl(PyModuleDef *module, path_t *path, PyObject *times, |
| 4717 | PyObject *ns, int dir_fd, int follow_symlinks) |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4718 | /*[clinic end generated code: output=31f3434e560ba2f0 input=081cdc54ca685385]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4719 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4720 | #ifdef MS_WINDOWS |
| 4721 | HANDLE hFile; |
| 4722 | FILETIME atime, mtime; |
| 4723 | #else |
| 4724 | int result; |
| 4725 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4726 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4727 | PyObject *return_value = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4728 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4729 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4730 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4731 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4732 | if (times && (times != Py_None) && ns) { |
| 4733 | PyErr_SetString(PyExc_ValueError, |
| 4734 | "utime: you may specify either 'times'" |
| 4735 | " or 'ns' but not both"); |
| 4736 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4737 | } |
| 4738 | |
| 4739 | if (times && (times != Py_None)) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4740 | time_t a_sec, m_sec; |
| 4741 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4742 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4743 | PyErr_SetString(PyExc_TypeError, |
| 4744 | "utime: 'times' must be either" |
| 4745 | " a tuple of two ints or None"); |
| 4746 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4747 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4748 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4749 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4750 | &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4751 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4752 | &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4753 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4754 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4755 | utime.atime_s = a_sec; |
| 4756 | utime.atime_ns = a_nsec; |
| 4757 | utime.mtime_s = m_sec; |
| 4758 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4759 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4760 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4761 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4762 | PyErr_SetString(PyExc_TypeError, |
| 4763 | "utime: 'ns' must be a tuple of two ints"); |
| 4764 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4765 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4766 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4767 | 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] | 4768 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4769 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4770 | &utime.mtime_s, &utime.mtime_ns)) { |
| 4771 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4772 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4773 | } |
| 4774 | else { |
| 4775 | /* times and ns are both None/unspecified. use "now". */ |
| 4776 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4777 | } |
| 4778 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4779 | #if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4780 | if (follow_symlinks_specified("utime", follow_symlinks)) |
| 4781 | goto exit; |
| 4782 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4783 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4784 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
| 4785 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || |
| 4786 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4787 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4788 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4789 | #if !defined(HAVE_UTIMENSAT) |
| 4790 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4791 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4792 | "utime: cannot use dir_fd and follow_symlinks " |
| 4793 | "together on this platform"); |
| 4794 | goto exit; |
| 4795 | } |
| 4796 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4797 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4798 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4799 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4800 | if (path->wide) |
| 4801 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4802 | NULL, OPEN_EXISTING, |
| 4803 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4804 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4805 | hFile = CreateFileA(path->narrow, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4806 | NULL, OPEN_EXISTING, |
| 4807 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4808 | Py_END_ALLOW_THREADS |
| 4809 | if (hFile == INVALID_HANDLE_VALUE) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4810 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4811 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4812 | } |
| 4813 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4814 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 4815 | GetSystemTimeAsFileTime(&mtime); |
| 4816 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4817 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4818 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 4819 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4820 | _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] | 4821 | } |
| 4822 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4823 | /* Avoid putting the file name into the error here, |
| 4824 | as that may confuse the user into believing that |
| 4825 | something is wrong with the file, when it also |
| 4826 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4827 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4828 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4829 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4830 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4831 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4832 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4833 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4834 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4835 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4836 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4837 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4838 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4839 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4840 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4841 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4842 | else |
| 4843 | #endif |
| 4844 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4845 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4846 | if (path->fd != -1) |
| 4847 | result = utime_fd(&utime, path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4848 | else |
| 4849 | #endif |
| 4850 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4851 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4852 | |
| 4853 | Py_END_ALLOW_THREADS |
| 4854 | |
| 4855 | if (result < 0) { |
| 4856 | /* see previous comment about not putting filename in error here */ |
| 4857 | return_value = posix_error(); |
| 4858 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4859 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4860 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4861 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4862 | |
| 4863 | Py_INCREF(Py_None); |
| 4864 | return_value = Py_None; |
| 4865 | |
| 4866 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4867 | #ifdef MS_WINDOWS |
| 4868 | if (hFile != INVALID_HANDLE_VALUE) |
| 4869 | CloseHandle(hFile); |
| 4870 | #endif |
| 4871 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4872 | } |
| 4873 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4874 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4875 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4876 | |
| 4877 | /*[clinic input] |
| 4878 | os._exit |
| 4879 | |
| 4880 | status: int |
| 4881 | |
| 4882 | Exit to the system with specified status, without normal exit processing. |
| 4883 | [clinic start generated code]*/ |
| 4884 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4885 | static PyObject * |
| 4886 | os__exit_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4887 | /*[clinic end generated code: output=472a3cbaf68f3621 input=5e6d57556b0c4a62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4888 | { |
| 4889 | _exit(status); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4890 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4891 | } |
| 4892 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4893 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 4894 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4895 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4896 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4897 | Py_ssize_t i; |
| 4898 | for (i = 0; i < count; i++) |
| 4899 | PyMem_Free(array[i]); |
| 4900 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4901 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4902 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 4903 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4904 | int fsconvert_strdup(PyObject *o, char**out) |
| 4905 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4906 | PyObject *bytes; |
| 4907 | Py_ssize_t size; |
| 4908 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 4909 | return 0; |
| 4910 | size = PyBytes_GET_SIZE(bytes); |
| 4911 | *out = PyMem_Malloc(size+1); |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 4912 | if (!*out) { |
| 4913 | PyErr_NoMemory(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4914 | return 0; |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 4915 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4916 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 4917 | Py_DECREF(bytes); |
| 4918 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4919 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4920 | #endif |
| 4921 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4922 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4923 | static char** |
| 4924 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 4925 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4926 | char **envlist; |
| 4927 | Py_ssize_t i, pos, envc; |
| 4928 | PyObject *keys=NULL, *vals=NULL; |
| 4929 | PyObject *key, *val, *key2, *val2; |
| 4930 | char *p, *k, *v; |
| 4931 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4932 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4933 | i = PyMapping_Size(env); |
| 4934 | if (i < 0) |
| 4935 | return NULL; |
| 4936 | envlist = PyMem_NEW(char *, i + 1); |
| 4937 | if (envlist == NULL) { |
| 4938 | PyErr_NoMemory(); |
| 4939 | return NULL; |
| 4940 | } |
| 4941 | envc = 0; |
| 4942 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4943 | if (!keys) |
| 4944 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4945 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4946 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4947 | goto error; |
| 4948 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 4949 | PyErr_Format(PyExc_TypeError, |
| 4950 | "env.keys() or env.values() is not a list"); |
| 4951 | goto error; |
| 4952 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4953 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4954 | for (pos = 0; pos < i; pos++) { |
| 4955 | key = PyList_GetItem(keys, pos); |
| 4956 | val = PyList_GetItem(vals, pos); |
| 4957 | if (!key || !val) |
| 4958 | goto error; |
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 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 4961 | goto error; |
| 4962 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 4963 | Py_DECREF(key2); |
| 4964 | goto error; |
| 4965 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4966 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4967 | k = PyBytes_AsString(key2); |
| 4968 | v = PyBytes_AsString(val2); |
| 4969 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4970 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4971 | p = PyMem_NEW(char, len); |
| 4972 | if (p == NULL) { |
| 4973 | PyErr_NoMemory(); |
| 4974 | Py_DECREF(key2); |
| 4975 | Py_DECREF(val2); |
| 4976 | goto error; |
| 4977 | } |
| 4978 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 4979 | envlist[envc++] = p; |
| 4980 | Py_DECREF(key2); |
| 4981 | Py_DECREF(val2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4982 | } |
| 4983 | Py_DECREF(vals); |
| 4984 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4985 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4986 | envlist[envc] = 0; |
| 4987 | *envc_ptr = envc; |
| 4988 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4989 | |
| 4990 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4991 | Py_XDECREF(keys); |
| 4992 | Py_XDECREF(vals); |
| 4993 | while (--envc >= 0) |
| 4994 | PyMem_DEL(envlist[envc]); |
| 4995 | PyMem_DEL(envlist); |
| 4996 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4997 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4998 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4999 | static char** |
| 5000 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 5001 | { |
| 5002 | int i; |
| 5003 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 5004 | if (argvlist == NULL) { |
| 5005 | PyErr_NoMemory(); |
| 5006 | return NULL; |
| 5007 | } |
| 5008 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5009 | PyObject* item = PySequence_ITEM(argv, i); |
| 5010 | if (item == NULL) |
| 5011 | goto fail; |
| 5012 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 5013 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5014 | goto fail; |
| 5015 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5016 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5017 | } |
| 5018 | argvlist[*argc] = NULL; |
| 5019 | return argvlist; |
| 5020 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5021 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5022 | free_string_array(argvlist, *argc); |
| 5023 | return NULL; |
| 5024 | } |
| 5025 | #endif |
| 5026 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5027 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5028 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5029 | /*[clinic input] |
| 5030 | os.execv |
| 5031 | |
| 5032 | path: FSConverter |
| 5033 | Path of executable file. |
| 5034 | argv: object |
| 5035 | Tuple or list of strings. |
| 5036 | / |
| 5037 | |
| 5038 | Execute an executable path with arguments, replacing current process. |
| 5039 | [clinic start generated code]*/ |
| 5040 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5041 | static PyObject * |
| 5042 | os_execv_impl(PyModuleDef *module, PyObject *path, PyObject *argv) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5043 | /*[clinic end generated code: output=9221f08143146fff input=96041559925e5229]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5044 | { |
| 5045 | char *path_char; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5046 | char **argvlist; |
| 5047 | Py_ssize_t argc; |
| 5048 | |
| 5049 | /* execv has two arguments: (path, argv), where |
| 5050 | argv is a list or tuple of strings. */ |
| 5051 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5052 | path_char = PyBytes_AsString(path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5053 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 5054 | PyErr_SetString(PyExc_TypeError, |
| 5055 | "execv() arg 2 must be a tuple or list"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5056 | return NULL; |
| 5057 | } |
| 5058 | argc = PySequence_Size(argv); |
| 5059 | if (argc < 1) { |
| 5060 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5061 | return NULL; |
| 5062 | } |
| 5063 | |
| 5064 | argvlist = parse_arglist(argv, &argc); |
| 5065 | if (argvlist == NULL) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5066 | return NULL; |
| 5067 | } |
| 5068 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5069 | execv(path_char, argvlist); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5070 | |
| 5071 | /* If we get here it's definitely an error */ |
| 5072 | |
| 5073 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5074 | return posix_error(); |
| 5075 | } |
| 5076 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5077 | |
| 5078 | /*[clinic input] |
| 5079 | os.execve |
| 5080 | |
| 5081 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') |
| 5082 | Path of executable file. |
| 5083 | argv: object |
| 5084 | Tuple or list of strings. |
| 5085 | env: object |
| 5086 | Dictionary of strings mapping to strings. |
| 5087 | |
| 5088 | Execute an executable path with arguments, replacing current process. |
| 5089 | [clinic start generated code]*/ |
| 5090 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5091 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 5092 | os_execve_impl(PyModuleDef *module, path_t *path, PyObject *argv, |
| 5093 | PyObject *env) |
| 5094 | /*[clinic end generated code: output=181884fcdb21508e input=626804fa092606d9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5095 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5096 | char **argvlist = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5097 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5098 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5099 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5100 | /* execve has three arguments: (path, argv, env), where |
| 5101 | argv is a list or tuple of strings and env is a dictionary |
| 5102 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5103 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5104 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5105 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5106 | "execve: argv must be a tuple or list"); |
| 5107 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5108 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5109 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5110 | if (!PyMapping_Check(env)) { |
| 5111 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5112 | "execve: environment must be a mapping object"); |
| 5113 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5114 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5115 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5116 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5117 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5118 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5119 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5120 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5121 | envlist = parse_envlist(env, &envc); |
| 5122 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5123 | goto fail; |
| 5124 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5125 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5126 | if (path->fd > -1) |
| 5127 | fexecve(path->fd, argvlist, envlist); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5128 | else |
| 5129 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5130 | execve(path->narrow, argvlist, envlist); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5131 | |
| 5132 | /* If we get here it's definitely an error */ |
| 5133 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5134 | path_error(path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5135 | |
| 5136 | while (--envc >= 0) |
| 5137 | PyMem_DEL(envlist[envc]); |
| 5138 | PyMem_DEL(envlist); |
| 5139 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5140 | if (argvlist) |
| 5141 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5142 | return NULL; |
| 5143 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5144 | #endif /* HAVE_EXECV */ |
| 5145 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5146 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5147 | #ifdef HAVE_SPAWNV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5148 | /*[clinic input] |
| 5149 | os.spawnv |
| 5150 | |
| 5151 | mode: int |
| 5152 | Mode of process creation. |
| 5153 | path: FSConverter |
| 5154 | Path of executable file. |
| 5155 | argv: object |
| 5156 | Tuple or list of strings. |
| 5157 | / |
| 5158 | |
| 5159 | Execute the program specified by path in a new process. |
| 5160 | [clinic start generated code]*/ |
| 5161 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5162 | static PyObject * |
| 5163 | os_spawnv_impl(PyModuleDef *module, int mode, PyObject *path, PyObject *argv) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5164 | /*[clinic end generated code: output=140a7945484c8cc5 input=042c91dfc1e6debc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5165 | { |
| 5166 | char *path_char; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5167 | char **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5168 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5169 | Py_ssize_t argc; |
| 5170 | Py_intptr_t spawnval; |
| 5171 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5172 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5173 | /* spawnv has three arguments: (mode, path, argv), where |
| 5174 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5175 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5176 | path_char = PyBytes_AsString(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5177 | if (PyList_Check(argv)) { |
| 5178 | argc = PyList_Size(argv); |
| 5179 | getitem = PyList_GetItem; |
| 5180 | } |
| 5181 | else if (PyTuple_Check(argv)) { |
| 5182 | argc = PyTuple_Size(argv); |
| 5183 | getitem = PyTuple_GetItem; |
| 5184 | } |
| 5185 | else { |
| 5186 | PyErr_SetString(PyExc_TypeError, |
| 5187 | "spawnv() arg 2 must be a tuple or list"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5188 | return NULL; |
| 5189 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5190 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5191 | argvlist = PyMem_NEW(char *, argc+1); |
| 5192 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5193 | return PyErr_NoMemory(); |
| 5194 | } |
| 5195 | for (i = 0; i < argc; i++) { |
| 5196 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5197 | &argvlist[i])) { |
| 5198 | free_string_array(argvlist, i); |
| 5199 | PyErr_SetString( |
| 5200 | PyExc_TypeError, |
| 5201 | "spawnv() arg 2 must contain only strings"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5202 | return NULL; |
| 5203 | } |
| 5204 | } |
| 5205 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5206 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5207 | if (mode == _OLD_P_OVERLAY) |
| 5208 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5209 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5210 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5211 | spawnval = _spawnv(mode, path_char, argvlist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5212 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5213 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5214 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5215 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5216 | if (spawnval == -1) |
| 5217 | return posix_error(); |
| 5218 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5219 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5220 | } |
| 5221 | |
| 5222 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5223 | /*[clinic input] |
| 5224 | os.spawnve |
| 5225 | |
| 5226 | mode: int |
| 5227 | Mode of process creation. |
| 5228 | path: FSConverter |
| 5229 | Path of executable file. |
| 5230 | argv: object |
| 5231 | Tuple or list of strings. |
| 5232 | env: object |
| 5233 | Dictionary of strings mapping to strings. |
| 5234 | / |
| 5235 | |
| 5236 | Execute the program specified by path in a new process. |
| 5237 | [clinic start generated code]*/ |
| 5238 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5239 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 5240 | os_spawnve_impl(PyModuleDef *module, int mode, PyObject *path, |
| 5241 | PyObject *argv, PyObject *env) |
| 5242 | /*[clinic end generated code: output=e7f5f0703610531f input=02362fd937963f8f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5243 | { |
| 5244 | char *path_char; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5245 | char **argvlist; |
| 5246 | char **envlist; |
| 5247 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5248 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5249 | Py_intptr_t spawnval; |
| 5250 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 5251 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5252 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5253 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5254 | argv is a list or tuple of strings and env is a dictionary |
| 5255 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5256 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5257 | path_char = PyBytes_AsString(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5258 | if (PyList_Check(argv)) { |
| 5259 | argc = PyList_Size(argv); |
| 5260 | getitem = PyList_GetItem; |
| 5261 | } |
| 5262 | else if (PyTuple_Check(argv)) { |
| 5263 | argc = PyTuple_Size(argv); |
| 5264 | getitem = PyTuple_GetItem; |
| 5265 | } |
| 5266 | else { |
| 5267 | PyErr_SetString(PyExc_TypeError, |
| 5268 | "spawnve() arg 2 must be a tuple or list"); |
| 5269 | goto fail_0; |
| 5270 | } |
| 5271 | if (!PyMapping_Check(env)) { |
| 5272 | PyErr_SetString(PyExc_TypeError, |
| 5273 | "spawnve() arg 3 must be a mapping object"); |
| 5274 | goto fail_0; |
| 5275 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5276 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5277 | argvlist = PyMem_NEW(char *, argc+1); |
| 5278 | if (argvlist == NULL) { |
| 5279 | PyErr_NoMemory(); |
| 5280 | goto fail_0; |
| 5281 | } |
| 5282 | for (i = 0; i < argc; i++) { |
| 5283 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5284 | &argvlist[i])) |
| 5285 | { |
| 5286 | lastarg = i; |
| 5287 | goto fail_1; |
| 5288 | } |
| 5289 | } |
| 5290 | lastarg = argc; |
| 5291 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5292 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5293 | envlist = parse_envlist(env, &envc); |
| 5294 | if (envlist == NULL) |
| 5295 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5296 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5297 | if (mode == _OLD_P_OVERLAY) |
| 5298 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5299 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5300 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5301 | spawnval = _spawnve(mode, path_char, argvlist, envlist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5302 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5303 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5304 | if (spawnval == -1) |
| 5305 | (void) posix_error(); |
| 5306 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5307 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5308 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5309 | while (--envc >= 0) |
| 5310 | PyMem_DEL(envlist[envc]); |
| 5311 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5312 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5313 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5314 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5315 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5316 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5317 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5318 | #endif /* HAVE_SPAWNV */ |
| 5319 | |
| 5320 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5321 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5322 | /*[clinic input] |
| 5323 | os.fork1 |
| 5324 | |
| 5325 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 5326 | |
| 5327 | Return 0 to child process and PID of child to parent process. |
| 5328 | [clinic start generated code]*/ |
| 5329 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5330 | static PyObject * |
| 5331 | os_fork1_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5332 | /*[clinic end generated code: output=e27b4f66419c9dcf input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5333 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5334 | pid_t pid; |
| 5335 | int result = 0; |
| 5336 | _PyImport_AcquireLock(); |
| 5337 | pid = fork1(); |
| 5338 | if (pid == 0) { |
| 5339 | /* child: this clobbers and resets the import lock. */ |
| 5340 | PyOS_AfterFork(); |
| 5341 | } else { |
| 5342 | /* parent: release the import lock. */ |
| 5343 | result = _PyImport_ReleaseLock(); |
| 5344 | } |
| 5345 | if (pid == -1) |
| 5346 | return posix_error(); |
| 5347 | if (result < 0) { |
| 5348 | /* Don't clobber the OSError if the fork failed. */ |
| 5349 | PyErr_SetString(PyExc_RuntimeError, |
| 5350 | "not holding the import lock"); |
| 5351 | return NULL; |
| 5352 | } |
| 5353 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5354 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5355 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5356 | |
| 5357 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5358 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5359 | /*[clinic input] |
| 5360 | os.fork |
| 5361 | |
| 5362 | Fork a child process. |
| 5363 | |
| 5364 | Return 0 to child process and PID of child to parent process. |
| 5365 | [clinic start generated code]*/ |
| 5366 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5367 | static PyObject * |
| 5368 | os_fork_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5369 | /*[clinic end generated code: output=898b1ecd3498ba12 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5370 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5371 | pid_t pid; |
| 5372 | int result = 0; |
| 5373 | _PyImport_AcquireLock(); |
| 5374 | pid = fork(); |
| 5375 | if (pid == 0) { |
| 5376 | /* child: this clobbers and resets the import lock. */ |
| 5377 | PyOS_AfterFork(); |
| 5378 | } else { |
| 5379 | /* parent: release the import lock. */ |
| 5380 | result = _PyImport_ReleaseLock(); |
| 5381 | } |
| 5382 | if (pid == -1) |
| 5383 | return posix_error(); |
| 5384 | if (result < 0) { |
| 5385 | /* Don't clobber the OSError if the fork failed. */ |
| 5386 | PyErr_SetString(PyExc_RuntimeError, |
| 5387 | "not holding the import lock"); |
| 5388 | return NULL; |
| 5389 | } |
| 5390 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5391 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5392 | #endif /* HAVE_FORK */ |
| 5393 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5394 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5395 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5396 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5397 | /*[clinic input] |
| 5398 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5399 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5400 | policy: int |
| 5401 | |
| 5402 | Get the maximum scheduling priority for policy. |
| 5403 | [clinic start generated code]*/ |
| 5404 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5405 | static PyObject * |
| 5406 | os_sched_get_priority_max_impl(PyModuleDef *module, int policy) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5407 | /*[clinic end generated code: output=a6a30fa5071f2d81 input=2097b7998eca6874]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5408 | { |
| 5409 | int max; |
| 5410 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5411 | max = sched_get_priority_max(policy); |
| 5412 | if (max < 0) |
| 5413 | return posix_error(); |
| 5414 | return PyLong_FromLong(max); |
| 5415 | } |
| 5416 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5417 | |
| 5418 | /*[clinic input] |
| 5419 | os.sched_get_priority_min |
| 5420 | |
| 5421 | policy: int |
| 5422 | |
| 5423 | Get the minimum scheduling priority for policy. |
| 5424 | [clinic start generated code]*/ |
| 5425 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5426 | static PyObject * |
| 5427 | os_sched_get_priority_min_impl(PyModuleDef *module, int policy) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5428 | /*[clinic end generated code: output=5ca3ed6bc43e9b20 input=21bc8fa0d70983bf]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5429 | { |
| 5430 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5431 | if (min < 0) |
| 5432 | return posix_error(); |
| 5433 | return PyLong_FromLong(min); |
| 5434 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5435 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 5436 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5437 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5438 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5439 | /*[clinic input] |
| 5440 | os.sched_getscheduler |
| 5441 | pid: pid_t |
| 5442 | / |
| 5443 | |
| 5444 | Get the scheduling policy for the process identifiedy by pid. |
| 5445 | |
| 5446 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 5447 | [clinic start generated code]*/ |
| 5448 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5449 | static PyObject * |
| 5450 | os_sched_getscheduler_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5451 | /*[clinic end generated code: output=8cd63c15caf54fa9 input=5f14cfd1f189e1a0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5452 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5453 | int policy; |
| 5454 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5455 | policy = sched_getscheduler(pid); |
| 5456 | if (policy < 0) |
| 5457 | return posix_error(); |
| 5458 | return PyLong_FromLong(policy); |
| 5459 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5460 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5461 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5462 | |
| 5463 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5464 | /*[clinic input] |
| 5465 | class os.sched_param "PyObject *" "&SchedParamType" |
| 5466 | |
| 5467 | @classmethod |
| 5468 | os.sched_param.__new__ |
| 5469 | |
| 5470 | sched_priority: object |
| 5471 | A scheduling parameter. |
| 5472 | |
| 5473 | Current has only one field: sched_priority"); |
| 5474 | [clinic start generated code]*/ |
| 5475 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5476 | static PyObject * |
| 5477 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5478 | /*[clinic end generated code: output=48f4067d60f48c13 input=73a4c22f7071fc62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5479 | { |
| 5480 | PyObject *res; |
| 5481 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5482 | res = PyStructSequence_New(type); |
| 5483 | if (!res) |
| 5484 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5485 | Py_INCREF(sched_priority); |
| 5486 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5487 | return res; |
| 5488 | } |
| 5489 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5490 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5491 | PyDoc_VAR(os_sched_param__doc__); |
| 5492 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5493 | static PyStructSequence_Field sched_param_fields[] = { |
| 5494 | {"sched_priority", "the scheduling priority"}, |
| 5495 | {0} |
| 5496 | }; |
| 5497 | |
| 5498 | static PyStructSequence_Desc sched_param_desc = { |
| 5499 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5500 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5501 | sched_param_fields, |
| 5502 | 1 |
| 5503 | }; |
| 5504 | |
| 5505 | static int |
| 5506 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 5507 | { |
| 5508 | long priority; |
| 5509 | |
| 5510 | if (Py_TYPE(param) != &SchedParamType) { |
| 5511 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 5512 | return 0; |
| 5513 | } |
| 5514 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 5515 | if (priority == -1 && PyErr_Occurred()) |
| 5516 | return 0; |
| 5517 | if (priority > INT_MAX || priority < INT_MIN) { |
| 5518 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 5519 | return 0; |
| 5520 | } |
| 5521 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 5522 | return 1; |
| 5523 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5524 | #endif /* defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5525 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5526 | |
| 5527 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5528 | /*[clinic input] |
| 5529 | os.sched_setscheduler |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5530 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5531 | pid: pid_t |
| 5532 | policy: int |
| 5533 | param: sched_param |
| 5534 | / |
| 5535 | |
| 5536 | Set the scheduling policy for the process identified by pid. |
| 5537 | |
| 5538 | If pid is 0, the calling process is changed. |
| 5539 | param is an instance of sched_param. |
| 5540 | [clinic start generated code]*/ |
| 5541 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5542 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 5543 | os_sched_setscheduler_impl(PyModuleDef *module, pid_t pid, int policy, |
| 5544 | struct sched_param *param) |
| 5545 | /*[clinic end generated code: output=37053e5c528c35c9 input=c581f9469a5327dd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5546 | { |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5547 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 5548 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 5549 | ** scheduling policy under Solaris/Illumos, and others. |
| 5550 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5551 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5552 | if (sched_setscheduler(pid, policy, param) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5553 | return posix_error(); |
| 5554 | Py_RETURN_NONE; |
| 5555 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5556 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5557 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5558 | |
| 5559 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5560 | /*[clinic input] |
| 5561 | os.sched_getparam |
| 5562 | pid: pid_t |
| 5563 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5564 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5565 | Returns scheduling parameters for the process identified by pid. |
| 5566 | |
| 5567 | If pid is 0, returns parameters for the calling process. |
| 5568 | Return value is an instance of sched_param. |
| 5569 | [clinic start generated code]*/ |
| 5570 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5571 | static PyObject * |
| 5572 | os_sched_getparam_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5573 | /*[clinic end generated code: output=f42c5bd2604ecd08 input=18a1ef9c2efae296]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5574 | { |
| 5575 | struct sched_param param; |
| 5576 | PyObject *result; |
| 5577 | PyObject *priority; |
| 5578 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5579 | if (sched_getparam(pid, ¶m)) |
| 5580 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5581 | result = PyStructSequence_New(&SchedParamType); |
| 5582 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5583 | return NULL; |
| 5584 | priority = PyLong_FromLong(param.sched_priority); |
| 5585 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5586 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5587 | return NULL; |
| 5588 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5589 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 5590 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5591 | } |
| 5592 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5593 | |
| 5594 | /*[clinic input] |
| 5595 | os.sched_setparam |
| 5596 | pid: pid_t |
| 5597 | param: sched_param |
| 5598 | / |
| 5599 | |
| 5600 | Set scheduling parameters for the process identified by pid. |
| 5601 | |
| 5602 | If pid is 0, sets parameters for the calling process. |
| 5603 | param should be an instance of sched_param. |
| 5604 | [clinic start generated code]*/ |
| 5605 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5606 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 5607 | os_sched_setparam_impl(PyModuleDef *module, pid_t pid, |
| 5608 | struct sched_param *param) |
| 5609 | /*[clinic end generated code: output=b7a3c589436cec9b input=6b8d6dfcecdc21bd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5610 | { |
| 5611 | if (sched_setparam(pid, param)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5612 | return posix_error(); |
| 5613 | Py_RETURN_NONE; |
| 5614 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5615 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5616 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5617 | |
| 5618 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5619 | /*[clinic input] |
| 5620 | os.sched_rr_get_interval -> double |
| 5621 | pid: pid_t |
| 5622 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5623 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5624 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 5625 | |
| 5626 | Value returned is a float. |
| 5627 | [clinic start generated code]*/ |
| 5628 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5629 | static double |
| 5630 | os_sched_rr_get_interval_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5631 | /*[clinic end generated code: output=7adc137a86dea581 input=2a973da15cca6fae]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5632 | { |
| 5633 | struct timespec interval; |
| 5634 | if (sched_rr_get_interval(pid, &interval)) { |
| 5635 | posix_error(); |
| 5636 | return -1.0; |
| 5637 | } |
| 5638 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 5639 | } |
| 5640 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5641 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5642 | |
| 5643 | /*[clinic input] |
| 5644 | os.sched_yield |
| 5645 | |
| 5646 | Voluntarily relinquish the CPU. |
| 5647 | [clinic start generated code]*/ |
| 5648 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5649 | static PyObject * |
| 5650 | os_sched_yield_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5651 | /*[clinic end generated code: output=d7bd51869c4cb6a8 input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5652 | { |
| 5653 | if (sched_yield()) |
| 5654 | return posix_error(); |
| 5655 | Py_RETURN_NONE; |
| 5656 | } |
| 5657 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5658 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5659 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 5660 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5661 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5662 | /*[clinic input] |
| 5663 | os.sched_setaffinity |
| 5664 | pid: pid_t |
| 5665 | mask : object |
| 5666 | / |
| 5667 | |
| 5668 | Set the CPU affinity of the process identified by pid to mask. |
| 5669 | |
| 5670 | mask should be an iterable of integers identifying CPUs. |
| 5671 | [clinic start generated code]*/ |
| 5672 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5673 | static PyObject * |
| 5674 | os_sched_setaffinity_impl(PyModuleDef *module, pid_t pid, PyObject *mask) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5675 | /*[clinic end generated code: output=582bcbf40d3253a9 input=a0791a597c7085ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5676 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5677 | int ncpus; |
| 5678 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5679 | cpu_set_t *cpu_set = NULL; |
| 5680 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5681 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5682 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5683 | if (iterator == NULL) |
| 5684 | return NULL; |
| 5685 | |
| 5686 | ncpus = NCPUS_START; |
| 5687 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5688 | cpu_set = CPU_ALLOC(ncpus); |
| 5689 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5690 | PyErr_NoMemory(); |
| 5691 | goto error; |
| 5692 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5693 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5694 | |
| 5695 | while ((item = PyIter_Next(iterator))) { |
| 5696 | long cpu; |
| 5697 | if (!PyLong_Check(item)) { |
| 5698 | PyErr_Format(PyExc_TypeError, |
| 5699 | "expected an iterator of ints, " |
| 5700 | "but iterator yielded %R", |
| 5701 | Py_TYPE(item)); |
| 5702 | Py_DECREF(item); |
| 5703 | goto error; |
| 5704 | } |
| 5705 | cpu = PyLong_AsLong(item); |
| 5706 | Py_DECREF(item); |
| 5707 | if (cpu < 0) { |
| 5708 | if (!PyErr_Occurred()) |
| 5709 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 5710 | goto error; |
| 5711 | } |
| 5712 | if (cpu > INT_MAX - 1) { |
| 5713 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 5714 | goto error; |
| 5715 | } |
| 5716 | if (cpu >= ncpus) { |
| 5717 | /* Grow CPU mask to fit the CPU number */ |
| 5718 | int newncpus = ncpus; |
| 5719 | cpu_set_t *newmask; |
| 5720 | size_t newsetsize; |
| 5721 | while (newncpus <= cpu) { |
| 5722 | if (newncpus > INT_MAX / 2) |
| 5723 | newncpus = cpu + 1; |
| 5724 | else |
| 5725 | newncpus = newncpus * 2; |
| 5726 | } |
| 5727 | newmask = CPU_ALLOC(newncpus); |
| 5728 | if (newmask == NULL) { |
| 5729 | PyErr_NoMemory(); |
| 5730 | goto error; |
| 5731 | } |
| 5732 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 5733 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5734 | memcpy(newmask, cpu_set, setsize); |
| 5735 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5736 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5737 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5738 | ncpus = newncpus; |
| 5739 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5740 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5741 | } |
| 5742 | Py_CLEAR(iterator); |
| 5743 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5744 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5745 | posix_error(); |
| 5746 | goto error; |
| 5747 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5748 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5749 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5750 | |
| 5751 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5752 | if (cpu_set) |
| 5753 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5754 | Py_XDECREF(iterator); |
| 5755 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5756 | } |
| 5757 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5758 | |
| 5759 | /*[clinic input] |
| 5760 | os.sched_getaffinity |
| 5761 | pid: pid_t |
| 5762 | / |
| 5763 | |
Charles-François Natali | dc87e4b | 2015-07-13 21:01:39 +0100 | [diff] [blame] | 5764 | 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] | 5765 | |
| 5766 | The affinity is returned as a set of CPU identifiers. |
| 5767 | [clinic start generated code]*/ |
| 5768 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5769 | static PyObject * |
| 5770 | os_sched_getaffinity_impl(PyModuleDef *module, pid_t pid) |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 5771 | /*[clinic end generated code: output=b431a8f310e369e7 input=983ce7cb4a565980]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5772 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5773 | int cpu, ncpus, count; |
| 5774 | size_t setsize; |
| 5775 | cpu_set_t *mask = NULL; |
| 5776 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5777 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5778 | ncpus = NCPUS_START; |
| 5779 | while (1) { |
| 5780 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5781 | mask = CPU_ALLOC(ncpus); |
| 5782 | if (mask == NULL) |
| 5783 | return PyErr_NoMemory(); |
| 5784 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 5785 | break; |
| 5786 | CPU_FREE(mask); |
| 5787 | if (errno != EINVAL) |
| 5788 | return posix_error(); |
| 5789 | if (ncpus > INT_MAX / 2) { |
| 5790 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 5791 | "a large enough CPU set"); |
| 5792 | return NULL; |
| 5793 | } |
| 5794 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5795 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5796 | |
| 5797 | res = PySet_New(NULL); |
| 5798 | if (res == NULL) |
| 5799 | goto error; |
| 5800 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 5801 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 5802 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 5803 | --count; |
| 5804 | if (cpu_num == NULL) |
| 5805 | goto error; |
| 5806 | if (PySet_Add(res, cpu_num)) { |
| 5807 | Py_DECREF(cpu_num); |
| 5808 | goto error; |
| 5809 | } |
| 5810 | Py_DECREF(cpu_num); |
| 5811 | } |
| 5812 | } |
| 5813 | CPU_FREE(mask); |
| 5814 | return res; |
| 5815 | |
| 5816 | error: |
| 5817 | if (mask) |
| 5818 | CPU_FREE(mask); |
| 5819 | Py_XDECREF(res); |
| 5820 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5821 | } |
| 5822 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5823 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5824 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5825 | #endif /* HAVE_SCHED_H */ |
| 5826 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5827 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5828 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5829 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5830 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5831 | #define DEV_PTY_FILE "/dev/ptc" |
| 5832 | #define HAVE_DEV_PTMX |
| 5833 | #else |
| 5834 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5835 | #endif |
| 5836 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5837 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5838 | #ifdef HAVE_PTY_H |
| 5839 | #include <pty.h> |
| 5840 | #else |
| 5841 | #ifdef HAVE_LIBUTIL_H |
| 5842 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5843 | #else |
| 5844 | #ifdef HAVE_UTIL_H |
| 5845 | #include <util.h> |
| 5846 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5847 | #endif /* HAVE_LIBUTIL_H */ |
| 5848 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5849 | #ifdef HAVE_STROPTS_H |
| 5850 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5851 | #endif |
| 5852 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5853 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5854 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5855 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5856 | /*[clinic input] |
| 5857 | os.openpty |
| 5858 | |
| 5859 | Open a pseudo-terminal. |
| 5860 | |
| 5861 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 5862 | for both the master and slave ends. |
| 5863 | [clinic start generated code]*/ |
| 5864 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5865 | static PyObject * |
| 5866 | os_openpty_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5867 | /*[clinic end generated code: output=358e571c1ba135ee input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5868 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5869 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5870 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5871 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5872 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5873 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5874 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5875 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5876 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5877 | #endif |
| 5878 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5879 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5880 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5881 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5882 | goto posix_error; |
| 5883 | |
| 5884 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5885 | goto error; |
| 5886 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 5887 | goto error; |
| 5888 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5889 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5890 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5891 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5892 | goto posix_error; |
| 5893 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5894 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5895 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5896 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5897 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 5898 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5899 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5900 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 5901 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5902 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5903 | goto posix_error; |
| 5904 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5905 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5906 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5907 | /* change permission of slave */ |
| 5908 | if (grantpt(master_fd) < 0) { |
| 5909 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5910 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5911 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5912 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5913 | /* unlock slave */ |
| 5914 | if (unlockpt(master_fd) < 0) { |
| 5915 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5916 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5917 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5918 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5919 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5920 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5921 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 5922 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5923 | goto posix_error; |
| 5924 | |
| 5925 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 5926 | if (slave_fd == -1) |
| 5927 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 5928 | |
| 5929 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5930 | goto posix_error; |
| 5931 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5932 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5933 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 5934 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5935 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5936 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5937 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5938 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 5939 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5940 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5941 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5942 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5943 | posix_error: |
| 5944 | posix_error(); |
| 5945 | error: |
| 5946 | if (master_fd != -1) |
| 5947 | close(master_fd); |
| 5948 | if (slave_fd != -1) |
| 5949 | close(slave_fd); |
| 5950 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5951 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5952 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5953 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5954 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5955 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5956 | /*[clinic input] |
| 5957 | os.forkpty |
| 5958 | |
| 5959 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 5960 | |
| 5961 | Returns a tuple of (pid, master_fd). |
| 5962 | Like fork(), return pid of 0 to the child process, |
| 5963 | and pid of child to the parent process. |
| 5964 | To both, return fd of newly opened pseudo-terminal. |
| 5965 | [clinic start generated code]*/ |
| 5966 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5967 | static PyObject * |
| 5968 | os_forkpty_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5969 | /*[clinic end generated code: output=a11b8391dce3cb57 input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5970 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5971 | int master_fd = -1, result = 0; |
| 5972 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5973 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5974 | _PyImport_AcquireLock(); |
| 5975 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 5976 | if (pid == 0) { |
| 5977 | /* child: this clobbers and resets the import lock. */ |
| 5978 | PyOS_AfterFork(); |
| 5979 | } else { |
| 5980 | /* parent: release the import lock. */ |
| 5981 | result = _PyImport_ReleaseLock(); |
| 5982 | } |
| 5983 | if (pid == -1) |
| 5984 | return posix_error(); |
| 5985 | if (result < 0) { |
| 5986 | /* Don't clobber the OSError if the fork failed. */ |
| 5987 | PyErr_SetString(PyExc_RuntimeError, |
| 5988 | "not holding the import lock"); |
| 5989 | return NULL; |
| 5990 | } |
| 5991 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5992 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5993 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5994 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5995 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5996 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5997 | /*[clinic input] |
| 5998 | os.getegid |
| 5999 | |
| 6000 | Return the current process's effective group id. |
| 6001 | [clinic start generated code]*/ |
| 6002 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6003 | static PyObject * |
| 6004 | os_getegid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6005 | /*[clinic end generated code: output=90f433a8c0b1d919 input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6006 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6007 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6008 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6009 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6010 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6011 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6012 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6013 | /*[clinic input] |
| 6014 | os.geteuid |
| 6015 | |
| 6016 | Return the current process's effective user id. |
| 6017 | [clinic start generated code]*/ |
| 6018 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6019 | static PyObject * |
| 6020 | os_geteuid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6021 | /*[clinic end generated code: output=1a532c4a66874357 input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6022 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6023 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6024 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6025 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6026 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6027 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6028 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6029 | /*[clinic input] |
| 6030 | os.getgid |
| 6031 | |
| 6032 | Return the current process's group id. |
| 6033 | [clinic start generated code]*/ |
| 6034 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6035 | static PyObject * |
| 6036 | os_getgid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6037 | /*[clinic end generated code: output=91a22021b74ea46b input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6038 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6039 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6040 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6041 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6042 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6043 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6044 | /*[clinic input] |
| 6045 | os.getpid |
| 6046 | |
| 6047 | Return the current process id. |
| 6048 | [clinic start generated code]*/ |
| 6049 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6050 | static PyObject * |
| 6051 | os_getpid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6052 | /*[clinic end generated code: output=8fbf3a934ee09e62 input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6053 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6054 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6055 | } |
| 6056 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6057 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6058 | |
| 6059 | /* AC 3.5: funny apple logic below */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6060 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 6061 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 6062 | Returns a list of groups to which a user belongs.\n\n\ |
| 6063 | user: username to lookup\n\ |
| 6064 | group: base group id of the user"); |
| 6065 | |
| 6066 | static PyObject * |
| 6067 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 6068 | { |
| 6069 | #ifdef NGROUPS_MAX |
| 6070 | #define MAX_GROUPS NGROUPS_MAX |
| 6071 | #else |
| 6072 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6073 | #define MAX_GROUPS 64 |
| 6074 | #endif |
| 6075 | |
| 6076 | const char *user; |
| 6077 | int i, ngroups; |
| 6078 | PyObject *list; |
| 6079 | #ifdef __APPLE__ |
| 6080 | int *groups, basegid; |
| 6081 | #else |
| 6082 | gid_t *groups, basegid; |
| 6083 | #endif |
| 6084 | ngroups = MAX_GROUPS; |
| 6085 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6086 | #ifdef __APPLE__ |
| 6087 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6088 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6089 | #else |
| 6090 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 6091 | _Py_Gid_Converter, &basegid)) |
| 6092 | return NULL; |
| 6093 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6094 | |
| 6095 | #ifdef __APPLE__ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6096 | groups = PyMem_New(int, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6097 | #else |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6098 | groups = PyMem_New(gid_t, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6099 | #endif |
| 6100 | if (groups == NULL) |
| 6101 | return PyErr_NoMemory(); |
| 6102 | |
| 6103 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 6104 | PyMem_Del(groups); |
| 6105 | return posix_error(); |
| 6106 | } |
| 6107 | |
| 6108 | list = PyList_New(ngroups); |
| 6109 | if (list == NULL) { |
| 6110 | PyMem_Del(groups); |
| 6111 | return NULL; |
| 6112 | } |
| 6113 | |
| 6114 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6115 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6116 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6117 | #else |
| 6118 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 6119 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6120 | if (o == NULL) { |
| 6121 | Py_DECREF(list); |
| 6122 | PyMem_Del(groups); |
| 6123 | return NULL; |
| 6124 | } |
| 6125 | PyList_SET_ITEM(list, i, o); |
| 6126 | } |
| 6127 | |
| 6128 | PyMem_Del(groups); |
| 6129 | |
| 6130 | return list; |
| 6131 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6132 | #endif /* HAVE_GETGROUPLIST */ |
| 6133 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6134 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6135 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6136 | /*[clinic input] |
| 6137 | os.getgroups |
| 6138 | |
| 6139 | Return list of supplemental group IDs for the process. |
| 6140 | [clinic start generated code]*/ |
| 6141 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6142 | static PyObject * |
| 6143 | os_getgroups_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6144 | /*[clinic end generated code: output=6e7c4fd2db6d5c60 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6145 | { |
| 6146 | PyObject *result = NULL; |
| 6147 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6148 | #ifdef NGROUPS_MAX |
| 6149 | #define MAX_GROUPS NGROUPS_MAX |
| 6150 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6151 | /* 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] | 6152 | #define MAX_GROUPS 64 |
| 6153 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6154 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6155 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6156 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6157 | * This is a helper variable to store the intermediate result when |
| 6158 | * that happens. |
| 6159 | * |
| 6160 | * To keep the code readable the OSX behaviour is unconditional, |
| 6161 | * according to the POSIX spec this should be safe on all unix-y |
| 6162 | * systems. |
| 6163 | */ |
| 6164 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6165 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6166 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6167 | #ifdef __APPLE__ |
| 6168 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 6169 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 6170 | * always first call getgroups with length 0 to get the actual number |
| 6171 | * of groups. |
| 6172 | */ |
| 6173 | n = getgroups(0, NULL); |
| 6174 | if (n < 0) { |
| 6175 | return posix_error(); |
| 6176 | } else if (n <= MAX_GROUPS) { |
| 6177 | /* groups will fit in existing array */ |
| 6178 | alt_grouplist = grouplist; |
| 6179 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6180 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6181 | if (alt_grouplist == NULL) { |
| 6182 | errno = EINVAL; |
| 6183 | return posix_error(); |
| 6184 | } |
| 6185 | } |
| 6186 | |
| 6187 | n = getgroups(n, alt_grouplist); |
| 6188 | if (n == -1) { |
| 6189 | if (alt_grouplist != grouplist) { |
| 6190 | PyMem_Free(alt_grouplist); |
| 6191 | } |
| 6192 | return posix_error(); |
| 6193 | } |
| 6194 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6195 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6196 | if (n < 0) { |
| 6197 | if (errno == EINVAL) { |
| 6198 | n = getgroups(0, NULL); |
| 6199 | if (n == -1) { |
| 6200 | return posix_error(); |
| 6201 | } |
| 6202 | if (n == 0) { |
| 6203 | /* Avoid malloc(0) */ |
| 6204 | alt_grouplist = grouplist; |
| 6205 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6206 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6207 | if (alt_grouplist == NULL) { |
| 6208 | errno = EINVAL; |
| 6209 | return posix_error(); |
| 6210 | } |
| 6211 | n = getgroups(n, alt_grouplist); |
| 6212 | if (n == -1) { |
| 6213 | PyMem_Free(alt_grouplist); |
| 6214 | return posix_error(); |
| 6215 | } |
| 6216 | } |
| 6217 | } else { |
| 6218 | return posix_error(); |
| 6219 | } |
| 6220 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6221 | #endif |
| 6222 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6223 | result = PyList_New(n); |
| 6224 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6225 | int i; |
| 6226 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6227 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6228 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6229 | Py_DECREF(result); |
| 6230 | result = NULL; |
| 6231 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6232 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6233 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6234 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6235 | } |
| 6236 | |
| 6237 | if (alt_grouplist != grouplist) { |
| 6238 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6239 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6240 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6241 | return result; |
| 6242 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6243 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6244 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6245 | #ifdef HAVE_INITGROUPS |
| 6246 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 6247 | "initgroups(username, gid) -> None\n\n\ |
| 6248 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 6249 | the groups of which the specified username is a member, plus the specified\n\ |
| 6250 | group id."); |
| 6251 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6252 | /* AC 3.5: funny apple logic */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6253 | static PyObject * |
| 6254 | posix_initgroups(PyObject *self, PyObject *args) |
| 6255 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6256 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6257 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6258 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6259 | #ifdef __APPLE__ |
| 6260 | int gid; |
| 6261 | #else |
| 6262 | gid_t gid; |
| 6263 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6264 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6265 | #ifdef __APPLE__ |
| 6266 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 6267 | PyUnicode_FSConverter, &oname, |
| 6268 | &gid)) |
| 6269 | #else |
| 6270 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 6271 | PyUnicode_FSConverter, &oname, |
| 6272 | _Py_Gid_Converter, &gid)) |
| 6273 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6274 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6275 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6276 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6277 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6278 | Py_DECREF(oname); |
| 6279 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6280 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6281 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6282 | Py_INCREF(Py_None); |
| 6283 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6284 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6285 | #endif /* HAVE_INITGROUPS */ |
| 6286 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6287 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6288 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6289 | /*[clinic input] |
| 6290 | os.getpgid |
| 6291 | |
| 6292 | pid: pid_t |
| 6293 | |
| 6294 | Call the system call getpgid(), and return the result. |
| 6295 | [clinic start generated code]*/ |
| 6296 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6297 | static PyObject * |
| 6298 | os_getpgid_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6299 | /*[clinic end generated code: output=70e713b4d54b7c61 input=39d710ae3baaf1c7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6300 | { |
| 6301 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6302 | if (pgid < 0) |
| 6303 | return posix_error(); |
| 6304 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6305 | } |
| 6306 | #endif /* HAVE_GETPGID */ |
| 6307 | |
| 6308 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6309 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6310 | /*[clinic input] |
| 6311 | os.getpgrp |
| 6312 | |
| 6313 | Return the current process group id. |
| 6314 | [clinic start generated code]*/ |
| 6315 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6316 | static PyObject * |
| 6317 | os_getpgrp_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6318 | /*[clinic end generated code: output=cf3403585846811f input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6319 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6320 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6321 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6322 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6323 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6324 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6325 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6326 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6327 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6328 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6329 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6330 | /*[clinic input] |
| 6331 | os.setpgrp |
| 6332 | |
| 6333 | Make the current process the leader of its process group. |
| 6334 | [clinic start generated code]*/ |
| 6335 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6336 | static PyObject * |
| 6337 | os_setpgrp_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6338 | /*[clinic end generated code: output=59650f55a963d7ac input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6339 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6340 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6341 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6342 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6343 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6344 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6345 | return posix_error(); |
| 6346 | Py_INCREF(Py_None); |
| 6347 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6348 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6349 | #endif /* HAVE_SETPGRP */ |
| 6350 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6351 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6352 | |
| 6353 | #ifdef MS_WINDOWS |
| 6354 | #include <tlhelp32.h> |
| 6355 | |
| 6356 | static PyObject* |
| 6357 | win32_getppid() |
| 6358 | { |
| 6359 | HANDLE snapshot; |
| 6360 | pid_t mypid; |
| 6361 | PyObject* result = NULL; |
| 6362 | BOOL have_record; |
| 6363 | PROCESSENTRY32 pe; |
| 6364 | |
| 6365 | mypid = getpid(); /* This function never fails */ |
| 6366 | |
| 6367 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 6368 | if (snapshot == INVALID_HANDLE_VALUE) |
| 6369 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 6370 | |
| 6371 | pe.dwSize = sizeof(pe); |
| 6372 | have_record = Process32First(snapshot, &pe); |
| 6373 | while (have_record) { |
| 6374 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 6375 | /* We could cache the ulong value in a static variable. */ |
| 6376 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 6377 | break; |
| 6378 | } |
| 6379 | |
| 6380 | have_record = Process32Next(snapshot, &pe); |
| 6381 | } |
| 6382 | |
| 6383 | /* If our loop exits and our pid was not found (result will be NULL) |
| 6384 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 6385 | * error anyway, so let's raise it. */ |
| 6386 | if (!result) |
| 6387 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6388 | |
| 6389 | CloseHandle(snapshot); |
| 6390 | |
| 6391 | return result; |
| 6392 | } |
| 6393 | #endif /*MS_WINDOWS*/ |
| 6394 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6395 | |
| 6396 | /*[clinic input] |
| 6397 | os.getppid |
| 6398 | |
| 6399 | Return the parent's process id. |
| 6400 | |
| 6401 | If the parent process has already exited, Windows machines will still |
| 6402 | return its id; others systems will return the id of the 'init' process (1). |
| 6403 | [clinic start generated code]*/ |
| 6404 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6405 | static PyObject * |
| 6406 | os_getppid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6407 | /*[clinic end generated code: output=4e49c8e7a8738cd2 input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6408 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6409 | #ifdef MS_WINDOWS |
| 6410 | return win32_getppid(); |
| 6411 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6412 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6413 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6414 | } |
| 6415 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6416 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6417 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6418 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6419 | /*[clinic input] |
| 6420 | os.getlogin |
| 6421 | |
| 6422 | Return the actual login name. |
| 6423 | [clinic start generated code]*/ |
| 6424 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6425 | static PyObject * |
| 6426 | os_getlogin_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6427 | /*[clinic end generated code: output=037ebdb3e4b5dac1 input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6428 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6429 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6430 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6431 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6432 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6433 | |
| 6434 | if (GetUserNameW(user_name, &num_chars)) { |
| 6435 | /* num_chars is the number of unicode chars plus null terminator */ |
| 6436 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6437 | } |
| 6438 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6439 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6440 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6441 | char *name; |
| 6442 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6443 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6444 | errno = 0; |
| 6445 | name = getlogin(); |
| 6446 | if (name == NULL) { |
| 6447 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6448 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6449 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6450 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6451 | } |
| 6452 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6453 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6454 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6455 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6456 | return result; |
| 6457 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6458 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6459 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6460 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6461 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6462 | /*[clinic input] |
| 6463 | os.getuid |
| 6464 | |
| 6465 | Return the current process's user id. |
| 6466 | [clinic start generated code]*/ |
| 6467 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6468 | static PyObject * |
| 6469 | os_getuid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6470 | /*[clinic end generated code: output=03a8b894cefb3fa5 input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6471 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6472 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6473 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6474 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6475 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6476 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6477 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6478 | #define HAVE_KILL |
| 6479 | #endif /* MS_WINDOWS */ |
| 6480 | |
| 6481 | #ifdef HAVE_KILL |
| 6482 | /*[clinic input] |
| 6483 | os.kill |
| 6484 | |
| 6485 | pid: pid_t |
| 6486 | signal: Py_ssize_t |
| 6487 | / |
| 6488 | |
| 6489 | Kill a process with a signal. |
| 6490 | [clinic start generated code]*/ |
| 6491 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6492 | static PyObject * |
| 6493 | os_kill_impl(PyModuleDef *module, pid_t pid, Py_ssize_t signal) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6494 | /*[clinic end generated code: output=74f907dd00a83c26 input=61a36b86ca275ab9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6495 | #ifndef MS_WINDOWS |
| 6496 | { |
| 6497 | if (kill(pid, (int)signal) == -1) |
| 6498 | return posix_error(); |
| 6499 | Py_RETURN_NONE; |
| 6500 | } |
| 6501 | #else /* !MS_WINDOWS */ |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6502 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 6503 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6504 | DWORD sig = (DWORD)signal; |
| 6505 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6506 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6507 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6508 | /* Console processes which share a common console can be sent CTRL+C or |
| 6509 | CTRL+BREAK events, provided they handle said events. */ |
| 6510 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6511 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6512 | err = GetLastError(); |
| 6513 | PyErr_SetFromWindowsErr(err); |
| 6514 | } |
| 6515 | else |
| 6516 | Py_RETURN_NONE; |
| 6517 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6518 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6519 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 6520 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6521 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6522 | if (handle == NULL) { |
| 6523 | err = GetLastError(); |
| 6524 | return PyErr_SetFromWindowsErr(err); |
| 6525 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6526 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6527 | if (TerminateProcess(handle, sig) == 0) { |
| 6528 | err = GetLastError(); |
| 6529 | result = PyErr_SetFromWindowsErr(err); |
| 6530 | } else { |
| 6531 | Py_INCREF(Py_None); |
| 6532 | result = Py_None; |
| 6533 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6534 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6535 | CloseHandle(handle); |
| 6536 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6537 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6538 | #endif /* !MS_WINDOWS */ |
| 6539 | #endif /* HAVE_KILL */ |
| 6540 | |
| 6541 | |
| 6542 | #ifdef HAVE_KILLPG |
| 6543 | /*[clinic input] |
| 6544 | os.killpg |
| 6545 | |
| 6546 | pgid: pid_t |
| 6547 | signal: int |
| 6548 | / |
| 6549 | |
| 6550 | Kill a process group with a signal. |
| 6551 | [clinic start generated code]*/ |
| 6552 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6553 | static PyObject * |
| 6554 | os_killpg_impl(PyModuleDef *module, pid_t pgid, int signal) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6555 | /*[clinic end generated code: output=3434a766ef945f93 input=38b5449eb8faec19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6556 | { |
| 6557 | /* XXX some man pages make the `pgid` parameter an int, others |
| 6558 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 6559 | take the same type. Moreover, pid_t is always at least as wide as |
| 6560 | int (else compilation of this module fails), which is safe. */ |
| 6561 | if (killpg(pgid, signal) == -1) |
| 6562 | return posix_error(); |
| 6563 | Py_RETURN_NONE; |
| 6564 | } |
| 6565 | #endif /* HAVE_KILLPG */ |
| 6566 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6567 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6568 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6569 | #ifdef HAVE_SYS_LOCK_H |
| 6570 | #include <sys/lock.h> |
| 6571 | #endif |
| 6572 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6573 | /*[clinic input] |
| 6574 | os.plock |
| 6575 | op: int |
| 6576 | / |
| 6577 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6578 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6579 | [clinic start generated code]*/ |
| 6580 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6581 | static PyObject * |
| 6582 | os_plock_impl(PyModuleDef *module, int op) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6583 | /*[clinic end generated code: output=5cb851f81b914984 input=e6e5e348e1525f60]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6584 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6585 | if (plock(op) == -1) |
| 6586 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6587 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6588 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6589 | #endif /* HAVE_PLOCK */ |
| 6590 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6591 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6592 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6593 | /*[clinic input] |
| 6594 | os.setuid |
| 6595 | |
| 6596 | uid: uid_t |
| 6597 | / |
| 6598 | |
| 6599 | Set the current process's user id. |
| 6600 | [clinic start generated code]*/ |
| 6601 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6602 | static PyObject * |
| 6603 | os_setuid_impl(PyModuleDef *module, uid_t uid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6604 | /*[clinic end generated code: output=941ea9a8d1e5d565 input=c921a3285aa22256]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6605 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6606 | if (setuid(uid) < 0) |
| 6607 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6608 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6609 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6610 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6611 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6612 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6613 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6614 | /*[clinic input] |
| 6615 | os.seteuid |
| 6616 | |
| 6617 | euid: uid_t |
| 6618 | / |
| 6619 | |
| 6620 | Set the current process's effective user id. |
| 6621 | [clinic start generated code]*/ |
| 6622 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6623 | static PyObject * |
| 6624 | os_seteuid_impl(PyModuleDef *module, uid_t euid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6625 | /*[clinic end generated code: output=66f4f6823a648d6d input=ba93d927e4781aa9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6626 | { |
| 6627 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6628 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6629 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6630 | } |
| 6631 | #endif /* HAVE_SETEUID */ |
| 6632 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6633 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6634 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6635 | /*[clinic input] |
| 6636 | os.setegid |
| 6637 | |
| 6638 | egid: gid_t |
| 6639 | / |
| 6640 | |
| 6641 | Set the current process's effective group id. |
| 6642 | [clinic start generated code]*/ |
| 6643 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6644 | static PyObject * |
| 6645 | os_setegid_impl(PyModuleDef *module, gid_t egid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6646 | /*[clinic end generated code: output=ca094a69a081a60f input=4080526d0ccd6ce3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6647 | { |
| 6648 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6649 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6650 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6651 | } |
| 6652 | #endif /* HAVE_SETEGID */ |
| 6653 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6654 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6655 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6656 | /*[clinic input] |
| 6657 | os.setreuid |
| 6658 | |
| 6659 | ruid: uid_t |
| 6660 | euid: uid_t |
| 6661 | / |
| 6662 | |
| 6663 | Set the current process's real and effective user ids. |
| 6664 | [clinic start generated code]*/ |
| 6665 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6666 | static PyObject * |
| 6667 | os_setreuid_impl(PyModuleDef *module, uid_t ruid, uid_t euid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6668 | /*[clinic end generated code: output=b2938c3e73d27ec7 input=0ca8978de663880c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6669 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6670 | if (setreuid(ruid, euid) < 0) { |
| 6671 | return posix_error(); |
| 6672 | } else { |
| 6673 | Py_INCREF(Py_None); |
| 6674 | return Py_None; |
| 6675 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6676 | } |
| 6677 | #endif /* HAVE_SETREUID */ |
| 6678 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6679 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6680 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6681 | /*[clinic input] |
| 6682 | os.setregid |
| 6683 | |
| 6684 | rgid: gid_t |
| 6685 | egid: gid_t |
| 6686 | / |
| 6687 | |
| 6688 | Set the current process's real and effective group ids. |
| 6689 | [clinic start generated code]*/ |
| 6690 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6691 | static PyObject * |
| 6692 | os_setregid_impl(PyModuleDef *module, gid_t rgid, gid_t egid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6693 | /*[clinic end generated code: output=db18f1839ababe3d input=c59499f72846db78]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6694 | { |
| 6695 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6696 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6697 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6698 | } |
| 6699 | #endif /* HAVE_SETREGID */ |
| 6700 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6701 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6702 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6703 | /*[clinic input] |
| 6704 | os.setgid |
| 6705 | gid: gid_t |
| 6706 | / |
| 6707 | |
| 6708 | Set the current process's group id. |
| 6709 | [clinic start generated code]*/ |
| 6710 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6711 | static PyObject * |
| 6712 | os_setgid_impl(PyModuleDef *module, gid_t gid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6713 | /*[clinic end generated code: output=756cb42c6abd9d87 input=27d30c4059045dc6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6714 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6715 | if (setgid(gid) < 0) |
| 6716 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6717 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6718 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6719 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6720 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6721 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6722 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6723 | /*[clinic input] |
| 6724 | os.setgroups |
| 6725 | |
| 6726 | groups: object |
| 6727 | / |
| 6728 | |
| 6729 | Set the groups of the current process to list. |
| 6730 | [clinic start generated code]*/ |
| 6731 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6732 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6733 | os_setgroups(PyModuleDef *module, PyObject *groups) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6734 | /*[clinic end generated code: output=7945c2e3cc817c58 input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6735 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6736 | int i, len; |
| 6737 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6738 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6739 | if (!PySequence_Check(groups)) { |
| 6740 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6741 | return NULL; |
| 6742 | } |
| 6743 | len = PySequence_Size(groups); |
| 6744 | if (len > MAX_GROUPS) { |
| 6745 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6746 | return NULL; |
| 6747 | } |
| 6748 | for(i = 0; i < len; i++) { |
| 6749 | PyObject *elem; |
| 6750 | elem = PySequence_GetItem(groups, i); |
| 6751 | if (!elem) |
| 6752 | return NULL; |
| 6753 | if (!PyLong_Check(elem)) { |
| 6754 | PyErr_SetString(PyExc_TypeError, |
| 6755 | "groups must be integers"); |
| 6756 | Py_DECREF(elem); |
| 6757 | return NULL; |
| 6758 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6759 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6760 | Py_DECREF(elem); |
| 6761 | return NULL; |
| 6762 | } |
| 6763 | } |
| 6764 | Py_DECREF(elem); |
| 6765 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6766 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6767 | if (setgroups(len, grouplist) < 0) |
| 6768 | return posix_error(); |
| 6769 | Py_INCREF(Py_None); |
| 6770 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6771 | } |
| 6772 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6773 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6774 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6775 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6776 | wait_helper(pid_t pid, int status, struct rusage *ru) |
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 | PyObject *result; |
| 6779 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6780 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6781 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6782 | if (pid == -1) |
| 6783 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6784 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6785 | if (struct_rusage == NULL) { |
| 6786 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6787 | if (m == NULL) |
| 6788 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6789 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6790 | Py_DECREF(m); |
| 6791 | if (struct_rusage == NULL) |
| 6792 | return NULL; |
| 6793 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6794 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6795 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6796 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6797 | if (!result) |
| 6798 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6799 | |
| 6800 | #ifndef doubletime |
| 6801 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6802 | #endif |
| 6803 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6804 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6805 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6806 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6807 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6808 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6809 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6810 | SET_INT(result, 2, ru->ru_maxrss); |
| 6811 | SET_INT(result, 3, ru->ru_ixrss); |
| 6812 | SET_INT(result, 4, ru->ru_idrss); |
| 6813 | SET_INT(result, 5, ru->ru_isrss); |
| 6814 | SET_INT(result, 6, ru->ru_minflt); |
| 6815 | SET_INT(result, 7, ru->ru_majflt); |
| 6816 | SET_INT(result, 8, ru->ru_nswap); |
| 6817 | SET_INT(result, 9, ru->ru_inblock); |
| 6818 | SET_INT(result, 10, ru->ru_oublock); |
| 6819 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6820 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6821 | SET_INT(result, 13, ru->ru_nsignals); |
| 6822 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6823 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6824 | #undef SET_INT |
| 6825 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6826 | if (PyErr_Occurred()) { |
| 6827 | Py_DECREF(result); |
| 6828 | return NULL; |
| 6829 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6830 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6831 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6832 | } |
| 6833 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6834 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6835 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6836 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6837 | /*[clinic input] |
| 6838 | os.wait3 |
| 6839 | |
| 6840 | options: int |
| 6841 | Wait for completion of a child process. |
| 6842 | |
| 6843 | Returns a tuple of information about the child process: |
| 6844 | (pid, status, rusage) |
| 6845 | [clinic start generated code]*/ |
| 6846 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6847 | static PyObject * |
| 6848 | os_wait3_impl(PyModuleDef *module, int options) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6849 | /*[clinic end generated code: output=e18af4924dc54945 input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6850 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6851 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6852 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6853 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6854 | WAIT_TYPE status; |
| 6855 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6856 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6857 | do { |
| 6858 | Py_BEGIN_ALLOW_THREADS |
| 6859 | pid = wait3(&status, options, &ru); |
| 6860 | Py_END_ALLOW_THREADS |
| 6861 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6862 | if (pid < 0) |
| 6863 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6864 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6865 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6866 | } |
| 6867 | #endif /* HAVE_WAIT3 */ |
| 6868 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6869 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6870 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6871 | /*[clinic input] |
| 6872 | |
| 6873 | os.wait4 |
| 6874 | |
| 6875 | pid: pid_t |
| 6876 | options: int |
| 6877 | |
| 6878 | Wait for completion of a specific child process. |
| 6879 | |
| 6880 | Returns a tuple of information about the child process: |
| 6881 | (pid, status, rusage) |
| 6882 | [clinic start generated code]*/ |
| 6883 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6884 | static PyObject * |
| 6885 | os_wait4_impl(PyModuleDef *module, pid_t pid, int options) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6886 | /*[clinic end generated code: output=714f19e6ff01e099 input=d11deed0750600ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6887 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6888 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6889 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6890 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6891 | WAIT_TYPE status; |
| 6892 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6893 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6894 | do { |
| 6895 | Py_BEGIN_ALLOW_THREADS |
| 6896 | res = wait4(pid, &status, options, &ru); |
| 6897 | Py_END_ALLOW_THREADS |
| 6898 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6899 | if (res < 0) |
| 6900 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6901 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6902 | return wait_helper(res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6903 | } |
| 6904 | #endif /* HAVE_WAIT4 */ |
| 6905 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6906 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6907 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6908 | /*[clinic input] |
| 6909 | os.waitid |
| 6910 | |
| 6911 | idtype: idtype_t |
| 6912 | Must be one of be P_PID, P_PGID or P_ALL. |
| 6913 | id: id_t |
| 6914 | The id to wait on. |
| 6915 | options: int |
| 6916 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 6917 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 6918 | / |
| 6919 | |
| 6920 | Returns the result of waiting for a process or processes. |
| 6921 | |
| 6922 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 6923 | no children in a waitable state. |
| 6924 | [clinic start generated code]*/ |
| 6925 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6926 | static PyObject * |
| 6927 | 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] | 6928 | /*[clinic end generated code: output=5c0192750e22fa2e input=d8e7f76e052b7920]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6929 | { |
| 6930 | PyObject *result; |
| 6931 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6932 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6933 | siginfo_t si; |
| 6934 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6935 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6936 | do { |
| 6937 | Py_BEGIN_ALLOW_THREADS |
| 6938 | res = waitid(idtype, id, &si, options); |
| 6939 | Py_END_ALLOW_THREADS |
| 6940 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6941 | if (res < 0) |
| 6942 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6943 | |
| 6944 | if (si.si_pid == 0) |
| 6945 | Py_RETURN_NONE; |
| 6946 | |
| 6947 | result = PyStructSequence_New(&WaitidResultType); |
| 6948 | if (!result) |
| 6949 | return NULL; |
| 6950 | |
| 6951 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6952 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6953 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6954 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6955 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6956 | if (PyErr_Occurred()) { |
| 6957 | Py_DECREF(result); |
| 6958 | return NULL; |
| 6959 | } |
| 6960 | |
| 6961 | return result; |
| 6962 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6963 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6964 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6965 | |
| 6966 | #if defined(HAVE_WAITPID) |
| 6967 | /*[clinic input] |
| 6968 | os.waitpid |
| 6969 | pid: pid_t |
| 6970 | options: int |
| 6971 | / |
| 6972 | |
| 6973 | Wait for completion of a given child process. |
| 6974 | |
| 6975 | Returns a tuple of information regarding the child process: |
| 6976 | (pid, status) |
| 6977 | |
| 6978 | The options argument is ignored on Windows. |
| 6979 | [clinic start generated code]*/ |
| 6980 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6981 | static PyObject * |
| 6982 | os_waitpid_impl(PyModuleDef *module, pid_t pid, int options) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6983 | /*[clinic end generated code: output=5e3593353d54b15b input=0bf1666b8758fda3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6984 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6985 | pid_t res; |
| 6986 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6987 | WAIT_TYPE status; |
| 6988 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6989 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6990 | do { |
| 6991 | Py_BEGIN_ALLOW_THREADS |
| 6992 | res = waitpid(pid, &status, options); |
| 6993 | Py_END_ALLOW_THREADS |
| 6994 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6995 | if (res < 0) |
| 6996 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6997 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6998 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6999 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7000 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7001 | /* 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] | 7002 | /*[clinic input] |
| 7003 | os.waitpid |
| 7004 | pid: Py_intptr_t |
| 7005 | options: int |
| 7006 | / |
| 7007 | |
| 7008 | Wait for completion of a given process. |
| 7009 | |
| 7010 | Returns a tuple of information regarding the process: |
| 7011 | (pid, status << 8) |
| 7012 | |
| 7013 | The options argument is ignored on Windows. |
| 7014 | [clinic start generated code]*/ |
| 7015 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7016 | static PyObject * |
| 7017 | os_waitpid_impl(PyModuleDef *module, Py_intptr_t pid, int options) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7018 | /*[clinic end generated code: output=fc1d520db019625f input=444c8f51cca5b862]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7019 | { |
| 7020 | int status; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7021 | Py_intptr_t res; |
| 7022 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7023 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7024 | do { |
| 7025 | Py_BEGIN_ALLOW_THREADS |
| 7026 | res = _cwait(&status, pid, options); |
| 7027 | Py_END_ALLOW_THREADS |
| 7028 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 7029 | if (res < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7030 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7031 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7032 | /* 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] | 7033 | return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7034 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7035 | #endif |
| 7036 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7037 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7038 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7039 | /*[clinic input] |
| 7040 | os.wait |
| 7041 | |
| 7042 | Wait for completion of a child process. |
| 7043 | |
| 7044 | Returns a tuple of information about the child process: |
| 7045 | (pid, status) |
| 7046 | [clinic start generated code]*/ |
| 7047 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7048 | static PyObject * |
| 7049 | os_wait_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7050 | /*[clinic end generated code: output=4a7f4978393e0654 input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7051 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7052 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7053 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7054 | WAIT_TYPE status; |
| 7055 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7056 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7057 | do { |
| 7058 | Py_BEGIN_ALLOW_THREADS |
| 7059 | pid = wait(&status); |
| 7060 | Py_END_ALLOW_THREADS |
| 7061 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7062 | if (pid < 0) |
| 7063 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7064 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7065 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7066 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7067 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7068 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7069 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7070 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
| 7071 | PyDoc_STRVAR(readlink__doc__, |
| 7072 | "readlink(path, *, dir_fd=None) -> path\n\n\ |
| 7073 | Return a string representing the path to which the symbolic link points.\n\ |
| 7074 | \n\ |
| 7075 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7076 | and path should be relative; path will then be relative to that directory.\n\ |
| 7077 | dir_fd may not be implemented on your platform.\n\ |
| 7078 | If it is unavailable, using it will raise a NotImplementedError."); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7079 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7080 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7081 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7082 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7083 | /* AC 3.5: merge win32 and not together */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7084 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7085 | posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7086 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7087 | path_t path; |
| 7088 | int dir_fd = DEFAULT_DIR_FD; |
| 7089 | char buffer[MAXPATHLEN]; |
| 7090 | ssize_t length; |
| 7091 | PyObject *return_value = NULL; |
| 7092 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7093 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7094 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7095 | path.function_name = "readlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7096 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords, |
| 7097 | path_converter, &path, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7098 | READLINKAT_DIR_FD_CONVERTER, &dir_fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7099 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7100 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7101 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7102 | #ifdef HAVE_READLINKAT |
| 7103 | if (dir_fd != DEFAULT_DIR_FD) |
| 7104 | length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer)); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 7105 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7106 | #endif |
| 7107 | length = readlink(path.narrow, buffer, sizeof(buffer)); |
| 7108 | Py_END_ALLOW_THREADS |
| 7109 | |
| 7110 | if (length < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7111 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7112 | goto exit; |
| 7113 | } |
| 7114 | |
| 7115 | if (PyUnicode_Check(path.object)) |
| 7116 | return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 7117 | else |
| 7118 | return_value = PyBytes_FromStringAndSize(buffer, length); |
| 7119 | exit: |
| 7120 | path_cleanup(&path); |
| 7121 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7122 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7123 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7124 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7125 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7126 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 7127 | |
| 7128 | static PyObject * |
| 7129 | win_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
| 7130 | { |
| 7131 | wchar_t *path; |
| 7132 | DWORD n_bytes_returned; |
| 7133 | DWORD io_result; |
| 7134 | PyObject *po, *result; |
| 7135 | int dir_fd; |
| 7136 | HANDLE reparse_point_handle; |
| 7137 | |
| 7138 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 7139 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 7140 | wchar_t *print_name; |
| 7141 | |
| 7142 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 7143 | |
| 7144 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords, |
| 7145 | &po, |
| 7146 | dir_fd_unavailable, &dir_fd |
| 7147 | )) |
| 7148 | return NULL; |
| 7149 | |
| 7150 | path = PyUnicode_AsUnicode(po); |
| 7151 | if (path == NULL) |
| 7152 | return NULL; |
| 7153 | |
| 7154 | /* First get a handle to the reparse point */ |
| 7155 | Py_BEGIN_ALLOW_THREADS |
| 7156 | reparse_point_handle = CreateFileW( |
| 7157 | path, |
| 7158 | 0, |
| 7159 | 0, |
| 7160 | 0, |
| 7161 | OPEN_EXISTING, |
| 7162 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 7163 | 0); |
| 7164 | Py_END_ALLOW_THREADS |
| 7165 | |
| 7166 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
| 7167 | return win32_error_object("readlink", po); |
| 7168 | |
| 7169 | Py_BEGIN_ALLOW_THREADS |
| 7170 | /* New call DeviceIoControl to read the reparse point */ |
| 7171 | io_result = DeviceIoControl( |
| 7172 | reparse_point_handle, |
| 7173 | FSCTL_GET_REPARSE_POINT, |
| 7174 | 0, 0, /* in buffer */ |
| 7175 | target_buffer, sizeof(target_buffer), |
| 7176 | &n_bytes_returned, |
| 7177 | 0 /* we're not using OVERLAPPED_IO */ |
| 7178 | ); |
| 7179 | CloseHandle(reparse_point_handle); |
| 7180 | Py_END_ALLOW_THREADS |
| 7181 | |
| 7182 | if (io_result==0) |
| 7183 | return win32_error_object("readlink", po); |
| 7184 | |
| 7185 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 7186 | { |
| 7187 | PyErr_SetString(PyExc_ValueError, |
| 7188 | "not a symbolic link"); |
| 7189 | return NULL; |
| 7190 | } |
| 7191 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7192 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 7193 | |
| 7194 | result = PyUnicode_FromWideChar(print_name, |
| 7195 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
| 7196 | return result; |
| 7197 | } |
| 7198 | |
| 7199 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 7200 | |
| 7201 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7202 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7203 | #ifdef HAVE_SYMLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7204 | |
| 7205 | #if defined(MS_WINDOWS) |
| 7206 | |
| 7207 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 7208 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD) = NULL; |
| 7209 | static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPSTR, LPSTR, DWORD) = NULL; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7210 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7211 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7212 | check_CreateSymbolicLink(void) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7213 | { |
| 7214 | HINSTANCE hKernel32; |
| 7215 | /* only recheck */ |
| 7216 | if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA) |
| 7217 | return 1; |
| 7218 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 7219 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 7220 | "CreateSymbolicLinkW"); |
| 7221 | *(FARPROC*)&Py_CreateSymbolicLinkA = GetProcAddress(hKernel32, |
| 7222 | "CreateSymbolicLinkA"); |
| 7223 | return (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA); |
| 7224 | } |
| 7225 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7226 | /* Remove the last portion of the path */ |
| 7227 | static void |
| 7228 | _dirnameW(WCHAR *path) |
| 7229 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7230 | WCHAR *ptr; |
| 7231 | |
| 7232 | /* walk the path from the end until a backslash is encountered */ |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7233 | for(ptr = path + wcslen(path); ptr != path; ptr--) { |
Victor Stinner | 072318b | 2013-06-05 02:07:46 +0200 | [diff] [blame] | 7234 | if (*ptr == L'\\' || *ptr == L'/') |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7235 | break; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7236 | } |
| 7237 | *ptr = 0; |
| 7238 | } |
| 7239 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7240 | /* Remove the last portion of the path */ |
| 7241 | static void |
| 7242 | _dirnameA(char *path) |
| 7243 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7244 | char *ptr; |
| 7245 | |
| 7246 | /* walk the path from the end until a backslash is encountered */ |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7247 | for(ptr = path + strlen(path); ptr != path; ptr--) { |
| 7248 | if (*ptr == '\\' || *ptr == '/') |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7249 | break; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7250 | } |
| 7251 | *ptr = 0; |
| 7252 | } |
| 7253 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7254 | /* Is this path absolute? */ |
| 7255 | static int |
| 7256 | _is_absW(const WCHAR *path) |
| 7257 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7258 | return path[0] == L'\\' || path[0] == L'/' || path[1] == L':'; |
| 7259 | |
| 7260 | } |
| 7261 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7262 | /* Is this path absolute? */ |
| 7263 | static int |
| 7264 | _is_absA(const char *path) |
| 7265 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7266 | return path[0] == '\\' || path[0] == '/' || path[1] == ':'; |
| 7267 | |
| 7268 | } |
| 7269 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7270 | /* join root and rest with a backslash */ |
| 7271 | static void |
| 7272 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 7273 | { |
Victor Stinner | e7e7eba | 2013-06-05 00:35:54 +0200 | [diff] [blame] | 7274 | size_t root_len; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7275 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7276 | if (_is_absW(rest)) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7277 | wcscpy(dest_path, rest); |
| 7278 | return; |
| 7279 | } |
| 7280 | |
| 7281 | root_len = wcslen(root); |
| 7282 | |
| 7283 | wcscpy(dest_path, root); |
| 7284 | if(root_len) { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7285 | dest_path[root_len] = L'\\'; |
| 7286 | root_len++; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7287 | } |
| 7288 | wcscpy(dest_path+root_len, rest); |
| 7289 | } |
| 7290 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7291 | /* join root and rest with a backslash */ |
| 7292 | static void |
| 7293 | _joinA(char *dest_path, const char *root, const char *rest) |
| 7294 | { |
Victor Stinner | e7e7eba | 2013-06-05 00:35:54 +0200 | [diff] [blame] | 7295 | size_t root_len; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7296 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7297 | if (_is_absA(rest)) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7298 | strcpy(dest_path, rest); |
| 7299 | return; |
| 7300 | } |
| 7301 | |
| 7302 | root_len = strlen(root); |
| 7303 | |
| 7304 | strcpy(dest_path, root); |
| 7305 | if(root_len) { |
| 7306 | dest_path[root_len] = '\\'; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7307 | root_len++; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7308 | } |
| 7309 | strcpy(dest_path+root_len, rest); |
| 7310 | } |
| 7311 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7312 | /* Return True if the path at src relative to dest is a directory */ |
| 7313 | static int |
| 7314 | _check_dirW(WCHAR *src, WCHAR *dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7315 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7316 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7317 | WCHAR dest_parent[MAX_PATH]; |
| 7318 | WCHAR src_resolved[MAX_PATH] = L""; |
| 7319 | |
| 7320 | /* dest_parent = os.path.dirname(dest) */ |
| 7321 | wcscpy(dest_parent, dest); |
| 7322 | _dirnameW(dest_parent); |
| 7323 | /* src_resolved = os.path.join(dest_parent, src) */ |
| 7324 | _joinW(src_resolved, dest_parent, src); |
| 7325 | return ( |
| 7326 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 7327 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7328 | ); |
| 7329 | } |
| 7330 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7331 | /* Return True if the path at src relative to dest is a directory */ |
| 7332 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 7333 | _check_dirA(const char *src, char *dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7334 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7335 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7336 | char dest_parent[MAX_PATH]; |
| 7337 | char src_resolved[MAX_PATH] = ""; |
| 7338 | |
| 7339 | /* dest_parent = os.path.dirname(dest) */ |
| 7340 | strcpy(dest_parent, dest); |
Victor Stinner | 5a43676 | 2013-06-05 00:37:12 +0200 | [diff] [blame] | 7341 | _dirnameA(dest_parent); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7342 | /* src_resolved = os.path.join(dest_parent, src) */ |
Victor Stinner | 5a43676 | 2013-06-05 00:37:12 +0200 | [diff] [blame] | 7343 | _joinA(src_resolved, dest_parent, src); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7344 | return ( |
| 7345 | GetFileAttributesExA(src_resolved, GetFileExInfoStandard, &src_info) |
| 7346 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7347 | ); |
| 7348 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7349 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7350 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7351 | |
| 7352 | /*[clinic input] |
| 7353 | os.symlink |
| 7354 | src: path_t |
| 7355 | dst: path_t |
| 7356 | target_is_directory: bool = False |
| 7357 | * |
| 7358 | dir_fd: dir_fd(requires='symlinkat')=None |
| 7359 | |
| 7360 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 7361 | |
| 7362 | Create a symbolic link pointing to src named dst. |
| 7363 | |
| 7364 | target_is_directory is required on Windows if the target is to be |
| 7365 | interpreted as a directory. (On Windows, symlink requires |
| 7366 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 7367 | target_is_directory is ignored on non-Windows platforms. |
| 7368 | |
| 7369 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7370 | and path should be relative; path will then be relative to that directory. |
| 7371 | dir_fd may not be implemented on your platform. |
| 7372 | If it is unavailable, using it will raise a NotImplementedError. |
| 7373 | |
| 7374 | [clinic start generated code]*/ |
| 7375 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7376 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7377 | os_symlink_impl(PyModuleDef *module, path_t *src, path_t *dst, |
| 7378 | int target_is_directory, int dir_fd) |
| 7379 | /*[clinic end generated code: output=a01b4bcf32403ccd input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7380 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7381 | #ifdef MS_WINDOWS |
| 7382 | DWORD result; |
| 7383 | #else |
| 7384 | int result; |
| 7385 | #endif |
| 7386 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7387 | #ifdef MS_WINDOWS |
| 7388 | if (!check_CreateSymbolicLink()) { |
| 7389 | PyErr_SetString(PyExc_NotImplementedError, |
| 7390 | "CreateSymbolicLink functions not found"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7391 | return NULL; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7392 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7393 | if (!win32_can_symlink) { |
| 7394 | PyErr_SetString(PyExc_OSError, "symbolic link privilege not held"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7395 | return NULL; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7396 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7397 | #endif |
| 7398 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7399 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7400 | PyErr_SetString(PyExc_ValueError, |
| 7401 | "symlink: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7402 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7403 | } |
| 7404 | |
| 7405 | #ifdef MS_WINDOWS |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7406 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7407 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7408 | if (dst->wide) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7409 | /* if src is a directory, ensure target_is_directory==1 */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7410 | target_is_directory |= _check_dirW(src->wide, dst->wide); |
| 7411 | result = Py_CreateSymbolicLinkW(dst->wide, src->wide, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7412 | target_is_directory); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7413 | } |
| 7414 | else { |
| 7415 | /* if src is a directory, ensure target_is_directory==1 */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7416 | target_is_directory |= _check_dirA(src->narrow, dst->narrow); |
| 7417 | result = Py_CreateSymbolicLinkA(dst->narrow, src->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7418 | target_is_directory); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7419 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7420 | Py_END_ALLOW_THREADS |
| 7421 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7422 | if (!result) |
| 7423 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7424 | |
| 7425 | #else |
| 7426 | |
| 7427 | Py_BEGIN_ALLOW_THREADS |
| 7428 | #if HAVE_SYMLINKAT |
| 7429 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7430 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7431 | else |
| 7432 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7433 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7434 | Py_END_ALLOW_THREADS |
| 7435 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7436 | if (result) |
| 7437 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7438 | #endif |
| 7439 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7440 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7441 | } |
| 7442 | #endif /* HAVE_SYMLINK */ |
| 7443 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7444 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7445 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7446 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7447 | static PyStructSequence_Field times_result_fields[] = { |
| 7448 | {"user", "user time"}, |
| 7449 | {"system", "system time"}, |
| 7450 | {"children_user", "user time of children"}, |
| 7451 | {"children_system", "system time of children"}, |
| 7452 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 7453 | {NULL} |
| 7454 | }; |
| 7455 | |
| 7456 | PyDoc_STRVAR(times_result__doc__, |
| 7457 | "times_result: Result from os.times().\n\n\ |
| 7458 | This object may be accessed either as a tuple of\n\ |
| 7459 | (user, system, children_user, children_system, elapsed),\n\ |
| 7460 | or via the attributes user, system, children_user, children_system,\n\ |
| 7461 | and elapsed.\n\ |
| 7462 | \n\ |
| 7463 | See os.times for more information."); |
| 7464 | |
| 7465 | static PyStructSequence_Desc times_result_desc = { |
| 7466 | "times_result", /* name */ |
| 7467 | times_result__doc__, /* doc */ |
| 7468 | times_result_fields, |
| 7469 | 5 |
| 7470 | }; |
| 7471 | |
| 7472 | static PyTypeObject TimesResultType; |
| 7473 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7474 | #ifdef MS_WINDOWS |
| 7475 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 7476 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7477 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7478 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7479 | |
| 7480 | static PyObject * |
| 7481 | build_times_result(double user, double system, |
| 7482 | double children_user, double children_system, |
| 7483 | double elapsed) |
| 7484 | { |
| 7485 | PyObject *value = PyStructSequence_New(&TimesResultType); |
| 7486 | if (value == NULL) |
| 7487 | return NULL; |
| 7488 | |
| 7489 | #define SET(i, field) \ |
| 7490 | { \ |
| 7491 | PyObject *o = PyFloat_FromDouble(field); \ |
| 7492 | if (!o) { \ |
| 7493 | Py_DECREF(value); \ |
| 7494 | return NULL; \ |
| 7495 | } \ |
| 7496 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 7497 | } \ |
| 7498 | |
| 7499 | SET(0, user); |
| 7500 | SET(1, system); |
| 7501 | SET(2, children_user); |
| 7502 | SET(3, children_system); |
| 7503 | SET(4, elapsed); |
| 7504 | |
| 7505 | #undef SET |
| 7506 | |
| 7507 | return value; |
| 7508 | } |
| 7509 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7510 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7511 | #ifndef MS_WINDOWS |
| 7512 | #define NEED_TICKS_PER_SECOND |
| 7513 | static long ticks_per_second = -1; |
| 7514 | #endif /* MS_WINDOWS */ |
| 7515 | |
| 7516 | /*[clinic input] |
| 7517 | os.times |
| 7518 | |
| 7519 | Return a collection containing process timing information. |
| 7520 | |
| 7521 | The object returned behaves like a named tuple with these fields: |
| 7522 | (utime, stime, cutime, cstime, elapsed_time) |
| 7523 | All fields are floating point numbers. |
| 7524 | [clinic start generated code]*/ |
| 7525 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7526 | static PyObject * |
| 7527 | os_times_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7528 | /*[clinic end generated code: output=df0a63ebe6e6f091 input=2bf9df3d6ab2e48b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7529 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7530 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7531 | FILETIME create, exit, kernel, user; |
| 7532 | HANDLE hProc; |
| 7533 | hProc = GetCurrentProcess(); |
| 7534 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 7535 | /* The fields of a FILETIME structure are the hi and lo part |
| 7536 | of a 64-bit value expressed in 100 nanosecond units. |
| 7537 | 1e7 is one second in such units; 1e-7 the inverse. |
| 7538 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 7539 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7540 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7541 | (double)(user.dwHighDateTime*429.4967296 + |
| 7542 | user.dwLowDateTime*1e-7), |
| 7543 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 7544 | kernel.dwLowDateTime*1e-7), |
| 7545 | (double)0, |
| 7546 | (double)0, |
| 7547 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7548 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7549 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7550 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7551 | |
| 7552 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7553 | struct tms t; |
| 7554 | clock_t c; |
| 7555 | errno = 0; |
| 7556 | c = times(&t); |
| 7557 | if (c == (clock_t) -1) |
| 7558 | return posix_error(); |
| 7559 | return build_times_result( |
| 7560 | (double)t.tms_utime / ticks_per_second, |
| 7561 | (double)t.tms_stime / ticks_per_second, |
| 7562 | (double)t.tms_cutime / ticks_per_second, |
| 7563 | (double)t.tms_cstime / ticks_per_second, |
| 7564 | (double)c / ticks_per_second); |
| 7565 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7566 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7567 | #endif /* HAVE_TIMES */ |
| 7568 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7569 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7570 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7571 | /*[clinic input] |
| 7572 | os.getsid |
| 7573 | |
| 7574 | pid: pid_t |
| 7575 | / |
| 7576 | |
| 7577 | Call the system call getsid(pid) and return the result. |
| 7578 | [clinic start generated code]*/ |
| 7579 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7580 | static PyObject * |
| 7581 | os_getsid_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7582 | /*[clinic end generated code: output=a074f80c0e6bfb38 input=eeb2b923a30ce04e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7583 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7584 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7585 | sid = getsid(pid); |
| 7586 | if (sid < 0) |
| 7587 | return posix_error(); |
| 7588 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7589 | } |
| 7590 | #endif /* HAVE_GETSID */ |
| 7591 | |
| 7592 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7593 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7594 | /*[clinic input] |
| 7595 | os.setsid |
| 7596 | |
| 7597 | Call the system call setsid(). |
| 7598 | [clinic start generated code]*/ |
| 7599 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7600 | static PyObject * |
| 7601 | os_setsid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7602 | /*[clinic end generated code: output=398fc152ae327330 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7603 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7604 | if (setsid() < 0) |
| 7605 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7606 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7607 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7608 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7609 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7610 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7611 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7612 | /*[clinic input] |
| 7613 | os.setpgid |
| 7614 | |
| 7615 | pid: pid_t |
| 7616 | pgrp: pid_t |
| 7617 | / |
| 7618 | |
| 7619 | Call the system call setpgid(pid, pgrp). |
| 7620 | [clinic start generated code]*/ |
| 7621 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7622 | static PyObject * |
| 7623 | os_setpgid_impl(PyModuleDef *module, pid_t pid, pid_t pgrp) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7624 | /*[clinic end generated code: output=7079a8e932912841 input=fceb395eca572e1a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7625 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7626 | if (setpgid(pid, pgrp) < 0) |
| 7627 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7628 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7629 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7630 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7631 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7632 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7633 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7634 | /*[clinic input] |
| 7635 | os.tcgetpgrp |
| 7636 | |
| 7637 | fd: int |
| 7638 | / |
| 7639 | |
| 7640 | Return the process group associated with the terminal specified by fd. |
| 7641 | [clinic start generated code]*/ |
| 7642 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7643 | static PyObject * |
| 7644 | os_tcgetpgrp_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7645 | /*[clinic end generated code: output=ebb6dc5f111c7dc0 input=7f6c18eac10ada86]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7646 | { |
| 7647 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7648 | if (pgid < 0) |
| 7649 | return posix_error(); |
| 7650 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7651 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7652 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7653 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7654 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7655 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7656 | /*[clinic input] |
| 7657 | os.tcsetpgrp |
| 7658 | |
| 7659 | fd: int |
| 7660 | pgid: pid_t |
| 7661 | / |
| 7662 | |
| 7663 | Set the process group associated with the terminal specified by fd. |
| 7664 | [clinic start generated code]*/ |
| 7665 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7666 | static PyObject * |
| 7667 | os_tcsetpgrp_impl(PyModuleDef *module, int fd, pid_t pgid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7668 | /*[clinic end generated code: output=3e4b05177462cd22 input=5bdc997c6a619020]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7669 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7670 | if (tcsetpgrp(fd, pgid) < 0) |
| 7671 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7672 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7673 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7674 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7675 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7676 | /* Functions acting on file descriptors */ |
| 7677 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7678 | #ifdef O_CLOEXEC |
| 7679 | extern int _Py_open_cloexec_works; |
| 7680 | #endif |
| 7681 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7682 | |
| 7683 | /*[clinic input] |
| 7684 | os.open -> int |
| 7685 | path: path_t |
| 7686 | flags: int |
| 7687 | mode: int = 0o777 |
| 7688 | * |
| 7689 | dir_fd: dir_fd(requires='openat') = None |
| 7690 | |
| 7691 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 7692 | |
| 7693 | Open a file for low level IO. Returns a file descriptor (integer). |
| 7694 | |
| 7695 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7696 | and path should be relative; path will then be relative to that directory. |
| 7697 | dir_fd may not be implemented on your platform. |
| 7698 | If it is unavailable, using it will raise a NotImplementedError. |
| 7699 | [clinic start generated code]*/ |
| 7700 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7701 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7702 | os_open_impl(PyModuleDef *module, path_t *path, int flags, int mode, |
| 7703 | int dir_fd) |
| 7704 | /*[clinic end generated code: output=47e8cc63559f5ddd input=ad8623b29acd2934]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7705 | { |
| 7706 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7707 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7708 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7709 | #ifdef O_CLOEXEC |
| 7710 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 7711 | #elif !defined(MS_WINDOWS) |
| 7712 | int *atomic_flag_works = NULL; |
| 7713 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7714 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7715 | #ifdef MS_WINDOWS |
| 7716 | flags |= O_NOINHERIT; |
| 7717 | #elif defined(O_CLOEXEC) |
| 7718 | flags |= O_CLOEXEC; |
| 7719 | #endif |
| 7720 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7721 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7722 | do { |
| 7723 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7724 | #ifdef MS_WINDOWS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7725 | if (path->wide) |
| 7726 | fd = _wopen(path->wide, flags, mode); |
| 7727 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7728 | #endif |
| 7729 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7730 | if (dir_fd != DEFAULT_DIR_FD) |
| 7731 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 7732 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7733 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7734 | fd = open(path->narrow, flags, mode); |
| 7735 | Py_END_ALLOW_THREADS |
| 7736 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7737 | _Py_END_SUPPRESS_IPH |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7738 | |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 7739 | if (fd < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7740 | if (!async_err) |
| 7741 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7742 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7743 | } |
| 7744 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7745 | #ifndef MS_WINDOWS |
| 7746 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 7747 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7748 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7749 | } |
| 7750 | #endif |
| 7751 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7752 | return fd; |
| 7753 | } |
| 7754 | |
| 7755 | |
| 7756 | /*[clinic input] |
| 7757 | os.close |
| 7758 | |
| 7759 | fd: int |
| 7760 | |
| 7761 | Close a file descriptor. |
| 7762 | [clinic start generated code]*/ |
| 7763 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7764 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7765 | os_close_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7766 | /*[clinic end generated code: output=47bf2ea536445a26 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7767 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7768 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7769 | if (!_PyVerify_fd(fd)) |
| 7770 | return posix_error(); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7771 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 7772 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 7773 | * for more details. |
| 7774 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7775 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7776 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7777 | res = close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7778 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7779 | Py_END_ALLOW_THREADS |
| 7780 | if (res < 0) |
| 7781 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7782 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7783 | } |
| 7784 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7785 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7786 | /*[clinic input] |
| 7787 | os.closerange |
| 7788 | |
| 7789 | fd_low: int |
| 7790 | fd_high: int |
| 7791 | / |
| 7792 | |
| 7793 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 7794 | [clinic start generated code]*/ |
| 7795 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7796 | static PyObject * |
| 7797 | os_closerange_impl(PyModuleDef *module, int fd_low, int fd_high) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7798 | /*[clinic end generated code: output=70e6adb95220ba96 input=5855a3d053ebd4ec]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7799 | { |
| 7800 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7801 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7802 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7803 | for (i = fd_low; i < fd_high; i++) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7804 | if (_PyVerify_fd(i)) |
| 7805 | close(i); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7806 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7807 | Py_END_ALLOW_THREADS |
| 7808 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7809 | } |
| 7810 | |
| 7811 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7812 | /*[clinic input] |
| 7813 | os.dup -> int |
| 7814 | |
| 7815 | fd: int |
| 7816 | / |
| 7817 | |
| 7818 | Return a duplicate of a file descriptor. |
| 7819 | [clinic start generated code]*/ |
| 7820 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7821 | static int |
| 7822 | os_dup_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7823 | /*[clinic end generated code: output=f4bbac8c7652d05e input=6f10f7ea97f7852a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7824 | { |
| 7825 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7826 | } |
| 7827 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7828 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7829 | /*[clinic input] |
| 7830 | os.dup2 |
| 7831 | fd: int |
| 7832 | fd2: int |
| 7833 | inheritable: bool=True |
| 7834 | |
| 7835 | Duplicate file descriptor. |
| 7836 | [clinic start generated code]*/ |
| 7837 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7838 | static PyObject * |
| 7839 | os_dup2_impl(PyModuleDef *module, int fd, int fd2, int inheritable) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7840 | /*[clinic end generated code: output=9a099d95881a7923 input=76e96f511be0352f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7841 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7842 | int res; |
| 7843 | #if defined(HAVE_DUP3) && \ |
| 7844 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 7845 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
| 7846 | int dup3_works = -1; |
| 7847 | #endif |
| 7848 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7849 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 7850 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7851 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7852 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 7853 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 7854 | * upon close(), and therefore below. |
| 7855 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7856 | #ifdef MS_WINDOWS |
| 7857 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7858 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7859 | res = dup2(fd, fd2); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7860 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7861 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7862 | if (res < 0) |
| 7863 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7864 | |
| 7865 | /* Character files like console cannot be make non-inheritable */ |
| 7866 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7867 | close(fd2); |
| 7868 | return NULL; |
| 7869 | } |
| 7870 | |
| 7871 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 7872 | Py_BEGIN_ALLOW_THREADS |
| 7873 | if (!inheritable) |
| 7874 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 7875 | else |
| 7876 | res = dup2(fd, fd2); |
| 7877 | Py_END_ALLOW_THREADS |
| 7878 | if (res < 0) |
| 7879 | return posix_error(); |
| 7880 | |
| 7881 | #else |
| 7882 | |
| 7883 | #ifdef HAVE_DUP3 |
| 7884 | if (!inheritable && dup3_works != 0) { |
| 7885 | Py_BEGIN_ALLOW_THREADS |
| 7886 | res = dup3(fd, fd2, O_CLOEXEC); |
| 7887 | Py_END_ALLOW_THREADS |
| 7888 | if (res < 0) { |
| 7889 | if (dup3_works == -1) |
| 7890 | dup3_works = (errno != ENOSYS); |
| 7891 | if (dup3_works) |
| 7892 | return posix_error(); |
| 7893 | } |
| 7894 | } |
| 7895 | |
| 7896 | if (inheritable || dup3_works == 0) |
| 7897 | { |
| 7898 | #endif |
| 7899 | Py_BEGIN_ALLOW_THREADS |
| 7900 | res = dup2(fd, fd2); |
| 7901 | Py_END_ALLOW_THREADS |
| 7902 | if (res < 0) |
| 7903 | return posix_error(); |
| 7904 | |
| 7905 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7906 | close(fd2); |
| 7907 | return NULL; |
| 7908 | } |
| 7909 | #ifdef HAVE_DUP3 |
| 7910 | } |
| 7911 | #endif |
| 7912 | |
| 7913 | #endif |
| 7914 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7915 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7916 | } |
| 7917 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7918 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7919 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7920 | /*[clinic input] |
| 7921 | os.lockf |
| 7922 | |
| 7923 | fd: int |
| 7924 | An open file descriptor. |
| 7925 | command: int |
| 7926 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 7927 | length: Py_off_t |
| 7928 | The number of bytes to lock, starting at the current position. |
| 7929 | / |
| 7930 | |
| 7931 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 7932 | |
| 7933 | [clinic start generated code]*/ |
| 7934 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7935 | static PyObject * |
| 7936 | 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] | 7937 | /*[clinic end generated code: output=25ff778f9e2fbf1b input=65da41d2106e9b79]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7938 | { |
| 7939 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7940 | |
| 7941 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7942 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7943 | Py_END_ALLOW_THREADS |
| 7944 | |
| 7945 | if (res < 0) |
| 7946 | return posix_error(); |
| 7947 | |
| 7948 | Py_RETURN_NONE; |
| 7949 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7950 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7951 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7952 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7953 | /*[clinic input] |
| 7954 | os.lseek -> Py_off_t |
| 7955 | |
| 7956 | fd: int |
| 7957 | position: Py_off_t |
| 7958 | how: int |
| 7959 | / |
| 7960 | |
| 7961 | Set the position of a file descriptor. Return the new position. |
| 7962 | |
| 7963 | Return the new cursor position in number of bytes |
| 7964 | relative to the beginning of the file. |
| 7965 | [clinic start generated code]*/ |
| 7966 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7967 | static Py_off_t |
| 7968 | 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] | 7969 | /*[clinic end generated code: output=65d4ab96d664998c input=902654ad3f96a6d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7970 | { |
| 7971 | Py_off_t result; |
| 7972 | |
| 7973 | if (!_PyVerify_fd(fd)) { |
| 7974 | posix_error(); |
| 7975 | return -1; |
| 7976 | } |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7977 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7978 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 7979 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7980 | case 0: how = SEEK_SET; break; |
| 7981 | case 1: how = SEEK_CUR; break; |
| 7982 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7983 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7984 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7985 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7986 | if (PyErr_Occurred()) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7987 | return -1; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7988 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7989 | if (!_PyVerify_fd(fd)) { |
| 7990 | posix_error(); |
| 7991 | return -1; |
| 7992 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7993 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7994 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 7995 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7996 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7997 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7998 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7999 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8000 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8001 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8002 | if (result < 0) |
| 8003 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8004 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8005 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8006 | } |
| 8007 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8008 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8009 | /*[clinic input] |
| 8010 | os.read |
| 8011 | fd: int |
| 8012 | length: Py_ssize_t |
| 8013 | / |
| 8014 | |
| 8015 | Read from a file descriptor. Returns a bytes object. |
| 8016 | [clinic start generated code]*/ |
| 8017 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8018 | static PyObject * |
| 8019 | os_read_impl(PyModuleDef *module, int fd, Py_ssize_t length) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8020 | /*[clinic end generated code: output=be24f44178455e8b input=1df2eaa27c0bf1d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8021 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8022 | Py_ssize_t n; |
| 8023 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8024 | |
| 8025 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8026 | errno = EINVAL; |
| 8027 | return posix_error(); |
| 8028 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8029 | |
| 8030 | #ifdef MS_WINDOWS |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8031 | /* On Windows, the count parameter of read() is an int */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8032 | if (length > INT_MAX) |
| 8033 | length = INT_MAX; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8034 | #endif |
| 8035 | |
| 8036 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8037 | if (buffer == NULL) |
| 8038 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8039 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8040 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 8041 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8042 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8043 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8044 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8045 | |
| 8046 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8047 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8048 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8049 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8050 | } |
| 8051 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8052 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 8053 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8054 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8055 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 8056 | { |
| 8057 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8058 | Py_ssize_t blen, total = 0; |
| 8059 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8060 | *iov = PyMem_New(struct iovec, cnt); |
| 8061 | if (*iov == NULL) { |
| 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 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8065 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8066 | *buf = PyMem_New(Py_buffer, cnt); |
| 8067 | if (*buf == NULL) { |
| 8068 | PyMem_Del(*iov); |
| 8069 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8070 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8071 | } |
| 8072 | |
| 8073 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8074 | PyObject *item = PySequence_GetItem(seq, i); |
| 8075 | if (item == NULL) |
| 8076 | goto fail; |
| 8077 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 8078 | Py_DECREF(item); |
| 8079 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8080 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8081 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8082 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8083 | blen = (*buf)[i].len; |
| 8084 | (*iov)[i].iov_len = blen; |
| 8085 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8086 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8087 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8088 | |
| 8089 | fail: |
| 8090 | PyMem_Del(*iov); |
| 8091 | for (j = 0; j < i; j++) { |
| 8092 | PyBuffer_Release(&(*buf)[j]); |
| 8093 | } |
| 8094 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8095 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8096 | } |
| 8097 | |
| 8098 | static void |
| 8099 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 8100 | { |
| 8101 | int i; |
| 8102 | PyMem_Del(iov); |
| 8103 | for (i = 0; i < cnt; i++) { |
| 8104 | PyBuffer_Release(&buf[i]); |
| 8105 | } |
| 8106 | PyMem_Del(buf); |
| 8107 | } |
| 8108 | #endif |
| 8109 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8110 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8111 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8112 | /*[clinic input] |
| 8113 | os.readv -> Py_ssize_t |
| 8114 | |
| 8115 | fd: int |
| 8116 | buffers: object |
| 8117 | / |
| 8118 | |
| 8119 | Read from a file descriptor fd into an iterable of buffers. |
| 8120 | |
| 8121 | The buffers should be mutable buffers accepting bytes. |
| 8122 | readv will transfer data into each buffer until it is full |
| 8123 | and then move on to the next buffer in the sequence to hold |
| 8124 | the rest of the data. |
| 8125 | |
| 8126 | readv returns the total number of bytes read, |
| 8127 | which may be less than the total capacity of all the buffers. |
| 8128 | [clinic start generated code]*/ |
| 8129 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8130 | static Py_ssize_t |
| 8131 | os_readv_impl(PyModuleDef *module, int fd, PyObject *buffers) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8132 | /*[clinic end generated code: output=00fc56ff1800059f input=e679eb5dbfa0357d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8133 | { |
| 8134 | int cnt; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8135 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8136 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8137 | struct iovec *iov; |
| 8138 | Py_buffer *buf; |
| 8139 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8140 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8141 | PyErr_SetString(PyExc_TypeError, |
| 8142 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8143 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8144 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8145 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8146 | cnt = PySequence_Size(buffers); |
| 8147 | |
| 8148 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 8149 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8150 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8151 | do { |
| 8152 | Py_BEGIN_ALLOW_THREADS |
| 8153 | n = readv(fd, iov, cnt); |
| 8154 | Py_END_ALLOW_THREADS |
| 8155 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8156 | |
| 8157 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8158 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8159 | if (!async_err) |
| 8160 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8161 | return -1; |
| 8162 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8163 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8164 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8165 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8166 | #endif /* HAVE_READV */ |
| 8167 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8168 | |
| 8169 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8170 | /*[clinic input] |
| 8171 | # TODO length should be size_t! but Python doesn't support parsing size_t yet. |
| 8172 | os.pread |
| 8173 | |
| 8174 | fd: int |
| 8175 | length: int |
| 8176 | offset: Py_off_t |
| 8177 | / |
| 8178 | |
| 8179 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 8180 | |
| 8181 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 8182 | the beginning of the file. The file offset remains unchanged. |
| 8183 | [clinic start generated code]*/ |
| 8184 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8185 | static PyObject * |
| 8186 | 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] | 8187 | /*[clinic end generated code: output=90d1fed87f68fa33 input=084948dcbaa35d4c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8188 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8189 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8190 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8191 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8192 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8193 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8194 | errno = EINVAL; |
| 8195 | return posix_error(); |
| 8196 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8197 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8198 | if (buffer == NULL) |
| 8199 | return NULL; |
| 8200 | if (!_PyVerify_fd(fd)) { |
| 8201 | Py_DECREF(buffer); |
| 8202 | return posix_error(); |
| 8203 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8204 | |
| 8205 | do { |
| 8206 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8207 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8208 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8209 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8210 | Py_END_ALLOW_THREADS |
| 8211 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8212 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8213 | if (n < 0) { |
| 8214 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8215 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8216 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8217 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8218 | _PyBytes_Resize(&buffer, n); |
| 8219 | return buffer; |
| 8220 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8221 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8222 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8223 | |
| 8224 | /*[clinic input] |
| 8225 | os.write -> Py_ssize_t |
| 8226 | |
| 8227 | fd: int |
| 8228 | data: Py_buffer |
| 8229 | / |
| 8230 | |
| 8231 | Write a bytes object to a file descriptor. |
| 8232 | [clinic start generated code]*/ |
| 8233 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8234 | static Py_ssize_t |
| 8235 | os_write_impl(PyModuleDef *module, int fd, Py_buffer *data) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8236 | /*[clinic end generated code: output=58845c93c9ee1dda input=3207e28963234f3c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8237 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8238 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8239 | } |
| 8240 | |
| 8241 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8242 | PyDoc_STRVAR(posix_sendfile__doc__, |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8243 | "sendfile(out, in, offset, count) -> byteswritten\n\ |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8244 | sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8245 | -> byteswritten\n\ |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8246 | Copy count bytes from file descriptor in to file descriptor out."); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8247 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8248 | /* AC 3.5: don't bother converting, has optional group*/ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8249 | static PyObject * |
| 8250 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 8251 | { |
| 8252 | int in, out; |
| 8253 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8254 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8255 | off_t offset; |
| 8256 | |
| 8257 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 8258 | #ifndef __APPLE__ |
| 8259 | Py_ssize_t len; |
| 8260 | #endif |
| 8261 | PyObject *headers = NULL, *trailers = NULL; |
| 8262 | Py_buffer *hbuf, *tbuf; |
| 8263 | off_t sbytes; |
| 8264 | struct sf_hdtr sf; |
| 8265 | int flags = 0; |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8266 | /* Beware that "in" clashes with Python's own "in" operator keyword */ |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 8267 | static char *keywords[] = {"out", "in", |
| 8268 | "offset", "count", |
| 8269 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8270 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 8271 | sf.headers = NULL; |
| 8272 | sf.trailers = NULL; |
| 8273 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8274 | #ifdef __APPLE__ |
| 8275 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8276 | 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] | 8277 | #else |
| 8278 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8279 | keywords, &out, &in, Py_off_t_converter, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8280 | #endif |
| 8281 | &headers, &trailers, &flags)) |
| 8282 | return NULL; |
| 8283 | if (headers != NULL) { |
| 8284 | if (!PySequence_Check(headers)) { |
| 8285 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8286 | "sendfile() headers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8287 | return NULL; |
| 8288 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8289 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8290 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8291 | if (sf.hdr_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8292 | (i = iov_setup(&(sf.headers), &hbuf, |
| 8293 | headers, sf.hdr_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8294 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8295 | #ifdef __APPLE__ |
| 8296 | sbytes += i; |
| 8297 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8298 | } |
| 8299 | } |
| 8300 | if (trailers != NULL) { |
| 8301 | if (!PySequence_Check(trailers)) { |
| 8302 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8303 | "sendfile() trailers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8304 | return NULL; |
| 8305 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8306 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8307 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8308 | if (sf.trl_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8309 | (i = iov_setup(&(sf.trailers), &tbuf, |
| 8310 | trailers, sf.trl_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8311 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8312 | #ifdef __APPLE__ |
| 8313 | sbytes += i; |
| 8314 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8315 | } |
| 8316 | } |
| 8317 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8318 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8319 | do { |
| 8320 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8321 | #ifdef __APPLE__ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8322 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8323 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8324 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8325 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8326 | Py_END_ALLOW_THREADS |
| 8327 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8328 | _Py_END_SUPPRESS_IPH |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8329 | |
| 8330 | if (sf.headers != NULL) |
| 8331 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 8332 | if (sf.trailers != NULL) |
| 8333 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 8334 | |
| 8335 | if (ret < 0) { |
| 8336 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 8337 | if (sbytes != 0) { |
| 8338 | // some data has been sent |
| 8339 | goto done; |
| 8340 | } |
| 8341 | else { |
| 8342 | // no data has been sent; upper application is supposed |
| 8343 | // to retry on EAGAIN or EBUSY |
| 8344 | return posix_error(); |
| 8345 | } |
| 8346 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8347 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8348 | } |
| 8349 | goto done; |
| 8350 | |
| 8351 | done: |
| 8352 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 8353 | return Py_BuildValue("l", sbytes); |
| 8354 | #else |
| 8355 | return Py_BuildValue("L", sbytes); |
| 8356 | #endif |
| 8357 | |
| 8358 | #else |
| 8359 | Py_ssize_t count; |
| 8360 | PyObject *offobj; |
| 8361 | static char *keywords[] = {"out", "in", |
| 8362 | "offset", "count", NULL}; |
| 8363 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 8364 | keywords, &out, &in, &offobj, &count)) |
| 8365 | return NULL; |
| 8366 | #ifdef linux |
| 8367 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8368 | do { |
| 8369 | Py_BEGIN_ALLOW_THREADS |
| 8370 | ret = sendfile(out, in, NULL, count); |
| 8371 | Py_END_ALLOW_THREADS |
| 8372 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8373 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8374 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 8375 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8376 | } |
| 8377 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8378 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 8379 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8380 | |
| 8381 | do { |
| 8382 | Py_BEGIN_ALLOW_THREADS |
| 8383 | ret = sendfile(out, in, &offset, count); |
| 8384 | Py_END_ALLOW_THREADS |
| 8385 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8386 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8387 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8388 | return Py_BuildValue("n", ret); |
| 8389 | #endif |
| 8390 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8391 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8392 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8393 | |
| 8394 | /*[clinic input] |
| 8395 | os.fstat |
| 8396 | |
| 8397 | fd : int |
| 8398 | |
| 8399 | Perform a stat system call on the given file descriptor. |
| 8400 | |
| 8401 | Like stat(), but for an open file descriptor. |
| 8402 | Equivalent to os.stat(fd). |
| 8403 | [clinic start generated code]*/ |
| 8404 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8405 | static PyObject * |
| 8406 | os_fstat_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8407 | /*[clinic end generated code: output=d71fe98bf042b626 input=27e0e0ebbe5600c9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8408 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8409 | STRUCT_STAT st; |
| 8410 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8411 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8412 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8413 | do { |
| 8414 | Py_BEGIN_ALLOW_THREADS |
| 8415 | res = FSTAT(fd, &st); |
| 8416 | Py_END_ALLOW_THREADS |
| 8417 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8418 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8419 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8420 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8421 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8422 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8423 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8424 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8425 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8426 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8427 | } |
| 8428 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8429 | |
| 8430 | /*[clinic input] |
| 8431 | os.isatty -> bool |
| 8432 | fd: int |
| 8433 | / |
| 8434 | |
| 8435 | Return True if the fd is connected to a terminal. |
| 8436 | |
| 8437 | Return True if the file descriptor is an open file descriptor |
| 8438 | connected to the slave end of a terminal. |
| 8439 | [clinic start generated code]*/ |
| 8440 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8441 | static int |
| 8442 | os_isatty_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8443 | /*[clinic end generated code: output=acec9d3c29d16d33 input=08ce94aa1eaf7b5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8444 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8445 | int return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8446 | if (!_PyVerify_fd(fd)) |
| 8447 | return 0; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8448 | _Py_BEGIN_SUPPRESS_IPH |
| 8449 | return_value = isatty(fd); |
| 8450 | _Py_END_SUPPRESS_IPH |
| 8451 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8452 | } |
| 8453 | |
| 8454 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8455 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8456 | /*[clinic input] |
| 8457 | os.pipe |
| 8458 | |
| 8459 | Create a pipe. |
| 8460 | |
| 8461 | Returns a tuple of two file descriptors: |
| 8462 | (read_fd, write_fd) |
| 8463 | [clinic start generated code]*/ |
| 8464 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8465 | static PyObject * |
| 8466 | os_pipe_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8467 | /*[clinic end generated code: output=6b0cd3f868ec3c40 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8468 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8469 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8470 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8471 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8472 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8473 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8474 | #else |
| 8475 | int res; |
| 8476 | #endif |
| 8477 | |
| 8478 | #ifdef MS_WINDOWS |
| 8479 | attr.nLength = sizeof(attr); |
| 8480 | attr.lpSecurityDescriptor = NULL; |
| 8481 | attr.bInheritHandle = FALSE; |
| 8482 | |
| 8483 | Py_BEGIN_ALLOW_THREADS |
| 8484 | ok = CreatePipe(&read, &write, &attr, 0); |
| 8485 | if (ok) { |
| 8486 | fds[0] = _open_osfhandle((Py_intptr_t)read, _O_RDONLY); |
| 8487 | fds[1] = _open_osfhandle((Py_intptr_t)write, _O_WRONLY); |
| 8488 | if (fds[0] == -1 || fds[1] == -1) { |
| 8489 | CloseHandle(read); |
| 8490 | CloseHandle(write); |
| 8491 | ok = 0; |
| 8492 | } |
| 8493 | } |
| 8494 | Py_END_ALLOW_THREADS |
| 8495 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8496 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8497 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8498 | #else |
| 8499 | |
| 8500 | #ifdef HAVE_PIPE2 |
| 8501 | Py_BEGIN_ALLOW_THREADS |
| 8502 | res = pipe2(fds, O_CLOEXEC); |
| 8503 | Py_END_ALLOW_THREADS |
| 8504 | |
| 8505 | if (res != 0 && errno == ENOSYS) |
| 8506 | { |
| 8507 | #endif |
| 8508 | Py_BEGIN_ALLOW_THREADS |
| 8509 | res = pipe(fds); |
| 8510 | Py_END_ALLOW_THREADS |
| 8511 | |
| 8512 | if (res == 0) { |
| 8513 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 8514 | close(fds[0]); |
| 8515 | close(fds[1]); |
| 8516 | return NULL; |
| 8517 | } |
| 8518 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 8519 | close(fds[0]); |
| 8520 | close(fds[1]); |
| 8521 | return NULL; |
| 8522 | } |
| 8523 | } |
| 8524 | #ifdef HAVE_PIPE2 |
| 8525 | } |
| 8526 | #endif |
| 8527 | |
| 8528 | if (res != 0) |
| 8529 | return PyErr_SetFromErrno(PyExc_OSError); |
| 8530 | #endif /* !MS_WINDOWS */ |
| 8531 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8532 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8533 | #endif /* HAVE_PIPE */ |
| 8534 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8535 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8536 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8537 | /*[clinic input] |
| 8538 | os.pipe2 |
| 8539 | |
| 8540 | flags: int |
| 8541 | / |
| 8542 | |
| 8543 | Create a pipe with flags set atomically. |
| 8544 | |
| 8545 | Returns a tuple of two file descriptors: |
| 8546 | (read_fd, write_fd) |
| 8547 | |
| 8548 | flags can be constructed by ORing together one or more of these values: |
| 8549 | O_NONBLOCK, O_CLOEXEC. |
| 8550 | [clinic start generated code]*/ |
| 8551 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8552 | static PyObject * |
| 8553 | os_pipe2_impl(PyModuleDef *module, int flags) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8554 | /*[clinic end generated code: output=c15b6075d0c6b2e7 input=f261b6e7e63c6817]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8555 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8556 | int fds[2]; |
| 8557 | int res; |
| 8558 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8559 | res = pipe2(fds, flags); |
| 8560 | if (res != 0) |
| 8561 | return posix_error(); |
| 8562 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 8563 | } |
| 8564 | #endif /* HAVE_PIPE2 */ |
| 8565 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8566 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8567 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8568 | /*[clinic input] |
| 8569 | os.writev -> Py_ssize_t |
| 8570 | fd: int |
| 8571 | buffers: object |
| 8572 | / |
| 8573 | |
| 8574 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 8575 | |
| 8576 | Returns the total number of bytes written. |
| 8577 | buffers must be a sequence of bytes-like objects. |
| 8578 | [clinic start generated code]*/ |
| 8579 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8580 | static Py_ssize_t |
| 8581 | os_writev_impl(PyModuleDef *module, int fd, PyObject *buffers) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8582 | /*[clinic end generated code: output=a48925dbf2d5c238 input=5b8d17fe4189d2fe]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8583 | { |
| 8584 | int cnt; |
| 8585 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8586 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8587 | struct iovec *iov; |
| 8588 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8589 | |
| 8590 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8591 | PyErr_SetString(PyExc_TypeError, |
| 8592 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8593 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8594 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8595 | cnt = PySequence_Size(buffers); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8596 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8597 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 8598 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8599 | } |
| 8600 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8601 | do { |
| 8602 | Py_BEGIN_ALLOW_THREADS |
| 8603 | result = writev(fd, iov, cnt); |
| 8604 | Py_END_ALLOW_THREADS |
| 8605 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8606 | |
| 8607 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8608 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8609 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8610 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8611 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8612 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8613 | #endif /* HAVE_WRITEV */ |
| 8614 | |
| 8615 | |
| 8616 | #ifdef HAVE_PWRITE |
| 8617 | /*[clinic input] |
| 8618 | os.pwrite -> Py_ssize_t |
| 8619 | |
| 8620 | fd: int |
| 8621 | buffer: Py_buffer |
| 8622 | offset: Py_off_t |
| 8623 | / |
| 8624 | |
| 8625 | Write bytes to a file descriptor starting at a particular offset. |
| 8626 | |
| 8627 | Write buffer to fd, starting at offset bytes from the beginning of |
| 8628 | the file. Returns the number of bytes writte. Does not change the |
| 8629 | current file offset. |
| 8630 | [clinic start generated code]*/ |
| 8631 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8632 | static Py_ssize_t |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8633 | os_pwrite_impl(PyModuleDef *module, int fd, Py_buffer *buffer, |
| 8634 | Py_off_t offset) |
| 8635 | /*[clinic end generated code: output=93aabdb40e17d325 input=19903f1b3dd26377]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8636 | { |
| 8637 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8638 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8639 | |
| 8640 | if (!_PyVerify_fd(fd)) { |
| 8641 | posix_error(); |
| 8642 | return -1; |
| 8643 | } |
| 8644 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8645 | do { |
| 8646 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8647 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8648 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8649 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8650 | Py_END_ALLOW_THREADS |
| 8651 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8652 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8653 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8654 | posix_error(); |
| 8655 | return size; |
| 8656 | } |
| 8657 | #endif /* HAVE_PWRITE */ |
| 8658 | |
| 8659 | |
| 8660 | #ifdef HAVE_MKFIFO |
| 8661 | /*[clinic input] |
| 8662 | os.mkfifo |
| 8663 | |
| 8664 | path: path_t |
| 8665 | mode: int=0o666 |
| 8666 | * |
| 8667 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 8668 | |
| 8669 | Create a "fifo" (a POSIX named pipe). |
| 8670 | |
| 8671 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8672 | and path should be relative; path will then be relative to that directory. |
| 8673 | dir_fd may not be implemented on your platform. |
| 8674 | If it is unavailable, using it will raise a NotImplementedError. |
| 8675 | [clinic start generated code]*/ |
| 8676 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8677 | static PyObject * |
| 8678 | 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] | 8679 | /*[clinic end generated code: output=8f5f5e72c630049a input=73032e98a36e0e19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8680 | { |
| 8681 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8682 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8683 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8684 | do { |
| 8685 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8686 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8687 | if (dir_fd != DEFAULT_DIR_FD) |
| 8688 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 8689 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8690 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8691 | result = mkfifo(path->narrow, mode); |
| 8692 | Py_END_ALLOW_THREADS |
| 8693 | } while (result != 0 && errno == EINTR && |
| 8694 | !(async_err = PyErr_CheckSignals())); |
| 8695 | if (result != 0) |
| 8696 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8697 | |
| 8698 | Py_RETURN_NONE; |
| 8699 | } |
| 8700 | #endif /* HAVE_MKFIFO */ |
| 8701 | |
| 8702 | |
| 8703 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 8704 | /*[clinic input] |
| 8705 | os.mknod |
| 8706 | |
| 8707 | path: path_t |
| 8708 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8709 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8710 | * |
| 8711 | dir_fd: dir_fd(requires='mknodat')=None |
| 8712 | |
| 8713 | Create a node in the file system. |
| 8714 | |
| 8715 | Create a node in the file system (file, device special file or named pipe) |
| 8716 | at path. mode specifies both the permissions to use and the |
| 8717 | type of node to be created, being combined (bitwise OR) with one of |
| 8718 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 8719 | device defines the newly created device special file (probably using |
| 8720 | os.makedev()). Otherwise device is ignored. |
| 8721 | |
| 8722 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8723 | and path should be relative; path will then be relative to that directory. |
| 8724 | dir_fd may not be implemented on your platform. |
| 8725 | If it is unavailable, using it will raise a NotImplementedError. |
| 8726 | [clinic start generated code]*/ |
| 8727 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8728 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8729 | os_mknod_impl(PyModuleDef *module, path_t *path, int mode, dev_t device, |
| 8730 | int dir_fd) |
| 8731 | /*[clinic end generated code: output=5151a8a9f754d272 input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8732 | { |
| 8733 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8734 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8735 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8736 | do { |
| 8737 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8738 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8739 | if (dir_fd != DEFAULT_DIR_FD) |
| 8740 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 8741 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8742 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8743 | result = mknod(path->narrow, mode, device); |
| 8744 | Py_END_ALLOW_THREADS |
| 8745 | } while (result != 0 && errno == EINTR && |
| 8746 | !(async_err = PyErr_CheckSignals())); |
| 8747 | if (result != 0) |
| 8748 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8749 | |
| 8750 | Py_RETURN_NONE; |
| 8751 | } |
| 8752 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 8753 | |
| 8754 | |
| 8755 | #ifdef HAVE_DEVICE_MACROS |
| 8756 | /*[clinic input] |
| 8757 | os.major -> unsigned_int |
| 8758 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8759 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8760 | / |
| 8761 | |
| 8762 | Extracts a device major number from a raw device number. |
| 8763 | [clinic start generated code]*/ |
| 8764 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8765 | static unsigned int |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8766 | os_major_impl(PyModuleDef *module, dev_t device) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8767 | /*[clinic end generated code: output=ba55693ab49bac34 input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8768 | { |
| 8769 | return major(device); |
| 8770 | } |
| 8771 | |
| 8772 | |
| 8773 | /*[clinic input] |
| 8774 | os.minor -> unsigned_int |
| 8775 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8776 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8777 | / |
| 8778 | |
| 8779 | Extracts a device minor number from a raw device number. |
| 8780 | [clinic start generated code]*/ |
| 8781 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8782 | static unsigned int |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8783 | os_minor_impl(PyModuleDef *module, dev_t device) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8784 | /*[clinic end generated code: output=2867219ebf274e27 input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8785 | { |
| 8786 | return minor(device); |
| 8787 | } |
| 8788 | |
| 8789 | |
| 8790 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8791 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8792 | |
| 8793 | major: int |
| 8794 | minor: int |
| 8795 | / |
| 8796 | |
| 8797 | Composes a raw device number from the major and minor device numbers. |
| 8798 | [clinic start generated code]*/ |
| 8799 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8800 | static dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8801 | os_makedev_impl(PyModuleDef *module, int major, int minor) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8802 | /*[clinic end generated code: output=7cb6264352437660 input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8803 | { |
| 8804 | return makedev(major, minor); |
| 8805 | } |
| 8806 | #endif /* HAVE_DEVICE_MACROS */ |
| 8807 | |
| 8808 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8809 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8810 | /*[clinic input] |
| 8811 | os.ftruncate |
| 8812 | |
| 8813 | fd: int |
| 8814 | length: Py_off_t |
| 8815 | / |
| 8816 | |
| 8817 | Truncate a file, specified by file descriptor, to a specific length. |
| 8818 | [clinic start generated code]*/ |
| 8819 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8820 | static PyObject * |
| 8821 | os_ftruncate_impl(PyModuleDef *module, int fd, Py_off_t length) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8822 | /*[clinic end generated code: output=3666f401d76bf834 input=63b43641e52818f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8823 | { |
| 8824 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8825 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8826 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8827 | if (!_PyVerify_fd(fd)) |
| 8828 | return posix_error(); |
| 8829 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8830 | do { |
| 8831 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8832 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8833 | #ifdef MS_WINDOWS |
| 8834 | result = _chsize_s(fd, length); |
| 8835 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8836 | result = ftruncate(fd, length); |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8837 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8838 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8839 | Py_END_ALLOW_THREADS |
| 8840 | } while (result != 0 && errno == EINTR && |
| 8841 | !(async_err = PyErr_CheckSignals())); |
| 8842 | if (result != 0) |
| 8843 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8844 | Py_RETURN_NONE; |
| 8845 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8846 | #endif /* HAVE_FTRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8847 | |
| 8848 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8849 | #if defined HAVE_TRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8850 | /*[clinic input] |
| 8851 | os.truncate |
| 8852 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 8853 | length: Py_off_t |
| 8854 | |
| 8855 | Truncate a file, specified by path, to a specific length. |
| 8856 | |
| 8857 | On some platforms, path may also be specified as an open file descriptor. |
| 8858 | If this functionality is unavailable, using it raises an exception. |
| 8859 | [clinic start generated code]*/ |
| 8860 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8861 | static PyObject * |
| 8862 | os_truncate_impl(PyModuleDef *module, path_t *path, Py_off_t length) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8863 | /*[clinic end generated code: output=f60a9e08370e9e2e input=77229cf0b50a9b77]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8864 | { |
| 8865 | int result; |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8866 | #ifdef MS_WINDOWS |
| 8867 | int fd; |
| 8868 | #endif |
| 8869 | |
| 8870 | if (path->fd != -1) |
| 8871 | return os_ftruncate_impl(module, path->fd, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8872 | |
| 8873 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8874 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8875 | #ifdef MS_WINDOWS |
| 8876 | if (path->wide) |
| 8877 | fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8878 | else |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8879 | fd = _open(path->narrow, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Victor Stinner | cc0bbbc | 2015-04-25 00:21:52 +0200 | [diff] [blame] | 8880 | if (fd < 0) |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8881 | result = -1; |
| 8882 | else { |
| 8883 | result = _chsize_s(fd, length); |
| 8884 | close(fd); |
| 8885 | if (result < 0) |
| 8886 | errno = result; |
| 8887 | } |
| 8888 | #else |
| 8889 | result = truncate(path->narrow, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8890 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8891 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8892 | Py_END_ALLOW_THREADS |
| 8893 | if (result < 0) |
| 8894 | return path_error(path); |
| 8895 | |
| 8896 | Py_RETURN_NONE; |
| 8897 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8898 | #endif /* HAVE_TRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8899 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8900 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 8901 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 8902 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 8903 | defined, which is the case in Python on AIX. AIX bug report: |
| 8904 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 8905 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 8906 | # define POSIX_FADVISE_AIX_BUG |
| 8907 | #endif |
| 8908 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 8909 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 8910 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8911 | /*[clinic input] |
| 8912 | os.posix_fallocate |
| 8913 | |
| 8914 | fd: int |
| 8915 | offset: Py_off_t |
| 8916 | length: Py_off_t |
| 8917 | / |
| 8918 | |
| 8919 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 8920 | |
| 8921 | Ensure that the file specified by fd encompasses a range of bytes |
| 8922 | starting at offset bytes from the beginning and continuing for length bytes. |
| 8923 | [clinic start generated code]*/ |
| 8924 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8925 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8926 | os_posix_fallocate_impl(PyModuleDef *module, int fd, Py_off_t offset, |
| 8927 | Py_off_t length) |
| 8928 | /*[clinic end generated code: output=7f6f87a8c751e1b4 input=d7a2ef0ab2ca52fb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8929 | { |
| 8930 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8931 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8932 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8933 | do { |
| 8934 | Py_BEGIN_ALLOW_THREADS |
| 8935 | result = posix_fallocate(fd, offset, length); |
| 8936 | Py_END_ALLOW_THREADS |
| 8937 | } while (result != 0 && errno == EINTR && |
| 8938 | !(async_err = PyErr_CheckSignals())); |
| 8939 | if (result != 0) |
| 8940 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8941 | Py_RETURN_NONE; |
| 8942 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 8943 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8944 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8945 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 8946 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8947 | /*[clinic input] |
| 8948 | os.posix_fadvise |
| 8949 | |
| 8950 | fd: int |
| 8951 | offset: Py_off_t |
| 8952 | length: Py_off_t |
| 8953 | advice: int |
| 8954 | / |
| 8955 | |
| 8956 | Announce an intention to access data in a specific pattern. |
| 8957 | |
| 8958 | Announce an intention to access data in a specific pattern, thus allowing |
| 8959 | the kernel to make optimizations. |
| 8960 | The advice applies to the region of the file specified by fd starting at |
| 8961 | offset and continuing for length bytes. |
| 8962 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 8963 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 8964 | POSIX_FADV_DONTNEED. |
| 8965 | [clinic start generated code]*/ |
| 8966 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8967 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8968 | os_posix_fadvise_impl(PyModuleDef *module, int fd, Py_off_t offset, |
| 8969 | Py_off_t length, int advice) |
| 8970 | /*[clinic end generated code: output=457ce6a67189e10d input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8971 | { |
| 8972 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8973 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8974 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8975 | do { |
| 8976 | Py_BEGIN_ALLOW_THREADS |
| 8977 | result = posix_fadvise(fd, offset, length, advice); |
| 8978 | Py_END_ALLOW_THREADS |
| 8979 | } while (result != 0 && errno == EINTR && |
| 8980 | !(async_err = PyErr_CheckSignals())); |
| 8981 | if (result != 0) |
| 8982 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8983 | Py_RETURN_NONE; |
| 8984 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 8985 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8986 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8987 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8988 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8989 | /* Save putenv() parameters as values here, so we can collect them when they |
| 8990 | * get re-set with another call for the same key. */ |
| 8991 | static PyObject *posix_putenv_garbage; |
| 8992 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8993 | static void |
| 8994 | posix_putenv_garbage_setitem(PyObject *name, PyObject *value) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8995 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8996 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 8997 | * this will cause previous value to be collected. This has to |
| 8998 | * happen after the real putenv() call because the old value |
| 8999 | * was still accessible until then. */ |
| 9000 | if (PyDict_SetItem(posix_putenv_garbage, name, value)) |
| 9001 | /* really not much we can do; just leak */ |
| 9002 | PyErr_Clear(); |
| 9003 | else |
| 9004 | Py_DECREF(value); |
| 9005 | } |
| 9006 | |
| 9007 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 9008 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9009 | /*[clinic input] |
| 9010 | os.putenv |
| 9011 | |
| 9012 | name: unicode |
| 9013 | value: unicode |
| 9014 | / |
| 9015 | |
| 9016 | Change or add an environment variable. |
| 9017 | [clinic start generated code]*/ |
| 9018 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9019 | static PyObject * |
| 9020 | os_putenv_impl(PyModuleDef *module, PyObject *name, PyObject *value) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9021 | /*[clinic end generated code: output=a2438cf95e5a0c1c input=ba586581c2e6105f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9022 | { |
| 9023 | wchar_t *env; |
| 9024 | |
| 9025 | PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 9026 | if (unicode == NULL) { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9027 | PyErr_NoMemory(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9028 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9029 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9030 | if (_MAX_ENV < PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9031 | PyErr_Format(PyExc_ValueError, |
| 9032 | "the environment variable is longer than %u characters", |
| 9033 | _MAX_ENV); |
| 9034 | goto error; |
| 9035 | } |
| 9036 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9037 | env = PyUnicode_AsUnicode(unicode); |
| 9038 | if (env == NULL) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9039 | goto error; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9040 | if (_wputenv(env)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9041 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9042 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9043 | } |
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 | posix_putenv_garbage_setitem(name, unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9046 | Py_RETURN_NONE; |
| 9047 | |
| 9048 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9049 | Py_DECREF(unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9050 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9051 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9052 | #else /* MS_WINDOWS */ |
| 9053 | /*[clinic input] |
| 9054 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9055 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9056 | name: FSConverter |
| 9057 | value: FSConverter |
| 9058 | / |
| 9059 | |
| 9060 | Change or add an environment variable. |
| 9061 | [clinic start generated code]*/ |
| 9062 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9063 | static PyObject * |
| 9064 | os_putenv_impl(PyModuleDef *module, PyObject *name, PyObject *value) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9065 | /*[clinic end generated code: output=a2438cf95e5a0c1c input=a97bc6152f688d31]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9066 | { |
| 9067 | PyObject *bytes = NULL; |
| 9068 | char *env; |
| 9069 | char *name_string = PyBytes_AsString(name); |
| 9070 | char *value_string = PyBytes_AsString(value); |
| 9071 | |
| 9072 | bytes = PyBytes_FromFormat("%s=%s", name_string, value_string); |
| 9073 | if (bytes == NULL) { |
| 9074 | PyErr_NoMemory(); |
| 9075 | return NULL; |
| 9076 | } |
| 9077 | |
| 9078 | env = PyBytes_AS_STRING(bytes); |
| 9079 | if (putenv(env)) { |
| 9080 | Py_DECREF(bytes); |
| 9081 | return posix_error(); |
| 9082 | } |
| 9083 | |
| 9084 | posix_putenv_garbage_setitem(name, bytes); |
| 9085 | Py_RETURN_NONE; |
| 9086 | } |
| 9087 | #endif /* MS_WINDOWS */ |
| 9088 | #endif /* HAVE_PUTENV */ |
| 9089 | |
| 9090 | |
| 9091 | #ifdef HAVE_UNSETENV |
| 9092 | /*[clinic input] |
| 9093 | os.unsetenv |
| 9094 | name: FSConverter |
| 9095 | / |
| 9096 | |
| 9097 | Delete an environment variable. |
| 9098 | [clinic start generated code]*/ |
| 9099 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9100 | static PyObject * |
| 9101 | os_unsetenv_impl(PyModuleDef *module, PyObject *name) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9102 | /*[clinic end generated code: output=25994b57016a2dc9 input=2bb5288a599c7107]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9103 | { |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9104 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 9105 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9106 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9107 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9108 | #ifdef HAVE_BROKEN_UNSETENV |
| 9109 | unsetenv(PyBytes_AS_STRING(name)); |
| 9110 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9111 | err = unsetenv(PyBytes_AS_STRING(name)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9112 | if (err) |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 9113 | return posix_error(); |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9114 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9115 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9116 | /* Remove the key from posix_putenv_garbage; |
| 9117 | * this will cause it to be collected. This has to |
| 9118 | * happen after the real unsetenv() call because the |
| 9119 | * old value was still accessible until then. |
| 9120 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9121 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9122 | /* really not much we can do; just leak */ |
| 9123 | PyErr_Clear(); |
| 9124 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9125 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9126 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9127 | #endif /* HAVE_UNSETENV */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9128 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9129 | |
| 9130 | /*[clinic input] |
| 9131 | os.strerror |
| 9132 | |
| 9133 | code: int |
| 9134 | / |
| 9135 | |
| 9136 | Translate an error code to a message string. |
| 9137 | [clinic start generated code]*/ |
| 9138 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9139 | static PyObject * |
| 9140 | os_strerror_impl(PyModuleDef *module, int code) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9141 | /*[clinic end generated code: output=0280c6af51e5c9fe input=75a8673d97915a91]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9142 | { |
| 9143 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9144 | if (message == NULL) { |
| 9145 | PyErr_SetString(PyExc_ValueError, |
| 9146 | "strerror() argument out of range"); |
| 9147 | return NULL; |
| 9148 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 9149 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9150 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9151 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9152 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9153 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9154 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9155 | /*[clinic input] |
| 9156 | os.WCOREDUMP -> bool |
| 9157 | |
| 9158 | status: int |
| 9159 | / |
| 9160 | |
| 9161 | Return True if the process returning status was dumped to a core file. |
| 9162 | [clinic start generated code]*/ |
| 9163 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9164 | static int |
| 9165 | os_WCOREDUMP_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9166 | /*[clinic end generated code: output=134f70bbe63fbf41 input=8b05e7ab38528d04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9167 | { |
| 9168 | WAIT_TYPE wait_status; |
| 9169 | WAIT_STATUS_INT(wait_status) = status; |
| 9170 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9171 | } |
| 9172 | #endif /* WCOREDUMP */ |
| 9173 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9174 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9175 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9176 | /*[clinic input] |
| 9177 | os.WIFCONTINUED -> bool |
| 9178 | |
| 9179 | status: int |
| 9180 | |
| 9181 | Return True if a particular process was continued from a job control stop. |
| 9182 | |
| 9183 | Return True if the process returning status was continued from a |
| 9184 | job control stop. |
| 9185 | [clinic start generated code]*/ |
| 9186 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9187 | static int |
| 9188 | os_WIFCONTINUED_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9189 | /*[clinic end generated code: output=9cdd26543ebb6dcd input=e777e7d38eb25bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9190 | { |
| 9191 | WAIT_TYPE wait_status; |
| 9192 | WAIT_STATUS_INT(wait_status) = status; |
| 9193 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9194 | } |
| 9195 | #endif /* WIFCONTINUED */ |
| 9196 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9197 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9198 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9199 | /*[clinic input] |
| 9200 | os.WIFSTOPPED -> bool |
| 9201 | |
| 9202 | status: int |
| 9203 | |
| 9204 | Return True if the process returning status was stopped. |
| 9205 | [clinic start generated code]*/ |
| 9206 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9207 | static int |
| 9208 | os_WIFSTOPPED_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9209 | /*[clinic end generated code: output=73bf35e44994a724 input=043cb7f1289ef904]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9210 | { |
| 9211 | WAIT_TYPE wait_status; |
| 9212 | WAIT_STATUS_INT(wait_status) = status; |
| 9213 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9214 | } |
| 9215 | #endif /* WIFSTOPPED */ |
| 9216 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9217 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9218 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9219 | /*[clinic input] |
| 9220 | os.WIFSIGNALED -> bool |
| 9221 | |
| 9222 | status: int |
| 9223 | |
| 9224 | Return True if the process returning status was terminated by a signal. |
| 9225 | [clinic start generated code]*/ |
| 9226 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9227 | static int |
| 9228 | os_WIFSIGNALED_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9229 | /*[clinic end generated code: output=2697975771872420 input=d55ba7cc9ce5dc43]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9230 | { |
| 9231 | WAIT_TYPE wait_status; |
| 9232 | WAIT_STATUS_INT(wait_status) = status; |
| 9233 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9234 | } |
| 9235 | #endif /* WIFSIGNALED */ |
| 9236 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9237 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9238 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9239 | /*[clinic input] |
| 9240 | os.WIFEXITED -> bool |
| 9241 | |
| 9242 | status: int |
| 9243 | |
| 9244 | Return True if the process returning status exited via the exit() system call. |
| 9245 | [clinic start generated code]*/ |
| 9246 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9247 | static int |
| 9248 | os_WIFEXITED_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9249 | /*[clinic end generated code: output=ca8f8c61f0b8532e input=d63775a6791586c0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9250 | { |
| 9251 | WAIT_TYPE wait_status; |
| 9252 | WAIT_STATUS_INT(wait_status) = status; |
| 9253 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9254 | } |
| 9255 | #endif /* WIFEXITED */ |
| 9256 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9257 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 9258 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9259 | /*[clinic input] |
| 9260 | os.WEXITSTATUS -> int |
| 9261 | |
| 9262 | status: int |
| 9263 | |
| 9264 | Return the process return code from status. |
| 9265 | [clinic start generated code]*/ |
| 9266 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9267 | static int |
| 9268 | os_WEXITSTATUS_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9269 | /*[clinic end generated code: output=ea54da23d9e0f6af input=e1fb4944e377585b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9270 | { |
| 9271 | WAIT_TYPE wait_status; |
| 9272 | WAIT_STATUS_INT(wait_status) = status; |
| 9273 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9274 | } |
| 9275 | #endif /* WEXITSTATUS */ |
| 9276 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9277 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9278 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9279 | /*[clinic input] |
| 9280 | os.WTERMSIG -> int |
| 9281 | |
| 9282 | status: int |
| 9283 | |
| 9284 | Return the signal that terminated the process that provided the status value. |
| 9285 | [clinic start generated code]*/ |
| 9286 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9287 | static int |
| 9288 | os_WTERMSIG_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9289 | /*[clinic end generated code: output=4d25367026cb852c input=727fd7f84ec3f243]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9290 | { |
| 9291 | WAIT_TYPE wait_status; |
| 9292 | WAIT_STATUS_INT(wait_status) = status; |
| 9293 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9294 | } |
| 9295 | #endif /* WTERMSIG */ |
| 9296 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9297 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9298 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9299 | /*[clinic input] |
| 9300 | os.WSTOPSIG -> int |
| 9301 | |
| 9302 | status: int |
| 9303 | |
| 9304 | Return the signal that stopped the process that provided the status value. |
| 9305 | [clinic start generated code]*/ |
| 9306 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9307 | static int |
| 9308 | os_WSTOPSIG_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9309 | /*[clinic end generated code: output=54eb9c13b001adb4 input=46ebf1d1b293c5c1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9310 | { |
| 9311 | WAIT_TYPE wait_status; |
| 9312 | WAIT_STATUS_INT(wait_status) = status; |
| 9313 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9314 | } |
| 9315 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9316 | #endif /* HAVE_SYS_WAIT_H */ |
| 9317 | |
| 9318 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9319 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 9320 | #ifdef _SCO_DS |
| 9321 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 9322 | needed definitions in sys/statvfs.h */ |
| 9323 | #define _SVID3 |
| 9324 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9325 | #include <sys/statvfs.h> |
| 9326 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9327 | static PyObject* |
| 9328 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9329 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 9330 | if (v == NULL) |
| 9331 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9332 | |
| 9333 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9334 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9335 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9336 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 9337 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 9338 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 9339 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 9340 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 9341 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 9342 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9343 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9344 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9345 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9346 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9347 | PyStructSequence_SET_ITEM(v, 2, |
| 9348 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 9349 | PyStructSequence_SET_ITEM(v, 3, |
| 9350 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 9351 | PyStructSequence_SET_ITEM(v, 4, |
| 9352 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 9353 | PyStructSequence_SET_ITEM(v, 5, |
| 9354 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 9355 | PyStructSequence_SET_ITEM(v, 6, |
| 9356 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 9357 | PyStructSequence_SET_ITEM(v, 7, |
| 9358 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 9359 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9360 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9361 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 9362 | if (PyErr_Occurred()) { |
| 9363 | Py_DECREF(v); |
| 9364 | return NULL; |
| 9365 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9366 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9367 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9368 | } |
| 9369 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9370 | |
| 9371 | /*[clinic input] |
| 9372 | os.fstatvfs |
| 9373 | fd: int |
| 9374 | / |
| 9375 | |
| 9376 | Perform an fstatvfs system call on the given fd. |
| 9377 | |
| 9378 | Equivalent to statvfs(fd). |
| 9379 | [clinic start generated code]*/ |
| 9380 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9381 | static PyObject * |
| 9382 | os_fstatvfs_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9383 | /*[clinic end generated code: output=584a94a754497ac0 input=d8122243ac50975e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9384 | { |
| 9385 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9386 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9387 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9388 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9389 | do { |
| 9390 | Py_BEGIN_ALLOW_THREADS |
| 9391 | result = fstatvfs(fd, &st); |
| 9392 | Py_END_ALLOW_THREADS |
| 9393 | } while (result != 0 && errno == EINTR && |
| 9394 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9395 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9396 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9397 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9398 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9399 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9400 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9401 | |
| 9402 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9403 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9404 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9405 | /*[clinic input] |
| 9406 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9407 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9408 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 9409 | |
| 9410 | Perform a statvfs system call on the given path. |
| 9411 | |
| 9412 | path may always be specified as a string. |
| 9413 | On some platforms, path may also be specified as an open file descriptor. |
| 9414 | If this functionality is unavailable, using it raises an exception. |
| 9415 | [clinic start generated code]*/ |
| 9416 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9417 | static PyObject * |
| 9418 | os_statvfs_impl(PyModuleDef *module, path_t *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9419 | /*[clinic end generated code: output=5ced07a2cf931f41 input=3f5c35791c669bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9420 | { |
| 9421 | int result; |
| 9422 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9423 | |
| 9424 | Py_BEGIN_ALLOW_THREADS |
| 9425 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9426 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9427 | #ifdef __APPLE__ |
| 9428 | /* handle weak-linking on Mac OS X 10.3 */ |
| 9429 | if (fstatvfs == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9430 | fd_specified("statvfs", path->fd); |
| 9431 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9432 | } |
| 9433 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9434 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9435 | } |
| 9436 | else |
| 9437 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9438 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9439 | Py_END_ALLOW_THREADS |
| 9440 | |
| 9441 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9442 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9443 | } |
| 9444 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9445 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9446 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9447 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 9448 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9449 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9450 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9451 | /*[clinic input] |
| 9452 | os._getdiskusage |
| 9453 | |
| 9454 | path: Py_UNICODE |
| 9455 | |
| 9456 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 9457 | [clinic start generated code]*/ |
| 9458 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9459 | static PyObject * |
| 9460 | os__getdiskusage_impl(PyModuleDef *module, Py_UNICODE *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9461 | /*[clinic end generated code: output=60a9cf33449db1dd input=6458133aed893c78]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9462 | { |
| 9463 | BOOL retval; |
| 9464 | ULARGE_INTEGER _, total, free; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9465 | |
| 9466 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 9467 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9468 | Py_END_ALLOW_THREADS |
| 9469 | if (retval == 0) |
| 9470 | return PyErr_SetFromWindowsErr(0); |
| 9471 | |
| 9472 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 9473 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9474 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9475 | |
| 9476 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9477 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 9478 | * It maps strings representing configuration variable names to |
| 9479 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 9480 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9481 | * rarely-used constants. There are three separate tables that use |
| 9482 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9483 | * |
| 9484 | * This code is always included, even if none of the interfaces that |
| 9485 | * need it are included. The #if hackery needed to avoid it would be |
| 9486 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9487 | */ |
| 9488 | struct constdef { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 9489 | const char *name; |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 9490 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9491 | }; |
| 9492 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9493 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9494 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 9495 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9496 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9497 | if (PyLong_Check(arg)) { |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 9498 | int value = _PyLong_AsInt(arg); |
| 9499 | if (value == -1 && PyErr_Occurred()) |
| 9500 | return 0; |
| 9501 | *valuep = value; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9502 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9503 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 9504 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9505 | /* look up the value in the table using a binary search */ |
| 9506 | size_t lo = 0; |
| 9507 | size_t mid; |
| 9508 | size_t hi = tablesize; |
| 9509 | int cmp; |
| 9510 | const char *confname; |
| 9511 | if (!PyUnicode_Check(arg)) { |
| 9512 | PyErr_SetString(PyExc_TypeError, |
| 9513 | "configuration names must be strings or integers"); |
| 9514 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9515 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9516 | confname = _PyUnicode_AsString(arg); |
| 9517 | if (confname == NULL) |
| 9518 | return 0; |
| 9519 | while (lo < hi) { |
| 9520 | mid = (lo + hi) / 2; |
| 9521 | cmp = strcmp(confname, table[mid].name); |
| 9522 | if (cmp < 0) |
| 9523 | hi = mid; |
| 9524 | else if (cmp > 0) |
| 9525 | lo = mid + 1; |
| 9526 | else { |
| 9527 | *valuep = table[mid].value; |
| 9528 | return 1; |
| 9529 | } |
| 9530 | } |
| 9531 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 9532 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9533 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9534 | } |
| 9535 | |
| 9536 | |
| 9537 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 9538 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9539 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9540 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9541 | #endif |
| 9542 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9543 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9544 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9545 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9546 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9547 | #endif |
| 9548 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9549 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9550 | #endif |
| 9551 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9552 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9553 | #endif |
| 9554 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9555 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9556 | #endif |
| 9557 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9558 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9559 | #endif |
| 9560 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9561 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9562 | #endif |
| 9563 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9564 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9565 | #endif |
| 9566 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9567 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9568 | #endif |
| 9569 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9570 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9571 | #endif |
| 9572 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9573 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9574 | #endif |
| 9575 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9576 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9577 | #endif |
| 9578 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9579 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9580 | #endif |
| 9581 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9582 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9583 | #endif |
| 9584 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9585 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9586 | #endif |
| 9587 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9588 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9589 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 9590 | #ifdef _PC_ACL_ENABLED |
| 9591 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 9592 | #endif |
| 9593 | #ifdef _PC_MIN_HOLE_SIZE |
| 9594 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 9595 | #endif |
| 9596 | #ifdef _PC_ALLOC_SIZE_MIN |
| 9597 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 9598 | #endif |
| 9599 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 9600 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 9601 | #endif |
| 9602 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 9603 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 9604 | #endif |
| 9605 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 9606 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 9607 | #endif |
| 9608 | #ifdef _PC_REC_XFER_ALIGN |
| 9609 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 9610 | #endif |
| 9611 | #ifdef _PC_SYMLINK_MAX |
| 9612 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 9613 | #endif |
| 9614 | #ifdef _PC_XATTR_ENABLED |
| 9615 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 9616 | #endif |
| 9617 | #ifdef _PC_XATTR_EXISTS |
| 9618 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 9619 | #endif |
| 9620 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 9621 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 9622 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9623 | }; |
| 9624 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9625 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9626 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9627 | { |
| 9628 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 9629 | sizeof(posix_constants_pathconf) |
| 9630 | / sizeof(struct constdef)); |
| 9631 | } |
| 9632 | #endif |
| 9633 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9634 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9635 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9636 | /*[clinic input] |
| 9637 | os.fpathconf -> long |
| 9638 | |
| 9639 | fd: int |
| 9640 | name: path_confname |
| 9641 | / |
| 9642 | |
| 9643 | Return the configuration limit name for the file descriptor fd. |
| 9644 | |
| 9645 | If there is no limit, return -1. |
| 9646 | [clinic start generated code]*/ |
| 9647 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9648 | static long |
| 9649 | os_fpathconf_impl(PyModuleDef *module, int fd, int name) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9650 | /*[clinic end generated code: output=082b2922d4441de7 input=5942a024d3777810]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9651 | { |
| 9652 | long limit; |
| 9653 | |
| 9654 | errno = 0; |
| 9655 | limit = fpathconf(fd, name); |
| 9656 | if (limit == -1 && errno != 0) |
| 9657 | posix_error(); |
| 9658 | |
| 9659 | return limit; |
| 9660 | } |
| 9661 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9662 | |
| 9663 | |
| 9664 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9665 | /*[clinic input] |
| 9666 | os.pathconf -> long |
| 9667 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 9668 | name: path_confname |
| 9669 | |
| 9670 | Return the configuration limit name for the file or directory path. |
| 9671 | |
| 9672 | If there is no limit, return -1. |
| 9673 | On some platforms, path may also be specified as an open file descriptor. |
| 9674 | If this functionality is unavailable, using it raises an exception. |
| 9675 | [clinic start generated code]*/ |
| 9676 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9677 | static long |
| 9678 | os_pathconf_impl(PyModuleDef *module, path_t *path, int name) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9679 | /*[clinic end generated code: output=3713029e9501f5ab input=bc3e2a985af27e5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9680 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9681 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9682 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9683 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9684 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9685 | if (path->fd != -1) |
| 9686 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9687 | else |
| 9688 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9689 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9690 | if (limit == -1 && errno != 0) { |
| 9691 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 9692 | /* could be a path or name problem */ |
| 9693 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9694 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9695 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9696 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9697 | |
| 9698 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9699 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9700 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9701 | |
| 9702 | #ifdef HAVE_CONFSTR |
| 9703 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9704 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9705 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9706 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9707 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9708 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9709 | #endif |
| 9710 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9711 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9712 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9713 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9714 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9715 | #endif |
| 9716 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9717 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9718 | #endif |
| 9719 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9720 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9721 | #endif |
| 9722 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9723 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9724 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9725 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9726 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9727 | #endif |
| 9728 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9729 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9730 | #endif |
| 9731 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9732 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9733 | #endif |
| 9734 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9735 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9736 | #endif |
| 9737 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9738 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9739 | #endif |
| 9740 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9741 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9742 | #endif |
| 9743 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9744 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9745 | #endif |
| 9746 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9747 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9748 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9749 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9750 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9751 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9752 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9753 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9754 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9755 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9756 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9757 | #endif |
| 9758 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9759 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9760 | #endif |
| 9761 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9762 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9763 | #endif |
| 9764 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9765 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9766 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9767 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9768 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9769 | #endif |
| 9770 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9771 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9772 | #endif |
| 9773 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9774 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9775 | #endif |
| 9776 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9777 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9778 | #endif |
| 9779 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9780 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9781 | #endif |
| 9782 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9783 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9784 | #endif |
| 9785 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9786 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9787 | #endif |
| 9788 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9789 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9790 | #endif |
| 9791 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9792 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9793 | #endif |
| 9794 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9795 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9796 | #endif |
| 9797 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9798 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9799 | #endif |
| 9800 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9801 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9802 | #endif |
| 9803 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9804 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9805 | #endif |
| 9806 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9807 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9808 | #endif |
| 9809 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9810 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9811 | #endif |
| 9812 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9813 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9814 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9815 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9816 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9817 | #endif |
| 9818 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9819 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9820 | #endif |
| 9821 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9822 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9823 | #endif |
| 9824 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9825 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9826 | #endif |
| 9827 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9828 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9829 | #endif |
| 9830 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9831 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9832 | #endif |
| 9833 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9834 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9835 | #endif |
| 9836 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9837 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9838 | #endif |
| 9839 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9840 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9841 | #endif |
| 9842 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9843 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9844 | #endif |
| 9845 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9846 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9847 | #endif |
| 9848 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9849 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9850 | #endif |
| 9851 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9852 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9853 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9854 | }; |
| 9855 | |
| 9856 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9857 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9858 | { |
| 9859 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 9860 | sizeof(posix_constants_confstr) |
| 9861 | / sizeof(struct constdef)); |
| 9862 | } |
| 9863 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9864 | |
| 9865 | /*[clinic input] |
| 9866 | os.confstr |
| 9867 | |
| 9868 | name: confstr_confname |
| 9869 | / |
| 9870 | |
| 9871 | Return a string-valued system configuration variable. |
| 9872 | [clinic start generated code]*/ |
| 9873 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9874 | static PyObject * |
| 9875 | os_confstr_impl(PyModuleDef *module, int name) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9876 | /*[clinic end generated code: output=6ff79c9eed8c2daf input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9877 | { |
| 9878 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9879 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9880 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9881 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9882 | errno = 0; |
| 9883 | len = confstr(name, buffer, sizeof(buffer)); |
| 9884 | if (len == 0) { |
| 9885 | if (errno) { |
| 9886 | posix_error(); |
| 9887 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9888 | } |
| 9889 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9890 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9891 | } |
| 9892 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9893 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9894 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 9895 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9896 | char *buf = PyMem_Malloc(len); |
| 9897 | if (buf == NULL) |
| 9898 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 9899 | len2 = confstr(name, buf, len); |
| 9900 | assert(len == len2); |
Christian Heimes | 8714cfd | 2015-04-21 10:57:41 +0200 | [diff] [blame] | 9901 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9902 | PyMem_Free(buf); |
| 9903 | } |
| 9904 | else |
| 9905 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9906 | return result; |
| 9907 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9908 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9909 | |
| 9910 | |
| 9911 | #ifdef HAVE_SYSCONF |
| 9912 | static struct constdef posix_constants_sysconf[] = { |
| 9913 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9914 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9915 | #endif |
| 9916 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9917 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9918 | #endif |
| 9919 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9920 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9921 | #endif |
| 9922 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9923 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9924 | #endif |
| 9925 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9926 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9927 | #endif |
| 9928 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9929 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9930 | #endif |
| 9931 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9932 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9933 | #endif |
| 9934 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9935 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9936 | #endif |
| 9937 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9938 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9939 | #endif |
| 9940 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9941 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9942 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9943 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9944 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9945 | #endif |
| 9946 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9947 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9948 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9949 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9950 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9951 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9952 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9953 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9954 | #endif |
| 9955 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9956 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9957 | #endif |
| 9958 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9959 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9960 | #endif |
| 9961 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9962 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9963 | #endif |
| 9964 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9965 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9966 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9967 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9968 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9969 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9970 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9971 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9972 | #endif |
| 9973 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9974 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9975 | #endif |
| 9976 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9977 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9978 | #endif |
| 9979 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9980 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9981 | #endif |
| 9982 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9983 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9984 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9985 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9986 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9987 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9988 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9989 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9990 | #endif |
| 9991 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9992 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9993 | #endif |
| 9994 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9995 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9996 | #endif |
| 9997 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9998 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9999 | #endif |
| 10000 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10001 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10002 | #endif |
| 10003 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10004 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10005 | #endif |
| 10006 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10007 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10008 | #endif |
| 10009 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10010 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10011 | #endif |
| 10012 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10013 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10014 | #endif |
| 10015 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10016 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10017 | #endif |
| 10018 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10019 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10020 | #endif |
| 10021 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10022 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10023 | #endif |
| 10024 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10025 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10026 | #endif |
| 10027 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10028 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10029 | #endif |
| 10030 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10031 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10032 | #endif |
| 10033 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10034 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10035 | #endif |
| 10036 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10037 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10038 | #endif |
| 10039 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10040 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10041 | #endif |
| 10042 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10043 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10044 | #endif |
| 10045 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10046 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10047 | #endif |
| 10048 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10049 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10050 | #endif |
| 10051 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10052 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10053 | #endif |
| 10054 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10055 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10056 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10057 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10058 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10059 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10060 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10061 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10062 | #endif |
| 10063 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10064 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10065 | #endif |
| 10066 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10067 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10068 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10069 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10070 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10071 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10072 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10073 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10074 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10075 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10076 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10077 | #endif |
| 10078 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10079 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10080 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10081 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10082 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10083 | #endif |
| 10084 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10085 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10086 | #endif |
| 10087 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10088 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10089 | #endif |
| 10090 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10091 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10092 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10093 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10094 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10095 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10096 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10097 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10098 | #endif |
| 10099 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10100 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10101 | #endif |
| 10102 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10103 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10104 | #endif |
| 10105 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10106 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10107 | #endif |
| 10108 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10109 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10110 | #endif |
| 10111 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10112 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10113 | #endif |
| 10114 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10115 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10116 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10117 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10118 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10119 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10120 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10121 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10122 | #endif |
| 10123 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10124 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10125 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10126 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10127 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10128 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10129 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10130 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10131 | #endif |
| 10132 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10133 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10134 | #endif |
| 10135 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10136 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10137 | #endif |
| 10138 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10139 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10140 | #endif |
| 10141 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10142 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10143 | #endif |
| 10144 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10145 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10146 | #endif |
| 10147 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10148 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10149 | #endif |
| 10150 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10151 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10152 | #endif |
| 10153 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10154 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10155 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10156 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10157 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10158 | #endif |
| 10159 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10160 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10161 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10162 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10163 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10164 | #endif |
| 10165 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10166 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10167 | #endif |
| 10168 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10169 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10170 | #endif |
| 10171 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10172 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10173 | #endif |
| 10174 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10175 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10176 | #endif |
| 10177 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10178 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10179 | #endif |
| 10180 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10181 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10182 | #endif |
| 10183 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10184 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10185 | #endif |
| 10186 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10187 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10188 | #endif |
| 10189 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10190 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10191 | #endif |
| 10192 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10193 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10194 | #endif |
| 10195 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10196 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10197 | #endif |
| 10198 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10199 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10200 | #endif |
| 10201 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10202 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10203 | #endif |
| 10204 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10205 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10206 | #endif |
| 10207 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10208 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10209 | #endif |
| 10210 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10211 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10212 | #endif |
| 10213 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10214 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10215 | #endif |
| 10216 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10217 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10218 | #endif |
| 10219 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10220 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10221 | #endif |
| 10222 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10223 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10224 | #endif |
| 10225 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10226 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10227 | #endif |
| 10228 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10229 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10230 | #endif |
| 10231 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10232 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10233 | #endif |
| 10234 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10235 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10236 | #endif |
| 10237 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10238 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10239 | #endif |
| 10240 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10241 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10242 | #endif |
| 10243 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10244 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10245 | #endif |
| 10246 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10247 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10248 | #endif |
| 10249 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10250 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10251 | #endif |
| 10252 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10253 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10254 | #endif |
| 10255 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10256 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10257 | #endif |
| 10258 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10259 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10260 | #endif |
| 10261 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10262 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10263 | #endif |
| 10264 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10265 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10266 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10267 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10268 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10269 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10270 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10271 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10272 | #endif |
| 10273 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10274 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10275 | #endif |
| 10276 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10277 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10278 | #endif |
| 10279 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10280 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10281 | #endif |
| 10282 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10283 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10284 | #endif |
| 10285 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10286 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10287 | #endif |
| 10288 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10289 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10290 | #endif |
| 10291 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10292 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10293 | #endif |
| 10294 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10295 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10296 | #endif |
| 10297 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10298 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10299 | #endif |
| 10300 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10301 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10302 | #endif |
| 10303 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10304 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10305 | #endif |
| 10306 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10307 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10308 | #endif |
| 10309 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10310 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10311 | #endif |
| 10312 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10313 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10314 | #endif |
| 10315 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10316 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10317 | #endif |
| 10318 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10319 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10320 | #endif |
| 10321 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10322 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10323 | #endif |
| 10324 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10325 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10326 | #endif |
| 10327 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10328 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10329 | #endif |
| 10330 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10331 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10332 | #endif |
| 10333 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10334 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10335 | #endif |
| 10336 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10337 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10338 | #endif |
| 10339 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10340 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10341 | #endif |
| 10342 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10343 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10344 | #endif |
| 10345 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10346 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10347 | #endif |
| 10348 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10349 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10350 | #endif |
| 10351 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10352 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10353 | #endif |
| 10354 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10355 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10356 | #endif |
| 10357 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10358 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10359 | #endif |
| 10360 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10361 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10362 | #endif |
| 10363 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10364 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10365 | #endif |
| 10366 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10367 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10368 | #endif |
| 10369 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10370 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10371 | #endif |
| 10372 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10373 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10374 | #endif |
| 10375 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10376 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10377 | #endif |
| 10378 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10379 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10380 | #endif |
| 10381 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10382 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10383 | #endif |
| 10384 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10385 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10386 | #endif |
| 10387 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10388 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10389 | #endif |
| 10390 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10391 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10392 | #endif |
| 10393 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10394 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10395 | #endif |
| 10396 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10397 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10398 | #endif |
| 10399 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10400 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10401 | #endif |
| 10402 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10403 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10404 | #endif |
| 10405 | }; |
| 10406 | |
| 10407 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10408 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10409 | { |
| 10410 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 10411 | sizeof(posix_constants_sysconf) |
| 10412 | / sizeof(struct constdef)); |
| 10413 | } |
| 10414 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10415 | |
| 10416 | /*[clinic input] |
| 10417 | os.sysconf -> long |
| 10418 | name: sysconf_confname |
| 10419 | / |
| 10420 | |
| 10421 | Return an integer-valued system configuration variable. |
| 10422 | [clinic start generated code]*/ |
| 10423 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10424 | static long |
| 10425 | os_sysconf_impl(PyModuleDef *module, int name) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10426 | /*[clinic end generated code: output=ed567306f58d69c4 input=279e3430a33f29e4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10427 | { |
| 10428 | long value; |
| 10429 | |
| 10430 | errno = 0; |
| 10431 | value = sysconf(name); |
| 10432 | if (value == -1 && errno != 0) |
| 10433 | posix_error(); |
| 10434 | return value; |
| 10435 | } |
| 10436 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10437 | |
| 10438 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10439 | /* 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] | 10440 | * 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] | 10441 | * the exported dictionaries that are used to publish information about the |
| 10442 | * names available on the host platform. |
| 10443 | * |
| 10444 | * Sorting the table at runtime ensures that the table is properly ordered |
| 10445 | * when used, even for platforms we're not able to test on. It also makes |
| 10446 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10447 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10448 | |
| 10449 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10450 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10451 | { |
| 10452 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10453 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10454 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10455 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10456 | |
| 10457 | return strcmp(c1->name, c2->name); |
| 10458 | } |
| 10459 | |
| 10460 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10461 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10462 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10463 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10464 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10465 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10466 | |
| 10467 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 10468 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10469 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10470 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10471 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10472 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10473 | PyObject *o = PyLong_FromLong(table[i].value); |
| 10474 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 10475 | Py_XDECREF(o); |
| 10476 | Py_DECREF(d); |
| 10477 | return -1; |
| 10478 | } |
| 10479 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10480 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10481 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10482 | } |
| 10483 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10484 | /* Return -1 on failure, 0 on success. */ |
| 10485 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10486 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10487 | { |
| 10488 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10489 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10490 | sizeof(posix_constants_pathconf) |
| 10491 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10492 | "pathconf_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_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10496 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10497 | sizeof(posix_constants_confstr) |
| 10498 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10499 | "confstr_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 |
| 10502 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10503 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10504 | sizeof(posix_constants_sysconf) |
| 10505 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10506 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10507 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10508 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10509 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10510 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10511 | |
| 10512 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10513 | /*[clinic input] |
| 10514 | os.abort |
| 10515 | |
| 10516 | Abort the interpreter immediately. |
| 10517 | |
| 10518 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 10519 | on the hosting operating system. This function never returns. |
| 10520 | [clinic start generated code]*/ |
| 10521 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10522 | static PyObject * |
| 10523 | os_abort_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10524 | /*[clinic end generated code: output=486bb96647c299b3 input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10525 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10526 | abort(); |
| 10527 | /*NOTREACHED*/ |
| 10528 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 10529 | return NULL; |
| 10530 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10531 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10532 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10533 | /* 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] | 10534 | PyDoc_STRVAR(win32_startfile__doc__, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10535 | "startfile(filepath [, operation])\n\ |
| 10536 | \n\ |
| 10537 | Start a file with its associated application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10538 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 10539 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 10540 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 10541 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 10542 | application (if any) its extension is associated.\n\ |
| 10543 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 10544 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10545 | \n\ |
| 10546 | startfile returns as soon as the associated application is launched.\n\ |
| 10547 | There is no option to wait for the application to close, and no way\n\ |
| 10548 | to retrieve the application's exit status.\n\ |
| 10549 | \n\ |
| 10550 | The filepath is relative to the current directory. If you want to use\n\ |
| 10551 | 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] | 10552 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10553 | |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10554 | /* Grab ShellExecute dynamically from shell32 */ |
| 10555 | static int has_ShellExecute = -1; |
| 10556 | static HINSTANCE (CALLBACK *Py_ShellExecuteA)(HWND, LPCSTR, LPCSTR, LPCSTR, |
| 10557 | LPCSTR, INT); |
| 10558 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 10559 | LPCWSTR, INT); |
| 10560 | static int |
| 10561 | check_ShellExecute() |
| 10562 | { |
| 10563 | HINSTANCE hShell32; |
| 10564 | |
| 10565 | /* only recheck */ |
| 10566 | if (-1 == has_ShellExecute) { |
| 10567 | Py_BEGIN_ALLOW_THREADS |
| 10568 | hShell32 = LoadLibraryW(L"SHELL32"); |
| 10569 | Py_END_ALLOW_THREADS |
| 10570 | if (hShell32) { |
| 10571 | *(FARPROC*)&Py_ShellExecuteA = GetProcAddress(hShell32, |
| 10572 | "ShellExecuteA"); |
| 10573 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 10574 | "ShellExecuteW"); |
| 10575 | has_ShellExecute = Py_ShellExecuteA && |
| 10576 | Py_ShellExecuteW; |
| 10577 | } else { |
| 10578 | has_ShellExecute = 0; |
| 10579 | } |
| 10580 | } |
| 10581 | return has_ShellExecute; |
| 10582 | } |
| 10583 | |
| 10584 | |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10585 | static PyObject * |
| 10586 | win32_startfile(PyObject *self, PyObject *args) |
| 10587 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10588 | PyObject *ofilepath; |
| 10589 | char *filepath; |
| 10590 | char *operation = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10591 | wchar_t *wpath, *woperation; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10592 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 10593 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10594 | PyObject *unipath, *uoperation = NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10595 | |
| 10596 | if(!check_ShellExecute()) { |
| 10597 | /* If the OS doesn't have ShellExecute, return a |
| 10598 | NotImplementedError. */ |
| 10599 | return PyErr_Format(PyExc_NotImplementedError, |
| 10600 | "startfile not available on this platform"); |
| 10601 | } |
| 10602 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10603 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 10604 | &unipath, &operation)) { |
| 10605 | PyErr_Clear(); |
| 10606 | goto normal; |
| 10607 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10608 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10609 | if (operation) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10610 | uoperation = PyUnicode_DecodeASCII(operation, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10611 | strlen(operation), NULL); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10612 | if (!uoperation) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10613 | PyErr_Clear(); |
| 10614 | operation = NULL; |
| 10615 | goto normal; |
| 10616 | } |
| 10617 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10618 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10619 | wpath = PyUnicode_AsUnicode(unipath); |
| 10620 | if (wpath == NULL) |
| 10621 | goto normal; |
| 10622 | if (uoperation) { |
| 10623 | woperation = PyUnicode_AsUnicode(uoperation); |
| 10624 | if (woperation == NULL) |
| 10625 | goto normal; |
| 10626 | } |
| 10627 | else |
| 10628 | woperation = NULL; |
| 10629 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10630 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10631 | rc = Py_ShellExecuteW((HWND)0, woperation, wpath, |
| 10632 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10633 | Py_END_ALLOW_THREADS |
| 10634 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10635 | Py_XDECREF(uoperation); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10636 | if (rc <= (HINSTANCE)32) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10637 | win32_error_object("startfile", unipath); |
| 10638 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10639 | } |
| 10640 | Py_INCREF(Py_None); |
| 10641 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10642 | |
| 10643 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10644 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 10645 | PyUnicode_FSConverter, &ofilepath, |
| 10646 | &operation)) |
| 10647 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 10648 | if (win32_warn_bytes_api()) { |
| 10649 | Py_DECREF(ofilepath); |
| 10650 | return NULL; |
| 10651 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10652 | filepath = PyBytes_AsString(ofilepath); |
| 10653 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10654 | rc = Py_ShellExecuteA((HWND)0, operation, filepath, |
| 10655 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10656 | Py_END_ALLOW_THREADS |
| 10657 | if (rc <= (HINSTANCE)32) { |
| 10658 | PyObject *errval = win32_error("startfile", filepath); |
| 10659 | Py_DECREF(ofilepath); |
| 10660 | return errval; |
| 10661 | } |
| 10662 | Py_DECREF(ofilepath); |
| 10663 | Py_INCREF(Py_None); |
| 10664 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10665 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10666 | #endif /* MS_WINDOWS */ |
| 10667 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10668 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10669 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10670 | /*[clinic input] |
| 10671 | os.getloadavg |
| 10672 | |
| 10673 | Return average recent system load information. |
| 10674 | |
| 10675 | Return the number of processes in the system run queue averaged over |
| 10676 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 10677 | Raises OSError if the load average was unobtainable. |
| 10678 | [clinic start generated code]*/ |
| 10679 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10680 | static PyObject * |
| 10681 | os_getloadavg_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10682 | /*[clinic end generated code: output=2b64c5b675d74c14 input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10683 | { |
| 10684 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10685 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10686 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 10687 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10688 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10689 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10690 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10691 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10692 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10693 | |
| 10694 | /*[clinic input] |
| 10695 | os.device_encoding |
| 10696 | fd: int |
| 10697 | |
| 10698 | Return a string describing the encoding of a terminal's file descriptor. |
| 10699 | |
| 10700 | The file descriptor must be attached to a terminal. |
| 10701 | If the device is not a terminal, return None. |
| 10702 | [clinic start generated code]*/ |
| 10703 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10704 | static PyObject * |
| 10705 | os_device_encoding_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10706 | /*[clinic end generated code: output=34f14e33468419c1 input=9e1d4a42b66df312]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10707 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10708 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 10709 | } |
| 10710 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10711 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10712 | #ifdef HAVE_SETRESUID |
| 10713 | /*[clinic input] |
| 10714 | os.setresuid |
| 10715 | |
| 10716 | ruid: uid_t |
| 10717 | euid: uid_t |
| 10718 | suid: uid_t |
| 10719 | / |
| 10720 | |
| 10721 | Set the current process's real, effective, and saved user ids. |
| 10722 | [clinic start generated code]*/ |
| 10723 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10724 | static PyObject * |
| 10725 | 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] | 10726 | /*[clinic end generated code: output=92cc330812c6ed0f input=9e33cb79a82792f3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10727 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10728 | if (setresuid(ruid, euid, suid) < 0) |
| 10729 | return posix_error(); |
| 10730 | Py_RETURN_NONE; |
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 | #endif /* HAVE_SETRESUID */ |
| 10733 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10734 | |
| 10735 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10736 | /*[clinic input] |
| 10737 | os.setresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10738 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10739 | rgid: gid_t |
| 10740 | egid: gid_t |
| 10741 | sgid: gid_t |
| 10742 | / |
| 10743 | |
| 10744 | Set the current process's real, effective, and saved group ids. |
| 10745 | [clinic start generated code]*/ |
| 10746 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10747 | static PyObject * |
| 10748 | 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] | 10749 | /*[clinic end generated code: output=e91dc4842a604429 input=33e9e0785ef426b1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10750 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10751 | if (setresgid(rgid, egid, sgid) < 0) |
| 10752 | return posix_error(); |
| 10753 | Py_RETURN_NONE; |
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 | #endif /* HAVE_SETRESGID */ |
| 10756 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10757 | |
| 10758 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10759 | /*[clinic input] |
| 10760 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10761 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10762 | Return a tuple of the current process's real, effective, and saved user ids. |
| 10763 | [clinic start generated code]*/ |
| 10764 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10765 | static PyObject * |
| 10766 | os_getresuid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10767 | /*[clinic end generated code: output=9ddef62faae8e477 input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10768 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10769 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10770 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 10771 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10772 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 10773 | _PyLong_FromUid(euid), |
| 10774 | _PyLong_FromUid(suid)); |
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 | #endif /* HAVE_GETRESUID */ |
| 10777 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10778 | |
| 10779 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10780 | /*[clinic input] |
| 10781 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10782 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10783 | Return a tuple of the current process's real, effective, and saved group ids. |
| 10784 | [clinic start generated code]*/ |
| 10785 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10786 | static PyObject * |
| 10787 | os_getresgid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10788 | /*[clinic end generated code: output=e1a553cbcf16234c input=517e68db9ca32df6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10789 | { |
| 10790 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10791 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 10792 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10793 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 10794 | _PyLong_FromGid(egid), |
| 10795 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10796 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10797 | #endif /* HAVE_GETRESGID */ |
| 10798 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10799 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10800 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10801 | /*[clinic input] |
| 10802 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10803 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10804 | path: path_t(allow_fd=True) |
| 10805 | attribute: path_t |
| 10806 | * |
| 10807 | follow_symlinks: bool = True |
| 10808 | |
| 10809 | Return the value of extended attribute attribute on path. |
| 10810 | |
| 10811 | path may be either a string or an open file descriptor. |
| 10812 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10813 | link, getxattr will examine the symbolic link itself instead of the file |
| 10814 | the link points to. |
| 10815 | |
| 10816 | [clinic start generated code]*/ |
| 10817 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10818 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10819 | os_getxattr_impl(PyModuleDef *module, path_t *path, path_t *attribute, |
| 10820 | int follow_symlinks) |
| 10821 | /*[clinic end generated code: output=cf2cede74bd5d412 input=8c8ea3bab78d89c2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10822 | { |
| 10823 | Py_ssize_t i; |
| 10824 | PyObject *buffer = NULL; |
| 10825 | |
| 10826 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 10827 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10828 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10829 | for (i = 0; ; i++) { |
| 10830 | void *ptr; |
| 10831 | ssize_t result; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 10832 | static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10833 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10834 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10835 | path_error(path); |
| 10836 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10837 | } |
| 10838 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 10839 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10840 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10841 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10842 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10843 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10844 | if (path->fd >= 0) |
| 10845 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10846 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10847 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10848 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10849 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10850 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10851 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10852 | if (result < 0) { |
| 10853 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10854 | if (errno == ERANGE) |
| 10855 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10856 | path_error(path); |
| 10857 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 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 | if (result != buffer_size) { |
| 10861 | /* Can only shrink. */ |
| 10862 | _PyBytes_Resize(&buffer, result); |
| 10863 | } |
| 10864 | break; |
| 10865 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10866 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10867 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10868 | } |
| 10869 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10870 | |
| 10871 | /*[clinic input] |
| 10872 | os.setxattr |
| 10873 | |
| 10874 | path: path_t(allow_fd=True) |
| 10875 | attribute: path_t |
| 10876 | value: Py_buffer |
| 10877 | flags: int = 0 |
| 10878 | * |
| 10879 | follow_symlinks: bool = True |
| 10880 | |
| 10881 | Set extended attribute attribute on path to value. |
| 10882 | |
| 10883 | path may be either a string or an open file descriptor. |
| 10884 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10885 | link, setxattr will modify the symbolic link itself instead of the file |
| 10886 | the link points to. |
| 10887 | |
| 10888 | [clinic start generated code]*/ |
| 10889 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10890 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10891 | os_setxattr_impl(PyModuleDef *module, path_t *path, path_t *attribute, |
| 10892 | Py_buffer *value, int flags, int follow_symlinks) |
| 10893 | /*[clinic end generated code: output=1b395ef82880fea0 input=f0d26833992015c2]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10894 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10895 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10896 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10897 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10898 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10899 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10900 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10901 | if (path->fd > -1) |
| 10902 | result = fsetxattr(path->fd, attribute->narrow, |
| 10903 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10904 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10905 | result = setxattr(path->narrow, attribute->narrow, |
| 10906 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10907 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10908 | result = lsetxattr(path->narrow, attribute->narrow, |
| 10909 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10910 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10911 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10912 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10913 | path_error(path); |
| 10914 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10915 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10916 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10917 | Py_RETURN_NONE; |
| 10918 | } |
| 10919 | |
| 10920 | |
| 10921 | /*[clinic input] |
| 10922 | os.removexattr |
| 10923 | |
| 10924 | path: path_t(allow_fd=True) |
| 10925 | attribute: path_t |
| 10926 | * |
| 10927 | follow_symlinks: bool = True |
| 10928 | |
| 10929 | Remove extended attribute attribute on path. |
| 10930 | |
| 10931 | path may be either a string or an open file descriptor. |
| 10932 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10933 | link, removexattr will modify the symbolic link itself instead of the file |
| 10934 | the link points to. |
| 10935 | |
| 10936 | [clinic start generated code]*/ |
| 10937 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10938 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10939 | os_removexattr_impl(PyModuleDef *module, path_t *path, path_t *attribute, |
| 10940 | int follow_symlinks) |
| 10941 | /*[clinic end generated code: output=f92bb39ab992650d input=cdb54834161e3329]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10942 | { |
| 10943 | ssize_t result; |
| 10944 | |
| 10945 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 10946 | return NULL; |
| 10947 | |
| 10948 | Py_BEGIN_ALLOW_THREADS; |
| 10949 | if (path->fd > -1) |
| 10950 | result = fremovexattr(path->fd, attribute->narrow); |
| 10951 | else if (follow_symlinks) |
| 10952 | result = removexattr(path->narrow, attribute->narrow); |
| 10953 | else |
| 10954 | result = lremovexattr(path->narrow, attribute->narrow); |
| 10955 | Py_END_ALLOW_THREADS; |
| 10956 | |
| 10957 | if (result) { |
| 10958 | return path_error(path); |
| 10959 | } |
| 10960 | |
| 10961 | Py_RETURN_NONE; |
| 10962 | } |
| 10963 | |
| 10964 | |
| 10965 | /*[clinic input] |
| 10966 | os.listxattr |
| 10967 | |
| 10968 | path: path_t(allow_fd=True, nullable=True) = None |
| 10969 | * |
| 10970 | follow_symlinks: bool = True |
| 10971 | |
| 10972 | Return a list of extended attributes on path. |
| 10973 | |
| 10974 | path may be either None, a string, or an open file descriptor. |
| 10975 | if path is None, listxattr will examine the current directory. |
| 10976 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10977 | link, listxattr will examine the symbolic link itself instead of the file |
| 10978 | the link points to. |
| 10979 | [clinic start generated code]*/ |
| 10980 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10981 | static PyObject * |
| 10982 | os_listxattr_impl(PyModuleDef *module, path_t *path, int follow_symlinks) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10983 | /*[clinic end generated code: output=a87ad6ce56e42a4f input=08cca53ac0b07c13]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10984 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10985 | Py_ssize_t i; |
| 10986 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10987 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10988 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10989 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10990 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10991 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10992 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10993 | name = path->narrow ? path->narrow : "."; |
| 10994 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10995 | for (i = 0; ; i++) { |
| 10996 | char *start, *trace, *end; |
| 10997 | ssize_t length; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 10998 | static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10999 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 11000 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 11001 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11002 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11003 | break; |
| 11004 | } |
| 11005 | buffer = PyMem_MALLOC(buffer_size); |
| 11006 | if (!buffer) { |
| 11007 | PyErr_NoMemory(); |
| 11008 | break; |
| 11009 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11010 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11011 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11012 | if (path->fd > -1) |
| 11013 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11014 | else if (follow_symlinks) |
| 11015 | length = listxattr(name, buffer, buffer_size); |
| 11016 | else |
| 11017 | length = llistxattr(name, buffer, buffer_size); |
| 11018 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11019 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11020 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 11021 | if (errno == ERANGE) { |
| 11022 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 11023 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11024 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 11025 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11026 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11027 | break; |
| 11028 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11029 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11030 | result = PyList_New(0); |
| 11031 | if (!result) { |
| 11032 | goto exit; |
| 11033 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11034 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11035 | end = buffer + length; |
| 11036 | for (trace = start = buffer; trace != end; trace++) { |
| 11037 | if (!*trace) { |
| 11038 | int error; |
| 11039 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 11040 | trace - start); |
| 11041 | if (!attribute) { |
| 11042 | Py_DECREF(result); |
| 11043 | result = NULL; |
| 11044 | goto exit; |
| 11045 | } |
| 11046 | error = PyList_Append(result, attribute); |
| 11047 | Py_DECREF(attribute); |
| 11048 | if (error) { |
| 11049 | Py_DECREF(result); |
| 11050 | result = NULL; |
| 11051 | goto exit; |
| 11052 | } |
| 11053 | start = trace + 1; |
| 11054 | } |
| 11055 | } |
| 11056 | break; |
| 11057 | } |
| 11058 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11059 | if (buffer) |
| 11060 | PyMem_FREE(buffer); |
| 11061 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11062 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11063 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11064 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11065 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11066 | /*[clinic input] |
| 11067 | os.urandom |
| 11068 | |
| 11069 | size: Py_ssize_t |
| 11070 | / |
| 11071 | |
| 11072 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 11073 | [clinic start generated code]*/ |
| 11074 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11075 | static PyObject * |
| 11076 | os_urandom_impl(PyModuleDef *module, Py_ssize_t size) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 11077 | /*[clinic end generated code: output=e0011f021501f03b input=4067cdb1b6776c29]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11078 | { |
| 11079 | PyObject *bytes; |
| 11080 | int result; |
| 11081 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11082 | if (size < 0) |
| 11083 | return PyErr_Format(PyExc_ValueError, |
| 11084 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11085 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 11086 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11087 | return NULL; |
| 11088 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11089 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), |
| 11090 | PyBytes_GET_SIZE(bytes)); |
| 11091 | if (result == -1) { |
| 11092 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11093 | return NULL; |
| 11094 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11095 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11096 | } |
| 11097 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11098 | /* Terminal size querying */ |
| 11099 | |
| 11100 | static PyTypeObject TerminalSizeType; |
| 11101 | |
| 11102 | PyDoc_STRVAR(TerminalSize_docstring, |
| 11103 | "A tuple of (columns, lines) for holding terminal window size"); |
| 11104 | |
| 11105 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 11106 | {"columns", "width of the terminal window in characters"}, |
| 11107 | {"lines", "height of the terminal window in characters"}, |
| 11108 | {NULL, NULL} |
| 11109 | }; |
| 11110 | |
| 11111 | static PyStructSequence_Desc TerminalSize_desc = { |
| 11112 | "os.terminal_size", |
| 11113 | TerminalSize_docstring, |
| 11114 | TerminalSize_fields, |
| 11115 | 2, |
| 11116 | }; |
| 11117 | |
| 11118 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11119 | /* AC 3.5: fd should accept None */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11120 | PyDoc_STRVAR(termsize__doc__, |
| 11121 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 11122 | "\n" \ |
| 11123 | "The optional argument fd (default standard output) specifies\n" \ |
| 11124 | "which file descriptor should be queried.\n" \ |
| 11125 | "\n" \ |
| 11126 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 11127 | "is thrown.\n" \ |
| 11128 | "\n" \ |
| 11129 | "This function will only be defined if an implementation is\n" \ |
| 11130 | "available for this system.\n" \ |
| 11131 | "\n" \ |
| 11132 | "shutil.get_terminal_size is the high-level function which should \n" \ |
| 11133 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 11134 | |
| 11135 | static PyObject* |
| 11136 | get_terminal_size(PyObject *self, PyObject *args) |
| 11137 | { |
| 11138 | int columns, lines; |
| 11139 | PyObject *termsize; |
| 11140 | |
| 11141 | int fd = fileno(stdout); |
| 11142 | /* Under some conditions stdout may not be connected and |
| 11143 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 11144 | * GUI apps don't have valid standard streams by default. |
| 11145 | * |
| 11146 | * If this happens, and the optional fd argument is not present, |
| 11147 | * the ioctl below will fail returning EBADF. This is what we want. |
| 11148 | */ |
| 11149 | |
| 11150 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 11151 | return NULL; |
| 11152 | |
| 11153 | #ifdef TERMSIZE_USE_IOCTL |
| 11154 | { |
| 11155 | struct winsize w; |
| 11156 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 11157 | return PyErr_SetFromErrno(PyExc_OSError); |
| 11158 | columns = w.ws_col; |
| 11159 | lines = w.ws_row; |
| 11160 | } |
| 11161 | #endif /* TERMSIZE_USE_IOCTL */ |
| 11162 | |
| 11163 | #ifdef TERMSIZE_USE_CONIO |
| 11164 | { |
| 11165 | DWORD nhandle; |
| 11166 | HANDLE handle; |
| 11167 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 11168 | switch (fd) { |
| 11169 | case 0: nhandle = STD_INPUT_HANDLE; |
| 11170 | break; |
| 11171 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 11172 | break; |
| 11173 | case 2: nhandle = STD_ERROR_HANDLE; |
| 11174 | break; |
| 11175 | default: |
| 11176 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 11177 | } |
| 11178 | handle = GetStdHandle(nhandle); |
| 11179 | if (handle == NULL) |
| 11180 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 11181 | if (handle == INVALID_HANDLE_VALUE) |
| 11182 | return PyErr_SetFromWindowsErr(0); |
| 11183 | |
| 11184 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 11185 | return PyErr_SetFromWindowsErr(0); |
| 11186 | |
| 11187 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 11188 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 11189 | } |
| 11190 | #endif /* TERMSIZE_USE_CONIO */ |
| 11191 | |
| 11192 | termsize = PyStructSequence_New(&TerminalSizeType); |
| 11193 | if (termsize == NULL) |
| 11194 | return NULL; |
| 11195 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 11196 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 11197 | if (PyErr_Occurred()) { |
| 11198 | Py_DECREF(termsize); |
| 11199 | return NULL; |
| 11200 | } |
| 11201 | return termsize; |
| 11202 | } |
| 11203 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 11204 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11205 | |
| 11206 | /*[clinic input] |
| 11207 | os.cpu_count |
| 11208 | |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 11209 | Return the number of CPUs in the system; return None if indeterminable. |
| 11210 | |
| 11211 | This number is not equivalent to the number of CPUs the current process can |
| 11212 | use. The number of usable CPUs can be obtained with |
| 11213 | ``len(os.sched_getaffinity(0))`` |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11214 | [clinic start generated code]*/ |
| 11215 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11216 | static PyObject * |
| 11217 | os_cpu_count_impl(PyModuleDef *module) |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 11218 | /*[clinic end generated code: output=c59ee7f6bce832b8 input=e7c8f4ba6dbbadd3]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11219 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11220 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11221 | #ifdef MS_WINDOWS |
| 11222 | SYSTEM_INFO sysinfo; |
| 11223 | GetSystemInfo(&sysinfo); |
| 11224 | ncpu = sysinfo.dwNumberOfProcessors; |
| 11225 | #elif defined(__hpux) |
| 11226 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 11227 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 11228 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11229 | #elif defined(__DragonFly__) || \ |
| 11230 | defined(__OpenBSD__) || \ |
| 11231 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11232 | defined(__NetBSD__) || \ |
| 11233 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 11234 | int mib[2]; |
| 11235 | size_t len = sizeof(ncpu); |
| 11236 | mib[0] = CTL_HW; |
| 11237 | mib[1] = HW_NCPU; |
| 11238 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 11239 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11240 | #endif |
| 11241 | if (ncpu >= 1) |
| 11242 | return PyLong_FromLong(ncpu); |
| 11243 | else |
| 11244 | Py_RETURN_NONE; |
| 11245 | } |
| 11246 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11247 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11248 | /*[clinic input] |
| 11249 | os.get_inheritable -> bool |
| 11250 | |
| 11251 | fd: int |
| 11252 | / |
| 11253 | |
| 11254 | Get the close-on-exe flag of the specified file descriptor. |
| 11255 | [clinic start generated code]*/ |
| 11256 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11257 | static int |
| 11258 | os_get_inheritable_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 11259 | /*[clinic end generated code: output=36110bb36efaa21e input=89ac008dc9ab6b95]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11260 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11261 | int return_value; |
| 11262 | if (!_PyVerify_fd(fd)) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11263 | posix_error(); |
| 11264 | return -1; |
| 11265 | } |
| 11266 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11267 | _Py_BEGIN_SUPPRESS_IPH |
| 11268 | return_value = _Py_get_inheritable(fd); |
| 11269 | _Py_END_SUPPRESS_IPH |
| 11270 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11271 | } |
| 11272 | |
| 11273 | |
| 11274 | /*[clinic input] |
| 11275 | os.set_inheritable |
| 11276 | fd: int |
| 11277 | inheritable: int |
| 11278 | / |
| 11279 | |
| 11280 | Set the inheritable flag of the specified file descriptor. |
| 11281 | [clinic start generated code]*/ |
| 11282 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11283 | static PyObject * |
| 11284 | os_set_inheritable_impl(PyModuleDef *module, int fd, int inheritable) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 11285 | /*[clinic end generated code: output=2ac5c6ce8623f045 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11286 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11287 | int result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11288 | if (!_PyVerify_fd(fd)) |
| 11289 | return posix_error(); |
| 11290 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11291 | _Py_BEGIN_SUPPRESS_IPH |
| 11292 | result = _Py_set_inheritable(fd, inheritable, NULL); |
| 11293 | _Py_END_SUPPRESS_IPH |
| 11294 | if (result < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11295 | return NULL; |
| 11296 | Py_RETURN_NONE; |
| 11297 | } |
| 11298 | |
| 11299 | |
| 11300 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11301 | /*[clinic input] |
| 11302 | os.get_handle_inheritable -> bool |
| 11303 | handle: Py_intptr_t |
| 11304 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11305 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11306 | Get the close-on-exe flag of the specified file descriptor. |
| 11307 | [clinic start generated code]*/ |
| 11308 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11309 | static int |
| 11310 | os_get_handle_inheritable_impl(PyModuleDef *module, Py_intptr_t handle) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 11311 | /*[clinic end generated code: output=3b7b3e1b43f312b6 input=5f7759443aae3dc5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11312 | { |
| 11313 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11314 | |
| 11315 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 11316 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11317 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11318 | } |
| 11319 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11320 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11321 | } |
| 11322 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11323 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11324 | /*[clinic input] |
| 11325 | os.set_handle_inheritable |
| 11326 | handle: Py_intptr_t |
| 11327 | inheritable: bool |
| 11328 | / |
| 11329 | |
| 11330 | Set the inheritable flag of the specified handle. |
| 11331 | [clinic start generated code]*/ |
| 11332 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11333 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11334 | os_set_handle_inheritable_impl(PyModuleDef *module, Py_intptr_t handle, |
| 11335 | int inheritable) |
| 11336 | /*[clinic end generated code: output=d2e111a96c9eb296 input=e64b2b2730469def]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11337 | { |
| 11338 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11339 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 11340 | PyErr_SetFromWindowsErr(0); |
| 11341 | return NULL; |
| 11342 | } |
| 11343 | Py_RETURN_NONE; |
| 11344 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11345 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11346 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11347 | #ifndef MS_WINDOWS |
| 11348 | PyDoc_STRVAR(get_blocking__doc__, |
| 11349 | "get_blocking(fd) -> bool\n" \ |
| 11350 | "\n" \ |
| 11351 | "Get the blocking mode of the file descriptor:\n" \ |
| 11352 | "False if the O_NONBLOCK flag is set, True if the flag is cleared."); |
| 11353 | |
| 11354 | static PyObject* |
| 11355 | posix_get_blocking(PyObject *self, PyObject *args) |
| 11356 | { |
| 11357 | int fd; |
| 11358 | int blocking; |
| 11359 | |
| 11360 | if (!PyArg_ParseTuple(args, "i:get_blocking", &fd)) |
| 11361 | return NULL; |
| 11362 | |
| 11363 | if (!_PyVerify_fd(fd)) |
| 11364 | return posix_error(); |
| 11365 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11366 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11367 | blocking = _Py_get_blocking(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11368 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11369 | if (blocking < 0) |
| 11370 | return NULL; |
| 11371 | return PyBool_FromLong(blocking); |
| 11372 | } |
| 11373 | |
| 11374 | PyDoc_STRVAR(set_blocking__doc__, |
| 11375 | "set_blocking(fd, blocking)\n" \ |
| 11376 | "\n" \ |
| 11377 | "Set the blocking mode of the specified file descriptor.\n" \ |
| 11378 | "Set the O_NONBLOCK flag if blocking is False,\n" \ |
| 11379 | "clear the O_NONBLOCK flag otherwise."); |
| 11380 | |
| 11381 | static PyObject* |
| 11382 | posix_set_blocking(PyObject *self, PyObject *args) |
| 11383 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11384 | int fd, blocking, result; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11385 | |
| 11386 | if (!PyArg_ParseTuple(args, "ii:set_blocking", &fd, &blocking)) |
| 11387 | return NULL; |
| 11388 | |
| 11389 | if (!_PyVerify_fd(fd)) |
| 11390 | return posix_error(); |
| 11391 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11392 | _Py_BEGIN_SUPPRESS_IPH |
| 11393 | result = _Py_set_blocking(fd, blocking); |
| 11394 | _Py_END_SUPPRESS_IPH |
| 11395 | if (result < 0) |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11396 | return NULL; |
| 11397 | Py_RETURN_NONE; |
| 11398 | } |
| 11399 | #endif /* !MS_WINDOWS */ |
| 11400 | |
| 11401 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11402 | PyDoc_STRVAR(posix_scandir__doc__, |
| 11403 | "scandir(path='.') -> iterator of DirEntry objects for given path"); |
| 11404 | |
| 11405 | static char *follow_symlinks_keywords[] = {"follow_symlinks", NULL}; |
| 11406 | |
| 11407 | typedef struct { |
| 11408 | PyObject_HEAD |
| 11409 | PyObject *name; |
| 11410 | PyObject *path; |
| 11411 | PyObject *stat; |
| 11412 | PyObject *lstat; |
| 11413 | #ifdef MS_WINDOWS |
| 11414 | struct _Py_stat_struct win32_lstat; |
| 11415 | __int64 win32_file_index; |
| 11416 | int got_file_index; |
| 11417 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11418 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11419 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11420 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11421 | ino_t d_ino; |
| 11422 | #endif |
| 11423 | } DirEntry; |
| 11424 | |
| 11425 | static void |
| 11426 | DirEntry_dealloc(DirEntry *entry) |
| 11427 | { |
| 11428 | Py_XDECREF(entry->name); |
| 11429 | Py_XDECREF(entry->path); |
| 11430 | Py_XDECREF(entry->stat); |
| 11431 | Py_XDECREF(entry->lstat); |
| 11432 | Py_TYPE(entry)->tp_free((PyObject *)entry); |
| 11433 | } |
| 11434 | |
| 11435 | /* Forward reference */ |
| 11436 | static int |
| 11437 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits); |
| 11438 | |
| 11439 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 11440 | static int |
| 11441 | DirEntry_is_symlink(DirEntry *self) |
| 11442 | { |
| 11443 | #ifdef MS_WINDOWS |
| 11444 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11445 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 11446 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11447 | if (self->d_type != DT_UNKNOWN) |
| 11448 | return self->d_type == DT_LNK; |
| 11449 | else |
| 11450 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11451 | #else |
| 11452 | /* POSIX without d_type */ |
| 11453 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11454 | #endif |
| 11455 | } |
| 11456 | |
| 11457 | static PyObject * |
| 11458 | DirEntry_py_is_symlink(DirEntry *self) |
| 11459 | { |
| 11460 | int result; |
| 11461 | |
| 11462 | result = DirEntry_is_symlink(self); |
| 11463 | if (result == -1) |
| 11464 | return NULL; |
| 11465 | return PyBool_FromLong(result); |
| 11466 | } |
| 11467 | |
| 11468 | static PyObject * |
| 11469 | DirEntry_fetch_stat(DirEntry *self, int follow_symlinks) |
| 11470 | { |
| 11471 | int result; |
| 11472 | struct _Py_stat_struct st; |
| 11473 | |
| 11474 | #ifdef MS_WINDOWS |
| 11475 | wchar_t *path; |
| 11476 | |
| 11477 | path = PyUnicode_AsUnicode(self->path); |
| 11478 | if (!path) |
| 11479 | return NULL; |
| 11480 | |
| 11481 | if (follow_symlinks) |
| 11482 | result = win32_stat_w(path, &st); |
| 11483 | else |
| 11484 | result = win32_lstat_w(path, &st); |
| 11485 | |
| 11486 | if (result != 0) { |
| 11487 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 11488 | 0, self->path); |
| 11489 | } |
| 11490 | #else /* POSIX */ |
| 11491 | PyObject *bytes; |
| 11492 | char *path; |
| 11493 | |
| 11494 | if (!PyUnicode_FSConverter(self->path, &bytes)) |
| 11495 | return NULL; |
| 11496 | path = PyBytes_AS_STRING(bytes); |
| 11497 | |
| 11498 | if (follow_symlinks) |
| 11499 | result = STAT(path, &st); |
| 11500 | else |
| 11501 | result = LSTAT(path, &st); |
| 11502 | Py_DECREF(bytes); |
| 11503 | |
| 11504 | if (result != 0) |
| 11505 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, self->path); |
| 11506 | #endif |
| 11507 | |
| 11508 | return _pystat_fromstructstat(&st); |
| 11509 | } |
| 11510 | |
| 11511 | static PyObject * |
| 11512 | DirEntry_get_lstat(DirEntry *self) |
| 11513 | { |
| 11514 | if (!self->lstat) { |
| 11515 | #ifdef MS_WINDOWS |
| 11516 | self->lstat = _pystat_fromstructstat(&self->win32_lstat); |
| 11517 | #else /* POSIX */ |
| 11518 | self->lstat = DirEntry_fetch_stat(self, 0); |
| 11519 | #endif |
| 11520 | } |
| 11521 | Py_XINCREF(self->lstat); |
| 11522 | return self->lstat; |
| 11523 | } |
| 11524 | |
| 11525 | static PyObject * |
| 11526 | DirEntry_get_stat(DirEntry *self, int follow_symlinks) |
| 11527 | { |
| 11528 | if (!follow_symlinks) |
| 11529 | return DirEntry_get_lstat(self); |
| 11530 | |
| 11531 | if (!self->stat) { |
| 11532 | int result = DirEntry_is_symlink(self); |
| 11533 | if (result == -1) |
| 11534 | return NULL; |
| 11535 | else if (result) |
| 11536 | self->stat = DirEntry_fetch_stat(self, 1); |
| 11537 | else |
| 11538 | self->stat = DirEntry_get_lstat(self); |
| 11539 | } |
| 11540 | |
| 11541 | Py_XINCREF(self->stat); |
| 11542 | return self->stat; |
| 11543 | } |
| 11544 | |
| 11545 | static PyObject * |
| 11546 | DirEntry_stat(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 11547 | { |
| 11548 | int follow_symlinks = 1; |
| 11549 | |
| 11550 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.stat", |
| 11551 | follow_symlinks_keywords, &follow_symlinks)) |
| 11552 | return NULL; |
| 11553 | |
| 11554 | return DirEntry_get_stat(self, follow_symlinks); |
| 11555 | } |
| 11556 | |
| 11557 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 11558 | static int |
| 11559 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 11560 | { |
| 11561 | PyObject *stat = NULL; |
| 11562 | PyObject *st_mode = NULL; |
| 11563 | long mode; |
| 11564 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11565 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11566 | int is_symlink; |
| 11567 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11568 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11569 | #ifdef MS_WINDOWS |
| 11570 | unsigned long dir_bits; |
| 11571 | #endif |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11572 | _Py_IDENTIFIER(st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11573 | |
| 11574 | #ifdef MS_WINDOWS |
| 11575 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 11576 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11577 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11578 | is_symlink = self->d_type == DT_LNK; |
| 11579 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 11580 | #endif |
| 11581 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11582 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11583 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11584 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11585 | stat = DirEntry_get_stat(self, follow_symlinks); |
| 11586 | if (!stat) { |
| 11587 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 11588 | /* If file doesn't exist (anymore), then return False |
| 11589 | (i.e., say it's not a file/directory) */ |
| 11590 | PyErr_Clear(); |
| 11591 | return 0; |
| 11592 | } |
| 11593 | goto error; |
| 11594 | } |
| 11595 | st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode); |
| 11596 | if (!st_mode) |
| 11597 | goto error; |
| 11598 | |
| 11599 | mode = PyLong_AsLong(st_mode); |
| 11600 | if (mode == -1 && PyErr_Occurred()) |
| 11601 | goto error; |
| 11602 | Py_CLEAR(st_mode); |
| 11603 | Py_CLEAR(stat); |
| 11604 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11605 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11606 | } |
| 11607 | else if (is_symlink) { |
| 11608 | assert(mode_bits != S_IFLNK); |
| 11609 | result = 0; |
| 11610 | } |
| 11611 | else { |
| 11612 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 11613 | #ifdef MS_WINDOWS |
| 11614 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 11615 | if (mode_bits == S_IFDIR) |
| 11616 | result = dir_bits != 0; |
| 11617 | else |
| 11618 | result = dir_bits == 0; |
| 11619 | #else /* POSIX */ |
| 11620 | if (mode_bits == S_IFDIR) |
| 11621 | result = self->d_type == DT_DIR; |
| 11622 | else |
| 11623 | result = self->d_type == DT_REG; |
| 11624 | #endif |
| 11625 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11626 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11627 | |
| 11628 | return result; |
| 11629 | |
| 11630 | error: |
| 11631 | Py_XDECREF(st_mode); |
| 11632 | Py_XDECREF(stat); |
| 11633 | return -1; |
| 11634 | } |
| 11635 | |
| 11636 | static PyObject * |
| 11637 | DirEntry_py_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 11638 | { |
| 11639 | int result; |
| 11640 | |
| 11641 | result = DirEntry_test_mode(self, follow_symlinks, mode_bits); |
| 11642 | if (result == -1) |
| 11643 | return NULL; |
| 11644 | return PyBool_FromLong(result); |
| 11645 | } |
| 11646 | |
| 11647 | static PyObject * |
| 11648 | DirEntry_is_dir(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 11649 | { |
| 11650 | int follow_symlinks = 1; |
| 11651 | |
| 11652 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_dir", |
| 11653 | follow_symlinks_keywords, &follow_symlinks)) |
| 11654 | return NULL; |
| 11655 | |
| 11656 | return DirEntry_py_test_mode(self, follow_symlinks, S_IFDIR); |
| 11657 | } |
| 11658 | |
| 11659 | static PyObject * |
| 11660 | DirEntry_is_file(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 11661 | { |
| 11662 | int follow_symlinks = 1; |
| 11663 | |
| 11664 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_file", |
| 11665 | follow_symlinks_keywords, &follow_symlinks)) |
| 11666 | return NULL; |
| 11667 | |
| 11668 | return DirEntry_py_test_mode(self, follow_symlinks, S_IFREG); |
| 11669 | } |
| 11670 | |
| 11671 | static PyObject * |
| 11672 | DirEntry_inode(DirEntry *self) |
| 11673 | { |
| 11674 | #ifdef MS_WINDOWS |
| 11675 | if (!self->got_file_index) { |
| 11676 | wchar_t *path; |
| 11677 | struct _Py_stat_struct stat; |
| 11678 | |
| 11679 | path = PyUnicode_AsUnicode(self->path); |
| 11680 | if (!path) |
| 11681 | return NULL; |
| 11682 | |
| 11683 | if (win32_lstat_w(path, &stat) != 0) { |
| 11684 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 11685 | 0, self->path); |
| 11686 | } |
| 11687 | |
| 11688 | self->win32_file_index = stat.st_ino; |
| 11689 | self->got_file_index = 1; |
| 11690 | } |
| 11691 | return PyLong_FromLongLong((PY_LONG_LONG)self->win32_file_index); |
| 11692 | #else /* POSIX */ |
| 11693 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 11694 | return PyLong_FromLongLong((PY_LONG_LONG)self->d_ino); |
| 11695 | #else |
| 11696 | return PyLong_FromLong((long)self->d_ino); |
| 11697 | #endif |
| 11698 | #endif |
| 11699 | } |
| 11700 | |
| 11701 | static PyObject * |
| 11702 | DirEntry_repr(DirEntry *self) |
| 11703 | { |
| 11704 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 11705 | } |
| 11706 | |
| 11707 | static PyMemberDef DirEntry_members[] = { |
| 11708 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 11709 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 11710 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 11711 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 11712 | {NULL} |
| 11713 | }; |
| 11714 | |
| 11715 | static PyMethodDef DirEntry_methods[] = { |
| 11716 | {"is_dir", (PyCFunction)DirEntry_is_dir, METH_VARARGS | METH_KEYWORDS, |
| 11717 | "return True if the entry is a directory; cached per entry" |
| 11718 | }, |
| 11719 | {"is_file", (PyCFunction)DirEntry_is_file, METH_VARARGS | METH_KEYWORDS, |
| 11720 | "return True if the entry is a file; cached per entry" |
| 11721 | }, |
| 11722 | {"is_symlink", (PyCFunction)DirEntry_py_is_symlink, METH_NOARGS, |
| 11723 | "return True if the entry is a symbolic link; cached per entry" |
| 11724 | }, |
| 11725 | {"stat", (PyCFunction)DirEntry_stat, METH_VARARGS | METH_KEYWORDS, |
| 11726 | "return stat_result object for the entry; cached per entry" |
| 11727 | }, |
| 11728 | {"inode", (PyCFunction)DirEntry_inode, METH_NOARGS, |
| 11729 | "return inode of the entry; cached per entry", |
| 11730 | }, |
| 11731 | {NULL} |
| 11732 | }; |
| 11733 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 11734 | static PyTypeObject DirEntryType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11735 | PyVarObject_HEAD_INIT(NULL, 0) |
| 11736 | MODNAME ".DirEntry", /* tp_name */ |
| 11737 | sizeof(DirEntry), /* tp_basicsize */ |
| 11738 | 0, /* tp_itemsize */ |
| 11739 | /* methods */ |
| 11740 | (destructor)DirEntry_dealloc, /* tp_dealloc */ |
| 11741 | 0, /* tp_print */ |
| 11742 | 0, /* tp_getattr */ |
| 11743 | 0, /* tp_setattr */ |
| 11744 | 0, /* tp_compare */ |
| 11745 | (reprfunc)DirEntry_repr, /* tp_repr */ |
| 11746 | 0, /* tp_as_number */ |
| 11747 | 0, /* tp_as_sequence */ |
| 11748 | 0, /* tp_as_mapping */ |
| 11749 | 0, /* tp_hash */ |
| 11750 | 0, /* tp_call */ |
| 11751 | 0, /* tp_str */ |
| 11752 | 0, /* tp_getattro */ |
| 11753 | 0, /* tp_setattro */ |
| 11754 | 0, /* tp_as_buffer */ |
| 11755 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 11756 | 0, /* tp_doc */ |
| 11757 | 0, /* tp_traverse */ |
| 11758 | 0, /* tp_clear */ |
| 11759 | 0, /* tp_richcompare */ |
| 11760 | 0, /* tp_weaklistoffset */ |
| 11761 | 0, /* tp_iter */ |
| 11762 | 0, /* tp_iternext */ |
| 11763 | DirEntry_methods, /* tp_methods */ |
| 11764 | DirEntry_members, /* tp_members */ |
| 11765 | }; |
| 11766 | |
| 11767 | #ifdef MS_WINDOWS |
| 11768 | |
| 11769 | static wchar_t * |
| 11770 | join_path_filenameW(wchar_t *path_wide, wchar_t* filename) |
| 11771 | { |
| 11772 | Py_ssize_t path_len; |
| 11773 | Py_ssize_t size; |
| 11774 | wchar_t *result; |
| 11775 | wchar_t ch; |
| 11776 | |
| 11777 | if (!path_wide) { /* Default arg: "." */ |
| 11778 | path_wide = L"."; |
| 11779 | path_len = 1; |
| 11780 | } |
| 11781 | else { |
| 11782 | path_len = wcslen(path_wide); |
| 11783 | } |
| 11784 | |
| 11785 | /* The +1's are for the path separator and the NUL */ |
| 11786 | size = path_len + 1 + wcslen(filename) + 1; |
| 11787 | result = PyMem_New(wchar_t, size); |
| 11788 | if (!result) { |
| 11789 | PyErr_NoMemory(); |
| 11790 | return NULL; |
| 11791 | } |
| 11792 | wcscpy(result, path_wide); |
| 11793 | if (path_len > 0) { |
| 11794 | ch = result[path_len - 1]; |
| 11795 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 11796 | result[path_len++] = SEP; |
| 11797 | wcscpy(result + path_len, filename); |
| 11798 | } |
| 11799 | return result; |
| 11800 | } |
| 11801 | |
| 11802 | static PyObject * |
| 11803 | DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW) |
| 11804 | { |
| 11805 | DirEntry *entry; |
| 11806 | BY_HANDLE_FILE_INFORMATION file_info; |
| 11807 | ULONG reparse_tag; |
| 11808 | wchar_t *joined_path; |
| 11809 | |
| 11810 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 11811 | if (!entry) |
| 11812 | return NULL; |
| 11813 | entry->name = NULL; |
| 11814 | entry->path = NULL; |
| 11815 | entry->stat = NULL; |
| 11816 | entry->lstat = NULL; |
| 11817 | entry->got_file_index = 0; |
| 11818 | |
| 11819 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 11820 | if (!entry->name) |
| 11821 | goto error; |
| 11822 | |
| 11823 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 11824 | if (!joined_path) |
| 11825 | goto error; |
| 11826 | |
| 11827 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 11828 | PyMem_Free(joined_path); |
| 11829 | if (!entry->path) |
| 11830 | goto error; |
| 11831 | |
| 11832 | find_data_to_file_info_w(dataW, &file_info, &reparse_tag); |
| 11833 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 11834 | |
| 11835 | return (PyObject *)entry; |
| 11836 | |
| 11837 | error: |
| 11838 | Py_DECREF(entry); |
| 11839 | return NULL; |
| 11840 | } |
| 11841 | |
| 11842 | #else /* POSIX */ |
| 11843 | |
| 11844 | static char * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 11845 | join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11846 | { |
| 11847 | Py_ssize_t path_len; |
| 11848 | Py_ssize_t size; |
| 11849 | char *result; |
| 11850 | |
| 11851 | if (!path_narrow) { /* Default arg: "." */ |
| 11852 | path_narrow = "."; |
| 11853 | path_len = 1; |
| 11854 | } |
| 11855 | else { |
| 11856 | path_len = strlen(path_narrow); |
| 11857 | } |
| 11858 | |
| 11859 | if (filename_len == -1) |
| 11860 | filename_len = strlen(filename); |
| 11861 | |
| 11862 | /* The +1's are for the path separator and the NUL */ |
| 11863 | size = path_len + 1 + filename_len + 1; |
| 11864 | result = PyMem_New(char, size); |
| 11865 | if (!result) { |
| 11866 | PyErr_NoMemory(); |
| 11867 | return NULL; |
| 11868 | } |
| 11869 | strcpy(result, path_narrow); |
| 11870 | if (path_len > 0 && result[path_len - 1] != '/') |
| 11871 | result[path_len++] = '/'; |
| 11872 | strcpy(result + path_len, filename); |
| 11873 | return result; |
| 11874 | } |
| 11875 | |
| 11876 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 11877 | DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11878 | ino_t d_ino |
| 11879 | #ifdef HAVE_DIRENT_D_TYPE |
| 11880 | , unsigned char d_type |
| 11881 | #endif |
| 11882 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11883 | { |
| 11884 | DirEntry *entry; |
| 11885 | char *joined_path; |
| 11886 | |
| 11887 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 11888 | if (!entry) |
| 11889 | return NULL; |
| 11890 | entry->name = NULL; |
| 11891 | entry->path = NULL; |
| 11892 | entry->stat = NULL; |
| 11893 | entry->lstat = NULL; |
| 11894 | |
| 11895 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 11896 | if (!joined_path) |
| 11897 | goto error; |
| 11898 | |
| 11899 | if (!path->narrow || !PyBytes_Check(path->object)) { |
| 11900 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
| 11901 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
| 11902 | } |
| 11903 | else { |
| 11904 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
| 11905 | entry->path = PyBytes_FromString(joined_path); |
| 11906 | } |
| 11907 | PyMem_Free(joined_path); |
| 11908 | if (!entry->name || !entry->path) |
| 11909 | goto error; |
| 11910 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11911 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11912 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11913 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11914 | entry->d_ino = d_ino; |
| 11915 | |
| 11916 | return (PyObject *)entry; |
| 11917 | |
| 11918 | error: |
| 11919 | Py_XDECREF(entry); |
| 11920 | return NULL; |
| 11921 | } |
| 11922 | |
| 11923 | #endif |
| 11924 | |
| 11925 | |
| 11926 | typedef struct { |
| 11927 | PyObject_HEAD |
| 11928 | path_t path; |
| 11929 | #ifdef MS_WINDOWS |
| 11930 | HANDLE handle; |
| 11931 | WIN32_FIND_DATAW file_data; |
| 11932 | int first_time; |
| 11933 | #else /* POSIX */ |
| 11934 | DIR *dirp; |
| 11935 | #endif |
| 11936 | } ScandirIterator; |
| 11937 | |
| 11938 | #ifdef MS_WINDOWS |
| 11939 | |
| 11940 | static void |
| 11941 | ScandirIterator_close(ScandirIterator *iterator) |
| 11942 | { |
| 11943 | if (iterator->handle == INVALID_HANDLE_VALUE) |
| 11944 | return; |
| 11945 | |
| 11946 | Py_BEGIN_ALLOW_THREADS |
| 11947 | FindClose(iterator->handle); |
| 11948 | Py_END_ALLOW_THREADS |
| 11949 | iterator->handle = INVALID_HANDLE_VALUE; |
| 11950 | } |
| 11951 | |
| 11952 | static PyObject * |
| 11953 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 11954 | { |
| 11955 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 11956 | BOOL success; |
| 11957 | |
| 11958 | /* Happens if the iterator is iterated twice */ |
| 11959 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 11960 | PyErr_SetNone(PyExc_StopIteration); |
| 11961 | return NULL; |
| 11962 | } |
| 11963 | |
| 11964 | while (1) { |
| 11965 | if (!iterator->first_time) { |
| 11966 | Py_BEGIN_ALLOW_THREADS |
| 11967 | success = FindNextFileW(iterator->handle, file_data); |
| 11968 | Py_END_ALLOW_THREADS |
| 11969 | if (!success) { |
| 11970 | if (GetLastError() != ERROR_NO_MORE_FILES) |
| 11971 | return path_error(&iterator->path); |
| 11972 | /* No more files found in directory, stop iterating */ |
| 11973 | break; |
| 11974 | } |
| 11975 | } |
| 11976 | iterator->first_time = 0; |
| 11977 | |
| 11978 | /* Skip over . and .. */ |
| 11979 | if (wcscmp(file_data->cFileName, L".") != 0 && |
| 11980 | wcscmp(file_data->cFileName, L"..") != 0) |
| 11981 | return DirEntry_from_find_data(&iterator->path, file_data); |
| 11982 | |
| 11983 | /* Loop till we get a non-dot directory or finish iterating */ |
| 11984 | } |
| 11985 | |
| 11986 | ScandirIterator_close(iterator); |
| 11987 | |
| 11988 | PyErr_SetNone(PyExc_StopIteration); |
| 11989 | return NULL; |
| 11990 | } |
| 11991 | |
| 11992 | #else /* POSIX */ |
| 11993 | |
| 11994 | static void |
| 11995 | ScandirIterator_close(ScandirIterator *iterator) |
| 11996 | { |
| 11997 | if (!iterator->dirp) |
| 11998 | return; |
| 11999 | |
| 12000 | Py_BEGIN_ALLOW_THREADS |
| 12001 | closedir(iterator->dirp); |
| 12002 | Py_END_ALLOW_THREADS |
| 12003 | iterator->dirp = NULL; |
| 12004 | return; |
| 12005 | } |
| 12006 | |
| 12007 | static PyObject * |
| 12008 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 12009 | { |
| 12010 | struct dirent *direntp; |
| 12011 | Py_ssize_t name_len; |
| 12012 | int is_dot; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12013 | |
| 12014 | /* Happens if the iterator is iterated twice */ |
| 12015 | if (!iterator->dirp) { |
| 12016 | PyErr_SetNone(PyExc_StopIteration); |
| 12017 | return NULL; |
| 12018 | } |
| 12019 | |
| 12020 | while (1) { |
| 12021 | errno = 0; |
| 12022 | Py_BEGIN_ALLOW_THREADS |
| 12023 | direntp = readdir(iterator->dirp); |
| 12024 | Py_END_ALLOW_THREADS |
| 12025 | |
| 12026 | if (!direntp) { |
| 12027 | if (errno != 0) |
| 12028 | return path_error(&iterator->path); |
| 12029 | /* No more files found in directory, stop iterating */ |
| 12030 | break; |
| 12031 | } |
| 12032 | |
| 12033 | /* Skip over . and .. */ |
| 12034 | name_len = NAMLEN(direntp); |
| 12035 | is_dot = direntp->d_name[0] == '.' && |
| 12036 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 12037 | if (!is_dot) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12038 | return DirEntry_from_posix_info(&iterator->path, direntp->d_name, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12039 | name_len, direntp->d_ino |
| 12040 | #ifdef HAVE_DIRENT_D_TYPE |
| 12041 | , direntp->d_type |
| 12042 | #endif |
| 12043 | ); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12044 | } |
| 12045 | |
| 12046 | /* Loop till we get a non-dot directory or finish iterating */ |
| 12047 | } |
| 12048 | |
| 12049 | ScandirIterator_close(iterator); |
| 12050 | |
| 12051 | PyErr_SetNone(PyExc_StopIteration); |
| 12052 | return NULL; |
| 12053 | } |
| 12054 | |
| 12055 | #endif |
| 12056 | |
| 12057 | static void |
| 12058 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 12059 | { |
| 12060 | ScandirIterator_close(iterator); |
| 12061 | Py_XDECREF(iterator->path.object); |
| 12062 | path_cleanup(&iterator->path); |
| 12063 | Py_TYPE(iterator)->tp_free((PyObject *)iterator); |
| 12064 | } |
| 12065 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 12066 | static PyTypeObject ScandirIteratorType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12067 | PyVarObject_HEAD_INIT(NULL, 0) |
| 12068 | MODNAME ".ScandirIterator", /* tp_name */ |
| 12069 | sizeof(ScandirIterator), /* tp_basicsize */ |
| 12070 | 0, /* tp_itemsize */ |
| 12071 | /* methods */ |
| 12072 | (destructor)ScandirIterator_dealloc, /* tp_dealloc */ |
| 12073 | 0, /* tp_print */ |
| 12074 | 0, /* tp_getattr */ |
| 12075 | 0, /* tp_setattr */ |
| 12076 | 0, /* tp_compare */ |
| 12077 | 0, /* tp_repr */ |
| 12078 | 0, /* tp_as_number */ |
| 12079 | 0, /* tp_as_sequence */ |
| 12080 | 0, /* tp_as_mapping */ |
| 12081 | 0, /* tp_hash */ |
| 12082 | 0, /* tp_call */ |
| 12083 | 0, /* tp_str */ |
| 12084 | 0, /* tp_getattro */ |
| 12085 | 0, /* tp_setattro */ |
| 12086 | 0, /* tp_as_buffer */ |
| 12087 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 12088 | 0, /* tp_doc */ |
| 12089 | 0, /* tp_traverse */ |
| 12090 | 0, /* tp_clear */ |
| 12091 | 0, /* tp_richcompare */ |
| 12092 | 0, /* tp_weaklistoffset */ |
| 12093 | PyObject_SelfIter, /* tp_iter */ |
| 12094 | (iternextfunc)ScandirIterator_iternext, /* tp_iternext */ |
| 12095 | }; |
| 12096 | |
| 12097 | static PyObject * |
| 12098 | posix_scandir(PyObject *self, PyObject *args, PyObject *kwargs) |
| 12099 | { |
| 12100 | ScandirIterator *iterator; |
| 12101 | static char *keywords[] = {"path", NULL}; |
| 12102 | #ifdef MS_WINDOWS |
| 12103 | wchar_t *path_strW; |
| 12104 | #else |
| 12105 | char *path; |
| 12106 | #endif |
| 12107 | |
| 12108 | iterator = PyObject_New(ScandirIterator, &ScandirIteratorType); |
| 12109 | if (!iterator) |
| 12110 | return NULL; |
| 12111 | memset(&iterator->path, 0, sizeof(path_t)); |
| 12112 | iterator->path.function_name = "scandir"; |
| 12113 | iterator->path.nullable = 1; |
| 12114 | |
| 12115 | #ifdef MS_WINDOWS |
| 12116 | iterator->handle = INVALID_HANDLE_VALUE; |
| 12117 | #else |
| 12118 | iterator->dirp = NULL; |
| 12119 | #endif |
| 12120 | |
| 12121 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:scandir", keywords, |
| 12122 | path_converter, &iterator->path)) |
| 12123 | goto error; |
| 12124 | |
| 12125 | /* path_converter doesn't keep path.object around, so do it |
| 12126 | manually for the lifetime of the iterator here (the refcount |
| 12127 | is decremented in ScandirIterator_dealloc) |
| 12128 | */ |
| 12129 | Py_XINCREF(iterator->path.object); |
| 12130 | |
| 12131 | #ifdef MS_WINDOWS |
| 12132 | if (iterator->path.narrow) { |
| 12133 | PyErr_SetString(PyExc_TypeError, |
| 12134 | "os.scandir() doesn't support bytes path on Windows, use Unicode instead"); |
| 12135 | goto error; |
| 12136 | } |
| 12137 | iterator->first_time = 1; |
| 12138 | |
| 12139 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 12140 | if (!path_strW) |
| 12141 | goto error; |
| 12142 | |
| 12143 | Py_BEGIN_ALLOW_THREADS |
| 12144 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 12145 | Py_END_ALLOW_THREADS |
| 12146 | |
| 12147 | PyMem_Free(path_strW); |
| 12148 | |
| 12149 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 12150 | path_error(&iterator->path); |
| 12151 | goto error; |
| 12152 | } |
| 12153 | #else /* POSIX */ |
| 12154 | if (iterator->path.narrow) |
| 12155 | path = iterator->path.narrow; |
| 12156 | else |
| 12157 | path = "."; |
| 12158 | |
| 12159 | errno = 0; |
| 12160 | Py_BEGIN_ALLOW_THREADS |
| 12161 | iterator->dirp = opendir(path); |
| 12162 | Py_END_ALLOW_THREADS |
| 12163 | |
| 12164 | if (!iterator->dirp) { |
| 12165 | path_error(&iterator->path); |
| 12166 | goto error; |
| 12167 | } |
| 12168 | #endif |
| 12169 | |
| 12170 | return (PyObject *)iterator; |
| 12171 | |
| 12172 | error: |
| 12173 | Py_DECREF(iterator); |
| 12174 | return NULL; |
| 12175 | } |
| 12176 | |
| 12177 | |
Serhiy Storchaka | a4c6bad | 2015-04-04 23:35:52 +0300 | [diff] [blame] | 12178 | #include "clinic/posixmodule.c.h" |
| 12179 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 12180 | /*[clinic input] |
| 12181 | dump buffer |
| 12182 | [clinic start generated code]*/ |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 12183 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/ |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 12184 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12185 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12186 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12187 | |
| 12188 | OS_STAT_METHODDEF |
| 12189 | OS_ACCESS_METHODDEF |
| 12190 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12191 | OS_CHDIR_METHODDEF |
| 12192 | OS_CHFLAGS_METHODDEF |
| 12193 | OS_CHMOD_METHODDEF |
| 12194 | OS_FCHMOD_METHODDEF |
| 12195 | OS_LCHMOD_METHODDEF |
| 12196 | OS_CHOWN_METHODDEF |
| 12197 | OS_FCHOWN_METHODDEF |
| 12198 | OS_LCHOWN_METHODDEF |
| 12199 | OS_LCHFLAGS_METHODDEF |
| 12200 | OS_CHROOT_METHODDEF |
| 12201 | OS_CTERMID_METHODDEF |
| 12202 | OS_GETCWD_METHODDEF |
| 12203 | OS_GETCWDB_METHODDEF |
| 12204 | OS_LINK_METHODDEF |
| 12205 | OS_LISTDIR_METHODDEF |
| 12206 | OS_LSTAT_METHODDEF |
| 12207 | OS_MKDIR_METHODDEF |
| 12208 | OS_NICE_METHODDEF |
| 12209 | OS_GETPRIORITY_METHODDEF |
| 12210 | OS_SETPRIORITY_METHODDEF |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12211 | #ifdef HAVE_READLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12212 | {"readlink", (PyCFunction)posix_readlink, |
| 12213 | METH_VARARGS | METH_KEYWORDS, |
| 12214 | readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 12215 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 12216 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12217 | {"readlink", (PyCFunction)win_readlink, |
| 12218 | METH_VARARGS | METH_KEYWORDS, |
| 12219 | readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 12220 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12221 | OS_RENAME_METHODDEF |
| 12222 | OS_REPLACE_METHODDEF |
| 12223 | OS_RMDIR_METHODDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12224 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12225 | OS_SYMLINK_METHODDEF |
| 12226 | OS_SYSTEM_METHODDEF |
| 12227 | OS_UMASK_METHODDEF |
| 12228 | OS_UNAME_METHODDEF |
| 12229 | OS_UNLINK_METHODDEF |
| 12230 | OS_REMOVE_METHODDEF |
| 12231 | OS_UTIME_METHODDEF |
| 12232 | OS_TIMES_METHODDEF |
| 12233 | OS__EXIT_METHODDEF |
| 12234 | OS_EXECV_METHODDEF |
| 12235 | OS_EXECVE_METHODDEF |
| 12236 | OS_SPAWNV_METHODDEF |
| 12237 | OS_SPAWNVE_METHODDEF |
| 12238 | OS_FORK1_METHODDEF |
| 12239 | OS_FORK_METHODDEF |
| 12240 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 12241 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 12242 | OS_SCHED_GETPARAM_METHODDEF |
| 12243 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 12244 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 12245 | OS_SCHED_SETPARAM_METHODDEF |
| 12246 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 12247 | OS_SCHED_YIELD_METHODDEF |
| 12248 | OS_SCHED_SETAFFINITY_METHODDEF |
| 12249 | OS_SCHED_GETAFFINITY_METHODDEF |
| 12250 | OS_OPENPTY_METHODDEF |
| 12251 | OS_FORKPTY_METHODDEF |
| 12252 | OS_GETEGID_METHODDEF |
| 12253 | OS_GETEUID_METHODDEF |
| 12254 | OS_GETGID_METHODDEF |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 12255 | #ifdef HAVE_GETGROUPLIST |
| 12256 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 12257 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12258 | OS_GETGROUPS_METHODDEF |
| 12259 | OS_GETPID_METHODDEF |
| 12260 | OS_GETPGRP_METHODDEF |
| 12261 | OS_GETPPID_METHODDEF |
| 12262 | OS_GETUID_METHODDEF |
| 12263 | OS_GETLOGIN_METHODDEF |
| 12264 | OS_KILL_METHODDEF |
| 12265 | OS_KILLPG_METHODDEF |
| 12266 | OS_PLOCK_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 12267 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12268 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 12269 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12270 | OS_SETUID_METHODDEF |
| 12271 | OS_SETEUID_METHODDEF |
| 12272 | OS_SETREUID_METHODDEF |
| 12273 | OS_SETGID_METHODDEF |
| 12274 | OS_SETEGID_METHODDEF |
| 12275 | OS_SETREGID_METHODDEF |
| 12276 | OS_SETGROUPS_METHODDEF |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 12277 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12278 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 12279 | #endif /* HAVE_INITGROUPS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12280 | OS_GETPGID_METHODDEF |
| 12281 | OS_SETPGRP_METHODDEF |
| 12282 | OS_WAIT_METHODDEF |
| 12283 | OS_WAIT3_METHODDEF |
| 12284 | OS_WAIT4_METHODDEF |
| 12285 | OS_WAITID_METHODDEF |
| 12286 | OS_WAITPID_METHODDEF |
| 12287 | OS_GETSID_METHODDEF |
| 12288 | OS_SETSID_METHODDEF |
| 12289 | OS_SETPGID_METHODDEF |
| 12290 | OS_TCGETPGRP_METHODDEF |
| 12291 | OS_TCSETPGRP_METHODDEF |
| 12292 | OS_OPEN_METHODDEF |
| 12293 | OS_CLOSE_METHODDEF |
| 12294 | OS_CLOSERANGE_METHODDEF |
| 12295 | OS_DEVICE_ENCODING_METHODDEF |
| 12296 | OS_DUP_METHODDEF |
| 12297 | OS_DUP2_METHODDEF |
| 12298 | OS_LOCKF_METHODDEF |
| 12299 | OS_LSEEK_METHODDEF |
| 12300 | OS_READ_METHODDEF |
| 12301 | OS_READV_METHODDEF |
| 12302 | OS_PREAD_METHODDEF |
| 12303 | OS_WRITE_METHODDEF |
| 12304 | OS_WRITEV_METHODDEF |
| 12305 | OS_PWRITE_METHODDEF |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12306 | #ifdef HAVE_SENDFILE |
| 12307 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 12308 | posix_sendfile__doc__}, |
| 12309 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12310 | OS_FSTAT_METHODDEF |
| 12311 | OS_ISATTY_METHODDEF |
| 12312 | OS_PIPE_METHODDEF |
| 12313 | OS_PIPE2_METHODDEF |
| 12314 | OS_MKFIFO_METHODDEF |
| 12315 | OS_MKNOD_METHODDEF |
| 12316 | OS_MAJOR_METHODDEF |
| 12317 | OS_MINOR_METHODDEF |
| 12318 | OS_MAKEDEV_METHODDEF |
| 12319 | OS_FTRUNCATE_METHODDEF |
| 12320 | OS_TRUNCATE_METHODDEF |
| 12321 | OS_POSIX_FALLOCATE_METHODDEF |
| 12322 | OS_POSIX_FADVISE_METHODDEF |
| 12323 | OS_PUTENV_METHODDEF |
| 12324 | OS_UNSETENV_METHODDEF |
| 12325 | OS_STRERROR_METHODDEF |
| 12326 | OS_FCHDIR_METHODDEF |
| 12327 | OS_FSYNC_METHODDEF |
| 12328 | OS_SYNC_METHODDEF |
| 12329 | OS_FDATASYNC_METHODDEF |
| 12330 | OS_WCOREDUMP_METHODDEF |
| 12331 | OS_WIFCONTINUED_METHODDEF |
| 12332 | OS_WIFSTOPPED_METHODDEF |
| 12333 | OS_WIFSIGNALED_METHODDEF |
| 12334 | OS_WIFEXITED_METHODDEF |
| 12335 | OS_WEXITSTATUS_METHODDEF |
| 12336 | OS_WTERMSIG_METHODDEF |
| 12337 | OS_WSTOPSIG_METHODDEF |
| 12338 | OS_FSTATVFS_METHODDEF |
| 12339 | OS_STATVFS_METHODDEF |
| 12340 | OS_CONFSTR_METHODDEF |
| 12341 | OS_SYSCONF_METHODDEF |
| 12342 | OS_FPATHCONF_METHODDEF |
| 12343 | OS_PATHCONF_METHODDEF |
| 12344 | OS_ABORT_METHODDEF |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 12345 | OS__GETFULLPATHNAME_METHODDEF |
| 12346 | OS__ISDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12347 | OS__GETDISKUSAGE_METHODDEF |
| 12348 | OS__GETFINALPATHNAME_METHODDEF |
| 12349 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 12350 | OS_GETLOADAVG_METHODDEF |
| 12351 | OS_URANDOM_METHODDEF |
| 12352 | OS_SETRESUID_METHODDEF |
| 12353 | OS_SETRESGID_METHODDEF |
| 12354 | OS_GETRESUID_METHODDEF |
| 12355 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12356 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12357 | OS_GETXATTR_METHODDEF |
| 12358 | OS_SETXATTR_METHODDEF |
| 12359 | OS_REMOVEXATTR_METHODDEF |
| 12360 | OS_LISTXATTR_METHODDEF |
| 12361 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12362 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 12363 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 12364 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12365 | OS_CPU_COUNT_METHODDEF |
| 12366 | OS_GET_INHERITABLE_METHODDEF |
| 12367 | OS_SET_INHERITABLE_METHODDEF |
| 12368 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 12369 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12370 | #ifndef MS_WINDOWS |
| 12371 | {"get_blocking", posix_get_blocking, METH_VARARGS, get_blocking__doc__}, |
| 12372 | {"set_blocking", posix_set_blocking, METH_VARARGS, set_blocking__doc__}, |
| 12373 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12374 | {"scandir", (PyCFunction)posix_scandir, |
| 12375 | METH_VARARGS | METH_KEYWORDS, |
| 12376 | posix_scandir__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12377 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 12378 | }; |
| 12379 | |
| 12380 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12381 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12382 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12383 | enable_symlink() |
| 12384 | { |
| 12385 | HANDLE tok; |
| 12386 | TOKEN_PRIVILEGES tok_priv; |
| 12387 | LUID luid; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12388 | |
| 12389 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12390 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12391 | |
| 12392 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12393 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12394 | |
| 12395 | tok_priv.PrivilegeCount = 1; |
| 12396 | tok_priv.Privileges[0].Luid = luid; |
| 12397 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 12398 | |
| 12399 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 12400 | sizeof(TOKEN_PRIVILEGES), |
| 12401 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12402 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12403 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12404 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 12405 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12406 | } |
| 12407 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 12408 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12409 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12410 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12411 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12412 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12413 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12414 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12415 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12416 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12417 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12418 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12419 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12420 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12421 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12422 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12423 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12424 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12425 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12426 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12427 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12428 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12429 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12430 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12431 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12432 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12433 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12434 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12435 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12436 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12437 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12438 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12439 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12440 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12441 | #endif |
| 12442 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12443 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12444 | #endif |
| 12445 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12446 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12447 | #endif |
| 12448 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12449 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12450 | #endif |
| 12451 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12452 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12453 | #endif |
| 12454 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12455 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12456 | #endif |
| 12457 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12458 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12459 | #endif |
| 12460 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12461 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12462 | #endif |
| 12463 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12464 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12465 | #endif |
| 12466 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12467 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12468 | #endif |
| 12469 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12470 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12471 | #endif |
| 12472 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12473 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12474 | #endif |
| 12475 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12476 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12477 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 12478 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12479 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 12480 | #endif |
| 12481 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12482 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 12483 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12484 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12485 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12486 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12487 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12488 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12489 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 12490 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12491 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 12492 | #endif |
| 12493 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12494 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 12495 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12496 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12497 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12498 | #endif |
| 12499 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12500 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12501 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 12502 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12503 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 12504 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12505 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12506 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12507 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 12508 | #ifdef O_TMPFILE |
| 12509 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 12510 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12511 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12512 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12513 | #endif |
| 12514 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12515 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12516 | #endif |
| 12517 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12518 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12519 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 12520 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12521 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 12522 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12523 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12524 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12525 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12526 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12527 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 12528 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12529 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 12530 | #endif |
| 12531 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12532 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 12533 | #endif |
| 12534 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12535 | /* MS Windows */ |
| 12536 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12537 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12538 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12539 | #endif |
| 12540 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12541 | /* Optimize for short life (keep in memory). */ |
| 12542 | /* 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] | 12543 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12544 | #endif |
| 12545 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12546 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12547 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12548 | #endif |
| 12549 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12550 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12551 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12552 | #endif |
| 12553 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12554 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12555 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12556 | #endif |
| 12557 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12558 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 12559 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12560 | /* Send a SIGIO signal whenever input or output |
| 12561 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12562 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 12563 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12564 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12565 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12566 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12567 | #endif |
| 12568 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12569 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12570 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12571 | #endif |
| 12572 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12573 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12574 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12575 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12576 | #ifdef O_NOLINKS |
| 12577 | /* 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] | 12578 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12579 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 12580 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12581 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12582 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 12583 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 12584 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12585 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12586 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12587 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12588 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12589 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12590 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12591 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12592 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12593 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12594 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12595 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12596 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12597 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12598 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12599 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12600 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12601 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12602 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12603 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12604 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12605 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12606 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12607 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12608 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12609 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12610 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12611 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12612 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12613 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12614 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12615 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12616 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12617 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12618 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12619 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12620 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12621 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12622 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12623 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12624 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12625 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12626 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12627 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12628 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12629 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12630 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12631 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12632 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12633 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12634 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12635 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12636 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12637 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 12638 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 12639 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12640 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 12641 | #endif /* ST_RDONLY */ |
| 12642 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12643 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 12644 | #endif /* ST_NOSUID */ |
| 12645 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 12646 | /* GNU extensions */ |
| 12647 | #ifdef ST_NODEV |
| 12648 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 12649 | #endif /* ST_NODEV */ |
| 12650 | #ifdef ST_NOEXEC |
| 12651 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 12652 | #endif /* ST_NOEXEC */ |
| 12653 | #ifdef ST_SYNCHRONOUS |
| 12654 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 12655 | #endif /* ST_SYNCHRONOUS */ |
| 12656 | #ifdef ST_MANDLOCK |
| 12657 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 12658 | #endif /* ST_MANDLOCK */ |
| 12659 | #ifdef ST_WRITE |
| 12660 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 12661 | #endif /* ST_WRITE */ |
| 12662 | #ifdef ST_APPEND |
| 12663 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 12664 | #endif /* ST_APPEND */ |
| 12665 | #ifdef ST_NOATIME |
| 12666 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 12667 | #endif /* ST_NOATIME */ |
| 12668 | #ifdef ST_NODIRATIME |
| 12669 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 12670 | #endif /* ST_NODIRATIME */ |
| 12671 | #ifdef ST_RELATIME |
| 12672 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 12673 | #endif /* ST_RELATIME */ |
| 12674 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12675 | /* FreeBSD sendfile() constants */ |
| 12676 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12677 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12678 | #endif |
| 12679 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12680 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12681 | #endif |
| 12682 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12683 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12684 | #endif |
| 12685 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12686 | /* constants for posix_fadvise */ |
| 12687 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12688 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12689 | #endif |
| 12690 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12691 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12692 | #endif |
| 12693 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12694 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12695 | #endif |
| 12696 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12697 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12698 | #endif |
| 12699 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12700 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12701 | #endif |
| 12702 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12703 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12704 | #endif |
| 12705 | |
| 12706 | /* constants for waitid */ |
| 12707 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12708 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 12709 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 12710 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12711 | #endif |
| 12712 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12713 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12714 | #endif |
| 12715 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12716 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12717 | #endif |
| 12718 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12719 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12720 | #endif |
| 12721 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12722 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12723 | #endif |
| 12724 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12725 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12726 | #endif |
| 12727 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12728 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12729 | #endif |
| 12730 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12731 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12732 | #endif |
| 12733 | |
| 12734 | /* constants for lockf */ |
| 12735 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12736 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12737 | #endif |
| 12738 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12739 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12740 | #endif |
| 12741 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12742 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12743 | #endif |
| 12744 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12745 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12746 | #endif |
| 12747 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 12748 | #ifdef HAVE_SPAWNV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12749 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 12750 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
| 12751 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
| 12752 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
| 12753 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 12754 | #endif |
| 12755 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12756 | #ifdef HAVE_SCHED_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12757 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
| 12758 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
| 12759 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12760 | #ifdef SCHED_SPORADIC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12761 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12762 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12763 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12764 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12765 | #endif |
| 12766 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12767 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12768 | #endif |
| 12769 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12770 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12771 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12772 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12773 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12774 | #endif |
| 12775 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12776 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12777 | #endif |
| 12778 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12779 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12780 | #endif |
| 12781 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12782 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12783 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12784 | #endif |
| 12785 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12786 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12787 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 12788 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 12789 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12790 | #endif |
| 12791 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12792 | #ifdef RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12793 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12794 | #endif |
| 12795 | #ifdef RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12796 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12797 | #endif |
| 12798 | #ifdef RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12799 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12800 | #endif |
| 12801 | #ifdef RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12802 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12803 | #endif |
| 12804 | #ifdef RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12805 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12806 | #endif |
| 12807 | #ifdef RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12808 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12809 | #endif |
| 12810 | #ifdef RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12811 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12812 | #endif |
| 12813 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12814 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12815 | } |
| 12816 | |
| 12817 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12818 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12819 | PyModuleDef_HEAD_INIT, |
| 12820 | MODNAME, |
| 12821 | posix__doc__, |
| 12822 | -1, |
| 12823 | posix_methods, |
| 12824 | NULL, |
| 12825 | NULL, |
| 12826 | NULL, |
| 12827 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12828 | }; |
| 12829 | |
| 12830 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12831 | static const char * const have_functions[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12832 | |
| 12833 | #ifdef HAVE_FACCESSAT |
| 12834 | "HAVE_FACCESSAT", |
| 12835 | #endif |
| 12836 | |
| 12837 | #ifdef HAVE_FCHDIR |
| 12838 | "HAVE_FCHDIR", |
| 12839 | #endif |
| 12840 | |
| 12841 | #ifdef HAVE_FCHMOD |
| 12842 | "HAVE_FCHMOD", |
| 12843 | #endif |
| 12844 | |
| 12845 | #ifdef HAVE_FCHMODAT |
| 12846 | "HAVE_FCHMODAT", |
| 12847 | #endif |
| 12848 | |
| 12849 | #ifdef HAVE_FCHOWN |
| 12850 | "HAVE_FCHOWN", |
| 12851 | #endif |
| 12852 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 12853 | #ifdef HAVE_FCHOWNAT |
| 12854 | "HAVE_FCHOWNAT", |
| 12855 | #endif |
| 12856 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12857 | #ifdef HAVE_FEXECVE |
| 12858 | "HAVE_FEXECVE", |
| 12859 | #endif |
| 12860 | |
| 12861 | #ifdef HAVE_FDOPENDIR |
| 12862 | "HAVE_FDOPENDIR", |
| 12863 | #endif |
| 12864 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12865 | #ifdef HAVE_FPATHCONF |
| 12866 | "HAVE_FPATHCONF", |
| 12867 | #endif |
| 12868 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12869 | #ifdef HAVE_FSTATAT |
| 12870 | "HAVE_FSTATAT", |
| 12871 | #endif |
| 12872 | |
| 12873 | #ifdef HAVE_FSTATVFS |
| 12874 | "HAVE_FSTATVFS", |
| 12875 | #endif |
| 12876 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 12877 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12878 | "HAVE_FTRUNCATE", |
| 12879 | #endif |
| 12880 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12881 | #ifdef HAVE_FUTIMENS |
| 12882 | "HAVE_FUTIMENS", |
| 12883 | #endif |
| 12884 | |
| 12885 | #ifdef HAVE_FUTIMES |
| 12886 | "HAVE_FUTIMES", |
| 12887 | #endif |
| 12888 | |
| 12889 | #ifdef HAVE_FUTIMESAT |
| 12890 | "HAVE_FUTIMESAT", |
| 12891 | #endif |
| 12892 | |
| 12893 | #ifdef HAVE_LINKAT |
| 12894 | "HAVE_LINKAT", |
| 12895 | #endif |
| 12896 | |
| 12897 | #ifdef HAVE_LCHFLAGS |
| 12898 | "HAVE_LCHFLAGS", |
| 12899 | #endif |
| 12900 | |
| 12901 | #ifdef HAVE_LCHMOD |
| 12902 | "HAVE_LCHMOD", |
| 12903 | #endif |
| 12904 | |
| 12905 | #ifdef HAVE_LCHOWN |
| 12906 | "HAVE_LCHOWN", |
| 12907 | #endif |
| 12908 | |
| 12909 | #ifdef HAVE_LSTAT |
| 12910 | "HAVE_LSTAT", |
| 12911 | #endif |
| 12912 | |
| 12913 | #ifdef HAVE_LUTIMES |
| 12914 | "HAVE_LUTIMES", |
| 12915 | #endif |
| 12916 | |
| 12917 | #ifdef HAVE_MKDIRAT |
| 12918 | "HAVE_MKDIRAT", |
| 12919 | #endif |
| 12920 | |
| 12921 | #ifdef HAVE_MKFIFOAT |
| 12922 | "HAVE_MKFIFOAT", |
| 12923 | #endif |
| 12924 | |
| 12925 | #ifdef HAVE_MKNODAT |
| 12926 | "HAVE_MKNODAT", |
| 12927 | #endif |
| 12928 | |
| 12929 | #ifdef HAVE_OPENAT |
| 12930 | "HAVE_OPENAT", |
| 12931 | #endif |
| 12932 | |
| 12933 | #ifdef HAVE_READLINKAT |
| 12934 | "HAVE_READLINKAT", |
| 12935 | #endif |
| 12936 | |
| 12937 | #ifdef HAVE_RENAMEAT |
| 12938 | "HAVE_RENAMEAT", |
| 12939 | #endif |
| 12940 | |
| 12941 | #ifdef HAVE_SYMLINKAT |
| 12942 | "HAVE_SYMLINKAT", |
| 12943 | #endif |
| 12944 | |
| 12945 | #ifdef HAVE_UNLINKAT |
| 12946 | "HAVE_UNLINKAT", |
| 12947 | #endif |
| 12948 | |
| 12949 | #ifdef HAVE_UTIMENSAT |
| 12950 | "HAVE_UTIMENSAT", |
| 12951 | #endif |
| 12952 | |
| 12953 | #ifdef MS_WINDOWS |
| 12954 | "MS_WINDOWS", |
| 12955 | #endif |
| 12956 | |
| 12957 | NULL |
| 12958 | }; |
| 12959 | |
| 12960 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 12961 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 12962 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12963 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12964 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12965 | PyObject *list; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12966 | const char * const *trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12967 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12968 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12969 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12970 | #endif |
| 12971 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12972 | m = PyModule_Create(&posixmodule); |
| 12973 | if (m == NULL) |
| 12974 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12975 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12976 | /* Initialize environ dictionary */ |
| 12977 | v = convertenviron(); |
| 12978 | Py_XINCREF(v); |
| 12979 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 12980 | return NULL; |
| 12981 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12982 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12983 | if (all_ins(m)) |
| 12984 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12985 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12986 | if (setup_confname_tables(m)) |
| 12987 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12988 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12989 | Py_INCREF(PyExc_OSError); |
| 12990 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 12991 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12992 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12993 | if (posix_putenv_garbage == NULL) |
| 12994 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12995 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 12996 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12997 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12998 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 12999 | waitid_result_desc.name = MODNAME ".waitid_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13000 | if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0) |
| 13001 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13002 | #endif |
| 13003 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 13004 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13005 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 13006 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 13007 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13008 | if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0) |
| 13009 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13010 | structseq_new = StatResultType.tp_new; |
| 13011 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13012 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 13013 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13014 | if (PyStructSequence_InitType2(&StatVFSResultType, |
| 13015 | &statvfs_result_desc) < 0) |
| 13016 | return NULL; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 13017 | #ifdef NEED_TICKS_PER_SECOND |
| 13018 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13019 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 13020 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13021 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 13022 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13023 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 13024 | # endif |
| 13025 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13026 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 13027 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13028 | sched_param_desc.name = MODNAME ".sched_param"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13029 | if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0) |
| 13030 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13031 | SchedParamType.tp_new = os_sched_param; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13032 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13033 | |
| 13034 | /* initialize TerminalSize_info */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13035 | if (PyStructSequence_InitType2(&TerminalSizeType, |
| 13036 | &TerminalSize_desc) < 0) |
| 13037 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13038 | |
| 13039 | /* initialize scandir types */ |
| 13040 | if (PyType_Ready(&ScandirIteratorType) < 0) |
| 13041 | return NULL; |
| 13042 | if (PyType_Ready(&DirEntryType) < 0) |
| 13043 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13044 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13045 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 13046 | Py_INCREF((PyObject*) &WaitidResultType); |
| 13047 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 13048 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13049 | Py_INCREF((PyObject*) &StatResultType); |
| 13050 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 13051 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 13052 | PyModule_AddObject(m, "statvfs_result", |
| 13053 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 13054 | |
| 13055 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13056 | Py_INCREF(&SchedParamType); |
| 13057 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 13058 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13059 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 13060 | times_result_desc.name = MODNAME ".times_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13061 | if (PyStructSequence_InitType2(&TimesResultType, ×_result_desc) < 0) |
| 13062 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 13063 | PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType); |
| 13064 | |
| 13065 | uname_result_desc.name = MODNAME ".uname_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13066 | if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0) |
| 13067 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 13068 | PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType); |
| 13069 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13070 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13071 | /* |
| 13072 | * Step 2 of weak-linking support on Mac OS X. |
| 13073 | * |
| 13074 | * The code below removes functions that are not available on the |
| 13075 | * currently active platform. |
| 13076 | * |
| 13077 | * 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] | 13078 | * 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] | 13079 | * OSX 10.4. |
| 13080 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13081 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13082 | if (fstatvfs == NULL) { |
| 13083 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 13084 | return NULL; |
| 13085 | } |
| 13086 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13087 | #endif /* HAVE_FSTATVFS */ |
| 13088 | |
| 13089 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13090 | if (statvfs == NULL) { |
| 13091 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 13092 | return NULL; |
| 13093 | } |
| 13094 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13095 | #endif /* HAVE_STATVFS */ |
| 13096 | |
| 13097 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13098 | if (lchown == NULL) { |
| 13099 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 13100 | return NULL; |
| 13101 | } |
| 13102 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13103 | #endif /* HAVE_LCHOWN */ |
| 13104 | |
| 13105 | |
| 13106 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13107 | |
Antoine Pitrou | 9d20e0e | 2012-09-12 18:01:36 +0200 | [diff] [blame] | 13108 | Py_INCREF(&TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13109 | PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); |
| 13110 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 13111 | billion = PyLong_FromLong(1000000000); |
| 13112 | if (!billion) |
| 13113 | return NULL; |
| 13114 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13115 | /* suppress "function not used" warnings */ |
| 13116 | { |
| 13117 | int ignored; |
| 13118 | fd_specified("", -1); |
| 13119 | follow_symlinks_specified("", 1); |
| 13120 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 13121 | dir_fd_converter(Py_None, &ignored); |
| 13122 | dir_fd_unavailable(Py_None, &ignored); |
| 13123 | } |
| 13124 | |
| 13125 | /* |
| 13126 | * provide list of locally available functions |
| 13127 | * so os.py can populate support_* lists |
| 13128 | */ |
| 13129 | list = PyList_New(0); |
| 13130 | if (!list) |
| 13131 | return NULL; |
| 13132 | for (trace = have_functions; *trace; trace++) { |
| 13133 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 13134 | if (!unicode) |
| 13135 | return NULL; |
| 13136 | if (PyList_Append(list, unicode)) |
| 13137 | return NULL; |
| 13138 | Py_DECREF(unicode); |
| 13139 | } |
| 13140 | PyModule_AddObject(m, "_have_functions", list); |
| 13141 | |
| 13142 | initialized = 1; |
| 13143 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13144 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 13145 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13146 | |
| 13147 | #ifdef __cplusplus |
| 13148 | } |
| 13149 | #endif |