Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win. In that case the |
| 5 | module actually calls itself 'nt', not 'posix', and a few |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Victor Stinner | f427a14 | 2014-10-22 12:33:23 +0200 | [diff] [blame] | 9 | test macro, e.g. '_MSC_VER'. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 11 | |
| 12 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13 | #ifdef __APPLE__ |
| 14 | /* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15 | * Step 1 of support for weak-linking a number of symbols existing on |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 16 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 17 | * at the end of this file for more information. |
| 18 | */ |
| 19 | # pragma weak lchown |
| 20 | # pragma weak statvfs |
| 21 | # pragma weak fstatvfs |
| 22 | |
| 23 | #endif /* __APPLE__ */ |
| 24 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 25 | #define PY_SSIZE_T_CLEAN |
| 26 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 27 | #include "Python.h" |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 28 | #include "structmember.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 29 | #ifndef MS_WINDOWS |
| 30 | #include "posixmodule.h" |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 31 | #else |
| 32 | #include "winreparse.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 33 | #endif |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 34 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 39 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 40 | "This module provides access to operating system functionality that is\n\ |
| 41 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 42 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 43 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 45 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 46 | #ifdef HAVE_SYS_UIO_H |
| 47 | #include <sys/uio.h> |
| 48 | #endif |
| 49 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 50 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 51 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 52 | #endif /* HAVE_SYS_TYPES_H */ |
| 53 | |
| 54 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 55 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 56 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 58 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 59 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 60 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 61 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 62 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 63 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 64 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 66 | #ifdef HAVE_FCNTL_H |
| 67 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 68 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 70 | #ifdef HAVE_GRP_H |
| 71 | #include <grp.h> |
| 72 | #endif |
| 73 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 74 | #ifdef HAVE_SYSEXITS_H |
| 75 | #include <sysexits.h> |
| 76 | #endif /* HAVE_SYSEXITS_H */ |
| 77 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 78 | #ifdef HAVE_SYS_LOADAVG_H |
| 79 | #include <sys/loadavg.h> |
| 80 | #endif |
| 81 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 82 | #ifdef HAVE_LANGINFO_H |
| 83 | #include <langinfo.h> |
| 84 | #endif |
| 85 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 86 | #ifdef HAVE_SYS_SENDFILE_H |
| 87 | #include <sys/sendfile.h> |
| 88 | #endif |
| 89 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 90 | #ifdef HAVE_SCHED_H |
| 91 | #include <sched.h> |
| 92 | #endif |
| 93 | |
Benjamin Peterson | 2dbda07 | 2012-03-16 10:12:55 -0500 | [diff] [blame] | 94 | #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) |
Benjamin Peterson | 7b51b8d | 2012-03-14 22:28:25 -0500 | [diff] [blame] | 95 | #undef HAVE_SCHED_SETAFFINITY |
| 96 | #endif |
| 97 | |
doko@ubuntu.com | 4a173bc | 2014-04-17 19:47:16 +0200 | [diff] [blame] | 98 | #if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__) |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 99 | #define USE_XATTRS |
| 100 | #endif |
| 101 | |
| 102 | #ifdef USE_XATTRS |
Benjamin Peterson | b77fe17 | 2011-09-13 17:20:47 -0400 | [diff] [blame] | 103 | #include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 104 | #endif |
| 105 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 106 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 107 | #ifdef HAVE_SYS_SOCKET_H |
| 108 | #include <sys/socket.h> |
| 109 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 110 | #endif |
| 111 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 112 | #ifdef HAVE_DLFCN_H |
| 113 | #include <dlfcn.h> |
| 114 | #endif |
| 115 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 116 | #ifdef __hpux |
| 117 | #include <sys/mpctl.h> |
| 118 | #endif |
| 119 | |
| 120 | #if defined(__DragonFly__) || \ |
| 121 | defined(__OpenBSD__) || \ |
| 122 | defined(__FreeBSD__) || \ |
| 123 | defined(__NetBSD__) || \ |
| 124 | defined(__APPLE__) |
| 125 | #include <sys/sysctl.h> |
| 126 | #endif |
| 127 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 128 | #if defined(MS_WINDOWS) |
| 129 | # define TERMSIZE_USE_CONIO |
| 130 | #elif defined(HAVE_SYS_IOCTL_H) |
| 131 | # include <sys/ioctl.h> |
| 132 | # if defined(HAVE_TERMIOS_H) |
| 133 | # include <termios.h> |
| 134 | # endif |
| 135 | # if defined(TIOCGWINSZ) |
| 136 | # define TERMSIZE_USE_IOCTL |
| 137 | # endif |
| 138 | #endif /* MS_WINDOWS */ |
| 139 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 140 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 141 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 142 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 143 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 144 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 145 | #include <process.h> |
| 146 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 147 | #ifdef _MSC_VER /* Microsoft compiler */ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 148 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 149 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 150 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 151 | #define HAVE_EXECV 1 |
| 152 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 153 | #define HAVE_SYSTEM 1 |
| 154 | #define HAVE_CWAIT 1 |
| 155 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 156 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 157 | #else |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 158 | /* Unix functions that the configure script doesn't check for */ |
| 159 | #define HAVE_EXECV 1 |
| 160 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 161 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 162 | #define HAVE_FORK1 1 |
| 163 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 164 | #define HAVE_GETEGID 1 |
| 165 | #define HAVE_GETEUID 1 |
| 166 | #define HAVE_GETGID 1 |
| 167 | #define HAVE_GETPPID 1 |
| 168 | #define HAVE_GETUID 1 |
| 169 | #define HAVE_KILL 1 |
| 170 | #define HAVE_OPENDIR 1 |
| 171 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 172 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 173 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 174 | #define HAVE_TTYNAME 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 175 | #endif /* _MSC_VER */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 176 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 177 | |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 178 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 179 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 180 | # one of the few times we lie about this name! |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 181 | module os |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 182 | [clinic start generated code]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 183 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/ |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 184 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 185 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 186 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 187 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 188 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 189 | (default) */ |
| 190 | extern char *ctermid_r(char *); |
| 191 | #endif |
| 192 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 193 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 194 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 195 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 196 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 197 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 198 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 199 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 200 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 201 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 202 | #endif |
| 203 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 204 | extern int chdir(char *); |
| 205 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 206 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 207 | extern int chdir(const char *); |
| 208 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 209 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 210 | extern int chmod(const char *, mode_t); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 211 | /*#ifdef HAVE_FCHMOD |
| 212 | extern int fchmod(int, mode_t); |
| 213 | #endif*/ |
| 214 | /*#ifdef HAVE_LCHMOD |
| 215 | extern int lchmod(const char *, mode_t); |
| 216 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 217 | extern int chown(const char *, uid_t, gid_t); |
| 218 | extern char *getcwd(char *, int); |
| 219 | extern char *strerror(int); |
| 220 | extern int link(const char *, const char *); |
| 221 | extern int rename(const char *, const char *); |
| 222 | extern int stat(const char *, struct stat *); |
| 223 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 224 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 225 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 226 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 227 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 228 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 229 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 230 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 231 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 232 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 233 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 234 | #ifdef HAVE_UTIME_H |
| 235 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 236 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 237 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 238 | #ifdef HAVE_SYS_UTIME_H |
| 239 | #include <sys/utime.h> |
| 240 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 241 | #endif /* HAVE_SYS_UTIME_H */ |
| 242 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 243 | #ifdef HAVE_SYS_TIMES_H |
| 244 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 245 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 246 | |
| 247 | #ifdef HAVE_SYS_PARAM_H |
| 248 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 249 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 250 | |
| 251 | #ifdef HAVE_SYS_UTSNAME_H |
| 252 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 253 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 255 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 256 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 257 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 258 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 259 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 260 | #include <direct.h> |
| 261 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 262 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 263 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 264 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 265 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 266 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 267 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 268 | #endif |
| 269 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 270 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 271 | #endif |
| 272 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 273 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 274 | #endif |
| 275 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 276 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 277 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 278 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 279 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 280 | #endif |
| 281 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 282 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 283 | #endif |
| 284 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 285 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 286 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 287 | #ifndef VOLUME_NAME_DOS |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 288 | #define VOLUME_NAME_DOS 0x0 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 289 | #endif |
| 290 | #ifndef VOLUME_NAME_NT |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 291 | #define VOLUME_NAME_NT 0x2 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 292 | #endif |
| 293 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 294 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 295 | #endif |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 296 | #ifndef IO_REPARSE_TAG_MOUNT_POINT |
| 297 | #define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L) |
| 298 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 299 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 300 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 301 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 302 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 303 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 304 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 305 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 306 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 307 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 308 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 309 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 310 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 311 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 312 | #define MAXPATHLEN PATH_MAX |
| 313 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 314 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 315 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 316 | #endif /* MAXPATHLEN */ |
| 317 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 318 | #ifdef UNION_WAIT |
| 319 | /* Emulate some macros on systems that have a union instead of macros */ |
| 320 | |
| 321 | #ifndef WIFEXITED |
| 322 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 323 | #endif |
| 324 | |
| 325 | #ifndef WEXITSTATUS |
| 326 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 327 | #endif |
| 328 | |
| 329 | #ifndef WTERMSIG |
| 330 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 331 | #endif |
| 332 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 333 | #define WAIT_TYPE union wait |
| 334 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 335 | |
| 336 | #else /* !UNION_WAIT */ |
| 337 | #define WAIT_TYPE int |
| 338 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 339 | #endif /* UNION_WAIT */ |
| 340 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 341 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 342 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 343 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 344 | #define USE_CTERMID_R |
| 345 | #endif |
| 346 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 347 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 348 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 349 | #undef FSTAT |
| 350 | #undef STRUCT_STAT |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 351 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 352 | # define STAT win32_stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 353 | # define LSTAT win32_lstat |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 354 | # define FSTAT _Py_fstat_noraise |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 355 | # define STRUCT_STAT struct _Py_stat_struct |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 356 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 357 | # define STAT stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 358 | # define LSTAT lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 359 | # define FSTAT fstat |
| 360 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 361 | #endif |
| 362 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 363 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 364 | #include <sys/mkdev.h> |
| 365 | #else |
| 366 | #if defined(MAJOR_IN_SYSMACROS) |
| 367 | #include <sys/sysmacros.h> |
| 368 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 369 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 370 | #include <sys/mkdev.h> |
| 371 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 372 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 373 | |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 374 | #define DWORD_MAX 4294967295U |
| 375 | |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 376 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 377 | #define INITFUNC PyInit_nt |
| 378 | #define MODNAME "nt" |
| 379 | #else |
| 380 | #define INITFUNC PyInit_posix |
| 381 | #define MODNAME "posix" |
| 382 | #endif |
| 383 | |
| 384 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 385 | /* defined in fileutils.c */ |
| 386 | PyAPI_FUNC(void) _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *); |
| 387 | PyAPI_FUNC(void) _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *, |
| 388 | ULONG, struct _Py_stat_struct *); |
| 389 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 390 | |
| 391 | #ifdef MS_WINDOWS |
| 392 | static int |
| 393 | win32_warn_bytes_api() |
| 394 | { |
| 395 | return PyErr_WarnEx(PyExc_DeprecationWarning, |
| 396 | "The Windows bytes API has been deprecated, " |
| 397 | "use Unicode filenames instead", |
| 398 | 1); |
| 399 | } |
| 400 | #endif |
| 401 | |
| 402 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 403 | #ifndef MS_WINDOWS |
| 404 | PyObject * |
| 405 | _PyLong_FromUid(uid_t uid) |
| 406 | { |
| 407 | if (uid == (uid_t)-1) |
| 408 | return PyLong_FromLong(-1); |
| 409 | return PyLong_FromUnsignedLong(uid); |
| 410 | } |
| 411 | |
| 412 | PyObject * |
| 413 | _PyLong_FromGid(gid_t gid) |
| 414 | { |
| 415 | if (gid == (gid_t)-1) |
| 416 | return PyLong_FromLong(-1); |
| 417 | return PyLong_FromUnsignedLong(gid); |
| 418 | } |
| 419 | |
| 420 | int |
| 421 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 422 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 423 | uid_t uid; |
| 424 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 425 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 426 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 427 | unsigned long uresult; |
| 428 | |
| 429 | index = PyNumber_Index(obj); |
| 430 | if (index == NULL) { |
| 431 | PyErr_Format(PyExc_TypeError, |
| 432 | "uid should be integer, not %.200s", |
| 433 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 434 | return 0; |
| 435 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 436 | |
| 437 | /* |
| 438 | * Handling uid_t is complicated for two reasons: |
| 439 | * * Although uid_t is (always?) unsigned, it still |
| 440 | * accepts -1. |
| 441 | * * We don't know its size in advance--it may be |
| 442 | * bigger than an int, or it may be smaller than |
| 443 | * a long. |
| 444 | * |
| 445 | * So a bit of defensive programming is in order. |
| 446 | * Start with interpreting the value passed |
| 447 | * in as a signed long and see if it works. |
| 448 | */ |
| 449 | |
| 450 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 451 | |
| 452 | if (!overflow) { |
| 453 | uid = (uid_t)result; |
| 454 | |
| 455 | if (result == -1) { |
| 456 | if (PyErr_Occurred()) |
| 457 | goto fail; |
| 458 | /* It's a legitimate -1, we're done. */ |
| 459 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 460 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 461 | |
| 462 | /* Any other negative number is disallowed. */ |
| 463 | if (result < 0) |
| 464 | goto underflow; |
| 465 | |
| 466 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 467 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 468 | (long)uid != result) |
| 469 | goto underflow; |
| 470 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 471 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 472 | |
| 473 | if (overflow < 0) |
| 474 | goto underflow; |
| 475 | |
| 476 | /* |
| 477 | * Okay, the value overflowed a signed long. If it |
| 478 | * fits in an *unsigned* long, it may still be okay, |
| 479 | * as uid_t may be unsigned long on this platform. |
| 480 | */ |
| 481 | uresult = PyLong_AsUnsignedLong(index); |
| 482 | if (PyErr_Occurred()) { |
| 483 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 484 | goto overflow; |
| 485 | goto fail; |
| 486 | } |
| 487 | |
| 488 | uid = (uid_t)uresult; |
| 489 | |
| 490 | /* |
| 491 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, |
| 492 | * but this value would get interpreted as (uid_t)-1 by chown |
| 493 | * and its siblings. That's not what the user meant! So we |
| 494 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 495 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 496 | */ |
| 497 | if (uid == (uid_t)-1) |
| 498 | goto overflow; |
| 499 | |
| 500 | /* Ensure the value wasn't truncated. */ |
| 501 | if (sizeof(uid_t) < sizeof(long) && |
| 502 | (unsigned long)uid != uresult) |
| 503 | goto overflow; |
| 504 | /* fallthrough */ |
| 505 | |
| 506 | success: |
| 507 | Py_DECREF(index); |
| 508 | *(uid_t *)p = uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 509 | return 1; |
| 510 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 511 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 512 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 513 | "uid is less than minimum"); |
| 514 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 515 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 516 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 517 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 518 | "uid is greater than maximum"); |
| 519 | /* fallthrough */ |
| 520 | |
| 521 | fail: |
| 522 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | int |
| 527 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 528 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 529 | gid_t gid; |
| 530 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 531 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 532 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 533 | unsigned long uresult; |
| 534 | |
| 535 | index = PyNumber_Index(obj); |
| 536 | if (index == NULL) { |
| 537 | PyErr_Format(PyExc_TypeError, |
| 538 | "gid should be integer, not %.200s", |
| 539 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 540 | return 0; |
| 541 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 542 | |
| 543 | /* |
| 544 | * Handling gid_t is complicated for two reasons: |
| 545 | * * Although gid_t is (always?) unsigned, it still |
| 546 | * accepts -1. |
| 547 | * * We don't know its size in advance--it may be |
| 548 | * bigger than an int, or it may be smaller than |
| 549 | * a long. |
| 550 | * |
| 551 | * So a bit of defensive programming is in order. |
| 552 | * Start with interpreting the value passed |
| 553 | * in as a signed long and see if it works. |
| 554 | */ |
| 555 | |
| 556 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 557 | |
| 558 | if (!overflow) { |
| 559 | gid = (gid_t)result; |
| 560 | |
| 561 | if (result == -1) { |
| 562 | if (PyErr_Occurred()) |
| 563 | goto fail; |
| 564 | /* It's a legitimate -1, we're done. */ |
| 565 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 566 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 567 | |
| 568 | /* Any other negative number is disallowed. */ |
| 569 | if (result < 0) { |
| 570 | goto underflow; |
| 571 | } |
| 572 | |
| 573 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 574 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 575 | (long)gid != result) |
| 576 | goto underflow; |
| 577 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 578 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 579 | |
| 580 | if (overflow < 0) |
| 581 | goto underflow; |
| 582 | |
| 583 | /* |
| 584 | * Okay, the value overflowed a signed long. If it |
| 585 | * fits in an *unsigned* long, it may still be okay, |
| 586 | * as gid_t may be unsigned long on this platform. |
| 587 | */ |
| 588 | uresult = PyLong_AsUnsignedLong(index); |
| 589 | if (PyErr_Occurred()) { |
| 590 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 591 | goto overflow; |
| 592 | goto fail; |
| 593 | } |
| 594 | |
| 595 | gid = (gid_t)uresult; |
| 596 | |
| 597 | /* |
| 598 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, |
| 599 | * but this value would get interpreted as (gid_t)-1 by chown |
| 600 | * and its siblings. That's not what the user meant! So we |
| 601 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 602 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 603 | */ |
| 604 | if (gid == (gid_t)-1) |
| 605 | goto overflow; |
| 606 | |
| 607 | /* Ensure the value wasn't truncated. */ |
| 608 | if (sizeof(gid_t) < sizeof(long) && |
| 609 | (unsigned long)gid != uresult) |
| 610 | goto overflow; |
| 611 | /* fallthrough */ |
| 612 | |
| 613 | success: |
| 614 | Py_DECREF(index); |
| 615 | *(gid_t *)p = gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 616 | return 1; |
| 617 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 618 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 619 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 620 | "gid is less than minimum"); |
| 621 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 622 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 623 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 624 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 625 | "gid is greater than maximum"); |
| 626 | /* fallthrough */ |
| 627 | |
| 628 | fail: |
| 629 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 630 | return 0; |
| 631 | } |
| 632 | #endif /* MS_WINDOWS */ |
| 633 | |
| 634 | |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 635 | #ifdef HAVE_LONG_LONG |
| 636 | # define _PyLong_FromDev PyLong_FromLongLong |
| 637 | #else |
| 638 | # define _PyLong_FromDev PyLong_FromLong |
| 639 | #endif |
| 640 | |
| 641 | |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 642 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 643 | static int |
| 644 | _Py_Dev_Converter(PyObject *obj, void *p) |
| 645 | { |
| 646 | #ifdef HAVE_LONG_LONG |
| 647 | *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj); |
| 648 | #else |
| 649 | *((dev_t *)p) = PyLong_AsUnsignedLong(obj); |
| 650 | #endif |
| 651 | if (PyErr_Occurred()) |
| 652 | return 0; |
| 653 | return 1; |
| 654 | } |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 655 | #endif /* HAVE_MKNOD && HAVE_MAKEDEV */ |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 656 | |
| 657 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 658 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 659 | /* |
| 660 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 661 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 662 | * which doesn't play nicely with all the initializer lines in this file that |
| 663 | * look like this: |
| 664 | * int dir_fd = DEFAULT_DIR_FD; |
| 665 | */ |
| 666 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 667 | #else |
| 668 | #define DEFAULT_DIR_FD (-100) |
| 669 | #endif |
| 670 | |
| 671 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 672 | _fd_converter(PyObject *o, int *p, const char *allowed) |
| 673 | { |
| 674 | int overflow; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 675 | long long_value; |
| 676 | |
| 677 | PyObject *index = PyNumber_Index(o); |
| 678 | if (index == NULL) { |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 679 | PyErr_Format(PyExc_TypeError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 680 | "argument should be %s, not %.200s", |
| 681 | allowed, Py_TYPE(o)->tp_name); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 682 | return 0; |
| 683 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 684 | |
| 685 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
| 686 | Py_DECREF(index); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 687 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 688 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 689 | "fd is greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 690 | return 0; |
| 691 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 692 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 693 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 694 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 695 | return 0; |
| 696 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 697 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 698 | *p = (int)long_value; |
| 699 | return 1; |
| 700 | } |
| 701 | |
| 702 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 703 | dir_fd_converter(PyObject *o, void *p) |
| 704 | { |
| 705 | if (o == Py_None) { |
| 706 | *(int *)p = DEFAULT_DIR_FD; |
| 707 | return 1; |
| 708 | } |
| 709 | return _fd_converter(o, (int *)p, "integer"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 713 | /* |
| 714 | * A PyArg_ParseTuple "converter" function |
| 715 | * that handles filesystem paths in the manner |
| 716 | * preferred by the os module. |
| 717 | * |
| 718 | * path_converter accepts (Unicode) strings and their |
| 719 | * subclasses, and bytes and their subclasses. What |
| 720 | * it does with the argument depends on the platform: |
| 721 | * |
| 722 | * * On Windows, if we get a (Unicode) string we |
| 723 | * extract the wchar_t * and return it; if we get |
| 724 | * bytes we extract the char * and return that. |
| 725 | * |
| 726 | * * On all other platforms, strings are encoded |
| 727 | * to bytes using PyUnicode_FSConverter, then we |
| 728 | * extract the char * from the bytes object and |
| 729 | * return that. |
| 730 | * |
| 731 | * path_converter also optionally accepts signed |
| 732 | * integers (representing open file descriptors) instead |
| 733 | * of path strings. |
| 734 | * |
| 735 | * Input fields: |
| 736 | * path.nullable |
| 737 | * If nonzero, the path is permitted to be None. |
| 738 | * path.allow_fd |
| 739 | * If nonzero, the path is permitted to be a file handle |
| 740 | * (a signed int) instead of a string. |
| 741 | * path.function_name |
| 742 | * If non-NULL, path_converter will use that as the name |
| 743 | * of the function in error messages. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 744 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 745 | * path.argument_name |
| 746 | * If non-NULL, path_converter will use that as the name |
| 747 | * of the parameter in error messages. |
| 748 | * (If path.argument_name is NULL it uses "path".) |
| 749 | * |
| 750 | * Output fields: |
| 751 | * path.wide |
| 752 | * Points to the path if it was expressed as Unicode |
| 753 | * and was not encoded. (Only used on Windows.) |
| 754 | * path.narrow |
| 755 | * Points to the path if it was expressed as bytes, |
| 756 | * or it was Unicode and was encoded to bytes. |
| 757 | * path.fd |
| 758 | * Contains a file descriptor if path.accept_fd was true |
| 759 | * and the caller provided a signed integer instead of any |
| 760 | * sort of string. |
| 761 | * |
| 762 | * WARNING: if your "path" parameter is optional, and is |
| 763 | * unspecified, path_converter will never get called. |
| 764 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 765 | * yourself! |
| 766 | * path.length |
| 767 | * The length of the path in characters, if specified as |
| 768 | * a string. |
| 769 | * path.object |
| 770 | * The original object passed in. |
| 771 | * path.cleanup |
| 772 | * For internal use only. May point to a temporary object. |
| 773 | * (Pay no attention to the man behind the curtain.) |
| 774 | * |
| 775 | * At most one of path.wide or path.narrow will be non-NULL. |
| 776 | * If path was None and path.nullable was set, |
| 777 | * or if path was an integer and path.allow_fd was set, |
| 778 | * both path.wide and path.narrow will be NULL |
| 779 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 780 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 781 | * path_converter takes care to not write to the path_t |
| 782 | * unless it's successful. However it must reset the |
| 783 | * "cleanup" field each time it's called. |
| 784 | * |
| 785 | * Use as follows: |
| 786 | * path_t path; |
| 787 | * memset(&path, 0, sizeof(path)); |
| 788 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 789 | * // ... use values from path ... |
| 790 | * path_cleanup(&path); |
| 791 | * |
| 792 | * (Note that if PyArg_Parse fails you don't need to call |
| 793 | * path_cleanup(). However it is safe to do so.) |
| 794 | */ |
| 795 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 796 | const char *function_name; |
| 797 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 798 | int nullable; |
| 799 | int allow_fd; |
| 800 | wchar_t *wide; |
| 801 | char *narrow; |
| 802 | int fd; |
| 803 | Py_ssize_t length; |
| 804 | PyObject *object; |
| 805 | PyObject *cleanup; |
| 806 | } path_t; |
| 807 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 808 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 809 | {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL} |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 810 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 811 | static void |
| 812 | path_cleanup(path_t *path) { |
| 813 | if (path->cleanup) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 814 | Py_CLEAR(path->cleanup); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | |
| 818 | static int |
| 819 | path_converter(PyObject *o, void *p) { |
| 820 | path_t *path = (path_t *)p; |
| 821 | PyObject *unicode, *bytes; |
| 822 | Py_ssize_t length; |
| 823 | char *narrow; |
| 824 | |
| 825 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 826 | PyErr_Format(exc, "%s%s" fmt, \ |
| 827 | path->function_name ? path->function_name : "", \ |
| 828 | path->function_name ? ": " : "", \ |
| 829 | path->argument_name ? path->argument_name : "path") |
| 830 | |
| 831 | /* Py_CLEANUP_SUPPORTED support */ |
| 832 | if (o == NULL) { |
| 833 | path_cleanup(path); |
| 834 | return 1; |
| 835 | } |
| 836 | |
| 837 | /* ensure it's always safe to call path_cleanup() */ |
| 838 | path->cleanup = NULL; |
| 839 | |
| 840 | if (o == Py_None) { |
| 841 | if (!path->nullable) { |
| 842 | FORMAT_EXCEPTION(PyExc_TypeError, |
| 843 | "can't specify None for %s argument"); |
| 844 | return 0; |
| 845 | } |
| 846 | path->wide = NULL; |
| 847 | path->narrow = NULL; |
| 848 | path->length = 0; |
| 849 | path->object = o; |
| 850 | path->fd = -1; |
| 851 | return 1; |
| 852 | } |
| 853 | |
| 854 | unicode = PyUnicode_FromObject(o); |
| 855 | if (unicode) { |
| 856 | #ifdef MS_WINDOWS |
| 857 | wchar_t *wide; |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 858 | |
| 859 | wide = PyUnicode_AsUnicodeAndSize(unicode, &length); |
| 860 | if (!wide) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 861 | Py_DECREF(unicode); |
| 862 | return 0; |
| 863 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 864 | if (length > 32767) { |
| 865 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 866 | Py_DECREF(unicode); |
| 867 | return 0; |
| 868 | } |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 869 | if (wcslen(wide) != length) { |
Serhiy Storchaka | 7e9d1d1 | 2015-04-20 10:12:28 +0300 | [diff] [blame] | 870 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character"); |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 871 | Py_DECREF(unicode); |
| 872 | return 0; |
| 873 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 874 | |
| 875 | path->wide = wide; |
| 876 | path->narrow = NULL; |
| 877 | path->length = length; |
| 878 | path->object = o; |
| 879 | path->fd = -1; |
| 880 | path->cleanup = unicode; |
| 881 | return Py_CLEANUP_SUPPORTED; |
| 882 | #else |
| 883 | int converted = PyUnicode_FSConverter(unicode, &bytes); |
| 884 | Py_DECREF(unicode); |
| 885 | if (!converted) |
| 886 | bytes = NULL; |
| 887 | #endif |
| 888 | } |
| 889 | else { |
| 890 | PyErr_Clear(); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 891 | if (PyObject_CheckBuffer(o)) |
| 892 | bytes = PyBytes_FromObject(o); |
| 893 | else |
| 894 | bytes = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 895 | if (!bytes) { |
| 896 | PyErr_Clear(); |
| 897 | if (path->allow_fd) { |
| 898 | int fd; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 899 | int result = _fd_converter(o, &fd, |
| 900 | "string, bytes or integer"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 901 | if (result) { |
| 902 | path->wide = NULL; |
| 903 | path->narrow = NULL; |
| 904 | path->length = 0; |
| 905 | path->object = o; |
| 906 | path->fd = fd; |
| 907 | return result; |
| 908 | } |
| 909 | } |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | if (!bytes) { |
| 914 | if (!PyErr_Occurred()) |
| 915 | FORMAT_EXCEPTION(PyExc_TypeError, "illegal type for %s parameter"); |
| 916 | return 0; |
| 917 | } |
| 918 | |
| 919 | #ifdef MS_WINDOWS |
| 920 | if (win32_warn_bytes_api()) { |
| 921 | Py_DECREF(bytes); |
| 922 | return 0; |
| 923 | } |
| 924 | #endif |
| 925 | |
| 926 | length = PyBytes_GET_SIZE(bytes); |
| 927 | #ifdef MS_WINDOWS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 928 | if (length > MAX_PATH-1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 929 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
| 930 | Py_DECREF(bytes); |
| 931 | return 0; |
| 932 | } |
| 933 | #endif |
| 934 | |
| 935 | narrow = PyBytes_AS_STRING(bytes); |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 936 | if ((size_t)length != strlen(narrow)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 937 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 938 | Py_DECREF(bytes); |
| 939 | return 0; |
| 940 | } |
| 941 | |
| 942 | path->wide = NULL; |
| 943 | path->narrow = narrow; |
| 944 | path->length = length; |
| 945 | path->object = o; |
| 946 | path->fd = -1; |
| 947 | path->cleanup = bytes; |
| 948 | return Py_CLEANUP_SUPPORTED; |
| 949 | } |
| 950 | |
| 951 | static void |
| 952 | argument_unavailable_error(char *function_name, char *argument_name) { |
| 953 | PyErr_Format(PyExc_NotImplementedError, |
| 954 | "%s%s%s unavailable on this platform", |
| 955 | (function_name != NULL) ? function_name : "", |
| 956 | (function_name != NULL) ? ": ": "", |
| 957 | argument_name); |
| 958 | } |
| 959 | |
| 960 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 961 | dir_fd_unavailable(PyObject *o, void *p) |
| 962 | { |
| 963 | int dir_fd; |
| 964 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 965 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 966 | if (dir_fd != DEFAULT_DIR_FD) { |
| 967 | argument_unavailable_error(NULL, "dir_fd"); |
| 968 | return 0; |
| 969 | } |
| 970 | *(int *)p = dir_fd; |
| 971 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | static int |
| 975 | fd_specified(char *function_name, int fd) { |
| 976 | if (fd == -1) |
| 977 | return 0; |
| 978 | |
| 979 | argument_unavailable_error(function_name, "fd"); |
| 980 | return 1; |
| 981 | } |
| 982 | |
| 983 | static int |
| 984 | follow_symlinks_specified(char *function_name, int follow_symlinks) { |
| 985 | if (follow_symlinks) |
| 986 | return 0; |
| 987 | |
| 988 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 989 | return 1; |
| 990 | } |
| 991 | |
| 992 | static int |
| 993 | path_and_dir_fd_invalid(char *function_name, path_t *path, int dir_fd) { |
| 994 | if (!path->narrow && !path->wide && (dir_fd != DEFAULT_DIR_FD)) { |
| 995 | PyErr_Format(PyExc_ValueError, |
| 996 | "%s: can't specify dir_fd without matching path", |
| 997 | function_name); |
| 998 | return 1; |
| 999 | } |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | static int |
| 1004 | dir_fd_and_fd_invalid(char *function_name, int dir_fd, int fd) { |
| 1005 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 1006 | PyErr_Format(PyExc_ValueError, |
| 1007 | "%s: can't specify both dir_fd and fd", |
| 1008 | function_name); |
| 1009 | return 1; |
| 1010 | } |
| 1011 | return 0; |
| 1012 | } |
| 1013 | |
| 1014 | static int |
| 1015 | fd_and_follow_symlinks_invalid(char *function_name, int fd, |
| 1016 | int follow_symlinks) { |
| 1017 | if ((fd > 0) && (!follow_symlinks)) { |
| 1018 | PyErr_Format(PyExc_ValueError, |
| 1019 | "%s: cannot use fd and follow_symlinks together", |
| 1020 | function_name); |
| 1021 | return 1; |
| 1022 | } |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
| 1026 | static int |
| 1027 | dir_fd_and_follow_symlinks_invalid(char *function_name, int dir_fd, |
| 1028 | int follow_symlinks) { |
| 1029 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 1030 | PyErr_Format(PyExc_ValueError, |
| 1031 | "%s: cannot use dir_fd and follow_symlinks together", |
| 1032 | function_name); |
| 1033 | return 1; |
| 1034 | } |
| 1035 | return 0; |
| 1036 | } |
| 1037 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1038 | #ifdef MS_WINDOWS |
| 1039 | typedef PY_LONG_LONG Py_off_t; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1040 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1041 | typedef off_t Py_off_t; |
| 1042 | #endif |
| 1043 | |
| 1044 | static int |
| 1045 | Py_off_t_converter(PyObject *arg, void *addr) |
| 1046 | { |
| 1047 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1048 | *((Py_off_t *)addr) = PyLong_AsLongLong(arg); |
| 1049 | #else |
| 1050 | *((Py_off_t *)addr) = PyLong_AsLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1051 | #endif |
| 1052 | if (PyErr_Occurred()) |
| 1053 | return 0; |
| 1054 | return 1; |
| 1055 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1056 | |
| 1057 | static PyObject * |
| 1058 | PyLong_FromPy_off_t(Py_off_t offset) |
| 1059 | { |
| 1060 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1061 | return PyLong_FromLongLong(offset); |
| 1062 | #else |
| 1063 | return PyLong_FromLong(offset); |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1064 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1065 | } |
| 1066 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1067 | |
Steve Dower | d81431f | 2015-03-06 14:47:02 -0800 | [diff] [blame] | 1068 | #if defined _MSC_VER && _MSC_VER >= 1400 && _MSC_VER < 1900 |
| 1069 | /* Legacy implementation of _PyVerify_fd_dup2 while transitioning to |
| 1070 | * MSVC 14.0. This should eventually be removed. (issue23524) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1071 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1072 | #define IOINFO_L2E 5 |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1073 | #define IOINFO_ARRAYS 64 |
Steve Dower | 65e4cb1 | 2014-11-22 12:54:57 -0800 | [diff] [blame] | 1074 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1075 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1076 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 1077 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1078 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 1079 | static int |
| 1080 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 1081 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1082 | if (!_PyVerify_fd(fd1)) |
| 1083 | return 0; |
| 1084 | if (fd2 == _NO_CONSOLE_FILENO) |
| 1085 | return 0; |
| 1086 | if ((unsigned)fd2 < _NHANDLE_) |
| 1087 | return 1; |
| 1088 | else |
| 1089 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1090 | } |
| 1091 | #else |
Steve Dower | d81431f | 2015-03-06 14:47:02 -0800 | [diff] [blame] | 1092 | #define _PyVerify_fd_dup2(fd1, fd2) (_PyVerify_fd(fd1) && (fd2) >= 0) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1093 | #endif |
| 1094 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1095 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1096 | |
| 1097 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1098 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1099 | { |
| 1100 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1101 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 1102 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1103 | |
| 1104 | if (0 == DeviceIoControl( |
| 1105 | reparse_point_handle, |
| 1106 | FSCTL_GET_REPARSE_POINT, |
| 1107 | NULL, 0, /* in buffer */ |
| 1108 | target_buffer, sizeof(target_buffer), |
| 1109 | &n_bytes_returned, |
| 1110 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1111 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1112 | |
| 1113 | if (reparse_tag) |
| 1114 | *reparse_tag = rdb->ReparseTag; |
| 1115 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1116 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1117 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1118 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1119 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1120 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1121 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1122 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1123 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1124 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1125 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1126 | */ |
| 1127 | #include <crt_externs.h> |
| 1128 | static char **environ; |
| 1129 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1130 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1131 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1132 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1133 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1134 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1135 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1136 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1137 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1138 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1139 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1140 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1141 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1142 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1143 | d = PyDict_New(); |
| 1144 | if (d == NULL) |
| 1145 | return NULL; |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1146 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1147 | if (environ == NULL) |
| 1148 | environ = *_NSGetEnviron(); |
| 1149 | #endif |
| 1150 | #ifdef MS_WINDOWS |
| 1151 | /* _wenviron must be initialized in this way if the program is started |
| 1152 | through main() instead of wmain(). */ |
| 1153 | _wgetenv(L""); |
| 1154 | if (_wenviron == NULL) |
| 1155 | return d; |
| 1156 | /* This part ignores errors */ |
| 1157 | for (e = _wenviron; *e != NULL; e++) { |
| 1158 | PyObject *k; |
| 1159 | PyObject *v; |
| 1160 | wchar_t *p = wcschr(*e, L'='); |
| 1161 | if (p == NULL) |
| 1162 | continue; |
| 1163 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1164 | if (k == NULL) { |
| 1165 | PyErr_Clear(); |
| 1166 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1167 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1168 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1169 | if (v == NULL) { |
| 1170 | PyErr_Clear(); |
| 1171 | Py_DECREF(k); |
| 1172 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1173 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1174 | if (PyDict_GetItem(d, k) == NULL) { |
| 1175 | if (PyDict_SetItem(d, k, v) != 0) |
| 1176 | PyErr_Clear(); |
| 1177 | } |
| 1178 | Py_DECREF(k); |
| 1179 | Py_DECREF(v); |
| 1180 | } |
| 1181 | #else |
| 1182 | if (environ == NULL) |
| 1183 | return d; |
| 1184 | /* This part ignores errors */ |
| 1185 | for (e = environ; *e != NULL; e++) { |
| 1186 | PyObject *k; |
| 1187 | PyObject *v; |
| 1188 | char *p = strchr(*e, '='); |
| 1189 | if (p == NULL) |
| 1190 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1191 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1192 | if (k == NULL) { |
| 1193 | PyErr_Clear(); |
| 1194 | continue; |
| 1195 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1196 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1197 | if (v == NULL) { |
| 1198 | PyErr_Clear(); |
| 1199 | Py_DECREF(k); |
| 1200 | continue; |
| 1201 | } |
| 1202 | if (PyDict_GetItem(d, k) == NULL) { |
| 1203 | if (PyDict_SetItem(d, k, v) != 0) |
| 1204 | PyErr_Clear(); |
| 1205 | } |
| 1206 | Py_DECREF(k); |
| 1207 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1208 | } |
| 1209 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1210 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1213 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1214 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1215 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1216 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1217 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1218 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1219 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1220 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1221 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1222 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1223 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1224 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1225 | /* XXX We should pass the function name along in the future. |
| 1226 | (winreg.c also wants to pass the function name.) |
| 1227 | This would however require an additional param to the |
| 1228 | Windows error object, which is non-trivial. |
| 1229 | */ |
| 1230 | errno = GetLastError(); |
| 1231 | if (filename) |
| 1232 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1233 | else |
| 1234 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1235 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1236 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1237 | static PyObject * |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1238 | win32_error_object(char* function, PyObject* filename) |
| 1239 | { |
| 1240 | /* XXX - see win32_error for comments on 'function' */ |
| 1241 | errno = GetLastError(); |
| 1242 | if (filename) |
| 1243 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1244 | PyExc_OSError, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1245 | errno, |
| 1246 | filename); |
| 1247 | else |
| 1248 | return PyErr_SetFromWindowsErr(errno); |
| 1249 | } |
| 1250 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1251 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1252 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1253 | static PyObject * |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1254 | path_error(path_t *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1255 | { |
| 1256 | #ifdef MS_WINDOWS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1257 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 1258 | 0, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1259 | #else |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1260 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1261 | #endif |
| 1262 | } |
| 1263 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1264 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1265 | static PyObject * |
| 1266 | path_error2(path_t *path, path_t *path2) |
| 1267 | { |
| 1268 | #ifdef MS_WINDOWS |
| 1269 | return PyErr_SetExcFromWindowsErrWithFilenameObjects(PyExc_OSError, |
| 1270 | 0, path->object, path2->object); |
| 1271 | #else |
| 1272 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, |
| 1273 | path->object, path2->object); |
| 1274 | #endif |
| 1275 | } |
| 1276 | |
| 1277 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1278 | /* POSIX generic methods */ |
| 1279 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1280 | static int |
| 1281 | fildes_converter(PyObject *o, void *p) |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1282 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1283 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1284 | int *pointer = (int *)p; |
| 1285 | fd = PyObject_AsFileDescriptor(o); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1286 | if (fd < 0) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1287 | return 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1288 | *pointer = fd; |
| 1289 | return 1; |
| 1290 | } |
| 1291 | |
| 1292 | static PyObject * |
| 1293 | posix_fildes_fd(int fd, int (*func)(int)) |
| 1294 | { |
| 1295 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1296 | int async_err = 0; |
| 1297 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1298 | if (!_PyVerify_fd(fd)) |
| 1299 | return posix_error(); |
| 1300 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1301 | do { |
| 1302 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1303 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1304 | res = (*func)(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1305 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1306 | Py_END_ALLOW_THREADS |
| 1307 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 1308 | if (res != 0) |
| 1309 | return (!async_err) ? posix_error() : NULL; |
| 1310 | Py_RETURN_NONE; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1311 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1312 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1313 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1314 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1315 | /* This is a reimplementation of the C library's chdir function, |
| 1316 | but one that produces Win32 errors instead of DOS error codes. |
| 1317 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1318 | it also needs to set "magic" environment variables indicating |
| 1319 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1320 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1321 | win32_chdir(LPCSTR path) |
| 1322 | { |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1323 | char new_path[MAX_PATH]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1324 | int result; |
| 1325 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1326 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1327 | if(!SetCurrentDirectoryA(path)) |
| 1328 | return FALSE; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1329 | result = GetCurrentDirectoryA(Py_ARRAY_LENGTH(new_path), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1330 | if (!result) |
| 1331 | return FALSE; |
| 1332 | /* In the ANSI API, there should not be any paths longer |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1333 | than MAX_PATH-1 (not including the final null character). */ |
| 1334 | assert(result < Py_ARRAY_LENGTH(new_path)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1335 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 1336 | strncmp(new_path, "//", 2) == 0) |
| 1337 | /* UNC path, nothing to do. */ |
| 1338 | return TRUE; |
| 1339 | env[1] = new_path[0]; |
| 1340 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | /* The Unicode version differs from the ANSI version |
| 1344 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1345 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1346 | win32_wchdir(LPCWSTR path) |
| 1347 | { |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1348 | wchar_t path_buf[MAX_PATH], *new_path = path_buf; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1349 | int result; |
| 1350 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1351 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1352 | if(!SetCurrentDirectoryW(path)) |
| 1353 | return FALSE; |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1354 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1355 | if (!result) |
| 1356 | return FALSE; |
Victor Stinner | e847d71 | 2015-12-14 00:21:50 +0100 | [diff] [blame] | 1357 | if (result > Py_ARRAY_LENGTH(path_buf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1358 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1359 | if (!new_path) { |
| 1360 | SetLastError(ERROR_OUTOFMEMORY); |
| 1361 | return FALSE; |
| 1362 | } |
| 1363 | result = GetCurrentDirectoryW(result, new_path); |
| 1364 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1365 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1366 | return FALSE; |
| 1367 | } |
| 1368 | } |
| 1369 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1370 | wcsncmp(new_path, L"//", 2) == 0) |
| 1371 | /* UNC path, nothing to do. */ |
| 1372 | return TRUE; |
| 1373 | env[1] = new_path[0]; |
| 1374 | result = SetEnvironmentVariableW(env, new_path); |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1375 | if (new_path != path_buf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1376 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1377 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1378 | } |
| 1379 | #endif |
| 1380 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1381 | #ifdef MS_WINDOWS |
| 1382 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1383 | - time stamps are restricted to second resolution |
| 1384 | - file modification times suffer from forth-and-back conversions between |
| 1385 | UTC and local time |
| 1386 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1387 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1388 | #define HAVE_STAT_NSEC 1 |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1389 | #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1390 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1391 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1392 | attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1393 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1394 | HANDLE hFindFile; |
| 1395 | WIN32_FIND_DATAA FileData; |
| 1396 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1397 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1398 | return FALSE; |
| 1399 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1400 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1401 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1402 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1403 | info->ftCreationTime = FileData.ftCreationTime; |
| 1404 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1405 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1406 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1407 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1408 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1409 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1410 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1411 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1414 | static void |
| 1415 | find_data_to_file_info_w(WIN32_FIND_DATAW *pFileData, |
| 1416 | BY_HANDLE_FILE_INFORMATION *info, |
| 1417 | ULONG *reparse_tag) |
| 1418 | { |
| 1419 | memset(info, 0, sizeof(*info)); |
| 1420 | info->dwFileAttributes = pFileData->dwFileAttributes; |
| 1421 | info->ftCreationTime = pFileData->ftCreationTime; |
| 1422 | info->ftLastAccessTime = pFileData->ftLastAccessTime; |
| 1423 | info->ftLastWriteTime = pFileData->ftLastWriteTime; |
| 1424 | info->nFileSizeHigh = pFileData->nFileSizeHigh; |
| 1425 | info->nFileSizeLow = pFileData->nFileSizeLow; |
| 1426 | /* info->nNumberOfLinks = 1; */ |
| 1427 | if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1428 | *reparse_tag = pFileData->dwReserved0; |
| 1429 | else |
| 1430 | *reparse_tag = 0; |
| 1431 | } |
| 1432 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1433 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1434 | attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1435 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1436 | HANDLE hFindFile; |
| 1437 | WIN32_FIND_DATAW FileData; |
| 1438 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1439 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1440 | return FALSE; |
| 1441 | FindClose(hFindFile); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1442 | find_data_to_file_info_w(&FileData, info, reparse_tag); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1443 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1446 | static BOOL |
| 1447 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1448 | { |
| 1449 | int buf_size, result_length; |
| 1450 | wchar_t *buf; |
| 1451 | |
| 1452 | /* We have a good handle to the target, use it to determine |
| 1453 | the target path name (then we'll call lstat on it). */ |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1454 | buf_size = GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1455 | VOLUME_NAME_DOS); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1456 | if(!buf_size) |
| 1457 | return FALSE; |
| 1458 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 1459 | buf = PyMem_New(wchar_t, buf_size+1); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1460 | if (!buf) { |
| 1461 | SetLastError(ERROR_OUTOFMEMORY); |
| 1462 | return FALSE; |
| 1463 | } |
| 1464 | |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1465 | result_length = GetFinalPathNameByHandleW(hdl, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1466 | buf, buf_size, VOLUME_NAME_DOS); |
| 1467 | |
| 1468 | if(!result_length) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1469 | PyMem_Free(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1470 | return FALSE; |
| 1471 | } |
| 1472 | |
| 1473 | if(!CloseHandle(hdl)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1474 | PyMem_Free(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1475 | return FALSE; |
| 1476 | } |
| 1477 | |
| 1478 | buf[result_length] = 0; |
| 1479 | |
| 1480 | *target_path = buf; |
| 1481 | return TRUE; |
| 1482 | } |
| 1483 | |
| 1484 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1485 | win32_xstat_impl_w(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1486 | BOOL traverse); |
| 1487 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1488 | win32_xstat_impl(const char *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1489 | BOOL traverse) |
| 1490 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1491 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1492 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1493 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1494 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1495 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1496 | const char *dot; |
| 1497 | |
| 1498 | hFile = CreateFileA( |
| 1499 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1500 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1501 | 0, /* share mode */ |
| 1502 | NULL, /* security attributes */ |
| 1503 | OPEN_EXISTING, |
| 1504 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1505 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1506 | Because of this, calls like GetFinalPathNameByHandle will return |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 1507 | the symlink path again and not the actual final path. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1508 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1509 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1510 | NULL); |
| 1511 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1512 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1513 | /* Either the target doesn't exist, or we don't have access to |
| 1514 | get a handle to it. If the former, we need to return an error. |
| 1515 | If the latter, we can use attributes_from_dir. */ |
| 1516 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1517 | return -1; |
| 1518 | /* Could not get attributes on open file. Fall back to |
| 1519 | reading the directory. */ |
| 1520 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1521 | /* Very strange. This should not fail now */ |
| 1522 | return -1; |
| 1523 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1524 | if (traverse) { |
| 1525 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1526 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1527 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1528 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1529 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1530 | } else { |
| 1531 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1532 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1533 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1534 | } |
| 1535 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1536 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1537 | return -1; |
| 1538 | |
| 1539 | /* Close the outer open file handle now that we're about to |
| 1540 | reopen it with different flags. */ |
| 1541 | if (!CloseHandle(hFile)) |
| 1542 | return -1; |
| 1543 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1544 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1545 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1546 | the file without the reparse handling flag set. */ |
| 1547 | hFile2 = CreateFileA( |
| 1548 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1549 | NULL, OPEN_EXISTING, |
| 1550 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1551 | NULL); |
| 1552 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1553 | return -1; |
| 1554 | |
| 1555 | if (!get_target_path(hFile2, &target_path)) |
| 1556 | return -1; |
| 1557 | |
| 1558 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1559 | PyMem_Free(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1560 | return code; |
| 1561 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1562 | } else |
| 1563 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1564 | } |
Steve Dower | a2af1a5 | 2015-02-21 10:04:10 -0800 | [diff] [blame] | 1565 | _Py_attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1566 | |
| 1567 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1568 | dot = strrchr(path, '.'); |
| 1569 | if (dot) { |
| 1570 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1571 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1572 | result->st_mode |= 0111; |
| 1573 | } |
| 1574 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1578 | win32_xstat_impl_w(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1579 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1580 | { |
| 1581 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1582 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1583 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1584 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1585 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1586 | const wchar_t *dot; |
| 1587 | |
| 1588 | hFile = CreateFileW( |
| 1589 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1590 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1591 | 0, /* share mode */ |
| 1592 | NULL, /* security attributes */ |
| 1593 | OPEN_EXISTING, |
| 1594 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1595 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1596 | Because of this, calls like GetFinalPathNameByHandle will return |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 1597 | the symlink path again and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1598 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1599 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1600 | NULL); |
| 1601 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1602 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1603 | /* Either the target doesn't exist, or we don't have access to |
| 1604 | get a handle to it. If the former, we need to return an error. |
| 1605 | If the latter, we can use attributes_from_dir. */ |
| 1606 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1607 | return -1; |
| 1608 | /* Could not get attributes on open file. Fall back to |
| 1609 | reading the directory. */ |
| 1610 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1611 | /* Very strange. This should not fail now */ |
| 1612 | return -1; |
| 1613 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1614 | if (traverse) { |
| 1615 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1616 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1617 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1618 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1619 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1620 | } else { |
| 1621 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1622 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1623 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1624 | } |
| 1625 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1626 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1627 | return -1; |
| 1628 | |
| 1629 | /* Close the outer open file handle now that we're about to |
| 1630 | reopen it with different flags. */ |
| 1631 | if (!CloseHandle(hFile)) |
| 1632 | return -1; |
| 1633 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1634 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1635 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1636 | the file without the reparse handling flag set. */ |
| 1637 | hFile2 = CreateFileW( |
| 1638 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1639 | NULL, OPEN_EXISTING, |
| 1640 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1641 | NULL); |
| 1642 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1643 | return -1; |
| 1644 | |
| 1645 | if (!get_target_path(hFile2, &target_path)) |
| 1646 | return -1; |
| 1647 | |
| 1648 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1649 | PyMem_Free(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1650 | return code; |
| 1651 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1652 | } else |
| 1653 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1654 | } |
Steve Dower | a2af1a5 | 2015-02-21 10:04:10 -0800 | [diff] [blame] | 1655 | _Py_attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1656 | |
| 1657 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1658 | dot = wcsrchr(path, '.'); |
| 1659 | if (dot) { |
| 1660 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1661 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1662 | result->st_mode |= 0111; |
| 1663 | } |
| 1664 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1668 | win32_xstat(const char *path, struct _Py_stat_struct *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1669 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1670 | /* Protocol violation: we explicitly clear errno, instead of |
| 1671 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1672 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1673 | errno = 0; |
| 1674 | return code; |
| 1675 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1676 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1677 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1678 | win32_xstat_w(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1679 | { |
| 1680 | /* Protocol violation: we explicitly clear errno, instead of |
| 1681 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1682 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1683 | errno = 0; |
| 1684 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1685 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1686 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1687 | |
| 1688 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1689 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1690 | default does not traverse symlinks and instead returns attributes for |
| 1691 | the symlink. |
| 1692 | |
| 1693 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1694 | win32_stat will first explicitly resolve the symlink target and then will |
| 1695 | call win32_lstat on that result. |
| 1696 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1697 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1698 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1699 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1700 | win32_lstat(const char* path, struct _Py_stat_struct *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1701 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1702 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1705 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1706 | win32_lstat_w(const wchar_t* path, struct _Py_stat_struct *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1707 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1708 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1712 | win32_stat(const char* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1713 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1714 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1717 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1718 | win32_stat_w(const wchar_t* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1719 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1720 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1723 | #endif /* MS_WINDOWS */ |
| 1724 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1725 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1726 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1727 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1728 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1729 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1730 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1731 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1732 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1733 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1734 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1735 | |
| 1736 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1737 | {"st_mode", "protection bits"}, |
| 1738 | {"st_ino", "inode"}, |
| 1739 | {"st_dev", "device"}, |
| 1740 | {"st_nlink", "number of hard links"}, |
| 1741 | {"st_uid", "user ID of owner"}, |
| 1742 | {"st_gid", "group ID of owner"}, |
| 1743 | {"st_size", "total size, in bytes"}, |
| 1744 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1745 | {NULL, "integer time of last access"}, |
| 1746 | {NULL, "integer time of last modification"}, |
| 1747 | {NULL, "integer time of last change"}, |
| 1748 | {"st_atime", "time of last access"}, |
| 1749 | {"st_mtime", "time of last modification"}, |
| 1750 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1751 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1752 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1753 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1754 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1755 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1756 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1757 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1758 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1759 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1760 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1761 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1762 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1763 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1764 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1765 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1766 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1767 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1768 | #endif |
| 1769 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1770 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1771 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1772 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1773 | {"st_file_attributes", "Windows file attribute bits"}, |
| 1774 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1775 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1776 | }; |
| 1777 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1778 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1779 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1780 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1781 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1782 | #endif |
| 1783 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1784 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1785 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1786 | #else |
| 1787 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1788 | #endif |
| 1789 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1790 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1791 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1792 | #else |
| 1793 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1794 | #endif |
| 1795 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1796 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1797 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1798 | #else |
| 1799 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1800 | #endif |
| 1801 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1802 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1803 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1804 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1805 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1806 | #endif |
| 1807 | |
| 1808 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1809 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1810 | #else |
| 1811 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1812 | #endif |
| 1813 | |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1814 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1815 | #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1) |
| 1816 | #else |
| 1817 | #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX |
| 1818 | #endif |
| 1819 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1820 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1821 | "stat_result", /* name */ |
| 1822 | stat_result__doc__, /* doc */ |
| 1823 | stat_result_fields, |
| 1824 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1825 | }; |
| 1826 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1827 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1828 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1829 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1830 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1831 | or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1832 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1833 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1834 | |
| 1835 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1836 | {"f_bsize", }, |
| 1837 | {"f_frsize", }, |
| 1838 | {"f_blocks", }, |
| 1839 | {"f_bfree", }, |
| 1840 | {"f_bavail", }, |
| 1841 | {"f_files", }, |
| 1842 | {"f_ffree", }, |
| 1843 | {"f_favail", }, |
| 1844 | {"f_flag", }, |
| 1845 | {"f_namemax",}, |
| 1846 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1847 | }; |
| 1848 | |
| 1849 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1850 | "statvfs_result", /* name */ |
| 1851 | statvfs_result__doc__, /* doc */ |
| 1852 | statvfs_result_fields, |
| 1853 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1854 | }; |
| 1855 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1856 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1857 | PyDoc_STRVAR(waitid_result__doc__, |
| 1858 | "waitid_result: Result from waitid.\n\n\ |
| 1859 | This object may be accessed either as a tuple of\n\ |
| 1860 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1861 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1862 | \n\ |
| 1863 | See os.waitid for more information."); |
| 1864 | |
| 1865 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1866 | {"si_pid", }, |
| 1867 | {"si_uid", }, |
| 1868 | {"si_signo", }, |
| 1869 | {"si_status", }, |
| 1870 | {"si_code", }, |
| 1871 | {0} |
| 1872 | }; |
| 1873 | |
| 1874 | static PyStructSequence_Desc waitid_result_desc = { |
| 1875 | "waitid_result", /* name */ |
| 1876 | waitid_result__doc__, /* doc */ |
| 1877 | waitid_result_fields, |
| 1878 | 5 |
| 1879 | }; |
| 1880 | static PyTypeObject WaitidResultType; |
| 1881 | #endif |
| 1882 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1883 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1884 | static PyTypeObject StatResultType; |
| 1885 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1886 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 1887 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1888 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1889 | static newfunc structseq_new; |
| 1890 | |
| 1891 | static PyObject * |
| 1892 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1893 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1894 | PyStructSequence *result; |
| 1895 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1896 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1897 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1898 | if (!result) |
| 1899 | return NULL; |
| 1900 | /* If we have been initialized from a tuple, |
| 1901 | st_?time might be set to None. Initialize it |
| 1902 | from the int slots. */ |
| 1903 | for (i = 7; i <= 9; i++) { |
| 1904 | if (result->ob_item[i+3] == Py_None) { |
| 1905 | Py_DECREF(Py_None); |
| 1906 | Py_INCREF(result->ob_item[i]); |
| 1907 | result->ob_item[i+3] = result->ob_item[i]; |
| 1908 | } |
| 1909 | } |
| 1910 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | |
| 1914 | |
| 1915 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1916 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1917 | |
| 1918 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1919 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1920 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1921 | \n\ |
| 1922 | If value is True, future calls to stat() return floats; if it is False,\n\ |
| 1923 | future calls return ints.\n\ |
| 1924 | If value is omitted, return the current setting.\n"); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1925 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1926 | /* AC 3.5: the public default value should be None, not ready for that yet */ |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1927 | static PyObject* |
| 1928 | stat_float_times(PyObject* self, PyObject *args) |
| 1929 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1930 | int newval = -1; |
| 1931 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1932 | return NULL; |
Victor Stinner | 034d0aa | 2012-06-05 01:22:15 +0200 | [diff] [blame] | 1933 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 1934 | "stat_float_times() is deprecated", |
| 1935 | 1)) |
| 1936 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1937 | if (newval == -1) |
| 1938 | /* Return old value */ |
| 1939 | return PyBool_FromLong(_stat_float_times); |
| 1940 | _stat_float_times = newval; |
| 1941 | Py_INCREF(Py_None); |
| 1942 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1943 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1944 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1945 | static PyObject *billion = NULL; |
| 1946 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1947 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1948 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1949 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1950 | PyObject *s = _PyLong_FromTime_t(sec); |
| 1951 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 1952 | PyObject *s_in_ns = NULL; |
| 1953 | PyObject *ns_total = NULL; |
| 1954 | PyObject *float_s = NULL; |
| 1955 | |
| 1956 | if (!(s && ns_fractional)) |
| 1957 | goto exit; |
| 1958 | |
| 1959 | s_in_ns = PyNumber_Multiply(s, billion); |
| 1960 | if (!s_in_ns) |
| 1961 | goto exit; |
| 1962 | |
| 1963 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 1964 | if (!ns_total) |
| 1965 | goto exit; |
| 1966 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1967 | if (_stat_float_times) { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1968 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1969 | if (!float_s) |
| 1970 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1971 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1972 | else { |
| 1973 | float_s = s; |
| 1974 | Py_INCREF(float_s); |
| 1975 | } |
| 1976 | |
| 1977 | PyStructSequence_SET_ITEM(v, index, s); |
| 1978 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 1979 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 1980 | s = NULL; |
| 1981 | float_s = NULL; |
| 1982 | ns_total = NULL; |
| 1983 | exit: |
| 1984 | Py_XDECREF(s); |
| 1985 | Py_XDECREF(ns_fractional); |
| 1986 | Py_XDECREF(s_in_ns); |
| 1987 | Py_XDECREF(ns_total); |
| 1988 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1991 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1992 | (used by posix_stat() and posix_fstat()) */ |
| 1993 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1994 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1995 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1996 | unsigned long ansec, mnsec, cnsec; |
| 1997 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 1998 | if (v == NULL) |
| 1999 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2000 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2001 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2002 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2003 | PyStructSequence_SET_ITEM(v, 1, |
| 2004 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2005 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2006 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2007 | #endif |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2008 | #ifdef MS_WINDOWS |
| 2009 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2010 | #else |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 2011 | PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2012 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2013 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2014 | #if defined(MS_WINDOWS) |
| 2015 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2016 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2017 | #else |
| 2018 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2019 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2020 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2021 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2022 | PyStructSequence_SET_ITEM(v, 6, |
| 2023 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2024 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2025 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2026 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2027 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2028 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2029 | ansec = st->st_atim.tv_nsec; |
| 2030 | mnsec = st->st_mtim.tv_nsec; |
| 2031 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2032 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2033 | ansec = st->st_atimespec.tv_nsec; |
| 2034 | mnsec = st->st_mtimespec.tv_nsec; |
| 2035 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2036 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2037 | ansec = st->st_atime_nsec; |
| 2038 | mnsec = st->st_mtime_nsec; |
| 2039 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2040 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2041 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2042 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2043 | fill_time(v, 7, st->st_atime, ansec); |
| 2044 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2045 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2046 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2047 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2048 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2049 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2050 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2051 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2052 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2053 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2054 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2055 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2056 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2057 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2058 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2059 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2060 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2061 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2062 | #endif |
| 2063 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2064 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2065 | PyObject *val; |
| 2066 | unsigned long bsec,bnsec; |
| 2067 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2068 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2069 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2070 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2071 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2072 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2073 | if (_stat_float_times) { |
| 2074 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 2075 | } else { |
| 2076 | val = PyLong_FromLong((long)bsec); |
| 2077 | } |
| 2078 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2079 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2080 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2081 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2082 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2083 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2084 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2085 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2086 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2087 | PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX, |
| 2088 | PyLong_FromUnsignedLong(st->st_file_attributes)); |
| 2089 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2090 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2091 | if (PyErr_Occurred()) { |
| 2092 | Py_DECREF(v); |
| 2093 | return NULL; |
| 2094 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2095 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2096 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2099 | /* POSIX methods */ |
| 2100 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2101 | |
| 2102 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2103 | posix_do_stat(char *function_name, path_t *path, |
| 2104 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2105 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2106 | STRUCT_STAT st; |
| 2107 | int result; |
| 2108 | |
| 2109 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2110 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2111 | return NULL; |
| 2112 | #endif |
| 2113 | |
| 2114 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2115 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2116 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2117 | return NULL; |
| 2118 | |
| 2119 | Py_BEGIN_ALLOW_THREADS |
| 2120 | if (path->fd != -1) |
| 2121 | result = FSTAT(path->fd, &st); |
| 2122 | else |
| 2123 | #ifdef MS_WINDOWS |
| 2124 | if (path->wide) { |
| 2125 | if (follow_symlinks) |
| 2126 | result = win32_stat_w(path->wide, &st); |
| 2127 | else |
| 2128 | result = win32_lstat_w(path->wide, &st); |
| 2129 | } |
| 2130 | else |
| 2131 | #endif |
| 2132 | #if defined(HAVE_LSTAT) || defined(MS_WINDOWS) |
| 2133 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2134 | result = LSTAT(path->narrow, &st); |
| 2135 | else |
| 2136 | #endif |
| 2137 | #ifdef HAVE_FSTATAT |
| 2138 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2139 | result = fstatat(dir_fd, path->narrow, &st, |
| 2140 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2141 | else |
| 2142 | #endif |
| 2143 | result = STAT(path->narrow, &st); |
| 2144 | Py_END_ALLOW_THREADS |
| 2145 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2146 | if (result != 0) { |
| 2147 | return path_error(path); |
| 2148 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2149 | |
| 2150 | return _pystat_fromstructstat(&st); |
| 2151 | } |
| 2152 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2153 | /*[python input] |
| 2154 | |
| 2155 | for s in """ |
| 2156 | |
| 2157 | FACCESSAT |
| 2158 | FCHMODAT |
| 2159 | FCHOWNAT |
| 2160 | FSTATAT |
| 2161 | LINKAT |
| 2162 | MKDIRAT |
| 2163 | MKFIFOAT |
| 2164 | MKNODAT |
| 2165 | OPENAT |
| 2166 | READLINKAT |
| 2167 | SYMLINKAT |
| 2168 | UNLINKAT |
| 2169 | |
| 2170 | """.strip().split(): |
| 2171 | s = s.strip() |
| 2172 | print(""" |
| 2173 | #ifdef HAVE_{s} |
| 2174 | #define {s}_DIR_FD_CONVERTER dir_fd_converter |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2175 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2176 | #define {s}_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2177 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2178 | """.rstrip().format(s=s)) |
| 2179 | |
| 2180 | for s in """ |
| 2181 | |
| 2182 | FCHDIR |
| 2183 | FCHMOD |
| 2184 | FCHOWN |
| 2185 | FDOPENDIR |
| 2186 | FEXECVE |
| 2187 | FPATHCONF |
| 2188 | FSTATVFS |
| 2189 | FTRUNCATE |
| 2190 | |
| 2191 | """.strip().split(): |
| 2192 | s = s.strip() |
| 2193 | print(""" |
| 2194 | #ifdef HAVE_{s} |
| 2195 | #define PATH_HAVE_{s} 1 |
| 2196 | #else |
| 2197 | #define PATH_HAVE_{s} 0 |
| 2198 | #endif |
| 2199 | |
| 2200 | """.rstrip().format(s=s)) |
| 2201 | [python start generated code]*/ |
| 2202 | |
| 2203 | #ifdef HAVE_FACCESSAT |
| 2204 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter |
| 2205 | #else |
| 2206 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2207 | #endif |
| 2208 | |
| 2209 | #ifdef HAVE_FCHMODAT |
| 2210 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2211 | #else |
| 2212 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2213 | #endif |
| 2214 | |
| 2215 | #ifdef HAVE_FCHOWNAT |
| 2216 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter |
| 2217 | #else |
| 2218 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2219 | #endif |
| 2220 | |
| 2221 | #ifdef HAVE_FSTATAT |
| 2222 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter |
| 2223 | #else |
| 2224 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2225 | #endif |
| 2226 | |
| 2227 | #ifdef HAVE_LINKAT |
| 2228 | #define LINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2229 | #else |
| 2230 | #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2231 | #endif |
| 2232 | |
| 2233 | #ifdef HAVE_MKDIRAT |
| 2234 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter |
| 2235 | #else |
| 2236 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2237 | #endif |
| 2238 | |
| 2239 | #ifdef HAVE_MKFIFOAT |
| 2240 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter |
| 2241 | #else |
| 2242 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2243 | #endif |
| 2244 | |
| 2245 | #ifdef HAVE_MKNODAT |
| 2246 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2247 | #else |
| 2248 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2249 | #endif |
| 2250 | |
| 2251 | #ifdef HAVE_OPENAT |
| 2252 | #define OPENAT_DIR_FD_CONVERTER dir_fd_converter |
| 2253 | #else |
| 2254 | #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2255 | #endif |
| 2256 | |
| 2257 | #ifdef HAVE_READLINKAT |
| 2258 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2259 | #else |
| 2260 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2261 | #endif |
| 2262 | |
| 2263 | #ifdef HAVE_SYMLINKAT |
| 2264 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2265 | #else |
| 2266 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2267 | #endif |
| 2268 | |
| 2269 | #ifdef HAVE_UNLINKAT |
| 2270 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2271 | #else |
| 2272 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2273 | #endif |
| 2274 | |
| 2275 | #ifdef HAVE_FCHDIR |
| 2276 | #define PATH_HAVE_FCHDIR 1 |
| 2277 | #else |
| 2278 | #define PATH_HAVE_FCHDIR 0 |
| 2279 | #endif |
| 2280 | |
| 2281 | #ifdef HAVE_FCHMOD |
| 2282 | #define PATH_HAVE_FCHMOD 1 |
| 2283 | #else |
| 2284 | #define PATH_HAVE_FCHMOD 0 |
| 2285 | #endif |
| 2286 | |
| 2287 | #ifdef HAVE_FCHOWN |
| 2288 | #define PATH_HAVE_FCHOWN 1 |
| 2289 | #else |
| 2290 | #define PATH_HAVE_FCHOWN 0 |
| 2291 | #endif |
| 2292 | |
| 2293 | #ifdef HAVE_FDOPENDIR |
| 2294 | #define PATH_HAVE_FDOPENDIR 1 |
| 2295 | #else |
| 2296 | #define PATH_HAVE_FDOPENDIR 0 |
| 2297 | #endif |
| 2298 | |
| 2299 | #ifdef HAVE_FEXECVE |
| 2300 | #define PATH_HAVE_FEXECVE 1 |
| 2301 | #else |
| 2302 | #define PATH_HAVE_FEXECVE 0 |
| 2303 | #endif |
| 2304 | |
| 2305 | #ifdef HAVE_FPATHCONF |
| 2306 | #define PATH_HAVE_FPATHCONF 1 |
| 2307 | #else |
| 2308 | #define PATH_HAVE_FPATHCONF 0 |
| 2309 | #endif |
| 2310 | |
| 2311 | #ifdef HAVE_FSTATVFS |
| 2312 | #define PATH_HAVE_FSTATVFS 1 |
| 2313 | #else |
| 2314 | #define PATH_HAVE_FSTATVFS 0 |
| 2315 | #endif |
| 2316 | |
| 2317 | #ifdef HAVE_FTRUNCATE |
| 2318 | #define PATH_HAVE_FTRUNCATE 1 |
| 2319 | #else |
| 2320 | #define PATH_HAVE_FTRUNCATE 0 |
| 2321 | #endif |
| 2322 | /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2323 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 2324 | #ifdef MS_WINDOWS |
| 2325 | #undef PATH_HAVE_FTRUNCATE |
| 2326 | #define PATH_HAVE_FTRUNCATE 1 |
| 2327 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2328 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2329 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2330 | |
| 2331 | class path_t_converter(CConverter): |
| 2332 | |
| 2333 | type = "path_t" |
| 2334 | impl_by_reference = True |
| 2335 | parse_by_reference = True |
| 2336 | |
| 2337 | converter = 'path_converter' |
| 2338 | |
| 2339 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2340 | # right now path_t doesn't support default values. |
| 2341 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2342 | if self.default not in (unspecified, None): |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2343 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2344 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2345 | if self.c_default not in (None, 'Py_None'): |
| 2346 | raise RuntimeError("Can't specify a c_default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2347 | |
| 2348 | self.nullable = nullable |
| 2349 | self.allow_fd = allow_fd |
| 2350 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2351 | def pre_render(self): |
| 2352 | def strify(value): |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2353 | if isinstance(value, str): |
| 2354 | return value |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2355 | return str(int(bool(value))) |
| 2356 | |
| 2357 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2358 | self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format( |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2359 | self.function.name, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2360 | self.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2361 | strify(self.nullable), |
| 2362 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2363 | ) |
| 2364 | |
| 2365 | def cleanup(self): |
| 2366 | return "path_cleanup(&" + self.name + ");\n" |
| 2367 | |
| 2368 | |
| 2369 | class dir_fd_converter(CConverter): |
| 2370 | type = 'int' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2371 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2372 | def converter_init(self, requires=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2373 | if self.default in (unspecified, None): |
| 2374 | self.c_default = 'DEFAULT_DIR_FD' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2375 | if isinstance(requires, str): |
| 2376 | self.converter = requires.upper() + '_DIR_FD_CONVERTER' |
| 2377 | else: |
| 2378 | self.converter = 'dir_fd_converter' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2379 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2380 | class fildes_converter(CConverter): |
| 2381 | type = 'int' |
| 2382 | converter = 'fildes_converter' |
| 2383 | |
| 2384 | class uid_t_converter(CConverter): |
| 2385 | type = "uid_t" |
| 2386 | converter = '_Py_Uid_Converter' |
| 2387 | |
| 2388 | class gid_t_converter(CConverter): |
| 2389 | type = "gid_t" |
| 2390 | converter = '_Py_Gid_Converter' |
| 2391 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2392 | class dev_t_converter(CConverter): |
| 2393 | type = 'dev_t' |
| 2394 | converter = '_Py_Dev_Converter' |
| 2395 | |
| 2396 | class dev_t_return_converter(unsigned_long_return_converter): |
| 2397 | type = 'dev_t' |
| 2398 | conversion_fn = '_PyLong_FromDev' |
| 2399 | unsigned_cast = '(dev_t)' |
| 2400 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2401 | class FSConverter_converter(CConverter): |
| 2402 | type = 'PyObject *' |
| 2403 | converter = 'PyUnicode_FSConverter' |
| 2404 | def converter_init(self): |
| 2405 | if self.default is not unspecified: |
| 2406 | fail("FSConverter_converter does not support default values") |
| 2407 | self.c_default = 'NULL' |
| 2408 | |
| 2409 | def cleanup(self): |
| 2410 | return "Py_XDECREF(" + self.name + ");\n" |
| 2411 | |
| 2412 | class pid_t_converter(CConverter): |
| 2413 | type = 'pid_t' |
| 2414 | format_unit = '" _Py_PARSE_PID "' |
| 2415 | |
| 2416 | class idtype_t_converter(int_converter): |
| 2417 | type = 'idtype_t' |
| 2418 | |
| 2419 | class id_t_converter(CConverter): |
| 2420 | type = 'id_t' |
| 2421 | format_unit = '" _Py_PARSE_PID "' |
| 2422 | |
| 2423 | class Py_intptr_t_converter(CConverter): |
| 2424 | type = 'Py_intptr_t' |
| 2425 | format_unit = '" _Py_PARSE_INTPTR "' |
| 2426 | |
| 2427 | class Py_off_t_converter(CConverter): |
| 2428 | type = 'Py_off_t' |
| 2429 | converter = 'Py_off_t_converter' |
| 2430 | |
| 2431 | class Py_off_t_return_converter(long_return_converter): |
| 2432 | type = 'Py_off_t' |
| 2433 | conversion_fn = 'PyLong_FromPy_off_t' |
| 2434 | |
| 2435 | class path_confname_converter(CConverter): |
| 2436 | type="int" |
| 2437 | converter="conv_path_confname" |
| 2438 | |
| 2439 | class confstr_confname_converter(path_confname_converter): |
| 2440 | converter='conv_confstr_confname' |
| 2441 | |
| 2442 | class sysconf_confname_converter(path_confname_converter): |
| 2443 | converter="conv_sysconf_confname" |
| 2444 | |
| 2445 | class sched_param_converter(CConverter): |
| 2446 | type = 'struct sched_param' |
| 2447 | converter = 'convert_sched_param' |
| 2448 | impl_by_reference = True; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2449 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2450 | [python start generated code]*/ |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2451 | /*[python end generated code: output=da39a3ee5e6b4b0d input=affe68316f160401]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2452 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2453 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2454 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2455 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2456 | |
| 2457 | path : path_t(allow_fd=True) |
| 2458 | Path to be examined; can be string, bytes, or open-file-descriptor int. |
| 2459 | |
| 2460 | * |
| 2461 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2462 | dir_fd : dir_fd(requires='fstatat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2463 | If not None, it should be a file descriptor open to a directory, |
| 2464 | and path should be a relative string; path will then be relative to |
| 2465 | that directory. |
| 2466 | |
| 2467 | follow_symlinks: bool = True |
| 2468 | If False, and the last element of the path is a symbolic link, |
| 2469 | stat will examine the symbolic link itself instead of the file |
| 2470 | the link points to. |
| 2471 | |
| 2472 | Perform a stat system call on the given path. |
| 2473 | |
| 2474 | dir_fd and follow_symlinks may not be implemented |
| 2475 | on your platform. If they are unavailable, using them will raise a |
| 2476 | NotImplementedError. |
| 2477 | |
| 2478 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2479 | an open file descriptor. |
| 2480 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2481 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2482 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2483 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2484 | os_stat_impl(PyModuleDef *module, path_t *path, int dir_fd, |
| 2485 | int follow_symlinks) |
| 2486 | /*[clinic end generated code: output=e4f7569f95d523ca input=099d356c306fa24a]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2487 | { |
| 2488 | return posix_do_stat("stat", path, dir_fd, follow_symlinks); |
| 2489 | } |
| 2490 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2491 | |
| 2492 | /*[clinic input] |
| 2493 | os.lstat |
| 2494 | |
| 2495 | path : path_t |
| 2496 | |
| 2497 | * |
| 2498 | |
| 2499 | dir_fd : dir_fd(requires='fstatat') = None |
| 2500 | |
| 2501 | Perform a stat system call on the given path, without following symbolic links. |
| 2502 | |
| 2503 | Like stat(), but do not follow symbolic links. |
| 2504 | Equivalent to stat(path, follow_symlinks=False). |
| 2505 | [clinic start generated code]*/ |
| 2506 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2507 | static PyObject * |
| 2508 | os_lstat_impl(PyModuleDef *module, path_t *path, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2509 | /*[clinic end generated code: output=7a748e333fcb39bd input=0b7474765927b925]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2510 | { |
| 2511 | int follow_symlinks = 0; |
| 2512 | return posix_do_stat("lstat", path, dir_fd, follow_symlinks); |
| 2513 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2514 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2515 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2516 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2517 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2518 | |
| 2519 | path: path_t(allow_fd=True) |
| 2520 | Path to be tested; can be string, bytes, or open-file-descriptor int. |
| 2521 | |
| 2522 | mode: int |
| 2523 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2524 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2525 | |
| 2526 | * |
| 2527 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2528 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2529 | If not None, it should be a file descriptor open to a directory, |
| 2530 | and path should be relative; path will then be relative to that |
| 2531 | directory. |
| 2532 | |
| 2533 | effective_ids: bool = False |
| 2534 | If True, access will use the effective uid/gid instead of |
| 2535 | the real uid/gid. |
| 2536 | |
| 2537 | follow_symlinks: bool = True |
| 2538 | If False, and the last element of the path is a symbolic link, |
| 2539 | access will examine the symbolic link itself instead of the file |
| 2540 | the link points to. |
| 2541 | |
| 2542 | Use the real uid/gid to test for access to a path. |
| 2543 | |
| 2544 | {parameters} |
| 2545 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2546 | on your platform. If they are unavailable, using them will raise a |
| 2547 | NotImplementedError. |
| 2548 | |
| 2549 | Note that most operations will use the effective uid/gid, therefore this |
| 2550 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2551 | has the specified access to the path. |
| 2552 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2553 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2554 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2555 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2556 | os_access_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, |
| 2557 | int effective_ids, int follow_symlinks) |
| 2558 | /*[clinic end generated code: output=abaa53340210088d input=b75a756797af45ec]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2559 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2560 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2561 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2562 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2563 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2564 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2565 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2566 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2567 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2568 | #ifndef HAVE_FACCESSAT |
| 2569 | if (follow_symlinks_specified("access", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2570 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2571 | |
| 2572 | if (effective_ids) { |
| 2573 | argument_unavailable_error("access", "effective_ids"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2574 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2575 | } |
| 2576 | #endif |
| 2577 | |
| 2578 | #ifdef MS_WINDOWS |
| 2579 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | ebe83f9 | 2013-10-19 22:36:17 +0200 | [diff] [blame] | 2580 | if (path->wide != NULL) |
| 2581 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2582 | else |
Christian Heimes | ebe83f9 | 2013-10-19 22:36:17 +0200 | [diff] [blame] | 2583 | attr = GetFileAttributesA(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2584 | Py_END_ALLOW_THREADS |
| 2585 | |
| 2586 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2587 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2588 | * * we didn't get a -1, and |
| 2589 | * * write access wasn't requested, |
| 2590 | * * or the file isn't read-only, |
| 2591 | * * or it's a directory. |
| 2592 | * (Directories cannot be read-only on Windows.) |
| 2593 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2594 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2595 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2596 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2597 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2598 | #else |
| 2599 | |
| 2600 | Py_BEGIN_ALLOW_THREADS |
| 2601 | #ifdef HAVE_FACCESSAT |
| 2602 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2603 | effective_ids || |
| 2604 | !follow_symlinks) { |
| 2605 | int flags = 0; |
| 2606 | if (!follow_symlinks) |
| 2607 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2608 | if (effective_ids) |
| 2609 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2610 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2611 | } |
| 2612 | else |
| 2613 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2614 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2615 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2616 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2617 | #endif |
| 2618 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2619 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2622 | #ifndef F_OK |
| 2623 | #define F_OK 0 |
| 2624 | #endif |
| 2625 | #ifndef R_OK |
| 2626 | #define R_OK 4 |
| 2627 | #endif |
| 2628 | #ifndef W_OK |
| 2629 | #define W_OK 2 |
| 2630 | #endif |
| 2631 | #ifndef X_OK |
| 2632 | #define X_OK 1 |
| 2633 | #endif |
| 2634 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2635 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2636 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2637 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2638 | os.ttyname -> DecodeFSDefault |
| 2639 | |
| 2640 | fd: int |
| 2641 | Integer file descriptor handle. |
| 2642 | |
| 2643 | / |
| 2644 | |
| 2645 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2646 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2647 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2648 | static char * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2649 | os_ttyname_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2650 | /*[clinic end generated code: output=03ad3d5ccaef75c3 input=5f72ca83e76b3b45]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2651 | { |
| 2652 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2653 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2654 | ret = ttyname(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2655 | if (ret == NULL) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2656 | posix_error(); |
| 2657 | return ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2658 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2659 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2660 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2661 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2662 | /*[clinic input] |
| 2663 | os.ctermid |
| 2664 | |
| 2665 | Return the name of the controlling terminal for this process. |
| 2666 | [clinic start generated code]*/ |
| 2667 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2668 | static PyObject * |
| 2669 | os_ctermid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2670 | /*[clinic end generated code: output=1b73788201e0aebd input=3b87fdd52556382d]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2671 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2672 | char *ret; |
| 2673 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2674 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2675 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2676 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2677 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2678 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2679 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2680 | if (ret == NULL) |
| 2681 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2682 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2683 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2684 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2685 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2686 | |
| 2687 | /*[clinic input] |
| 2688 | os.chdir |
| 2689 | |
| 2690 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') |
| 2691 | |
| 2692 | Change the current working directory to the specified path. |
| 2693 | |
| 2694 | path may always be specified as a string. |
| 2695 | On some platforms, path may also be specified as an open file descriptor. |
| 2696 | If this functionality is unavailable, using it raises an exception. |
| 2697 | [clinic start generated code]*/ |
| 2698 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2699 | static PyObject * |
| 2700 | os_chdir_impl(PyModuleDef *module, path_t *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2701 | /*[clinic end generated code: output=7358e3a20fb5aa93 input=1a4a15b4d12cb15d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2702 | { |
| 2703 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2704 | |
| 2705 | Py_BEGIN_ALLOW_THREADS |
| 2706 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2707 | if (path->wide) |
| 2708 | result = win32_wchdir(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2709 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2710 | result = win32_chdir(path->narrow); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 2711 | result = !result; /* on unix, success = 0, on windows, success = !0 */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2712 | #else |
| 2713 | #ifdef HAVE_FCHDIR |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2714 | if (path->fd != -1) |
| 2715 | result = fchdir(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2716 | else |
| 2717 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2718 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2719 | #endif |
| 2720 | Py_END_ALLOW_THREADS |
| 2721 | |
| 2722 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2723 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2724 | } |
| 2725 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2726 | Py_RETURN_NONE; |
| 2727 | } |
| 2728 | |
| 2729 | |
| 2730 | #ifdef HAVE_FCHDIR |
| 2731 | /*[clinic input] |
| 2732 | os.fchdir |
| 2733 | |
| 2734 | fd: fildes |
| 2735 | |
| 2736 | Change to the directory of the given file descriptor. |
| 2737 | |
| 2738 | fd must be opened on a directory, not a file. |
| 2739 | Equivalent to os.chdir(fd). |
| 2740 | |
| 2741 | [clinic start generated code]*/ |
| 2742 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2743 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2744 | os_fchdir_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2745 | /*[clinic end generated code: output=361d30df6b2d3418 input=18e816479a2fa985]*/ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2746 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2747 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2748 | } |
| 2749 | #endif /* HAVE_FCHDIR */ |
| 2750 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2751 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2752 | /*[clinic input] |
| 2753 | os.chmod |
| 2754 | |
| 2755 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
| 2756 | Path to be modified. May always be specified as a str or bytes. |
| 2757 | On some platforms, path may also be specified as an open file descriptor. |
| 2758 | If this functionality is unavailable, using it raises an exception. |
| 2759 | |
| 2760 | mode: int |
| 2761 | Operating-system mode bitfield. |
| 2762 | |
| 2763 | * |
| 2764 | |
| 2765 | dir_fd : dir_fd(requires='fchmodat') = None |
| 2766 | If not None, it should be a file descriptor open to a directory, |
| 2767 | and path should be relative; path will then be relative to that |
| 2768 | directory. |
| 2769 | |
| 2770 | follow_symlinks: bool = True |
| 2771 | If False, and the last element of the path is a symbolic link, |
| 2772 | chmod will modify the symbolic link itself instead of the file |
| 2773 | the link points to. |
| 2774 | |
| 2775 | Change the access permissions of a file. |
| 2776 | |
| 2777 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 2778 | an open file descriptor. |
| 2779 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 2780 | If they are unavailable, using them will raise a NotImplementedError. |
| 2781 | |
| 2782 | [clinic start generated code]*/ |
| 2783 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2784 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2785 | os_chmod_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, |
| 2786 | int follow_symlinks) |
| 2787 | /*[clinic end generated code: output=05e7f73b1a843ba2 input=7f1618e5e15cc196]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2788 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2789 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2790 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2791 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2792 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2793 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2794 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2795 | #ifdef HAVE_FCHMODAT |
| 2796 | int fchmodat_nofollow_unsupported = 0; |
| 2797 | #endif |
| 2798 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2799 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2800 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2801 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2802 | #endif |
| 2803 | |
| 2804 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2805 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2806 | if (path->wide) |
| 2807 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2808 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2809 | attr = GetFileAttributesA(path->narrow); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 2810 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2811 | result = 0; |
| 2812 | else { |
| 2813 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2814 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2815 | else |
| 2816 | attr |= FILE_ATTRIBUTE_READONLY; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2817 | if (path->wide) |
| 2818 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2819 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2820 | result = SetFileAttributesA(path->narrow, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2821 | } |
| 2822 | Py_END_ALLOW_THREADS |
| 2823 | |
| 2824 | if (!result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2825 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2826 | } |
| 2827 | #else /* MS_WINDOWS */ |
| 2828 | Py_BEGIN_ALLOW_THREADS |
| 2829 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2830 | if (path->fd != -1) |
| 2831 | result = fchmod(path->fd, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2832 | else |
| 2833 | #endif |
| 2834 | #ifdef HAVE_LCHMOD |
| 2835 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2836 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2837 | else |
| 2838 | #endif |
| 2839 | #ifdef HAVE_FCHMODAT |
| 2840 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2841 | /* |
| 2842 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2843 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2844 | * and then says it isn't implemented yet. |
| 2845 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2846 | * |
| 2847 | * Once it is supported, os.chmod will automatically |
| 2848 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2849 | * Until then, we need to be careful what exception we raise. |
| 2850 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2851 | result = fchmodat(dir_fd, path->narrow, mode, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2852 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2853 | /* |
| 2854 | * But wait! We can't throw the exception without allowing threads, |
| 2855 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2856 | */ |
| 2857 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2858 | result && |
| 2859 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2860 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2861 | } |
| 2862 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2863 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2864 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2865 | Py_END_ALLOW_THREADS |
| 2866 | |
| 2867 | if (result) { |
| 2868 | #ifdef HAVE_FCHMODAT |
| 2869 | if (fchmodat_nofollow_unsupported) { |
| 2870 | if (dir_fd != DEFAULT_DIR_FD) |
| 2871 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 2872 | dir_fd, follow_symlinks); |
| 2873 | else |
| 2874 | follow_symlinks_specified("chmod", follow_symlinks); |
| 2875 | } |
| 2876 | else |
| 2877 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2878 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2879 | } |
| 2880 | #endif |
| 2881 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2882 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2885 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2886 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2887 | /*[clinic input] |
| 2888 | os.fchmod |
| 2889 | |
| 2890 | fd: int |
| 2891 | mode: int |
| 2892 | |
| 2893 | Change the access permissions of the file given by file descriptor fd. |
| 2894 | |
| 2895 | Equivalent to os.chmod(fd, mode). |
| 2896 | [clinic start generated code]*/ |
| 2897 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2898 | static PyObject * |
| 2899 | os_fchmod_impl(PyModuleDef *module, int fd, int mode) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2900 | /*[clinic end generated code: output=2ee31ca226d1ed33 input=8ab11975ca01ee5b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2901 | { |
| 2902 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2903 | int async_err = 0; |
| 2904 | |
| 2905 | do { |
| 2906 | Py_BEGIN_ALLOW_THREADS |
| 2907 | res = fchmod(fd, mode); |
| 2908 | Py_END_ALLOW_THREADS |
| 2909 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 2910 | if (res != 0) |
| 2911 | return (!async_err) ? posix_error() : NULL; |
| 2912 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2913 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2914 | } |
| 2915 | #endif /* HAVE_FCHMOD */ |
| 2916 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2917 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2918 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2919 | /*[clinic input] |
| 2920 | os.lchmod |
| 2921 | |
| 2922 | path: path_t |
| 2923 | mode: int |
| 2924 | |
| 2925 | Change the access permissions of a file, without following symbolic links. |
| 2926 | |
| 2927 | If path is a symlink, this affects the link itself rather than the target. |
| 2928 | Equivalent to chmod(path, mode, follow_symlinks=False)." |
| 2929 | [clinic start generated code]*/ |
| 2930 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2931 | static PyObject * |
| 2932 | os_lchmod_impl(PyModuleDef *module, path_t *path, int mode) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2933 | /*[clinic end generated code: output=7c0cc46588d89e46 input=90c5663c7465d24f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2934 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2935 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2936 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 2937 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2938 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2939 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2940 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2941 | return NULL; |
| 2942 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2943 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2944 | } |
| 2945 | #endif /* HAVE_LCHMOD */ |
| 2946 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2947 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2948 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2949 | /*[clinic input] |
| 2950 | os.chflags |
| 2951 | |
| 2952 | path: path_t |
| 2953 | flags: unsigned_long(bitwise=True) |
| 2954 | follow_symlinks: bool=True |
| 2955 | |
| 2956 | Set file flags. |
| 2957 | |
| 2958 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 2959 | link, chflags will change flags on the symbolic link itself instead of the |
| 2960 | file the link points to. |
| 2961 | follow_symlinks may not be implemented on your platform. If it is |
| 2962 | unavailable, using it will raise a NotImplementedError. |
| 2963 | |
| 2964 | [clinic start generated code]*/ |
| 2965 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2966 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2967 | os_chflags_impl(PyModuleDef *module, path_t *path, unsigned long flags, |
| 2968 | int follow_symlinks) |
| 2969 | /*[clinic end generated code: output=ff2d6e73534a95b9 input=0327e29feb876236]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2970 | { |
| 2971 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2972 | |
| 2973 | #ifndef HAVE_LCHFLAGS |
| 2974 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2975 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2976 | #endif |
| 2977 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2978 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2979 | #ifdef HAVE_LCHFLAGS |
| 2980 | if (!follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2981 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2982 | else |
| 2983 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2984 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2985 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2986 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2987 | if (result) |
| 2988 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2989 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2990 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2991 | } |
| 2992 | #endif /* HAVE_CHFLAGS */ |
| 2993 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2994 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2995 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2996 | /*[clinic input] |
| 2997 | os.lchflags |
| 2998 | |
| 2999 | path: path_t |
| 3000 | flags: unsigned_long(bitwise=True) |
| 3001 | |
| 3002 | Set file flags. |
| 3003 | |
| 3004 | This function will not follow symbolic links. |
| 3005 | Equivalent to chflags(path, flags, follow_symlinks=False). |
| 3006 | [clinic start generated code]*/ |
| 3007 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3008 | static PyObject * |
| 3009 | os_lchflags_impl(PyModuleDef *module, path_t *path, unsigned long flags) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3010 | /*[clinic end generated code: output=6741322fb949661b input=f9f82ea8b585ca9d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3011 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3012 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3013 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3014 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3015 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3016 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3017 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3018 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3019 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3020 | } |
| 3021 | #endif /* HAVE_LCHFLAGS */ |
| 3022 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3023 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3024 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3025 | /*[clinic input] |
| 3026 | os.chroot |
| 3027 | path: path_t |
| 3028 | |
| 3029 | Change root directory to path. |
| 3030 | |
| 3031 | [clinic start generated code]*/ |
| 3032 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3033 | static PyObject * |
| 3034 | os_chroot_impl(PyModuleDef *module, path_t *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3035 | /*[clinic end generated code: output=b6dbfabe74ecaa9d input=14822965652c3dc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3036 | { |
| 3037 | int res; |
| 3038 | Py_BEGIN_ALLOW_THREADS |
| 3039 | res = chroot(path->narrow); |
| 3040 | Py_END_ALLOW_THREADS |
| 3041 | if (res < 0) |
| 3042 | return path_error(path); |
| 3043 | Py_RETURN_NONE; |
| 3044 | } |
| 3045 | #endif /* HAVE_CHROOT */ |
| 3046 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3047 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3048 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3049 | /*[clinic input] |
| 3050 | os.fsync |
| 3051 | |
| 3052 | fd: fildes |
| 3053 | |
| 3054 | Force write of fd to disk. |
| 3055 | [clinic start generated code]*/ |
| 3056 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3057 | static PyObject * |
| 3058 | os_fsync_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3059 | /*[clinic end generated code: output=83a350851064aea7 input=21c3645c056967f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3060 | { |
| 3061 | return posix_fildes_fd(fd, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3062 | } |
| 3063 | #endif /* HAVE_FSYNC */ |
| 3064 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3065 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3066 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3067 | /*[clinic input] |
| 3068 | os.sync |
| 3069 | |
| 3070 | Force write of everything to disk. |
| 3071 | [clinic start generated code]*/ |
| 3072 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3073 | static PyObject * |
| 3074 | os_sync_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3075 | /*[clinic end generated code: output=ba524f656c201c40 input=84749fe5e9b404ff]*/ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3076 | { |
| 3077 | Py_BEGIN_ALLOW_THREADS |
| 3078 | sync(); |
| 3079 | Py_END_ALLOW_THREADS |
| 3080 | Py_RETURN_NONE; |
| 3081 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3082 | #endif /* HAVE_SYNC */ |
| 3083 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3084 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3085 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3086 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3087 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3088 | #endif |
| 3089 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3090 | /*[clinic input] |
| 3091 | os.fdatasync |
| 3092 | |
| 3093 | fd: fildes |
| 3094 | |
| 3095 | Force write of fd to disk without forcing update of metadata. |
| 3096 | [clinic start generated code]*/ |
| 3097 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3098 | static PyObject * |
| 3099 | os_fdatasync_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3100 | /*[clinic end generated code: output=e0f04a3aff515b75 input=bc74791ee54dd291]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3101 | { |
| 3102 | return posix_fildes_fd(fd, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3103 | } |
| 3104 | #endif /* HAVE_FDATASYNC */ |
| 3105 | |
| 3106 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3107 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3108 | /*[clinic input] |
| 3109 | os.chown |
| 3110 | |
| 3111 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') |
| 3112 | Path to be examined; can be string, bytes, or open-file-descriptor int. |
| 3113 | |
| 3114 | uid: uid_t |
| 3115 | |
| 3116 | gid: gid_t |
| 3117 | |
| 3118 | * |
| 3119 | |
| 3120 | dir_fd : dir_fd(requires='fchownat') = None |
| 3121 | If not None, it should be a file descriptor open to a directory, |
| 3122 | and path should be relative; path will then be relative to that |
| 3123 | directory. |
| 3124 | |
| 3125 | follow_symlinks: bool = True |
| 3126 | If False, and the last element of the path is a symbolic link, |
| 3127 | stat will examine the symbolic link itself instead of the file |
| 3128 | the link points to. |
| 3129 | |
| 3130 | Change the owner and group id of path to the numeric uid and gid.\ |
| 3131 | |
| 3132 | path may always be specified as a string. |
| 3133 | On some platforms, path may also be specified as an open file descriptor. |
| 3134 | If this functionality is unavailable, using it raises an exception. |
| 3135 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3136 | and path should be relative; path will then be relative to that directory. |
| 3137 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3138 | link, chown will modify the symbolic link itself instead of the file the |
| 3139 | link points to. |
| 3140 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3141 | an open file descriptor. |
| 3142 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3143 | If they are unavailable, using them will raise a NotImplementedError. |
| 3144 | |
| 3145 | [clinic start generated code]*/ |
| 3146 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3147 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3148 | os_chown_impl(PyModuleDef *module, path_t *path, uid_t uid, gid_t gid, |
| 3149 | int dir_fd, int follow_symlinks) |
| 3150 | /*[clinic end generated code: output=e0a4559f394dbd91 input=a61cc35574814d5d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3151 | { |
| 3152 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3153 | |
| 3154 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3155 | if (follow_symlinks_specified("chown", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3156 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3157 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3158 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
| 3159 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) |
| 3160 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3161 | |
| 3162 | #ifdef __APPLE__ |
| 3163 | /* |
| 3164 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3165 | * (But we still have an lchown symbol because of weak-linking.) |
| 3166 | * It doesn't have fchownat either. So there's no possibility |
| 3167 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3168 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3169 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3170 | follow_symlinks_specified("chown", follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3171 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3172 | } |
| 3173 | #endif |
| 3174 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3175 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3176 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3177 | if (path->fd != -1) |
| 3178 | result = fchown(path->fd, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3179 | else |
| 3180 | #endif |
| 3181 | #ifdef HAVE_LCHOWN |
| 3182 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3183 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3184 | else |
| 3185 | #endif |
| 3186 | #ifdef HAVE_FCHOWNAT |
| 3187 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3188 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3189 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3190 | else |
| 3191 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3192 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3193 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3194 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3195 | if (result) |
| 3196 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3197 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3198 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3199 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3200 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3201 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3202 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3203 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3204 | /*[clinic input] |
| 3205 | os.fchown |
| 3206 | |
| 3207 | fd: int |
| 3208 | uid: uid_t |
| 3209 | gid: gid_t |
| 3210 | |
| 3211 | Change the owner and group id of the file specified by file descriptor. |
| 3212 | |
| 3213 | Equivalent to os.chown(fd, uid, gid). |
| 3214 | |
| 3215 | [clinic start generated code]*/ |
| 3216 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3217 | static PyObject * |
| 3218 | os_fchown_impl(PyModuleDef *module, int fd, uid_t uid, gid_t gid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3219 | /*[clinic end generated code: output=7545abf8f6086d76 input=3af544ba1b13a0d7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3220 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3221 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3222 | int async_err = 0; |
| 3223 | |
| 3224 | do { |
| 3225 | Py_BEGIN_ALLOW_THREADS |
| 3226 | res = fchown(fd, uid, gid); |
| 3227 | Py_END_ALLOW_THREADS |
| 3228 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3229 | if (res != 0) |
| 3230 | return (!async_err) ? posix_error() : NULL; |
| 3231 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3232 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3233 | } |
| 3234 | #endif /* HAVE_FCHOWN */ |
| 3235 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3236 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3237 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3238 | /*[clinic input] |
| 3239 | os.lchown |
| 3240 | |
| 3241 | path : path_t |
| 3242 | uid: uid_t |
| 3243 | gid: gid_t |
| 3244 | |
| 3245 | Change the owner and group id of path to the numeric uid and gid. |
| 3246 | |
| 3247 | This function will not follow symbolic links. |
| 3248 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). |
| 3249 | [clinic start generated code]*/ |
| 3250 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3251 | static PyObject * |
| 3252 | os_lchown_impl(PyModuleDef *module, path_t *path, uid_t uid, gid_t gid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3253 | /*[clinic end generated code: output=bb0d2da1579ac275 input=b1c6014d563a7161]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3254 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3255 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3256 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3257 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3258 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3259 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3260 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3261 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3262 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3263 | } |
| 3264 | #endif /* HAVE_LCHOWN */ |
| 3265 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3266 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3267 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3268 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3269 | { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3270 | char *buf, *tmpbuf; |
| 3271 | char *cwd; |
| 3272 | const size_t chunk = 1024; |
| 3273 | size_t buflen = 0; |
| 3274 | PyObject *obj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3275 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3276 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3277 | if (!use_bytes) { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3278 | wchar_t wbuf[MAXPATHLEN]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3279 | wchar_t *wbuf2 = wbuf; |
| 3280 | PyObject *resobj; |
| 3281 | DWORD len; |
| 3282 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3283 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3284 | /* If the buffer is large enough, len does not include the |
| 3285 | terminating \0. If the buffer is too small, len includes |
| 3286 | the space needed for the terminator. */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3287 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3288 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3289 | if (wbuf2) |
| 3290 | len = GetCurrentDirectoryW(len, wbuf2); |
| 3291 | } |
| 3292 | Py_END_ALLOW_THREADS |
| 3293 | if (!wbuf2) { |
| 3294 | PyErr_NoMemory(); |
| 3295 | return NULL; |
| 3296 | } |
| 3297 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3298 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3299 | PyMem_RawFree(wbuf2); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3300 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3301 | } |
| 3302 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3303 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3304 | PyMem_RawFree(wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3305 | return resobj; |
| 3306 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3307 | |
| 3308 | if (win32_warn_bytes_api()) |
| 3309 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3310 | #endif |
| 3311 | |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3312 | buf = cwd = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3313 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3314 | do { |
| 3315 | buflen += chunk; |
| 3316 | tmpbuf = PyMem_RawRealloc(buf, buflen); |
| 3317 | if (tmpbuf == NULL) |
| 3318 | break; |
| 3319 | |
| 3320 | buf = tmpbuf; |
| 3321 | cwd = getcwd(buf, buflen); |
| 3322 | } while (cwd == NULL && errno == ERANGE); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3323 | Py_END_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3324 | |
| 3325 | if (cwd == NULL) { |
| 3326 | PyMem_RawFree(buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3327 | return posix_error(); |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3328 | } |
| 3329 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3330 | if (use_bytes) |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3331 | obj = PyBytes_FromStringAndSize(buf, strlen(buf)); |
| 3332 | else |
| 3333 | obj = PyUnicode_DecodeFSDefault(buf); |
| 3334 | PyMem_RawFree(buf); |
| 3335 | |
| 3336 | return obj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3337 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3338 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3339 | |
| 3340 | /*[clinic input] |
| 3341 | os.getcwd |
| 3342 | |
| 3343 | Return a unicode string representing the current working directory. |
| 3344 | [clinic start generated code]*/ |
| 3345 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3346 | static PyObject * |
| 3347 | os_getcwd_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3348 | /*[clinic end generated code: output=efe3a8c0121525ea input=f069211bb70e3d39]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3349 | { |
| 3350 | return posix_getcwd(0); |
| 3351 | } |
| 3352 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3353 | |
| 3354 | /*[clinic input] |
| 3355 | os.getcwdb |
| 3356 | |
| 3357 | Return a bytes string representing the current working directory. |
| 3358 | [clinic start generated code]*/ |
| 3359 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3360 | static PyObject * |
| 3361 | os_getcwdb_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3362 | /*[clinic end generated code: output=7fce42ee4b2a296a input=f6f6a378dad3d9cb]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3363 | { |
| 3364 | return posix_getcwd(1); |
| 3365 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3366 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3367 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3368 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3369 | #define HAVE_LINK 1 |
| 3370 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3371 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3372 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3373 | /*[clinic input] |
| 3374 | |
| 3375 | os.link |
| 3376 | |
| 3377 | src : path_t |
| 3378 | dst : path_t |
| 3379 | * |
| 3380 | src_dir_fd : dir_fd = None |
| 3381 | dst_dir_fd : dir_fd = None |
| 3382 | follow_symlinks: bool = True |
| 3383 | |
| 3384 | Create a hard link to a file. |
| 3385 | |
| 3386 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 3387 | descriptor open to a directory, and the respective path string (src or dst) |
| 3388 | should be relative; the path will then be relative to that directory. |
| 3389 | If follow_symlinks is False, and the last element of src is a symbolic |
| 3390 | link, link will create a link to the symbolic link itself instead of the |
| 3391 | file the link points to. |
| 3392 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 3393 | platform. If they are unavailable, using them will raise a |
| 3394 | NotImplementedError. |
| 3395 | [clinic start generated code]*/ |
| 3396 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3397 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3398 | os_link_impl(PyModuleDef *module, path_t *src, path_t *dst, int src_dir_fd, |
| 3399 | int dst_dir_fd, int follow_symlinks) |
| 3400 | /*[clinic end generated code: output=f47a7e88f7b391b6 input=b0095ebbcbaa7e04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3401 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3402 | #ifdef MS_WINDOWS |
| 3403 | BOOL result; |
| 3404 | #else |
| 3405 | int result; |
| 3406 | #endif |
| 3407 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3408 | #ifndef HAVE_LINKAT |
| 3409 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3410 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3411 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3412 | } |
| 3413 | #endif |
| 3414 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3415 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3416 | PyErr_SetString(PyExc_NotImplementedError, |
| 3417 | "link: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3418 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3419 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3420 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3421 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3422 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3423 | if (src->wide) |
| 3424 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3425 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3426 | result = CreateHardLinkA(dst->narrow, src->narrow, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3427 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3428 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3429 | if (!result) |
| 3430 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3431 | #else |
| 3432 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3433 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3434 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3435 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3436 | (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3437 | result = linkat(src_dir_fd, src->narrow, |
| 3438 | dst_dir_fd, dst->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3439 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3440 | else |
| 3441 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3442 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3443 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3444 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3445 | if (result) |
| 3446 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3447 | #endif |
| 3448 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3449 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3450 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3451 | #endif |
| 3452 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3453 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3454 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3455 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3456 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3457 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3458 | PyObject *v; |
| 3459 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3460 | BOOL result; |
| 3461 | WIN32_FIND_DATA FileData; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3462 | char namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3463 | char *bufptr = namebuf; |
| 3464 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3465 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3466 | PyObject *po = NULL; |
| 3467 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3468 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3469 | if (!path->narrow) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3470 | WIN32_FIND_DATAW wFileData; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3471 | wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3472 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3473 | if (!path->wide) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3474 | po_wchars = L"."; |
| 3475 | len = 1; |
| 3476 | } else { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3477 | po_wchars = path->wide; |
| 3478 | len = wcslen(path->wide); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3479 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3480 | /* The +5 is so we can append "\\*.*\0" */ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3481 | wnamebuf = PyMem_New(wchar_t, len + 5); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3482 | if (!wnamebuf) { |
| 3483 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3484 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3485 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3486 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3487 | if (len > 0) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3488 | wchar_t wch = wnamebuf[len-1]; |
Tim Golden | 781bbeb | 2013-10-25 20:24:06 +0100 | [diff] [blame] | 3489 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3490 | wnamebuf[len++] = SEP; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3491 | wcscpy(wnamebuf + len, L"*.*"); |
| 3492 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3493 | if ((list = PyList_New(0)) == NULL) { |
| 3494 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3495 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3496 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3497 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3498 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3499 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3500 | int error = GetLastError(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3501 | if (error == ERROR_FILE_NOT_FOUND) |
| 3502 | goto exit; |
| 3503 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3504 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3505 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3506 | } |
| 3507 | do { |
| 3508 | /* Skip over . and .. */ |
| 3509 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3510 | wcscmp(wFileData.cFileName, L"..") != 0) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3511 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3512 | wcslen(wFileData.cFileName)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3513 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3514 | Py_DECREF(list); |
| 3515 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3516 | break; |
| 3517 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3518 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3519 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3520 | Py_DECREF(list); |
| 3521 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3522 | break; |
| 3523 | } |
| 3524 | Py_DECREF(v); |
| 3525 | } |
| 3526 | Py_BEGIN_ALLOW_THREADS |
| 3527 | result = FindNextFileW(hFindFile, &wFileData); |
| 3528 | Py_END_ALLOW_THREADS |
| 3529 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3530 | it got to the end of the directory. */ |
| 3531 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3532 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3533 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3534 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3535 | } |
| 3536 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3537 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3538 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3539 | } |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3540 | strcpy(namebuf, path->narrow); |
| 3541 | len = path->length; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3542 | if (len > 0) { |
| 3543 | char ch = namebuf[len-1]; |
Tim Golden | 781bbeb | 2013-10-25 20:24:06 +0100 | [diff] [blame] | 3544 | if (ch != '\\' && ch != '/' && ch != ':') |
| 3545 | namebuf[len++] = '\\'; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3546 | strcpy(namebuf + len, "*.*"); |
| 3547 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3548 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3549 | if ((list = PyList_New(0)) == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3550 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3551 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3552 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3553 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3554 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3555 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3556 | int error = GetLastError(); |
| 3557 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3558 | goto exit; |
| 3559 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3560 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3561 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3562 | } |
| 3563 | do { |
| 3564 | /* Skip over . and .. */ |
| 3565 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 3566 | strcmp(FileData.cFileName, "..") != 0) { |
| 3567 | v = PyBytes_FromString(FileData.cFileName); |
| 3568 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3569 | Py_DECREF(list); |
| 3570 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3571 | break; |
| 3572 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3573 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3574 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3575 | Py_DECREF(list); |
| 3576 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3577 | break; |
| 3578 | } |
| 3579 | Py_DECREF(v); |
| 3580 | } |
| 3581 | Py_BEGIN_ALLOW_THREADS |
| 3582 | result = FindNextFile(hFindFile, &FileData); |
| 3583 | Py_END_ALLOW_THREADS |
| 3584 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3585 | it got to the end of the directory. */ |
| 3586 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3587 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3588 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3589 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3590 | } |
| 3591 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3592 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3593 | exit: |
| 3594 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3595 | if (FindClose(hFindFile) == FALSE) { |
| 3596 | if (list != NULL) { |
| 3597 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3598 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3599 | } |
| 3600 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3601 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3602 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3603 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3604 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3605 | } /* end of _listdir_windows_no_opendir */ |
| 3606 | |
| 3607 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3608 | |
| 3609 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3610 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3611 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3612 | PyObject *v; |
| 3613 | DIR *dirp = NULL; |
| 3614 | struct dirent *ep; |
| 3615 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3616 | #ifdef HAVE_FDOPENDIR |
| 3617 | int fd = -1; |
| 3618 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3619 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3620 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3621 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3622 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3623 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3624 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3625 | if (fd == -1) |
| 3626 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3627 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3628 | return_str = 1; |
| 3629 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3630 | Py_BEGIN_ALLOW_THREADS |
| 3631 | dirp = fdopendir(fd); |
| 3632 | Py_END_ALLOW_THREADS |
| 3633 | } |
| 3634 | else |
| 3635 | #endif |
| 3636 | { |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3637 | char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3638 | if (path->narrow) { |
| 3639 | name = path->narrow; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3640 | /* only return bytes if they specified a bytes object */ |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3641 | return_str = !(PyBytes_Check(path->object)); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3642 | } |
| 3643 | else { |
| 3644 | name = "."; |
| 3645 | return_str = 1; |
| 3646 | } |
| 3647 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3648 | Py_BEGIN_ALLOW_THREADS |
| 3649 | dirp = opendir(name); |
| 3650 | Py_END_ALLOW_THREADS |
| 3651 | } |
| 3652 | |
| 3653 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3654 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3655 | #ifdef HAVE_FDOPENDIR |
| 3656 | if (fd != -1) { |
| 3657 | Py_BEGIN_ALLOW_THREADS |
| 3658 | close(fd); |
| 3659 | Py_END_ALLOW_THREADS |
| 3660 | } |
| 3661 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3662 | goto exit; |
| 3663 | } |
| 3664 | if ((list = PyList_New(0)) == NULL) { |
| 3665 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3666 | } |
| 3667 | for (;;) { |
| 3668 | errno = 0; |
| 3669 | Py_BEGIN_ALLOW_THREADS |
| 3670 | ep = readdir(dirp); |
| 3671 | Py_END_ALLOW_THREADS |
| 3672 | if (ep == NULL) { |
| 3673 | if (errno == 0) { |
| 3674 | break; |
| 3675 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3676 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3677 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3678 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3679 | } |
| 3680 | } |
| 3681 | if (ep->d_name[0] == '.' && |
| 3682 | (NAMLEN(ep) == 1 || |
| 3683 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3684 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3685 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3686 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3687 | else |
| 3688 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3689 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3690 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3691 | break; |
| 3692 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3693 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3694 | Py_DECREF(v); |
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 | } |
| 3698 | Py_DECREF(v); |
| 3699 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3700 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3701 | exit: |
| 3702 | if (dirp != NULL) { |
| 3703 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3704 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3705 | if (fd > -1) |
| 3706 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3707 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3708 | closedir(dirp); |
| 3709 | Py_END_ALLOW_THREADS |
| 3710 | } |
| 3711 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3712 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3713 | } /* end of _posix_listdir */ |
| 3714 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3715 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3716 | |
| 3717 | /*[clinic input] |
| 3718 | os.listdir |
| 3719 | |
| 3720 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
| 3721 | |
| 3722 | Return a list containing the names of the files in the directory. |
| 3723 | |
| 3724 | path can be specified as either str or bytes. If path is bytes, |
| 3725 | the filenames returned will also be bytes; in all other circumstances |
| 3726 | the filenames returned will be str. |
| 3727 | If path is None, uses the path='.'. |
| 3728 | On some platforms, path may also be specified as an open file descriptor;\ |
| 3729 | the file descriptor must refer to a directory. |
| 3730 | If this functionality is unavailable, using it raises NotImplementedError. |
| 3731 | |
| 3732 | The list is in arbitrary order. It does not include the special |
| 3733 | entries '.' and '..' even if they are present in the directory. |
| 3734 | |
| 3735 | |
| 3736 | [clinic start generated code]*/ |
| 3737 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3738 | static PyObject * |
| 3739 | os_listdir_impl(PyModuleDef *module, path_t *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3740 | /*[clinic end generated code: output=1fbe67c1f780c8b7 input=09e300416e3cd729]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3741 | { |
| 3742 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3743 | return _listdir_windows_no_opendir(path, NULL); |
| 3744 | #else |
| 3745 | return _posix_listdir(path, NULL); |
| 3746 | #endif |
| 3747 | } |
| 3748 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3749 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3750 | /* A helper function for abspath on win32 */ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3751 | /*[clinic input] |
| 3752 | os._getfullpathname |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3753 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3754 | path: path_t |
| 3755 | / |
| 3756 | |
| 3757 | [clinic start generated code]*/ |
| 3758 | |
| 3759 | static PyObject * |
| 3760 | os__getfullpathname_impl(PyModuleDef *module, path_t *path) |
| 3761 | /*[clinic end generated code: output=b90b1f103b08773f input=332ed537c29d0a3e]*/ |
| 3762 | { |
| 3763 | if (!path->narrow) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3764 | { |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3765 | wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3766 | wchar_t *wtemp; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3767 | DWORD result; |
| 3768 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3769 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3770 | result = GetFullPathNameW(path->wide, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3771 | Py_ARRAY_LENGTH(woutbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3772 | woutbuf, &wtemp); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3773 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3774 | woutbufp = PyMem_New(wchar_t, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3775 | if (!woutbufp) |
| 3776 | return PyErr_NoMemory(); |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3777 | result = GetFullPathNameW(path->wide, result, woutbufp, &wtemp); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3778 | } |
| 3779 | if (result) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3780 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3781 | else |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3782 | v = win32_error_object("GetFullPathNameW", path->object); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3783 | if (woutbufp != woutbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3784 | PyMem_Free(woutbufp); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3785 | return v; |
| 3786 | } |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3787 | else { |
| 3788 | char outbuf[MAX_PATH]; |
| 3789 | char *temp; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3790 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3791 | if (!GetFullPathName(path->narrow, Py_ARRAY_LENGTH(outbuf), |
| 3792 | outbuf, &temp)) { |
| 3793 | win32_error_object("GetFullPathName", path->object); |
| 3794 | return NULL; |
| 3795 | } |
| 3796 | return PyBytes_FromString(outbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3797 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3798 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3799 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3800 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3801 | /*[clinic input] |
| 3802 | os._getfinalpathname |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3803 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3804 | path: unicode |
| 3805 | / |
| 3806 | |
| 3807 | A helper function for samepath on windows. |
| 3808 | [clinic start generated code]*/ |
| 3809 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3810 | static PyObject * |
| 3811 | os__getfinalpathname_impl(PyModuleDef *module, PyObject *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3812 | /*[clinic end generated code: output=8be81a5f51a34bcf input=71d5e89334891bf4]*/ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3813 | { |
| 3814 | HANDLE hFile; |
| 3815 | int buf_size; |
| 3816 | wchar_t *target_path; |
| 3817 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3818 | PyObject *result; |
| 3819 | wchar_t *path_wchar; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3820 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3821 | path_wchar = PyUnicode_AsUnicode(path); |
| 3822 | if (path_wchar == NULL) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3823 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3824 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3825 | hFile = CreateFileW( |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3826 | path_wchar, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3827 | 0, /* desired access */ |
| 3828 | 0, /* share mode */ |
| 3829 | NULL, /* security attributes */ |
| 3830 | OPEN_EXISTING, |
| 3831 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3832 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3833 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3834 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3835 | if(hFile == INVALID_HANDLE_VALUE) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3836 | return win32_error_object("CreateFileW", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3837 | |
| 3838 | /* We have a good handle to the target, use it to determine the |
| 3839 | target path name. */ |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 3840 | buf_size = GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3841 | |
| 3842 | if(!buf_size) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3843 | return win32_error_object("GetFinalPathNameByHandle", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3844 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3845 | target_path = PyMem_New(wchar_t, buf_size+1); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3846 | if(!target_path) |
| 3847 | return PyErr_NoMemory(); |
| 3848 | |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 3849 | result_length = GetFinalPathNameByHandleW(hFile, target_path, |
| 3850 | buf_size, VOLUME_NAME_DOS); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3851 | if(!result_length) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3852 | return win32_error_object("GetFinalPathNamyByHandle", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3853 | |
| 3854 | if(!CloseHandle(hFile)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3855 | return win32_error_object("CloseHandle", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3856 | |
| 3857 | target_path[result_length] = 0; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3858 | result = PyUnicode_FromWideChar(target_path, result_length); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3859 | PyMem_Free(target_path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3860 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3861 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3862 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3863 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3864 | "Return true if the pathname refers to an existing directory."); |
| 3865 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3866 | /*[clinic input] |
| 3867 | os._isdir |
| 3868 | |
| 3869 | path: path_t |
| 3870 | / |
| 3871 | |
| 3872 | [clinic start generated code]*/ |
| 3873 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3874 | static PyObject * |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3875 | os__isdir_impl(PyModuleDef *module, path_t *path) |
| 3876 | /*[clinic end generated code: output=f17b2d4e1994b0ff input=e794f12faab62a2a]*/ |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3877 | { |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3878 | DWORD attributes; |
| 3879 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3880 | if (!path->narrow) |
| 3881 | attributes = GetFileAttributesW(path->wide); |
| 3882 | else |
| 3883 | attributes = GetFileAttributesA(path->narrow); |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3884 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3885 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3886 | Py_RETURN_FALSE; |
| 3887 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3888 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3889 | Py_RETURN_TRUE; |
| 3890 | else |
| 3891 | Py_RETURN_FALSE; |
| 3892 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3893 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3894 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3895 | /*[clinic input] |
| 3896 | os._getvolumepathname |
| 3897 | |
| 3898 | path: unicode |
| 3899 | |
| 3900 | A helper function for ismount on Win32. |
| 3901 | [clinic start generated code]*/ |
| 3902 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3903 | static PyObject * |
| 3904 | os__getvolumepathname_impl(PyModuleDef *module, PyObject *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3905 | /*[clinic end generated code: output=79a0ba729f956dbe input=7eacadc40acbda6b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3906 | { |
| 3907 | PyObject *result; |
| 3908 | wchar_t *path_wchar, *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3909 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3910 | BOOL ret; |
| 3911 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3912 | path_wchar = PyUnicode_AsUnicodeAndSize(path, &buflen); |
| 3913 | if (path_wchar == NULL) |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3914 | return NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3915 | buflen += 1; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3916 | |
| 3917 | /* Volume path should be shorter than entire path */ |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3918 | buflen = Py_MAX(buflen, MAX_PATH); |
| 3919 | |
| 3920 | if (buflen > DWORD_MAX) { |
| 3921 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 3922 | return NULL; |
| 3923 | } |
| 3924 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3925 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3926 | if (mountpath == NULL) |
| 3927 | return PyErr_NoMemory(); |
| 3928 | |
| 3929 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3930 | ret = GetVolumePathNameW(path_wchar, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3931 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3932 | Py_END_ALLOW_THREADS |
| 3933 | |
| 3934 | if (!ret) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3935 | result = win32_error_object("_getvolumepathname", path); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3936 | goto exit; |
| 3937 | } |
| 3938 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
| 3939 | |
| 3940 | exit: |
| 3941 | PyMem_Free(mountpath); |
| 3942 | return result; |
| 3943 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3944 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3945 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3946 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3947 | |
| 3948 | /*[clinic input] |
| 3949 | os.mkdir |
| 3950 | |
| 3951 | path : path_t |
| 3952 | |
| 3953 | mode: int = 0o777 |
| 3954 | |
| 3955 | * |
| 3956 | |
| 3957 | dir_fd : dir_fd(requires='mkdirat') = None |
| 3958 | |
| 3959 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 3960 | |
| 3961 | Create a directory. |
| 3962 | |
| 3963 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3964 | and path should be relative; path will then be relative to that directory. |
| 3965 | dir_fd may not be implemented on your platform. |
| 3966 | If it is unavailable, using it will raise a NotImplementedError. |
| 3967 | |
| 3968 | The mode argument is ignored on Windows. |
| 3969 | [clinic start generated code]*/ |
| 3970 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3971 | static PyObject * |
| 3972 | 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] | 3973 | /*[clinic end generated code: output=8bf1f738873ef2c5 input=e965f68377e9b1ce]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3974 | { |
| 3975 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3976 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3977 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3978 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3979 | if (path->wide) |
| 3980 | result = CreateDirectoryW(path->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3981 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3982 | result = CreateDirectoryA(path->narrow, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3983 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3984 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3985 | if (!result) |
| 3986 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3987 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3988 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3989 | #if HAVE_MKDIRAT |
| 3990 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3991 | result = mkdirat(dir_fd, path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3992 | else |
| 3993 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3994 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3995 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3996 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3997 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3998 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3999 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4000 | if (result < 0) |
| 4001 | return path_error(path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4002 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4003 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4004 | } |
| 4005 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4006 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4007 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 4008 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4009 | #include <sys/resource.h> |
| 4010 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4011 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4012 | |
| 4013 | #ifdef HAVE_NICE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4014 | /*[clinic input] |
| 4015 | os.nice |
| 4016 | |
| 4017 | increment: int |
| 4018 | / |
| 4019 | |
| 4020 | Add increment to the priority of process and return the new priority. |
| 4021 | [clinic start generated code]*/ |
| 4022 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4023 | static PyObject * |
| 4024 | os_nice_impl(PyModuleDef *module, int increment) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4025 | /*[clinic end generated code: output=8870418a3fc07b51 input=864be2d402a21da2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4026 | { |
| 4027 | int value; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4028 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4029 | /* There are two flavours of 'nice': one that returns the new |
| 4030 | priority (as required by almost all standards out there) and the |
| 4031 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 4032 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4033 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4034 | If we are of the nice family that returns the new priority, we |
| 4035 | need to clear errno before the call, and check if errno is filled |
| 4036 | before calling posix_error() on a returnvalue of -1, because the |
| 4037 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4038 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4039 | errno = 0; |
| 4040 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4041 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4042 | if (value == 0) |
| 4043 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4044 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4045 | if (value == -1 && errno != 0) |
| 4046 | /* either nice() or getpriority() returned an error */ |
| 4047 | return posix_error(); |
| 4048 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4049 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4050 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4051 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4052 | |
| 4053 | #ifdef HAVE_GETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4054 | /*[clinic input] |
| 4055 | os.getpriority |
| 4056 | |
| 4057 | which: int |
| 4058 | who: int |
| 4059 | |
| 4060 | Return program scheduling priority. |
| 4061 | [clinic start generated code]*/ |
| 4062 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4063 | static PyObject * |
| 4064 | os_getpriority_impl(PyModuleDef *module, int which, int who) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4065 | /*[clinic end generated code: output=4759937aa5b67ed6 input=9be615d40e2544ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4066 | { |
| 4067 | int retval; |
| 4068 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4069 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4070 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4071 | if (errno != 0) |
| 4072 | return posix_error(); |
| 4073 | return PyLong_FromLong((long)retval); |
| 4074 | } |
| 4075 | #endif /* HAVE_GETPRIORITY */ |
| 4076 | |
| 4077 | |
| 4078 | #ifdef HAVE_SETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4079 | /*[clinic input] |
| 4080 | os.setpriority |
| 4081 | |
| 4082 | which: int |
| 4083 | who: int |
| 4084 | priority: int |
| 4085 | |
| 4086 | Set program scheduling priority. |
| 4087 | [clinic start generated code]*/ |
| 4088 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4089 | static PyObject * |
| 4090 | os_setpriority_impl(PyModuleDef *module, int which, int who, int priority) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4091 | /*[clinic end generated code: output=6497d3301547e7d5 input=710ccbf65b9dc513]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4092 | { |
| 4093 | int retval; |
| 4094 | |
| 4095 | retval = setpriority(which, who, priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4096 | if (retval == -1) |
| 4097 | return posix_error(); |
| 4098 | Py_RETURN_NONE; |
| 4099 | } |
| 4100 | #endif /* HAVE_SETPRIORITY */ |
| 4101 | |
| 4102 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4103 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4104 | 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] | 4105 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4106 | char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4107 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4108 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4109 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4110 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4111 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4112 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4113 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4114 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4115 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4116 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4117 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4118 | #ifndef HAVE_RENAMEAT |
| 4119 | if (dir_fd_specified) { |
| 4120 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4121 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4122 | } |
| 4123 | #endif |
| 4124 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4125 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4126 | PyErr_Format(PyExc_ValueError, |
| 4127 | "%s: src and dst must be the same type", function_name); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4128 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4129 | } |
| 4130 | |
| 4131 | #ifdef MS_WINDOWS |
| 4132 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4133 | if (src->wide) |
| 4134 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4135 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4136 | result = MoveFileExA(src->narrow, dst->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4137 | Py_END_ALLOW_THREADS |
| 4138 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4139 | if (!result) |
| 4140 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4141 | |
| 4142 | #else |
| 4143 | Py_BEGIN_ALLOW_THREADS |
| 4144 | #ifdef HAVE_RENAMEAT |
| 4145 | if (dir_fd_specified) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4146 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4147 | else |
| 4148 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4149 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4150 | Py_END_ALLOW_THREADS |
| 4151 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4152 | if (result) |
| 4153 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4154 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4155 | Py_RETURN_NONE; |
| 4156 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4157 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4158 | |
| 4159 | /*[clinic input] |
| 4160 | os.rename |
| 4161 | |
| 4162 | src : path_t |
| 4163 | dst : path_t |
| 4164 | * |
| 4165 | src_dir_fd : dir_fd = None |
| 4166 | dst_dir_fd : dir_fd = None |
| 4167 | |
| 4168 | Rename a file or directory. |
| 4169 | |
| 4170 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4171 | descriptor open to a directory, and the respective path string (src or dst) |
| 4172 | should be relative; the path will then be relative to that directory. |
| 4173 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4174 | If they are unavailable, using them will raise a NotImplementedError. |
| 4175 | [clinic start generated code]*/ |
| 4176 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4177 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4178 | os_rename_impl(PyModuleDef *module, path_t *src, path_t *dst, int src_dir_fd, |
| 4179 | int dst_dir_fd) |
| 4180 | /*[clinic end generated code: output=08033bb2ec27fb5f input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4181 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4182 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4183 | } |
| 4184 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4185 | |
| 4186 | /*[clinic input] |
| 4187 | os.replace = os.rename |
| 4188 | |
| 4189 | Rename a file or directory, overwriting the destination. |
| 4190 | |
| 4191 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4192 | descriptor open to a directory, and the respective path string (src or dst) |
| 4193 | should be relative; the path will then be relative to that directory. |
| 4194 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4195 | If they are unavailable, using them will raise a NotImplementedError." |
| 4196 | [clinic start generated code]*/ |
| 4197 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4198 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4199 | os_replace_impl(PyModuleDef *module, path_t *src, path_t *dst, |
| 4200 | int src_dir_fd, int dst_dir_fd) |
| 4201 | /*[clinic end generated code: output=131d012eed8d3b8b input=25515dfb107c8421]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4202 | { |
| 4203 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); |
| 4204 | } |
| 4205 | |
| 4206 | |
| 4207 | /*[clinic input] |
| 4208 | os.rmdir |
| 4209 | |
| 4210 | path: path_t |
| 4211 | * |
| 4212 | dir_fd: dir_fd(requires='unlinkat') = None |
| 4213 | |
| 4214 | Remove a directory. |
| 4215 | |
| 4216 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4217 | and path should be relative; path will then be relative to that directory. |
| 4218 | dir_fd may not be implemented on your platform. |
| 4219 | If it is unavailable, using it will raise a NotImplementedError. |
| 4220 | [clinic start generated code]*/ |
| 4221 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4222 | static PyObject * |
| 4223 | os_rmdir_impl(PyModuleDef *module, path_t *path, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4224 | /*[clinic end generated code: output=cabadec80d5a77c7 input=38c8b375ca34a7e2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4225 | { |
| 4226 | int result; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4227 | |
| 4228 | Py_BEGIN_ALLOW_THREADS |
| 4229 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4230 | if (path->wide) |
| 4231 | result = RemoveDirectoryW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4232 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4233 | result = RemoveDirectoryA(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4234 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4235 | #else |
| 4236 | #ifdef HAVE_UNLINKAT |
| 4237 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4238 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4239 | else |
| 4240 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4241 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4242 | #endif |
| 4243 | Py_END_ALLOW_THREADS |
| 4244 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4245 | if (result) |
| 4246 | return path_error(path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4247 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4248 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4251 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4252 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4253 | #ifdef MS_WINDOWS |
| 4254 | /*[clinic input] |
| 4255 | os.system -> long |
| 4256 | |
| 4257 | command: Py_UNICODE |
| 4258 | |
| 4259 | Execute the command in a subshell. |
| 4260 | [clinic start generated code]*/ |
| 4261 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4262 | static long |
| 4263 | os_system_impl(PyModuleDef *module, Py_UNICODE *command) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4264 | /*[clinic end generated code: output=4c3bd5abcd9c29e7 input=303f5ce97df606b0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4265 | { |
| 4266 | long result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4267 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4268 | result = _wsystem(command); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4269 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4270 | return result; |
| 4271 | } |
| 4272 | #else /* MS_WINDOWS */ |
| 4273 | /*[clinic input] |
| 4274 | os.system -> long |
| 4275 | |
| 4276 | command: FSConverter |
| 4277 | |
| 4278 | Execute the command in a subshell. |
| 4279 | [clinic start generated code]*/ |
| 4280 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4281 | static long |
| 4282 | os_system_impl(PyModuleDef *module, PyObject *command) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4283 | /*[clinic end generated code: output=800f775e10b7be55 input=86a58554ba6094af]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4284 | { |
| 4285 | long result; |
| 4286 | char *bytes = PyBytes_AsString(command); |
| 4287 | Py_BEGIN_ALLOW_THREADS |
| 4288 | result = system(bytes); |
| 4289 | Py_END_ALLOW_THREADS |
| 4290 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4291 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4292 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4293 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4294 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4295 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4296 | /*[clinic input] |
| 4297 | os.umask |
| 4298 | |
| 4299 | mask: int |
| 4300 | / |
| 4301 | |
| 4302 | Set the current numeric umask and return the previous umask. |
| 4303 | [clinic start generated code]*/ |
| 4304 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4305 | static PyObject * |
| 4306 | os_umask_impl(PyModuleDef *module, int mask) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4307 | /*[clinic end generated code: output=9e1fe3c9f14d6a05 input=ab6bfd9b24d8a7e8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4308 | { |
| 4309 | int i = (int)umask(mask); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4310 | if (i < 0) |
| 4311 | return posix_error(); |
| 4312 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4313 | } |
| 4314 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4315 | #ifdef MS_WINDOWS |
| 4316 | |
| 4317 | /* override the default DeleteFileW behavior so that directory |
| 4318 | symlinks can be removed with this function, the same as with |
| 4319 | Unix symlinks */ |
| 4320 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4321 | { |
| 4322 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4323 | WIN32_FIND_DATAW find_data; |
| 4324 | HANDLE find_data_handle; |
| 4325 | int is_directory = 0; |
| 4326 | int is_link = 0; |
| 4327 | |
| 4328 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4329 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4330 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4331 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4332 | it is a symlink */ |
| 4333 | if(is_directory && |
| 4334 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4335 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4336 | |
| 4337 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4338 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 4339 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 4340 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 4341 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4342 | FindClose(find_data_handle); |
| 4343 | } |
| 4344 | } |
| 4345 | } |
| 4346 | |
| 4347 | if (is_directory && is_link) |
| 4348 | return RemoveDirectoryW(lpFileName); |
| 4349 | |
| 4350 | return DeleteFileW(lpFileName); |
| 4351 | } |
| 4352 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4353 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4354 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4355 | /*[clinic input] |
| 4356 | os.unlink |
| 4357 | |
| 4358 | path: path_t |
| 4359 | * |
| 4360 | dir_fd: dir_fd(requires='unlinkat')=None |
| 4361 | |
| 4362 | Remove a file (same as remove()). |
| 4363 | |
| 4364 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4365 | and path should be relative; path will then be relative to that directory. |
| 4366 | dir_fd may not be implemented on your platform. |
| 4367 | If it is unavailable, using it will raise a NotImplementedError. |
| 4368 | |
| 4369 | [clinic start generated code]*/ |
| 4370 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4371 | static PyObject * |
| 4372 | os_unlink_impl(PyModuleDef *module, path_t *path, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4373 | /*[clinic end generated code: output=474afd5cd09b237e input=d7bcde2b1b2a2552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4374 | { |
| 4375 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4376 | |
| 4377 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4378 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4379 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4380 | if (path->wide) |
| 4381 | result = Py_DeleteFileW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4382 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4383 | result = DeleteFileA(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4384 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4385 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4386 | #ifdef HAVE_UNLINKAT |
| 4387 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4388 | result = unlinkat(dir_fd, path->narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4389 | else |
| 4390 | #endif /* HAVE_UNLINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4391 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4392 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4393 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4394 | Py_END_ALLOW_THREADS |
| 4395 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4396 | if (result) |
| 4397 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4398 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4399 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4400 | } |
| 4401 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4402 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4403 | /*[clinic input] |
| 4404 | os.remove = os.unlink |
| 4405 | |
| 4406 | Remove a file (same as unlink()). |
| 4407 | |
| 4408 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4409 | and path should be relative; path will then be relative to that directory. |
| 4410 | dir_fd may not be implemented on your platform. |
| 4411 | If it is unavailable, using it will raise a NotImplementedError. |
| 4412 | [clinic start generated code]*/ |
| 4413 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4414 | static PyObject * |
| 4415 | os_remove_impl(PyModuleDef *module, path_t *path, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4416 | /*[clinic end generated code: output=d0d5149e64832b9e input=e05c5ab55cd30983]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4417 | { |
| 4418 | return os_unlink_impl(module, path, dir_fd); |
| 4419 | } |
| 4420 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4421 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4422 | static PyStructSequence_Field uname_result_fields[] = { |
| 4423 | {"sysname", "operating system name"}, |
| 4424 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4425 | {"release", "operating system release"}, |
| 4426 | {"version", "operating system version"}, |
| 4427 | {"machine", "hardware identifier"}, |
| 4428 | {NULL} |
| 4429 | }; |
| 4430 | |
| 4431 | PyDoc_STRVAR(uname_result__doc__, |
| 4432 | "uname_result: Result from os.uname().\n\n\ |
| 4433 | This object may be accessed either as a tuple of\n\ |
| 4434 | (sysname, nodename, release, version, machine),\n\ |
| 4435 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4436 | \n\ |
| 4437 | See os.uname for more information."); |
| 4438 | |
| 4439 | static PyStructSequence_Desc uname_result_desc = { |
| 4440 | "uname_result", /* name */ |
| 4441 | uname_result__doc__, /* doc */ |
| 4442 | uname_result_fields, |
| 4443 | 5 |
| 4444 | }; |
| 4445 | |
| 4446 | static PyTypeObject UnameResultType; |
| 4447 | |
| 4448 | |
| 4449 | #ifdef HAVE_UNAME |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4450 | /*[clinic input] |
| 4451 | os.uname |
| 4452 | |
| 4453 | Return an object identifying the current operating system. |
| 4454 | |
| 4455 | The object behaves like a named tuple with the following fields: |
| 4456 | (sysname, nodename, release, version, machine) |
| 4457 | |
| 4458 | [clinic start generated code]*/ |
| 4459 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4460 | static PyObject * |
| 4461 | os_uname_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4462 | /*[clinic end generated code: output=01e1421b757e753f input=e68bd246db3043ed]*/ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4463 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4464 | struct utsname u; |
| 4465 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4466 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4467 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4468 | Py_BEGIN_ALLOW_THREADS |
| 4469 | res = uname(&u); |
| 4470 | Py_END_ALLOW_THREADS |
| 4471 | if (res < 0) |
| 4472 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4473 | |
| 4474 | value = PyStructSequence_New(&UnameResultType); |
| 4475 | if (value == NULL) |
| 4476 | return NULL; |
| 4477 | |
| 4478 | #define SET(i, field) \ |
| 4479 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4480 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4481 | if (!o) { \ |
| 4482 | Py_DECREF(value); \ |
| 4483 | return NULL; \ |
| 4484 | } \ |
| 4485 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4486 | } \ |
| 4487 | |
| 4488 | SET(0, u.sysname); |
| 4489 | SET(1, u.nodename); |
| 4490 | SET(2, u.release); |
| 4491 | SET(3, u.version); |
| 4492 | SET(4, u.machine); |
| 4493 | |
| 4494 | #undef SET |
| 4495 | |
| 4496 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4497 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4498 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4499 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4500 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4501 | |
| 4502 | typedef struct { |
| 4503 | int now; |
| 4504 | time_t atime_s; |
| 4505 | long atime_ns; |
| 4506 | time_t mtime_s; |
| 4507 | long mtime_ns; |
| 4508 | } utime_t; |
| 4509 | |
| 4510 | /* |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4511 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4512 | * they also intentionally leak the declaration of a pointer named "time" |
| 4513 | */ |
| 4514 | #define UTIME_TO_TIMESPEC \ |
| 4515 | struct timespec ts[2]; \ |
| 4516 | struct timespec *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4517 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4518 | time = NULL; \ |
| 4519 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4520 | ts[0].tv_sec = ut->atime_s; \ |
| 4521 | ts[0].tv_nsec = ut->atime_ns; \ |
| 4522 | ts[1].tv_sec = ut->mtime_s; \ |
| 4523 | ts[1].tv_nsec = ut->mtime_ns; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4524 | time = ts; \ |
| 4525 | } \ |
| 4526 | |
| 4527 | #define UTIME_TO_TIMEVAL \ |
| 4528 | struct timeval tv[2]; \ |
| 4529 | struct timeval *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4530 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4531 | time = NULL; \ |
| 4532 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4533 | tv[0].tv_sec = ut->atime_s; \ |
| 4534 | tv[0].tv_usec = ut->atime_ns / 1000; \ |
| 4535 | tv[1].tv_sec = ut->mtime_s; \ |
| 4536 | tv[1].tv_usec = ut->mtime_ns / 1000; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4537 | time = tv; \ |
| 4538 | } \ |
| 4539 | |
| 4540 | #define UTIME_TO_UTIMBUF \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4541 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4542 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4543 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4544 | time = NULL; \ |
| 4545 | else { \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4546 | u.actime = ut->atime_s; \ |
| 4547 | u.modtime = ut->mtime_s; \ |
| 4548 | time = &u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4549 | } |
| 4550 | |
| 4551 | #define UTIME_TO_TIME_T \ |
| 4552 | time_t timet[2]; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4553 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4554 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4555 | time = NULL; \ |
| 4556 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4557 | timet[0] = ut->atime_s; \ |
| 4558 | timet[1] = ut->mtime_s; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4559 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4560 | } \ |
| 4561 | |
| 4562 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4563 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4564 | |
| 4565 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4566 | utime_dir_fd(utime_t *ut, int dir_fd, char *path, int follow_symlinks) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4567 | { |
| 4568 | #ifdef HAVE_UTIMENSAT |
| 4569 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4570 | UTIME_TO_TIMESPEC; |
| 4571 | return utimensat(dir_fd, path, time, flags); |
| 4572 | #elif defined(HAVE_FUTIMESAT) |
| 4573 | UTIME_TO_TIMEVAL; |
| 4574 | /* |
| 4575 | * follow_symlinks will never be false here; |
| 4576 | * we only allow !follow_symlinks and dir_fd together |
| 4577 | * if we have utimensat() |
| 4578 | */ |
| 4579 | assert(follow_symlinks); |
| 4580 | return futimesat(dir_fd, path, time); |
| 4581 | #endif |
| 4582 | } |
| 4583 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4584 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
| 4585 | #else |
| 4586 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4587 | #endif |
| 4588 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4589 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4590 | |
| 4591 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4592 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4593 | { |
| 4594 | #ifdef HAVE_FUTIMENS |
| 4595 | UTIME_TO_TIMESPEC; |
| 4596 | return futimens(fd, time); |
| 4597 | #else |
| 4598 | UTIME_TO_TIMEVAL; |
| 4599 | return futimes(fd, time); |
| 4600 | #endif |
| 4601 | } |
| 4602 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4603 | #define PATH_UTIME_HAVE_FD 1 |
| 4604 | #else |
| 4605 | #define PATH_UTIME_HAVE_FD 0 |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4606 | #endif |
| 4607 | |
| 4608 | |
| 4609 | #define UTIME_HAVE_NOFOLLOW_SYMLINKS \ |
| 4610 | (defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)) |
| 4611 | |
| 4612 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4613 | |
| 4614 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4615 | utime_nofollow_symlinks(utime_t *ut, char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4616 | { |
| 4617 | #ifdef HAVE_UTIMENSAT |
| 4618 | UTIME_TO_TIMESPEC; |
| 4619 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4620 | #else |
| 4621 | UTIME_TO_TIMEVAL; |
| 4622 | return lutimes(path, time); |
| 4623 | #endif |
| 4624 | } |
| 4625 | |
| 4626 | #endif |
| 4627 | |
| 4628 | #ifndef MS_WINDOWS |
| 4629 | |
| 4630 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4631 | utime_default(utime_t *ut, char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4632 | { |
| 4633 | #ifdef HAVE_UTIMENSAT |
| 4634 | UTIME_TO_TIMESPEC; |
| 4635 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4636 | #elif defined(HAVE_UTIMES) |
| 4637 | UTIME_TO_TIMEVAL; |
| 4638 | return utimes(path, time); |
| 4639 | #elif defined(HAVE_UTIME_H) |
| 4640 | UTIME_TO_UTIMBUF; |
| 4641 | return utime(path, time); |
| 4642 | #else |
| 4643 | UTIME_TO_TIME_T; |
| 4644 | return utime(path, time); |
| 4645 | #endif |
| 4646 | } |
| 4647 | |
| 4648 | #endif |
| 4649 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4650 | static int |
| 4651 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4652 | { |
| 4653 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4654 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4655 | divmod = PyNumber_Divmod(py_long, billion); |
| 4656 | if (!divmod) |
| 4657 | goto exit; |
| 4658 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4659 | if ((*s == -1) && PyErr_Occurred()) |
| 4660 | goto exit; |
| 4661 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4662 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4663 | goto exit; |
| 4664 | |
| 4665 | result = 1; |
| 4666 | exit: |
| 4667 | Py_XDECREF(divmod); |
| 4668 | return result; |
| 4669 | } |
| 4670 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4671 | |
| 4672 | /*[clinic input] |
| 4673 | os.utime |
| 4674 | |
| 4675 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') |
| 4676 | times: object = NULL |
| 4677 | * |
| 4678 | ns: object = NULL |
| 4679 | dir_fd: dir_fd(requires='futimensat') = None |
| 4680 | follow_symlinks: bool=True |
| 4681 | |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4682 | # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4683 | |
| 4684 | Set the access and modified time of path. |
| 4685 | |
| 4686 | path may always be specified as a string. |
| 4687 | On some platforms, path may also be specified as an open file descriptor. |
| 4688 | If this functionality is unavailable, using it raises an exception. |
| 4689 | |
| 4690 | If times is not None, it must be a tuple (atime, mtime); |
| 4691 | atime and mtime should be expressed as float seconds since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4692 | 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] | 4693 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 4694 | since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4695 | 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] | 4696 | Specifying tuples for both times and ns is an error. |
| 4697 | |
| 4698 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4699 | and path should be relative; path will then be relative to that directory. |
| 4700 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 4701 | link, utime will modify the symbolic link itself instead of the file the |
| 4702 | link points to. |
| 4703 | It is an error to use dir_fd or follow_symlinks when specifying path |
| 4704 | as an open file descriptor. |
| 4705 | dir_fd and follow_symlinks may not be available on your platform. |
| 4706 | If they are unavailable, using them will raise a NotImplementedError. |
| 4707 | |
| 4708 | [clinic start generated code]*/ |
| 4709 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4710 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4711 | os_utime_impl(PyModuleDef *module, path_t *path, PyObject *times, |
| 4712 | PyObject *ns, int dir_fd, int follow_symlinks) |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4713 | /*[clinic end generated code: output=31f3434e560ba2f0 input=081cdc54ca685385]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4714 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4715 | #ifdef MS_WINDOWS |
| 4716 | HANDLE hFile; |
| 4717 | FILETIME atime, mtime; |
| 4718 | #else |
| 4719 | int result; |
| 4720 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4721 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4722 | PyObject *return_value = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4723 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4724 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4725 | memset(&utime, 0, sizeof(utime_t)); |
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 | if (times && (times != Py_None) && ns) { |
| 4728 | PyErr_SetString(PyExc_ValueError, |
| 4729 | "utime: you may specify either 'times'" |
| 4730 | " or 'ns' but not both"); |
| 4731 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4732 | } |
| 4733 | |
| 4734 | if (times && (times != Py_None)) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4735 | time_t a_sec, m_sec; |
| 4736 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4737 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4738 | PyErr_SetString(PyExc_TypeError, |
| 4739 | "utime: 'times' must be either" |
| 4740 | " a tuple of two ints or None"); |
| 4741 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4742 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4743 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4744 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4745 | &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4746 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4747 | &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4748 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4749 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4750 | utime.atime_s = a_sec; |
| 4751 | utime.atime_ns = a_nsec; |
| 4752 | utime.mtime_s = m_sec; |
| 4753 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4754 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4755 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4756 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4757 | PyErr_SetString(PyExc_TypeError, |
| 4758 | "utime: 'ns' must be a tuple of two ints"); |
| 4759 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4760 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4761 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4762 | 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] | 4763 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4764 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4765 | &utime.mtime_s, &utime.mtime_ns)) { |
| 4766 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4767 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4768 | } |
| 4769 | else { |
| 4770 | /* times and ns are both None/unspecified. use "now". */ |
| 4771 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4772 | } |
| 4773 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4774 | #if !UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4775 | if (follow_symlinks_specified("utime", follow_symlinks)) |
| 4776 | goto exit; |
| 4777 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4778 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4779 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
| 4780 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || |
| 4781 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4782 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4783 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4784 | #if !defined(HAVE_UTIMENSAT) |
| 4785 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4786 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4787 | "utime: cannot use dir_fd and follow_symlinks " |
| 4788 | "together on this platform"); |
| 4789 | goto exit; |
| 4790 | } |
| 4791 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4792 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4793 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4794 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4795 | if (path->wide) |
| 4796 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4797 | NULL, OPEN_EXISTING, |
| 4798 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4799 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4800 | hFile = CreateFileA(path->narrow, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4801 | NULL, OPEN_EXISTING, |
| 4802 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4803 | Py_END_ALLOW_THREADS |
| 4804 | if (hFile == INVALID_HANDLE_VALUE) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4805 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4806 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4807 | } |
| 4808 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4809 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 4810 | GetSystemTimeAsFileTime(&mtime); |
| 4811 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4812 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4813 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 4814 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4815 | _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] | 4816 | } |
| 4817 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4818 | /* Avoid putting the file name into the error here, |
| 4819 | as that may confuse the user into believing that |
| 4820 | something is wrong with the file, when it also |
| 4821 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4822 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4823 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4824 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4825 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4826 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4827 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4828 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4829 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4830 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4831 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4832 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4833 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4834 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4835 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4836 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4837 | else |
| 4838 | #endif |
| 4839 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4840 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4841 | if (path->fd != -1) |
| 4842 | result = utime_fd(&utime, path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4843 | else |
| 4844 | #endif |
| 4845 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4846 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4847 | |
| 4848 | Py_END_ALLOW_THREADS |
| 4849 | |
| 4850 | if (result < 0) { |
| 4851 | /* see previous comment about not putting filename in error here */ |
| 4852 | return_value = posix_error(); |
| 4853 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4854 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4855 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4856 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4857 | |
| 4858 | Py_INCREF(Py_None); |
| 4859 | return_value = Py_None; |
| 4860 | |
| 4861 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4862 | #ifdef MS_WINDOWS |
| 4863 | if (hFile != INVALID_HANDLE_VALUE) |
| 4864 | CloseHandle(hFile); |
| 4865 | #endif |
| 4866 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4867 | } |
| 4868 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4869 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4870 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4871 | |
| 4872 | /*[clinic input] |
| 4873 | os._exit |
| 4874 | |
| 4875 | status: int |
| 4876 | |
| 4877 | Exit to the system with specified status, without normal exit processing. |
| 4878 | [clinic start generated code]*/ |
| 4879 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4880 | static PyObject * |
| 4881 | os__exit_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4882 | /*[clinic end generated code: output=472a3cbaf68f3621 input=5e6d57556b0c4a62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4883 | { |
| 4884 | _exit(status); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4885 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4886 | } |
| 4887 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4888 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 4889 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4890 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4891 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4892 | Py_ssize_t i; |
| 4893 | for (i = 0; i < count; i++) |
| 4894 | PyMem_Free(array[i]); |
| 4895 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4896 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4897 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 4898 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4899 | int fsconvert_strdup(PyObject *o, char**out) |
| 4900 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4901 | PyObject *bytes; |
| 4902 | Py_ssize_t size; |
| 4903 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 4904 | return 0; |
| 4905 | size = PyBytes_GET_SIZE(bytes); |
| 4906 | *out = PyMem_Malloc(size+1); |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 4907 | if (!*out) { |
| 4908 | PyErr_NoMemory(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4909 | return 0; |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 4910 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4911 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 4912 | Py_DECREF(bytes); |
| 4913 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4914 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4915 | #endif |
| 4916 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4917 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4918 | static char** |
| 4919 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 4920 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4921 | char **envlist; |
| 4922 | Py_ssize_t i, pos, envc; |
| 4923 | PyObject *keys=NULL, *vals=NULL; |
| 4924 | PyObject *key, *val, *key2, *val2; |
| 4925 | char *p, *k, *v; |
| 4926 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4927 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4928 | i = PyMapping_Size(env); |
| 4929 | if (i < 0) |
| 4930 | return NULL; |
| 4931 | envlist = PyMem_NEW(char *, i + 1); |
| 4932 | if (envlist == NULL) { |
| 4933 | PyErr_NoMemory(); |
| 4934 | return NULL; |
| 4935 | } |
| 4936 | envc = 0; |
| 4937 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4938 | if (!keys) |
| 4939 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4940 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4941 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4942 | goto error; |
| 4943 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 4944 | PyErr_Format(PyExc_TypeError, |
| 4945 | "env.keys() or env.values() is not a list"); |
| 4946 | goto error; |
| 4947 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4948 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4949 | for (pos = 0; pos < i; pos++) { |
| 4950 | key = PyList_GetItem(keys, pos); |
| 4951 | val = PyList_GetItem(vals, pos); |
| 4952 | if (!key || !val) |
| 4953 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4954 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4955 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 4956 | goto error; |
| 4957 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 4958 | Py_DECREF(key2); |
| 4959 | goto error; |
| 4960 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4961 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4962 | k = PyBytes_AsString(key2); |
| 4963 | v = PyBytes_AsString(val2); |
| 4964 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4965 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4966 | p = PyMem_NEW(char, len); |
| 4967 | if (p == NULL) { |
| 4968 | PyErr_NoMemory(); |
| 4969 | Py_DECREF(key2); |
| 4970 | Py_DECREF(val2); |
| 4971 | goto error; |
| 4972 | } |
| 4973 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 4974 | envlist[envc++] = p; |
| 4975 | Py_DECREF(key2); |
| 4976 | Py_DECREF(val2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4977 | } |
| 4978 | Py_DECREF(vals); |
| 4979 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4980 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4981 | envlist[envc] = 0; |
| 4982 | *envc_ptr = envc; |
| 4983 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4984 | |
| 4985 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4986 | Py_XDECREF(keys); |
| 4987 | Py_XDECREF(vals); |
| 4988 | while (--envc >= 0) |
| 4989 | PyMem_DEL(envlist[envc]); |
| 4990 | PyMem_DEL(envlist); |
| 4991 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4992 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4993 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4994 | static char** |
| 4995 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 4996 | { |
| 4997 | int i; |
| 4998 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 4999 | if (argvlist == NULL) { |
| 5000 | PyErr_NoMemory(); |
| 5001 | return NULL; |
| 5002 | } |
| 5003 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5004 | PyObject* item = PySequence_ITEM(argv, i); |
| 5005 | if (item == NULL) |
| 5006 | goto fail; |
| 5007 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 5008 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5009 | goto fail; |
| 5010 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5011 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5012 | } |
| 5013 | argvlist[*argc] = NULL; |
| 5014 | return argvlist; |
| 5015 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5016 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5017 | free_string_array(argvlist, *argc); |
| 5018 | return NULL; |
| 5019 | } |
| 5020 | #endif |
| 5021 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5022 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5023 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5024 | /*[clinic input] |
| 5025 | os.execv |
| 5026 | |
| 5027 | path: FSConverter |
| 5028 | Path of executable file. |
| 5029 | argv: object |
| 5030 | Tuple or list of strings. |
| 5031 | / |
| 5032 | |
| 5033 | Execute an executable path with arguments, replacing current process. |
| 5034 | [clinic start generated code]*/ |
| 5035 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5036 | static PyObject * |
| 5037 | os_execv_impl(PyModuleDef *module, PyObject *path, PyObject *argv) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5038 | /*[clinic end generated code: output=9221f08143146fff input=96041559925e5229]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5039 | { |
| 5040 | char *path_char; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5041 | char **argvlist; |
| 5042 | Py_ssize_t argc; |
| 5043 | |
| 5044 | /* execv has two arguments: (path, argv), where |
| 5045 | argv is a list or tuple of strings. */ |
| 5046 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5047 | path_char = PyBytes_AsString(path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5048 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 5049 | PyErr_SetString(PyExc_TypeError, |
| 5050 | "execv() arg 2 must be a tuple or list"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5051 | return NULL; |
| 5052 | } |
| 5053 | argc = PySequence_Size(argv); |
| 5054 | if (argc < 1) { |
| 5055 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5056 | return NULL; |
| 5057 | } |
| 5058 | |
| 5059 | argvlist = parse_arglist(argv, &argc); |
| 5060 | if (argvlist == NULL) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5061 | return NULL; |
| 5062 | } |
| 5063 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5064 | execv(path_char, argvlist); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5065 | |
| 5066 | /* If we get here it's definitely an error */ |
| 5067 | |
| 5068 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5069 | return posix_error(); |
| 5070 | } |
| 5071 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5072 | |
| 5073 | /*[clinic input] |
| 5074 | os.execve |
| 5075 | |
| 5076 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') |
| 5077 | Path of executable file. |
| 5078 | argv: object |
| 5079 | Tuple or list of strings. |
| 5080 | env: object |
| 5081 | Dictionary of strings mapping to strings. |
| 5082 | |
| 5083 | Execute an executable path with arguments, replacing current process. |
| 5084 | [clinic start generated code]*/ |
| 5085 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5086 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 5087 | os_execve_impl(PyModuleDef *module, path_t *path, PyObject *argv, |
| 5088 | PyObject *env) |
| 5089 | /*[clinic end generated code: output=181884fcdb21508e input=626804fa092606d9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5090 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5091 | char **argvlist = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5092 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5093 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5094 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5095 | /* execve has three arguments: (path, argv, env), where |
| 5096 | argv is a list or tuple of strings and env is a dictionary |
| 5097 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5098 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5099 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5100 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5101 | "execve: argv must be a tuple or list"); |
| 5102 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5103 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5104 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5105 | if (!PyMapping_Check(env)) { |
| 5106 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5107 | "execve: environment must be a mapping object"); |
| 5108 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5109 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5110 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5111 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5112 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 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 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5116 | envlist = parse_envlist(env, &envc); |
| 5117 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5118 | goto fail; |
| 5119 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5120 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5121 | if (path->fd > -1) |
| 5122 | fexecve(path->fd, argvlist, envlist); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5123 | else |
| 5124 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5125 | execve(path->narrow, argvlist, envlist); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5126 | |
| 5127 | /* If we get here it's definitely an error */ |
| 5128 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5129 | path_error(path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5130 | |
| 5131 | while (--envc >= 0) |
| 5132 | PyMem_DEL(envlist[envc]); |
| 5133 | PyMem_DEL(envlist); |
| 5134 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5135 | if (argvlist) |
| 5136 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5137 | return NULL; |
| 5138 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5139 | #endif /* HAVE_EXECV */ |
| 5140 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5141 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5142 | #ifdef HAVE_SPAWNV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5143 | /*[clinic input] |
| 5144 | os.spawnv |
| 5145 | |
| 5146 | mode: int |
| 5147 | Mode of process creation. |
| 5148 | path: FSConverter |
| 5149 | Path of executable file. |
| 5150 | argv: object |
| 5151 | Tuple or list of strings. |
| 5152 | / |
| 5153 | |
| 5154 | Execute the program specified by path in a new process. |
| 5155 | [clinic start generated code]*/ |
| 5156 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5157 | static PyObject * |
| 5158 | os_spawnv_impl(PyModuleDef *module, int mode, PyObject *path, PyObject *argv) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5159 | /*[clinic end generated code: output=140a7945484c8cc5 input=042c91dfc1e6debc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5160 | { |
| 5161 | char *path_char; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5162 | char **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5163 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5164 | Py_ssize_t argc; |
| 5165 | Py_intptr_t spawnval; |
| 5166 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5167 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5168 | /* spawnv has three arguments: (mode, path, argv), where |
| 5169 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5170 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5171 | path_char = PyBytes_AsString(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5172 | if (PyList_Check(argv)) { |
| 5173 | argc = PyList_Size(argv); |
| 5174 | getitem = PyList_GetItem; |
| 5175 | } |
| 5176 | else if (PyTuple_Check(argv)) { |
| 5177 | argc = PyTuple_Size(argv); |
| 5178 | getitem = PyTuple_GetItem; |
| 5179 | } |
| 5180 | else { |
| 5181 | PyErr_SetString(PyExc_TypeError, |
| 5182 | "spawnv() arg 2 must be a tuple or list"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5183 | return NULL; |
| 5184 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5185 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5186 | argvlist = PyMem_NEW(char *, argc+1); |
| 5187 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5188 | return PyErr_NoMemory(); |
| 5189 | } |
| 5190 | for (i = 0; i < argc; i++) { |
| 5191 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5192 | &argvlist[i])) { |
| 5193 | free_string_array(argvlist, i); |
| 5194 | PyErr_SetString( |
| 5195 | PyExc_TypeError, |
| 5196 | "spawnv() arg 2 must contain only strings"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5197 | return NULL; |
| 5198 | } |
| 5199 | } |
| 5200 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5201 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5202 | if (mode == _OLD_P_OVERLAY) |
| 5203 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5204 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5205 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5206 | spawnval = _spawnv(mode, path_char, argvlist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5207 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5208 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5209 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5210 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5211 | if (spawnval == -1) |
| 5212 | return posix_error(); |
| 5213 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5214 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5215 | } |
| 5216 | |
| 5217 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5218 | /*[clinic input] |
| 5219 | os.spawnve |
| 5220 | |
| 5221 | mode: int |
| 5222 | Mode of process creation. |
| 5223 | path: FSConverter |
| 5224 | Path of executable file. |
| 5225 | argv: object |
| 5226 | Tuple or list of strings. |
| 5227 | env: object |
| 5228 | Dictionary of strings mapping to strings. |
| 5229 | / |
| 5230 | |
| 5231 | Execute the program specified by path in a new process. |
| 5232 | [clinic start generated code]*/ |
| 5233 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5234 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 5235 | os_spawnve_impl(PyModuleDef *module, int mode, PyObject *path, |
| 5236 | PyObject *argv, PyObject *env) |
| 5237 | /*[clinic end generated code: output=e7f5f0703610531f input=02362fd937963f8f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5238 | { |
| 5239 | char *path_char; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5240 | char **argvlist; |
| 5241 | char **envlist; |
| 5242 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5243 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5244 | Py_intptr_t spawnval; |
| 5245 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 5246 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5247 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5248 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5249 | argv is a list or tuple of strings and env is a dictionary |
| 5250 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5251 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5252 | path_char = PyBytes_AsString(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5253 | if (PyList_Check(argv)) { |
| 5254 | argc = PyList_Size(argv); |
| 5255 | getitem = PyList_GetItem; |
| 5256 | } |
| 5257 | else if (PyTuple_Check(argv)) { |
| 5258 | argc = PyTuple_Size(argv); |
| 5259 | getitem = PyTuple_GetItem; |
| 5260 | } |
| 5261 | else { |
| 5262 | PyErr_SetString(PyExc_TypeError, |
| 5263 | "spawnve() arg 2 must be a tuple or list"); |
| 5264 | goto fail_0; |
| 5265 | } |
| 5266 | if (!PyMapping_Check(env)) { |
| 5267 | PyErr_SetString(PyExc_TypeError, |
| 5268 | "spawnve() arg 3 must be a mapping object"); |
| 5269 | goto fail_0; |
| 5270 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5271 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5272 | argvlist = PyMem_NEW(char *, argc+1); |
| 5273 | if (argvlist == NULL) { |
| 5274 | PyErr_NoMemory(); |
| 5275 | goto fail_0; |
| 5276 | } |
| 5277 | for (i = 0; i < argc; i++) { |
| 5278 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5279 | &argvlist[i])) |
| 5280 | { |
| 5281 | lastarg = i; |
| 5282 | goto fail_1; |
| 5283 | } |
| 5284 | } |
| 5285 | lastarg = argc; |
| 5286 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5287 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5288 | envlist = parse_envlist(env, &envc); |
| 5289 | if (envlist == NULL) |
| 5290 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5291 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5292 | if (mode == _OLD_P_OVERLAY) |
| 5293 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5294 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5295 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5296 | spawnval = _spawnve(mode, path_char, argvlist, envlist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5297 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5298 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5299 | if (spawnval == -1) |
| 5300 | (void) posix_error(); |
| 5301 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5302 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5303 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5304 | while (--envc >= 0) |
| 5305 | PyMem_DEL(envlist[envc]); |
| 5306 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5307 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5308 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5309 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5310 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5311 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5312 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5313 | #endif /* HAVE_SPAWNV */ |
| 5314 | |
| 5315 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5316 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5317 | /*[clinic input] |
| 5318 | os.fork1 |
| 5319 | |
| 5320 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 5321 | |
| 5322 | Return 0 to child process and PID of child to parent process. |
| 5323 | [clinic start generated code]*/ |
| 5324 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5325 | static PyObject * |
| 5326 | os_fork1_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5327 | /*[clinic end generated code: output=e27b4f66419c9dcf input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5328 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5329 | pid_t pid; |
| 5330 | int result = 0; |
| 5331 | _PyImport_AcquireLock(); |
| 5332 | pid = fork1(); |
| 5333 | if (pid == 0) { |
| 5334 | /* child: this clobbers and resets the import lock. */ |
| 5335 | PyOS_AfterFork(); |
| 5336 | } else { |
| 5337 | /* parent: release the import lock. */ |
| 5338 | result = _PyImport_ReleaseLock(); |
| 5339 | } |
| 5340 | if (pid == -1) |
| 5341 | return posix_error(); |
| 5342 | if (result < 0) { |
| 5343 | /* Don't clobber the OSError if the fork failed. */ |
| 5344 | PyErr_SetString(PyExc_RuntimeError, |
| 5345 | "not holding the import lock"); |
| 5346 | return NULL; |
| 5347 | } |
| 5348 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5349 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5350 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5351 | |
| 5352 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5353 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5354 | /*[clinic input] |
| 5355 | os.fork |
| 5356 | |
| 5357 | Fork a child process. |
| 5358 | |
| 5359 | Return 0 to child process and PID of child to parent process. |
| 5360 | [clinic start generated code]*/ |
| 5361 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5362 | static PyObject * |
| 5363 | os_fork_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5364 | /*[clinic end generated code: output=898b1ecd3498ba12 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5365 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5366 | pid_t pid; |
| 5367 | int result = 0; |
| 5368 | _PyImport_AcquireLock(); |
| 5369 | pid = fork(); |
| 5370 | if (pid == 0) { |
| 5371 | /* child: this clobbers and resets the import lock. */ |
| 5372 | PyOS_AfterFork(); |
| 5373 | } else { |
| 5374 | /* parent: release the import lock. */ |
| 5375 | result = _PyImport_ReleaseLock(); |
| 5376 | } |
| 5377 | if (pid == -1) |
| 5378 | return posix_error(); |
| 5379 | if (result < 0) { |
| 5380 | /* Don't clobber the OSError if the fork failed. */ |
| 5381 | PyErr_SetString(PyExc_RuntimeError, |
| 5382 | "not holding the import lock"); |
| 5383 | return NULL; |
| 5384 | } |
| 5385 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5386 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5387 | #endif /* HAVE_FORK */ |
| 5388 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5389 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5390 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5391 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5392 | /*[clinic input] |
| 5393 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5394 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5395 | policy: int |
| 5396 | |
| 5397 | Get the maximum scheduling priority for policy. |
| 5398 | [clinic start generated code]*/ |
| 5399 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5400 | static PyObject * |
| 5401 | os_sched_get_priority_max_impl(PyModuleDef *module, int policy) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5402 | /*[clinic end generated code: output=a6a30fa5071f2d81 input=2097b7998eca6874]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5403 | { |
| 5404 | int max; |
| 5405 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5406 | max = sched_get_priority_max(policy); |
| 5407 | if (max < 0) |
| 5408 | return posix_error(); |
| 5409 | return PyLong_FromLong(max); |
| 5410 | } |
| 5411 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5412 | |
| 5413 | /*[clinic input] |
| 5414 | os.sched_get_priority_min |
| 5415 | |
| 5416 | policy: int |
| 5417 | |
| 5418 | Get the minimum scheduling priority for policy. |
| 5419 | [clinic start generated code]*/ |
| 5420 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5421 | static PyObject * |
| 5422 | os_sched_get_priority_min_impl(PyModuleDef *module, int policy) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5423 | /*[clinic end generated code: output=5ca3ed6bc43e9b20 input=21bc8fa0d70983bf]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5424 | { |
| 5425 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5426 | if (min < 0) |
| 5427 | return posix_error(); |
| 5428 | return PyLong_FromLong(min); |
| 5429 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5430 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 5431 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5432 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5433 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5434 | /*[clinic input] |
| 5435 | os.sched_getscheduler |
| 5436 | pid: pid_t |
| 5437 | / |
| 5438 | |
| 5439 | Get the scheduling policy for the process identifiedy by pid. |
| 5440 | |
| 5441 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 5442 | [clinic start generated code]*/ |
| 5443 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5444 | static PyObject * |
| 5445 | os_sched_getscheduler_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5446 | /*[clinic end generated code: output=8cd63c15caf54fa9 input=5f14cfd1f189e1a0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5447 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5448 | int policy; |
| 5449 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5450 | policy = sched_getscheduler(pid); |
| 5451 | if (policy < 0) |
| 5452 | return posix_error(); |
| 5453 | return PyLong_FromLong(policy); |
| 5454 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5455 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5456 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5457 | |
| 5458 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5459 | /*[clinic input] |
| 5460 | class os.sched_param "PyObject *" "&SchedParamType" |
| 5461 | |
| 5462 | @classmethod |
| 5463 | os.sched_param.__new__ |
| 5464 | |
| 5465 | sched_priority: object |
| 5466 | A scheduling parameter. |
| 5467 | |
| 5468 | Current has only one field: sched_priority"); |
| 5469 | [clinic start generated code]*/ |
| 5470 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5471 | static PyObject * |
| 5472 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5473 | /*[clinic end generated code: output=48f4067d60f48c13 input=73a4c22f7071fc62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5474 | { |
| 5475 | PyObject *res; |
| 5476 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5477 | res = PyStructSequence_New(type); |
| 5478 | if (!res) |
| 5479 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5480 | Py_INCREF(sched_priority); |
| 5481 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5482 | return res; |
| 5483 | } |
| 5484 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5485 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5486 | PyDoc_VAR(os_sched_param__doc__); |
| 5487 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5488 | static PyStructSequence_Field sched_param_fields[] = { |
| 5489 | {"sched_priority", "the scheduling priority"}, |
| 5490 | {0} |
| 5491 | }; |
| 5492 | |
| 5493 | static PyStructSequence_Desc sched_param_desc = { |
| 5494 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5495 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5496 | sched_param_fields, |
| 5497 | 1 |
| 5498 | }; |
| 5499 | |
| 5500 | static int |
| 5501 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 5502 | { |
| 5503 | long priority; |
| 5504 | |
| 5505 | if (Py_TYPE(param) != &SchedParamType) { |
| 5506 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 5507 | return 0; |
| 5508 | } |
| 5509 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 5510 | if (priority == -1 && PyErr_Occurred()) |
| 5511 | return 0; |
| 5512 | if (priority > INT_MAX || priority < INT_MIN) { |
| 5513 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 5514 | return 0; |
| 5515 | } |
| 5516 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 5517 | return 1; |
| 5518 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5519 | #endif /* defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5520 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5521 | |
| 5522 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5523 | /*[clinic input] |
| 5524 | os.sched_setscheduler |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5525 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5526 | pid: pid_t |
| 5527 | policy: int |
| 5528 | param: sched_param |
| 5529 | / |
| 5530 | |
| 5531 | Set the scheduling policy for the process identified by pid. |
| 5532 | |
| 5533 | If pid is 0, the calling process is changed. |
| 5534 | param is an instance of sched_param. |
| 5535 | [clinic start generated code]*/ |
| 5536 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5537 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 5538 | os_sched_setscheduler_impl(PyModuleDef *module, pid_t pid, int policy, |
| 5539 | struct sched_param *param) |
| 5540 | /*[clinic end generated code: output=37053e5c528c35c9 input=c581f9469a5327dd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5541 | { |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5542 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 5543 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 5544 | ** scheduling policy under Solaris/Illumos, and others. |
| 5545 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5546 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5547 | if (sched_setscheduler(pid, policy, param) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5548 | return posix_error(); |
| 5549 | Py_RETURN_NONE; |
| 5550 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5551 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5552 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5553 | |
| 5554 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5555 | /*[clinic input] |
| 5556 | os.sched_getparam |
| 5557 | pid: pid_t |
| 5558 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5559 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5560 | Returns scheduling parameters for the process identified by pid. |
| 5561 | |
| 5562 | If pid is 0, returns parameters for the calling process. |
| 5563 | Return value is an instance of sched_param. |
| 5564 | [clinic start generated code]*/ |
| 5565 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5566 | static PyObject * |
| 5567 | os_sched_getparam_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5568 | /*[clinic end generated code: output=f42c5bd2604ecd08 input=18a1ef9c2efae296]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5569 | { |
| 5570 | struct sched_param param; |
| 5571 | PyObject *result; |
| 5572 | PyObject *priority; |
| 5573 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5574 | if (sched_getparam(pid, ¶m)) |
| 5575 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5576 | result = PyStructSequence_New(&SchedParamType); |
| 5577 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5578 | return NULL; |
| 5579 | priority = PyLong_FromLong(param.sched_priority); |
| 5580 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5581 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5582 | return NULL; |
| 5583 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5584 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 5585 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5586 | } |
| 5587 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5588 | |
| 5589 | /*[clinic input] |
| 5590 | os.sched_setparam |
| 5591 | pid: pid_t |
| 5592 | param: sched_param |
| 5593 | / |
| 5594 | |
| 5595 | Set scheduling parameters for the process identified by pid. |
| 5596 | |
| 5597 | If pid is 0, sets parameters for the calling process. |
| 5598 | param should be an instance of sched_param. |
| 5599 | [clinic start generated code]*/ |
| 5600 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5601 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 5602 | os_sched_setparam_impl(PyModuleDef *module, pid_t pid, |
| 5603 | struct sched_param *param) |
| 5604 | /*[clinic end generated code: output=b7a3c589436cec9b input=6b8d6dfcecdc21bd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5605 | { |
| 5606 | if (sched_setparam(pid, param)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5607 | return posix_error(); |
| 5608 | Py_RETURN_NONE; |
| 5609 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5610 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5611 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5612 | |
| 5613 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5614 | /*[clinic input] |
| 5615 | os.sched_rr_get_interval -> double |
| 5616 | pid: pid_t |
| 5617 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5618 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5619 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 5620 | |
| 5621 | Value returned is a float. |
| 5622 | [clinic start generated code]*/ |
| 5623 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5624 | static double |
| 5625 | os_sched_rr_get_interval_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5626 | /*[clinic end generated code: output=7adc137a86dea581 input=2a973da15cca6fae]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5627 | { |
| 5628 | struct timespec interval; |
| 5629 | if (sched_rr_get_interval(pid, &interval)) { |
| 5630 | posix_error(); |
| 5631 | return -1.0; |
| 5632 | } |
| 5633 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 5634 | } |
| 5635 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5636 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5637 | |
| 5638 | /*[clinic input] |
| 5639 | os.sched_yield |
| 5640 | |
| 5641 | Voluntarily relinquish the CPU. |
| 5642 | [clinic start generated code]*/ |
| 5643 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5644 | static PyObject * |
| 5645 | os_sched_yield_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5646 | /*[clinic end generated code: output=d7bd51869c4cb6a8 input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5647 | { |
| 5648 | if (sched_yield()) |
| 5649 | return posix_error(); |
| 5650 | Py_RETURN_NONE; |
| 5651 | } |
| 5652 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5653 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5654 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 5655 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5656 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5657 | /*[clinic input] |
| 5658 | os.sched_setaffinity |
| 5659 | pid: pid_t |
| 5660 | mask : object |
| 5661 | / |
| 5662 | |
| 5663 | Set the CPU affinity of the process identified by pid to mask. |
| 5664 | |
| 5665 | mask should be an iterable of integers identifying CPUs. |
| 5666 | [clinic start generated code]*/ |
| 5667 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5668 | static PyObject * |
| 5669 | os_sched_setaffinity_impl(PyModuleDef *module, pid_t pid, PyObject *mask) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5670 | /*[clinic end generated code: output=582bcbf40d3253a9 input=a0791a597c7085ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5671 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5672 | int ncpus; |
| 5673 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5674 | cpu_set_t *cpu_set = NULL; |
| 5675 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5676 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5677 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5678 | if (iterator == NULL) |
| 5679 | return NULL; |
| 5680 | |
| 5681 | ncpus = NCPUS_START; |
| 5682 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5683 | cpu_set = CPU_ALLOC(ncpus); |
| 5684 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5685 | PyErr_NoMemory(); |
| 5686 | goto error; |
| 5687 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5688 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5689 | |
| 5690 | while ((item = PyIter_Next(iterator))) { |
| 5691 | long cpu; |
| 5692 | if (!PyLong_Check(item)) { |
| 5693 | PyErr_Format(PyExc_TypeError, |
| 5694 | "expected an iterator of ints, " |
| 5695 | "but iterator yielded %R", |
| 5696 | Py_TYPE(item)); |
| 5697 | Py_DECREF(item); |
| 5698 | goto error; |
| 5699 | } |
| 5700 | cpu = PyLong_AsLong(item); |
| 5701 | Py_DECREF(item); |
| 5702 | if (cpu < 0) { |
| 5703 | if (!PyErr_Occurred()) |
| 5704 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 5705 | goto error; |
| 5706 | } |
| 5707 | if (cpu > INT_MAX - 1) { |
| 5708 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 5709 | goto error; |
| 5710 | } |
| 5711 | if (cpu >= ncpus) { |
| 5712 | /* Grow CPU mask to fit the CPU number */ |
| 5713 | int newncpus = ncpus; |
| 5714 | cpu_set_t *newmask; |
| 5715 | size_t newsetsize; |
| 5716 | while (newncpus <= cpu) { |
| 5717 | if (newncpus > INT_MAX / 2) |
| 5718 | newncpus = cpu + 1; |
| 5719 | else |
| 5720 | newncpus = newncpus * 2; |
| 5721 | } |
| 5722 | newmask = CPU_ALLOC(newncpus); |
| 5723 | if (newmask == NULL) { |
| 5724 | PyErr_NoMemory(); |
| 5725 | goto error; |
| 5726 | } |
| 5727 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 5728 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5729 | memcpy(newmask, cpu_set, setsize); |
| 5730 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5731 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5732 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5733 | ncpus = newncpus; |
| 5734 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5735 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5736 | } |
| 5737 | Py_CLEAR(iterator); |
| 5738 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5739 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5740 | posix_error(); |
| 5741 | goto error; |
| 5742 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5743 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5744 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5745 | |
| 5746 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5747 | if (cpu_set) |
| 5748 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5749 | Py_XDECREF(iterator); |
| 5750 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5751 | } |
| 5752 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5753 | |
| 5754 | /*[clinic input] |
| 5755 | os.sched_getaffinity |
| 5756 | pid: pid_t |
| 5757 | / |
| 5758 | |
| 5759 | Return the affinity of the process identified by pid. |
| 5760 | |
| 5761 | The affinity is returned as a set of CPU identifiers. |
| 5762 | [clinic start generated code]*/ |
| 5763 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5764 | static PyObject * |
| 5765 | os_sched_getaffinity_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5766 | /*[clinic end generated code: output=b431a8f310e369e7 input=eaf161936874b8a1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5767 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5768 | int cpu, ncpus, count; |
| 5769 | size_t setsize; |
| 5770 | cpu_set_t *mask = NULL; |
| 5771 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5772 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5773 | ncpus = NCPUS_START; |
| 5774 | while (1) { |
| 5775 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5776 | mask = CPU_ALLOC(ncpus); |
| 5777 | if (mask == NULL) |
| 5778 | return PyErr_NoMemory(); |
| 5779 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 5780 | break; |
| 5781 | CPU_FREE(mask); |
| 5782 | if (errno != EINVAL) |
| 5783 | return posix_error(); |
| 5784 | if (ncpus > INT_MAX / 2) { |
| 5785 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 5786 | "a large enough CPU set"); |
| 5787 | return NULL; |
| 5788 | } |
| 5789 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5790 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5791 | |
| 5792 | res = PySet_New(NULL); |
| 5793 | if (res == NULL) |
| 5794 | goto error; |
| 5795 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 5796 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 5797 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 5798 | --count; |
| 5799 | if (cpu_num == NULL) |
| 5800 | goto error; |
| 5801 | if (PySet_Add(res, cpu_num)) { |
| 5802 | Py_DECREF(cpu_num); |
| 5803 | goto error; |
| 5804 | } |
| 5805 | Py_DECREF(cpu_num); |
| 5806 | } |
| 5807 | } |
| 5808 | CPU_FREE(mask); |
| 5809 | return res; |
| 5810 | |
| 5811 | error: |
| 5812 | if (mask) |
| 5813 | CPU_FREE(mask); |
| 5814 | Py_XDECREF(res); |
| 5815 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5816 | } |
| 5817 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5818 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5819 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5820 | #endif /* HAVE_SCHED_H */ |
| 5821 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5822 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5823 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5824 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5825 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5826 | #define DEV_PTY_FILE "/dev/ptc" |
| 5827 | #define HAVE_DEV_PTMX |
| 5828 | #else |
| 5829 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5830 | #endif |
| 5831 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5832 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5833 | #ifdef HAVE_PTY_H |
| 5834 | #include <pty.h> |
| 5835 | #else |
| 5836 | #ifdef HAVE_LIBUTIL_H |
| 5837 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5838 | #else |
| 5839 | #ifdef HAVE_UTIL_H |
| 5840 | #include <util.h> |
| 5841 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5842 | #endif /* HAVE_LIBUTIL_H */ |
| 5843 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5844 | #ifdef HAVE_STROPTS_H |
| 5845 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5846 | #endif |
| 5847 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5848 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5849 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5850 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5851 | /*[clinic input] |
| 5852 | os.openpty |
| 5853 | |
| 5854 | Open a pseudo-terminal. |
| 5855 | |
| 5856 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 5857 | for both the master and slave ends. |
| 5858 | [clinic start generated code]*/ |
| 5859 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5860 | static PyObject * |
| 5861 | os_openpty_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5862 | /*[clinic end generated code: output=358e571c1ba135ee input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5863 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5864 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5865 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5866 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5867 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5868 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5869 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5870 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5871 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5872 | #endif |
| 5873 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5874 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5875 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5876 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5877 | goto posix_error; |
| 5878 | |
| 5879 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5880 | goto error; |
| 5881 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 5882 | goto error; |
| 5883 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5884 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5885 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5886 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5887 | goto posix_error; |
| 5888 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5889 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5890 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5891 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5892 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 5893 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5894 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5895 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 5896 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5897 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5898 | goto posix_error; |
| 5899 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5900 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5901 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5902 | /* change permission of slave */ |
| 5903 | if (grantpt(master_fd) < 0) { |
| 5904 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5905 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5906 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5907 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5908 | /* unlock slave */ |
| 5909 | if (unlockpt(master_fd) < 0) { |
| 5910 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5911 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5912 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5913 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5914 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5915 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5916 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 5917 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5918 | goto posix_error; |
| 5919 | |
| 5920 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 5921 | if (slave_fd == -1) |
| 5922 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 5923 | |
| 5924 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5925 | goto posix_error; |
| 5926 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5927 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5928 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 5929 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5930 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5931 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5932 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5933 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 5934 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5935 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5936 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5937 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5938 | posix_error: |
| 5939 | posix_error(); |
| 5940 | error: |
| 5941 | if (master_fd != -1) |
| 5942 | close(master_fd); |
| 5943 | if (slave_fd != -1) |
| 5944 | close(slave_fd); |
| 5945 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5946 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5947 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5948 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5949 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5950 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5951 | /*[clinic input] |
| 5952 | os.forkpty |
| 5953 | |
| 5954 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 5955 | |
| 5956 | Returns a tuple of (pid, master_fd). |
| 5957 | Like fork(), return pid of 0 to the child process, |
| 5958 | and pid of child to the parent process. |
| 5959 | To both, return fd of newly opened pseudo-terminal. |
| 5960 | [clinic start generated code]*/ |
| 5961 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5962 | static PyObject * |
| 5963 | os_forkpty_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5964 | /*[clinic end generated code: output=a11b8391dce3cb57 input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5965 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5966 | int master_fd = -1, result = 0; |
| 5967 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5968 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5969 | _PyImport_AcquireLock(); |
| 5970 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 5971 | if (pid == 0) { |
| 5972 | /* child: this clobbers and resets the import lock. */ |
| 5973 | PyOS_AfterFork(); |
| 5974 | } else { |
| 5975 | /* parent: release the import lock. */ |
| 5976 | result = _PyImport_ReleaseLock(); |
| 5977 | } |
| 5978 | if (pid == -1) |
| 5979 | return posix_error(); |
| 5980 | if (result < 0) { |
| 5981 | /* Don't clobber the OSError if the fork failed. */ |
| 5982 | PyErr_SetString(PyExc_RuntimeError, |
| 5983 | "not holding the import lock"); |
| 5984 | return NULL; |
| 5985 | } |
| 5986 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5987 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5988 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5989 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5990 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5991 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5992 | /*[clinic input] |
| 5993 | os.getegid |
| 5994 | |
| 5995 | Return the current process's effective group id. |
| 5996 | [clinic start generated code]*/ |
| 5997 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5998 | static PyObject * |
| 5999 | os_getegid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6000 | /*[clinic end generated code: output=90f433a8c0b1d919 input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6001 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6002 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6003 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6004 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6005 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6006 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6007 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6008 | /*[clinic input] |
| 6009 | os.geteuid |
| 6010 | |
| 6011 | Return the current process's effective user id. |
| 6012 | [clinic start generated code]*/ |
| 6013 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6014 | static PyObject * |
| 6015 | os_geteuid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6016 | /*[clinic end generated code: output=1a532c4a66874357 input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6017 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6018 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6019 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6020 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6021 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6022 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6023 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6024 | /*[clinic input] |
| 6025 | os.getgid |
| 6026 | |
| 6027 | Return the current process's group id. |
| 6028 | [clinic start generated code]*/ |
| 6029 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6030 | static PyObject * |
| 6031 | os_getgid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6032 | /*[clinic end generated code: output=91a22021b74ea46b input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6033 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6034 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6035 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6036 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6037 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6038 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6039 | /*[clinic input] |
| 6040 | os.getpid |
| 6041 | |
| 6042 | Return the current process id. |
| 6043 | [clinic start generated code]*/ |
| 6044 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6045 | static PyObject * |
| 6046 | os_getpid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6047 | /*[clinic end generated code: output=8fbf3a934ee09e62 input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6048 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6049 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6050 | } |
| 6051 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6052 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6053 | |
| 6054 | /* AC 3.5: funny apple logic below */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6055 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 6056 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 6057 | Returns a list of groups to which a user belongs.\n\n\ |
| 6058 | user: username to lookup\n\ |
| 6059 | group: base group id of the user"); |
| 6060 | |
| 6061 | static PyObject * |
| 6062 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 6063 | { |
| 6064 | #ifdef NGROUPS_MAX |
| 6065 | #define MAX_GROUPS NGROUPS_MAX |
| 6066 | #else |
| 6067 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6068 | #define MAX_GROUPS 64 |
| 6069 | #endif |
| 6070 | |
| 6071 | const char *user; |
| 6072 | int i, ngroups; |
| 6073 | PyObject *list; |
| 6074 | #ifdef __APPLE__ |
| 6075 | int *groups, basegid; |
| 6076 | #else |
| 6077 | gid_t *groups, basegid; |
| 6078 | #endif |
| 6079 | ngroups = MAX_GROUPS; |
| 6080 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6081 | #ifdef __APPLE__ |
| 6082 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6083 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6084 | #else |
| 6085 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 6086 | _Py_Gid_Converter, &basegid)) |
| 6087 | return NULL; |
| 6088 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6089 | |
| 6090 | #ifdef __APPLE__ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6091 | groups = PyMem_New(int, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6092 | #else |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6093 | groups = PyMem_New(gid_t, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6094 | #endif |
| 6095 | if (groups == NULL) |
| 6096 | return PyErr_NoMemory(); |
| 6097 | |
| 6098 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 6099 | PyMem_Del(groups); |
| 6100 | return posix_error(); |
| 6101 | } |
| 6102 | |
| 6103 | list = PyList_New(ngroups); |
| 6104 | if (list == NULL) { |
| 6105 | PyMem_Del(groups); |
| 6106 | return NULL; |
| 6107 | } |
| 6108 | |
| 6109 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6110 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6111 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6112 | #else |
| 6113 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 6114 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6115 | if (o == NULL) { |
| 6116 | Py_DECREF(list); |
| 6117 | PyMem_Del(groups); |
| 6118 | return NULL; |
| 6119 | } |
| 6120 | PyList_SET_ITEM(list, i, o); |
| 6121 | } |
| 6122 | |
| 6123 | PyMem_Del(groups); |
| 6124 | |
| 6125 | return list; |
| 6126 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6127 | #endif /* HAVE_GETGROUPLIST */ |
| 6128 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6129 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6130 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6131 | /*[clinic input] |
| 6132 | os.getgroups |
| 6133 | |
| 6134 | Return list of supplemental group IDs for the process. |
| 6135 | [clinic start generated code]*/ |
| 6136 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6137 | static PyObject * |
| 6138 | os_getgroups_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6139 | /*[clinic end generated code: output=6e7c4fd2db6d5c60 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6140 | { |
| 6141 | PyObject *result = NULL; |
| 6142 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6143 | #ifdef NGROUPS_MAX |
| 6144 | #define MAX_GROUPS NGROUPS_MAX |
| 6145 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6146 | /* 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] | 6147 | #define MAX_GROUPS 64 |
| 6148 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6149 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6150 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6151 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6152 | * This is a helper variable to store the intermediate result when |
| 6153 | * that happens. |
| 6154 | * |
| 6155 | * To keep the code readable the OSX behaviour is unconditional, |
| 6156 | * according to the POSIX spec this should be safe on all unix-y |
| 6157 | * systems. |
| 6158 | */ |
| 6159 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6160 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6161 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6162 | #ifdef __APPLE__ |
| 6163 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 6164 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 6165 | * always first call getgroups with length 0 to get the actual number |
| 6166 | * of groups. |
| 6167 | */ |
| 6168 | n = getgroups(0, NULL); |
| 6169 | if (n < 0) { |
| 6170 | return posix_error(); |
| 6171 | } else if (n <= MAX_GROUPS) { |
| 6172 | /* groups will fit in existing array */ |
| 6173 | alt_grouplist = grouplist; |
| 6174 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6175 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6176 | if (alt_grouplist == NULL) { |
| 6177 | errno = EINVAL; |
| 6178 | return posix_error(); |
| 6179 | } |
| 6180 | } |
| 6181 | |
| 6182 | n = getgroups(n, alt_grouplist); |
| 6183 | if (n == -1) { |
| 6184 | if (alt_grouplist != grouplist) { |
| 6185 | PyMem_Free(alt_grouplist); |
| 6186 | } |
| 6187 | return posix_error(); |
| 6188 | } |
| 6189 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6190 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6191 | if (n < 0) { |
| 6192 | if (errno == EINVAL) { |
| 6193 | n = getgroups(0, NULL); |
| 6194 | if (n == -1) { |
| 6195 | return posix_error(); |
| 6196 | } |
| 6197 | if (n == 0) { |
| 6198 | /* Avoid malloc(0) */ |
| 6199 | alt_grouplist = grouplist; |
| 6200 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6201 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6202 | if (alt_grouplist == NULL) { |
| 6203 | errno = EINVAL; |
| 6204 | return posix_error(); |
| 6205 | } |
| 6206 | n = getgroups(n, alt_grouplist); |
| 6207 | if (n == -1) { |
| 6208 | PyMem_Free(alt_grouplist); |
| 6209 | return posix_error(); |
| 6210 | } |
| 6211 | } |
| 6212 | } else { |
| 6213 | return posix_error(); |
| 6214 | } |
| 6215 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6216 | #endif |
| 6217 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6218 | result = PyList_New(n); |
| 6219 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6220 | int i; |
| 6221 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6222 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6223 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6224 | Py_DECREF(result); |
| 6225 | result = NULL; |
| 6226 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6227 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6228 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6229 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6230 | } |
| 6231 | |
| 6232 | if (alt_grouplist != grouplist) { |
| 6233 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6234 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6235 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6236 | return result; |
| 6237 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6238 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6239 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6240 | #ifdef HAVE_INITGROUPS |
| 6241 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 6242 | "initgroups(username, gid) -> None\n\n\ |
| 6243 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 6244 | the groups of which the specified username is a member, plus the specified\n\ |
| 6245 | group id."); |
| 6246 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6247 | /* AC 3.5: funny apple logic */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6248 | static PyObject * |
| 6249 | posix_initgroups(PyObject *self, PyObject *args) |
| 6250 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6251 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6252 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6253 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6254 | #ifdef __APPLE__ |
| 6255 | int gid; |
| 6256 | #else |
| 6257 | gid_t gid; |
| 6258 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6259 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6260 | #ifdef __APPLE__ |
| 6261 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 6262 | PyUnicode_FSConverter, &oname, |
| 6263 | &gid)) |
| 6264 | #else |
| 6265 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 6266 | PyUnicode_FSConverter, &oname, |
| 6267 | _Py_Gid_Converter, &gid)) |
| 6268 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6269 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6270 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6271 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6272 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6273 | Py_DECREF(oname); |
| 6274 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6275 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6276 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6277 | Py_INCREF(Py_None); |
| 6278 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6279 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6280 | #endif /* HAVE_INITGROUPS */ |
| 6281 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6282 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6283 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6284 | /*[clinic input] |
| 6285 | os.getpgid |
| 6286 | |
| 6287 | pid: pid_t |
| 6288 | |
| 6289 | Call the system call getpgid(), and return the result. |
| 6290 | [clinic start generated code]*/ |
| 6291 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6292 | static PyObject * |
| 6293 | os_getpgid_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6294 | /*[clinic end generated code: output=70e713b4d54b7c61 input=39d710ae3baaf1c7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6295 | { |
| 6296 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6297 | if (pgid < 0) |
| 6298 | return posix_error(); |
| 6299 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6300 | } |
| 6301 | #endif /* HAVE_GETPGID */ |
| 6302 | |
| 6303 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6304 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6305 | /*[clinic input] |
| 6306 | os.getpgrp |
| 6307 | |
| 6308 | Return the current process group id. |
| 6309 | [clinic start generated code]*/ |
| 6310 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6311 | static PyObject * |
| 6312 | os_getpgrp_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6313 | /*[clinic end generated code: output=cf3403585846811f input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6314 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6315 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6316 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6317 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6318 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6319 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6320 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6321 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6322 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6323 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6324 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6325 | /*[clinic input] |
| 6326 | os.setpgrp |
| 6327 | |
| 6328 | Make the current process the leader of its process group. |
| 6329 | [clinic start generated code]*/ |
| 6330 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6331 | static PyObject * |
| 6332 | os_setpgrp_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6333 | /*[clinic end generated code: output=59650f55a963d7ac input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6334 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6335 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6336 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6337 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6338 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6339 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6340 | return posix_error(); |
| 6341 | Py_INCREF(Py_None); |
| 6342 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6343 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6344 | #endif /* HAVE_SETPGRP */ |
| 6345 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6346 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6347 | |
| 6348 | #ifdef MS_WINDOWS |
| 6349 | #include <tlhelp32.h> |
| 6350 | |
| 6351 | static PyObject* |
| 6352 | win32_getppid() |
| 6353 | { |
| 6354 | HANDLE snapshot; |
| 6355 | pid_t mypid; |
| 6356 | PyObject* result = NULL; |
| 6357 | BOOL have_record; |
| 6358 | PROCESSENTRY32 pe; |
| 6359 | |
| 6360 | mypid = getpid(); /* This function never fails */ |
| 6361 | |
| 6362 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 6363 | if (snapshot == INVALID_HANDLE_VALUE) |
| 6364 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 6365 | |
| 6366 | pe.dwSize = sizeof(pe); |
| 6367 | have_record = Process32First(snapshot, &pe); |
| 6368 | while (have_record) { |
| 6369 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 6370 | /* We could cache the ulong value in a static variable. */ |
| 6371 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 6372 | break; |
| 6373 | } |
| 6374 | |
| 6375 | have_record = Process32Next(snapshot, &pe); |
| 6376 | } |
| 6377 | |
| 6378 | /* If our loop exits and our pid was not found (result will be NULL) |
| 6379 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 6380 | * error anyway, so let's raise it. */ |
| 6381 | if (!result) |
| 6382 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6383 | |
| 6384 | CloseHandle(snapshot); |
| 6385 | |
| 6386 | return result; |
| 6387 | } |
| 6388 | #endif /*MS_WINDOWS*/ |
| 6389 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6390 | |
| 6391 | /*[clinic input] |
| 6392 | os.getppid |
| 6393 | |
| 6394 | Return the parent's process id. |
| 6395 | |
| 6396 | If the parent process has already exited, Windows machines will still |
| 6397 | return its id; others systems will return the id of the 'init' process (1). |
| 6398 | [clinic start generated code]*/ |
| 6399 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6400 | static PyObject * |
| 6401 | os_getppid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6402 | /*[clinic end generated code: output=4e49c8e7a8738cd2 input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6403 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6404 | #ifdef MS_WINDOWS |
| 6405 | return win32_getppid(); |
| 6406 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6407 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6408 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6409 | } |
| 6410 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6411 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6412 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6413 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6414 | /*[clinic input] |
| 6415 | os.getlogin |
| 6416 | |
| 6417 | Return the actual login name. |
| 6418 | [clinic start generated code]*/ |
| 6419 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6420 | static PyObject * |
| 6421 | os_getlogin_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6422 | /*[clinic end generated code: output=037ebdb3e4b5dac1 input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6423 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6424 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6425 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6426 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6427 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6428 | |
| 6429 | if (GetUserNameW(user_name, &num_chars)) { |
| 6430 | /* num_chars is the number of unicode chars plus null terminator */ |
| 6431 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6432 | } |
| 6433 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6434 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6435 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6436 | char *name; |
| 6437 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6438 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6439 | errno = 0; |
| 6440 | name = getlogin(); |
| 6441 | if (name == NULL) { |
| 6442 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6443 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6444 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6445 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6446 | } |
| 6447 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6448 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6449 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6450 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6451 | return result; |
| 6452 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6453 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6454 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6455 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6456 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6457 | /*[clinic input] |
| 6458 | os.getuid |
| 6459 | |
| 6460 | Return the current process's user id. |
| 6461 | [clinic start generated code]*/ |
| 6462 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6463 | static PyObject * |
| 6464 | os_getuid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6465 | /*[clinic end generated code: output=03a8b894cefb3fa5 input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6466 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6467 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6468 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6469 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6470 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6471 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6472 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6473 | #define HAVE_KILL |
| 6474 | #endif /* MS_WINDOWS */ |
| 6475 | |
| 6476 | #ifdef HAVE_KILL |
| 6477 | /*[clinic input] |
| 6478 | os.kill |
| 6479 | |
| 6480 | pid: pid_t |
| 6481 | signal: Py_ssize_t |
| 6482 | / |
| 6483 | |
| 6484 | Kill a process with a signal. |
| 6485 | [clinic start generated code]*/ |
| 6486 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6487 | static PyObject * |
| 6488 | os_kill_impl(PyModuleDef *module, pid_t pid, Py_ssize_t signal) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6489 | /*[clinic end generated code: output=74f907dd00a83c26 input=61a36b86ca275ab9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6490 | #ifndef MS_WINDOWS |
| 6491 | { |
| 6492 | if (kill(pid, (int)signal) == -1) |
| 6493 | return posix_error(); |
| 6494 | Py_RETURN_NONE; |
| 6495 | } |
| 6496 | #else /* !MS_WINDOWS */ |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6497 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 6498 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6499 | DWORD sig = (DWORD)signal; |
| 6500 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6501 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6502 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6503 | /* Console processes which share a common console can be sent CTRL+C or |
| 6504 | CTRL+BREAK events, provided they handle said events. */ |
| 6505 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6506 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6507 | err = GetLastError(); |
| 6508 | PyErr_SetFromWindowsErr(err); |
| 6509 | } |
| 6510 | else |
| 6511 | Py_RETURN_NONE; |
| 6512 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6513 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6514 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 6515 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6516 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6517 | if (handle == NULL) { |
| 6518 | err = GetLastError(); |
| 6519 | return PyErr_SetFromWindowsErr(err); |
| 6520 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6521 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6522 | if (TerminateProcess(handle, sig) == 0) { |
| 6523 | err = GetLastError(); |
| 6524 | result = PyErr_SetFromWindowsErr(err); |
| 6525 | } else { |
| 6526 | Py_INCREF(Py_None); |
| 6527 | result = Py_None; |
| 6528 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6529 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6530 | CloseHandle(handle); |
| 6531 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6532 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6533 | #endif /* !MS_WINDOWS */ |
| 6534 | #endif /* HAVE_KILL */ |
| 6535 | |
| 6536 | |
| 6537 | #ifdef HAVE_KILLPG |
| 6538 | /*[clinic input] |
| 6539 | os.killpg |
| 6540 | |
| 6541 | pgid: pid_t |
| 6542 | signal: int |
| 6543 | / |
| 6544 | |
| 6545 | Kill a process group with a signal. |
| 6546 | [clinic start generated code]*/ |
| 6547 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6548 | static PyObject * |
| 6549 | os_killpg_impl(PyModuleDef *module, pid_t pgid, int signal) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6550 | /*[clinic end generated code: output=3434a766ef945f93 input=38b5449eb8faec19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6551 | { |
| 6552 | /* XXX some man pages make the `pgid` parameter an int, others |
| 6553 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 6554 | take the same type. Moreover, pid_t is always at least as wide as |
| 6555 | int (else compilation of this module fails), which is safe. */ |
| 6556 | if (killpg(pgid, signal) == -1) |
| 6557 | return posix_error(); |
| 6558 | Py_RETURN_NONE; |
| 6559 | } |
| 6560 | #endif /* HAVE_KILLPG */ |
| 6561 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6562 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6563 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6564 | #ifdef HAVE_SYS_LOCK_H |
| 6565 | #include <sys/lock.h> |
| 6566 | #endif |
| 6567 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6568 | /*[clinic input] |
| 6569 | os.plock |
| 6570 | op: int |
| 6571 | / |
| 6572 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6573 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6574 | [clinic start generated code]*/ |
| 6575 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6576 | static PyObject * |
| 6577 | os_plock_impl(PyModuleDef *module, int op) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6578 | /*[clinic end generated code: output=5cb851f81b914984 input=e6e5e348e1525f60]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6579 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6580 | if (plock(op) == -1) |
| 6581 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6582 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6583 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6584 | #endif /* HAVE_PLOCK */ |
| 6585 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6586 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6587 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6588 | /*[clinic input] |
| 6589 | os.setuid |
| 6590 | |
| 6591 | uid: uid_t |
| 6592 | / |
| 6593 | |
| 6594 | Set the current process's user id. |
| 6595 | [clinic start generated code]*/ |
| 6596 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6597 | static PyObject * |
| 6598 | os_setuid_impl(PyModuleDef *module, uid_t uid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6599 | /*[clinic end generated code: output=941ea9a8d1e5d565 input=c921a3285aa22256]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6600 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6601 | if (setuid(uid) < 0) |
| 6602 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6603 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6604 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6605 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6606 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6607 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6608 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6609 | /*[clinic input] |
| 6610 | os.seteuid |
| 6611 | |
| 6612 | euid: uid_t |
| 6613 | / |
| 6614 | |
| 6615 | Set the current process's effective user id. |
| 6616 | [clinic start generated code]*/ |
| 6617 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6618 | static PyObject * |
| 6619 | os_seteuid_impl(PyModuleDef *module, uid_t euid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6620 | /*[clinic end generated code: output=66f4f6823a648d6d input=ba93d927e4781aa9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6621 | { |
| 6622 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6623 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6624 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6625 | } |
| 6626 | #endif /* HAVE_SETEUID */ |
| 6627 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6628 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6629 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6630 | /*[clinic input] |
| 6631 | os.setegid |
| 6632 | |
| 6633 | egid: gid_t |
| 6634 | / |
| 6635 | |
| 6636 | Set the current process's effective group id. |
| 6637 | [clinic start generated code]*/ |
| 6638 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6639 | static PyObject * |
| 6640 | os_setegid_impl(PyModuleDef *module, gid_t egid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6641 | /*[clinic end generated code: output=ca094a69a081a60f input=4080526d0ccd6ce3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6642 | { |
| 6643 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6644 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6645 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6646 | } |
| 6647 | #endif /* HAVE_SETEGID */ |
| 6648 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6649 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6650 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6651 | /*[clinic input] |
| 6652 | os.setreuid |
| 6653 | |
| 6654 | ruid: uid_t |
| 6655 | euid: uid_t |
| 6656 | / |
| 6657 | |
| 6658 | Set the current process's real and effective user ids. |
| 6659 | [clinic start generated code]*/ |
| 6660 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6661 | static PyObject * |
| 6662 | os_setreuid_impl(PyModuleDef *module, uid_t ruid, uid_t euid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6663 | /*[clinic end generated code: output=b2938c3e73d27ec7 input=0ca8978de663880c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6664 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6665 | if (setreuid(ruid, euid) < 0) { |
| 6666 | return posix_error(); |
| 6667 | } else { |
| 6668 | Py_INCREF(Py_None); |
| 6669 | return Py_None; |
| 6670 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6671 | } |
| 6672 | #endif /* HAVE_SETREUID */ |
| 6673 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6674 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6675 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6676 | /*[clinic input] |
| 6677 | os.setregid |
| 6678 | |
| 6679 | rgid: gid_t |
| 6680 | egid: gid_t |
| 6681 | / |
| 6682 | |
| 6683 | Set the current process's real and effective group ids. |
| 6684 | [clinic start generated code]*/ |
| 6685 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6686 | static PyObject * |
| 6687 | os_setregid_impl(PyModuleDef *module, gid_t rgid, gid_t egid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6688 | /*[clinic end generated code: output=db18f1839ababe3d input=c59499f72846db78]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6689 | { |
| 6690 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6691 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6692 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6693 | } |
| 6694 | #endif /* HAVE_SETREGID */ |
| 6695 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6696 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6697 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6698 | /*[clinic input] |
| 6699 | os.setgid |
| 6700 | gid: gid_t |
| 6701 | / |
| 6702 | |
| 6703 | Set the current process's group id. |
| 6704 | [clinic start generated code]*/ |
| 6705 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6706 | static PyObject * |
| 6707 | os_setgid_impl(PyModuleDef *module, gid_t gid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6708 | /*[clinic end generated code: output=756cb42c6abd9d87 input=27d30c4059045dc6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6709 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6710 | if (setgid(gid) < 0) |
| 6711 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6712 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6713 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6714 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6715 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6716 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6717 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6718 | /*[clinic input] |
| 6719 | os.setgroups |
| 6720 | |
| 6721 | groups: object |
| 6722 | / |
| 6723 | |
| 6724 | Set the groups of the current process to list. |
| 6725 | [clinic start generated code]*/ |
| 6726 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6727 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6728 | os_setgroups(PyModuleDef *module, PyObject *groups) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6729 | /*[clinic end generated code: output=7945c2e3cc817c58 input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6730 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6731 | int i, len; |
| 6732 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6733 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6734 | if (!PySequence_Check(groups)) { |
| 6735 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6736 | return NULL; |
| 6737 | } |
| 6738 | len = PySequence_Size(groups); |
| 6739 | if (len > MAX_GROUPS) { |
| 6740 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6741 | return NULL; |
| 6742 | } |
| 6743 | for(i = 0; i < len; i++) { |
| 6744 | PyObject *elem; |
| 6745 | elem = PySequence_GetItem(groups, i); |
| 6746 | if (!elem) |
| 6747 | return NULL; |
| 6748 | if (!PyLong_Check(elem)) { |
| 6749 | PyErr_SetString(PyExc_TypeError, |
| 6750 | "groups must be integers"); |
| 6751 | Py_DECREF(elem); |
| 6752 | return NULL; |
| 6753 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6754 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6755 | Py_DECREF(elem); |
| 6756 | return NULL; |
| 6757 | } |
| 6758 | } |
| 6759 | Py_DECREF(elem); |
| 6760 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6761 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6762 | if (setgroups(len, grouplist) < 0) |
| 6763 | return posix_error(); |
| 6764 | Py_INCREF(Py_None); |
| 6765 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6766 | } |
| 6767 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6768 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6769 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6770 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6771 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6772 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6773 | PyObject *result; |
| 6774 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6775 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6776 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6777 | if (pid == -1) |
| 6778 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6779 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6780 | if (struct_rusage == NULL) { |
| 6781 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6782 | if (m == NULL) |
| 6783 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6784 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6785 | Py_DECREF(m); |
| 6786 | if (struct_rusage == NULL) |
| 6787 | return NULL; |
| 6788 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6789 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6790 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6791 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6792 | if (!result) |
| 6793 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6794 | |
| 6795 | #ifndef doubletime |
| 6796 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6797 | #endif |
| 6798 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6799 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6800 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6801 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6802 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6803 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6804 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6805 | SET_INT(result, 2, ru->ru_maxrss); |
| 6806 | SET_INT(result, 3, ru->ru_ixrss); |
| 6807 | SET_INT(result, 4, ru->ru_idrss); |
| 6808 | SET_INT(result, 5, ru->ru_isrss); |
| 6809 | SET_INT(result, 6, ru->ru_minflt); |
| 6810 | SET_INT(result, 7, ru->ru_majflt); |
| 6811 | SET_INT(result, 8, ru->ru_nswap); |
| 6812 | SET_INT(result, 9, ru->ru_inblock); |
| 6813 | SET_INT(result, 10, ru->ru_oublock); |
| 6814 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6815 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6816 | SET_INT(result, 13, ru->ru_nsignals); |
| 6817 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6818 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6819 | #undef SET_INT |
| 6820 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6821 | if (PyErr_Occurred()) { |
| 6822 | Py_DECREF(result); |
| 6823 | return NULL; |
| 6824 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6825 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6826 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6827 | } |
| 6828 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6829 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6830 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6831 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6832 | /*[clinic input] |
| 6833 | os.wait3 |
| 6834 | |
| 6835 | options: int |
| 6836 | Wait for completion of a child process. |
| 6837 | |
| 6838 | Returns a tuple of information about the child process: |
| 6839 | (pid, status, rusage) |
| 6840 | [clinic start generated code]*/ |
| 6841 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6842 | static PyObject * |
| 6843 | os_wait3_impl(PyModuleDef *module, int options) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6844 | /*[clinic end generated code: output=e18af4924dc54945 input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6845 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6846 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6847 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6848 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6849 | WAIT_TYPE status; |
| 6850 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6851 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6852 | do { |
| 6853 | Py_BEGIN_ALLOW_THREADS |
| 6854 | pid = wait3(&status, options, &ru); |
| 6855 | Py_END_ALLOW_THREADS |
| 6856 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6857 | if (pid < 0) |
| 6858 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6859 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6860 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6861 | } |
| 6862 | #endif /* HAVE_WAIT3 */ |
| 6863 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6864 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6865 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6866 | /*[clinic input] |
| 6867 | |
| 6868 | os.wait4 |
| 6869 | |
| 6870 | pid: pid_t |
| 6871 | options: int |
| 6872 | |
| 6873 | Wait for completion of a specific child process. |
| 6874 | |
| 6875 | Returns a tuple of information about the child process: |
| 6876 | (pid, status, rusage) |
| 6877 | [clinic start generated code]*/ |
| 6878 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6879 | static PyObject * |
| 6880 | os_wait4_impl(PyModuleDef *module, pid_t pid, int options) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6881 | /*[clinic end generated code: output=714f19e6ff01e099 input=d11deed0750600ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6882 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6883 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6884 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6885 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6886 | WAIT_TYPE status; |
| 6887 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6888 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6889 | do { |
| 6890 | Py_BEGIN_ALLOW_THREADS |
| 6891 | res = wait4(pid, &status, options, &ru); |
| 6892 | Py_END_ALLOW_THREADS |
| 6893 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6894 | if (res < 0) |
| 6895 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6896 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6897 | return wait_helper(res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6898 | } |
| 6899 | #endif /* HAVE_WAIT4 */ |
| 6900 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6901 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6902 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6903 | /*[clinic input] |
| 6904 | os.waitid |
| 6905 | |
| 6906 | idtype: idtype_t |
| 6907 | Must be one of be P_PID, P_PGID or P_ALL. |
| 6908 | id: id_t |
| 6909 | The id to wait on. |
| 6910 | options: int |
| 6911 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 6912 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 6913 | / |
| 6914 | |
| 6915 | Returns the result of waiting for a process or processes. |
| 6916 | |
| 6917 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 6918 | no children in a waitable state. |
| 6919 | [clinic start generated code]*/ |
| 6920 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6921 | static PyObject * |
| 6922 | 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] | 6923 | /*[clinic end generated code: output=5c0192750e22fa2e input=d8e7f76e052b7920]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6924 | { |
| 6925 | PyObject *result; |
| 6926 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6927 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6928 | siginfo_t si; |
| 6929 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6930 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6931 | do { |
| 6932 | Py_BEGIN_ALLOW_THREADS |
| 6933 | res = waitid(idtype, id, &si, options); |
| 6934 | Py_END_ALLOW_THREADS |
| 6935 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6936 | if (res < 0) |
| 6937 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6938 | |
| 6939 | if (si.si_pid == 0) |
| 6940 | Py_RETURN_NONE; |
| 6941 | |
| 6942 | result = PyStructSequence_New(&WaitidResultType); |
| 6943 | if (!result) |
| 6944 | return NULL; |
| 6945 | |
| 6946 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6947 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6948 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6949 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6950 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6951 | if (PyErr_Occurred()) { |
| 6952 | Py_DECREF(result); |
| 6953 | return NULL; |
| 6954 | } |
| 6955 | |
| 6956 | return result; |
| 6957 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6958 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6959 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6960 | |
| 6961 | #if defined(HAVE_WAITPID) |
| 6962 | /*[clinic input] |
| 6963 | os.waitpid |
| 6964 | pid: pid_t |
| 6965 | options: int |
| 6966 | / |
| 6967 | |
| 6968 | Wait for completion of a given child process. |
| 6969 | |
| 6970 | Returns a tuple of information regarding the child process: |
| 6971 | (pid, status) |
| 6972 | |
| 6973 | The options argument is ignored on Windows. |
| 6974 | [clinic start generated code]*/ |
| 6975 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6976 | static PyObject * |
| 6977 | os_waitpid_impl(PyModuleDef *module, pid_t pid, int options) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6978 | /*[clinic end generated code: output=5e3593353d54b15b input=0bf1666b8758fda3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6979 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6980 | pid_t res; |
| 6981 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6982 | WAIT_TYPE status; |
| 6983 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6984 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6985 | do { |
| 6986 | Py_BEGIN_ALLOW_THREADS |
| 6987 | res = waitpid(pid, &status, options); |
| 6988 | Py_END_ALLOW_THREADS |
| 6989 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6990 | if (res < 0) |
| 6991 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6992 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6993 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6994 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6995 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6996 | /* 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] | 6997 | /*[clinic input] |
| 6998 | os.waitpid |
| 6999 | pid: Py_intptr_t |
| 7000 | options: int |
| 7001 | / |
| 7002 | |
| 7003 | Wait for completion of a given process. |
| 7004 | |
| 7005 | Returns a tuple of information regarding the process: |
| 7006 | (pid, status << 8) |
| 7007 | |
| 7008 | The options argument is ignored on Windows. |
| 7009 | [clinic start generated code]*/ |
| 7010 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7011 | static PyObject * |
| 7012 | os_waitpid_impl(PyModuleDef *module, Py_intptr_t pid, int options) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7013 | /*[clinic end generated code: output=fc1d520db019625f input=444c8f51cca5b862]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7014 | { |
| 7015 | int status; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7016 | Py_intptr_t res; |
| 7017 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7018 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7019 | do { |
| 7020 | Py_BEGIN_ALLOW_THREADS |
| 7021 | res = _cwait(&status, pid, options); |
| 7022 | Py_END_ALLOW_THREADS |
| 7023 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 7024 | if (res < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7025 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7026 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7027 | /* 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] | 7028 | return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7029 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7030 | #endif |
| 7031 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7032 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7033 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7034 | /*[clinic input] |
| 7035 | os.wait |
| 7036 | |
| 7037 | Wait for completion of a child process. |
| 7038 | |
| 7039 | Returns a tuple of information about the child process: |
| 7040 | (pid, status) |
| 7041 | [clinic start generated code]*/ |
| 7042 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7043 | static PyObject * |
| 7044 | os_wait_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7045 | /*[clinic end generated code: output=4a7f4978393e0654 input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7046 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7047 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7048 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7049 | WAIT_TYPE status; |
| 7050 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7051 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7052 | do { |
| 7053 | Py_BEGIN_ALLOW_THREADS |
| 7054 | pid = wait(&status); |
| 7055 | Py_END_ALLOW_THREADS |
| 7056 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7057 | if (pid < 0) |
| 7058 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7059 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7060 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7061 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7062 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7063 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7064 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7065 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
| 7066 | PyDoc_STRVAR(readlink__doc__, |
| 7067 | "readlink(path, *, dir_fd=None) -> path\n\n\ |
| 7068 | Return a string representing the path to which the symbolic link points.\n\ |
| 7069 | \n\ |
| 7070 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7071 | and path should be relative; path will then be relative to that directory.\n\ |
| 7072 | dir_fd may not be implemented on your platform.\n\ |
| 7073 | If it is unavailable, using it will raise a NotImplementedError."); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7074 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7075 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7076 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7077 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7078 | /* AC 3.5: merge win32 and not together */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7079 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7080 | posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7081 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7082 | path_t path; |
| 7083 | int dir_fd = DEFAULT_DIR_FD; |
| 7084 | char buffer[MAXPATHLEN]; |
| 7085 | ssize_t length; |
| 7086 | PyObject *return_value = NULL; |
| 7087 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7088 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7089 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7090 | path.function_name = "readlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7091 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords, |
| 7092 | path_converter, &path, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7093 | READLINKAT_DIR_FD_CONVERTER, &dir_fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7094 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7095 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7096 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7097 | #ifdef HAVE_READLINKAT |
| 7098 | if (dir_fd != DEFAULT_DIR_FD) |
| 7099 | length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer)); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 7100 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7101 | #endif |
| 7102 | length = readlink(path.narrow, buffer, sizeof(buffer)); |
| 7103 | Py_END_ALLOW_THREADS |
| 7104 | |
| 7105 | if (length < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7106 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7107 | goto exit; |
| 7108 | } |
| 7109 | |
| 7110 | if (PyUnicode_Check(path.object)) |
| 7111 | return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 7112 | else |
| 7113 | return_value = PyBytes_FromStringAndSize(buffer, length); |
| 7114 | exit: |
| 7115 | path_cleanup(&path); |
| 7116 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7117 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7118 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7119 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7120 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7121 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 7122 | |
| 7123 | static PyObject * |
| 7124 | win_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
| 7125 | { |
| 7126 | wchar_t *path; |
| 7127 | DWORD n_bytes_returned; |
| 7128 | DWORD io_result; |
| 7129 | PyObject *po, *result; |
| 7130 | int dir_fd; |
| 7131 | HANDLE reparse_point_handle; |
| 7132 | |
| 7133 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 7134 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 7135 | wchar_t *print_name; |
| 7136 | |
| 7137 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 7138 | |
| 7139 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords, |
| 7140 | &po, |
| 7141 | dir_fd_unavailable, &dir_fd |
| 7142 | )) |
| 7143 | return NULL; |
| 7144 | |
| 7145 | path = PyUnicode_AsUnicode(po); |
| 7146 | if (path == NULL) |
| 7147 | return NULL; |
| 7148 | |
| 7149 | /* First get a handle to the reparse point */ |
| 7150 | Py_BEGIN_ALLOW_THREADS |
| 7151 | reparse_point_handle = CreateFileW( |
| 7152 | path, |
| 7153 | 0, |
| 7154 | 0, |
| 7155 | 0, |
| 7156 | OPEN_EXISTING, |
| 7157 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 7158 | 0); |
| 7159 | Py_END_ALLOW_THREADS |
| 7160 | |
| 7161 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
| 7162 | return win32_error_object("readlink", po); |
| 7163 | |
| 7164 | Py_BEGIN_ALLOW_THREADS |
| 7165 | /* New call DeviceIoControl to read the reparse point */ |
| 7166 | io_result = DeviceIoControl( |
| 7167 | reparse_point_handle, |
| 7168 | FSCTL_GET_REPARSE_POINT, |
| 7169 | 0, 0, /* in buffer */ |
| 7170 | target_buffer, sizeof(target_buffer), |
| 7171 | &n_bytes_returned, |
| 7172 | 0 /* we're not using OVERLAPPED_IO */ |
| 7173 | ); |
| 7174 | CloseHandle(reparse_point_handle); |
| 7175 | Py_END_ALLOW_THREADS |
| 7176 | |
| 7177 | if (io_result==0) |
| 7178 | return win32_error_object("readlink", po); |
| 7179 | |
| 7180 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 7181 | { |
| 7182 | PyErr_SetString(PyExc_ValueError, |
| 7183 | "not a symbolic link"); |
| 7184 | return NULL; |
| 7185 | } |
| 7186 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7187 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 7188 | |
| 7189 | result = PyUnicode_FromWideChar(print_name, |
| 7190 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
| 7191 | return result; |
| 7192 | } |
| 7193 | |
| 7194 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 7195 | |
| 7196 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7197 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7198 | #ifdef HAVE_SYMLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7199 | |
| 7200 | #if defined(MS_WINDOWS) |
| 7201 | |
| 7202 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 7203 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD) = NULL; |
| 7204 | static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPSTR, LPSTR, DWORD) = NULL; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7205 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7206 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7207 | check_CreateSymbolicLink(void) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7208 | { |
| 7209 | HINSTANCE hKernel32; |
| 7210 | /* only recheck */ |
| 7211 | if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA) |
| 7212 | return 1; |
| 7213 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 7214 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 7215 | "CreateSymbolicLinkW"); |
| 7216 | *(FARPROC*)&Py_CreateSymbolicLinkA = GetProcAddress(hKernel32, |
| 7217 | "CreateSymbolicLinkA"); |
| 7218 | return (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA); |
| 7219 | } |
| 7220 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7221 | /* Remove the last portion of the path */ |
| 7222 | static void |
| 7223 | _dirnameW(WCHAR *path) |
| 7224 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7225 | WCHAR *ptr; |
| 7226 | |
| 7227 | /* walk the path from the end until a backslash is encountered */ |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7228 | for(ptr = path + wcslen(path); ptr != path; ptr--) { |
Victor Stinner | 072318b | 2013-06-05 02:07:46 +0200 | [diff] [blame] | 7229 | if (*ptr == L'\\' || *ptr == L'/') |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7230 | break; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7231 | } |
| 7232 | *ptr = 0; |
| 7233 | } |
| 7234 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7235 | /* Remove the last portion of the path */ |
| 7236 | static void |
| 7237 | _dirnameA(char *path) |
| 7238 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7239 | char *ptr; |
| 7240 | |
| 7241 | /* walk the path from the end until a backslash is encountered */ |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7242 | for(ptr = path + strlen(path); ptr != path; ptr--) { |
| 7243 | if (*ptr == '\\' || *ptr == '/') |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7244 | break; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7245 | } |
| 7246 | *ptr = 0; |
| 7247 | } |
| 7248 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7249 | /* Is this path absolute? */ |
| 7250 | static int |
| 7251 | _is_absW(const WCHAR *path) |
| 7252 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7253 | return path[0] == L'\\' || path[0] == L'/' || path[1] == L':'; |
| 7254 | |
| 7255 | } |
| 7256 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7257 | /* Is this path absolute? */ |
| 7258 | static int |
| 7259 | _is_absA(const char *path) |
| 7260 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7261 | return path[0] == '\\' || path[0] == '/' || path[1] == ':'; |
| 7262 | |
| 7263 | } |
| 7264 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7265 | /* join root and rest with a backslash */ |
| 7266 | static void |
| 7267 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 7268 | { |
Victor Stinner | e7e7eba | 2013-06-05 00:35:54 +0200 | [diff] [blame] | 7269 | size_t root_len; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7270 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7271 | if (_is_absW(rest)) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7272 | wcscpy(dest_path, rest); |
| 7273 | return; |
| 7274 | } |
| 7275 | |
| 7276 | root_len = wcslen(root); |
| 7277 | |
| 7278 | wcscpy(dest_path, root); |
| 7279 | if(root_len) { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7280 | dest_path[root_len] = L'\\'; |
| 7281 | root_len++; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7282 | } |
| 7283 | wcscpy(dest_path+root_len, rest); |
| 7284 | } |
| 7285 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7286 | /* join root and rest with a backslash */ |
| 7287 | static void |
| 7288 | _joinA(char *dest_path, const char *root, const char *rest) |
| 7289 | { |
Victor Stinner | e7e7eba | 2013-06-05 00:35:54 +0200 | [diff] [blame] | 7290 | size_t root_len; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7291 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7292 | if (_is_absA(rest)) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7293 | strcpy(dest_path, rest); |
| 7294 | return; |
| 7295 | } |
| 7296 | |
| 7297 | root_len = strlen(root); |
| 7298 | |
| 7299 | strcpy(dest_path, root); |
| 7300 | if(root_len) { |
| 7301 | dest_path[root_len] = '\\'; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7302 | root_len++; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7303 | } |
| 7304 | strcpy(dest_path+root_len, rest); |
| 7305 | } |
| 7306 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7307 | /* Return True if the path at src relative to dest is a directory */ |
| 7308 | static int |
| 7309 | _check_dirW(WCHAR *src, WCHAR *dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7310 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7311 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7312 | WCHAR dest_parent[MAX_PATH]; |
| 7313 | WCHAR src_resolved[MAX_PATH] = L""; |
| 7314 | |
| 7315 | /* dest_parent = os.path.dirname(dest) */ |
| 7316 | wcscpy(dest_parent, dest); |
| 7317 | _dirnameW(dest_parent); |
| 7318 | /* src_resolved = os.path.join(dest_parent, src) */ |
| 7319 | _joinW(src_resolved, dest_parent, src); |
| 7320 | return ( |
| 7321 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 7322 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7323 | ); |
| 7324 | } |
| 7325 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7326 | /* Return True if the path at src relative to dest is a directory */ |
| 7327 | static int |
| 7328 | _check_dirA(char *src, char *dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7329 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7330 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7331 | char dest_parent[MAX_PATH]; |
| 7332 | char src_resolved[MAX_PATH] = ""; |
| 7333 | |
| 7334 | /* dest_parent = os.path.dirname(dest) */ |
| 7335 | strcpy(dest_parent, dest); |
Victor Stinner | 5a43676 | 2013-06-05 00:37:12 +0200 | [diff] [blame] | 7336 | _dirnameA(dest_parent); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7337 | /* src_resolved = os.path.join(dest_parent, src) */ |
Victor Stinner | 5a43676 | 2013-06-05 00:37:12 +0200 | [diff] [blame] | 7338 | _joinA(src_resolved, dest_parent, src); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7339 | return ( |
| 7340 | GetFileAttributesExA(src_resolved, GetFileExInfoStandard, &src_info) |
| 7341 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7342 | ); |
| 7343 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7344 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7345 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7346 | |
| 7347 | /*[clinic input] |
| 7348 | os.symlink |
| 7349 | src: path_t |
| 7350 | dst: path_t |
| 7351 | target_is_directory: bool = False |
| 7352 | * |
| 7353 | dir_fd: dir_fd(requires='symlinkat')=None |
| 7354 | |
| 7355 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 7356 | |
| 7357 | Create a symbolic link pointing to src named dst. |
| 7358 | |
| 7359 | target_is_directory is required on Windows if the target is to be |
| 7360 | interpreted as a directory. (On Windows, symlink requires |
| 7361 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 7362 | target_is_directory is ignored on non-Windows platforms. |
| 7363 | |
| 7364 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7365 | and path should be relative; path will then be relative to that directory. |
| 7366 | dir_fd may not be implemented on your platform. |
| 7367 | If it is unavailable, using it will raise a NotImplementedError. |
| 7368 | |
| 7369 | [clinic start generated code]*/ |
| 7370 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7371 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7372 | os_symlink_impl(PyModuleDef *module, path_t *src, path_t *dst, |
| 7373 | int target_is_directory, int dir_fd) |
| 7374 | /*[clinic end generated code: output=a01b4bcf32403ccd input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7375 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7376 | #ifdef MS_WINDOWS |
| 7377 | DWORD result; |
| 7378 | #else |
| 7379 | int result; |
| 7380 | #endif |
| 7381 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7382 | #ifdef MS_WINDOWS |
| 7383 | if (!check_CreateSymbolicLink()) { |
| 7384 | PyErr_SetString(PyExc_NotImplementedError, |
| 7385 | "CreateSymbolicLink functions not found"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7386 | return NULL; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7387 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7388 | if (!win32_can_symlink) { |
| 7389 | PyErr_SetString(PyExc_OSError, "symbolic link privilege not held"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7390 | return NULL; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7391 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7392 | #endif |
| 7393 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7394 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7395 | PyErr_SetString(PyExc_ValueError, |
| 7396 | "symlink: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7397 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7398 | } |
| 7399 | |
| 7400 | #ifdef MS_WINDOWS |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7401 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7402 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7403 | if (dst->wide) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7404 | /* if src is a directory, ensure target_is_directory==1 */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7405 | target_is_directory |= _check_dirW(src->wide, dst->wide); |
| 7406 | result = Py_CreateSymbolicLinkW(dst->wide, src->wide, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7407 | target_is_directory); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7408 | } |
| 7409 | else { |
| 7410 | /* if src is a directory, ensure target_is_directory==1 */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7411 | target_is_directory |= _check_dirA(src->narrow, dst->narrow); |
| 7412 | result = Py_CreateSymbolicLinkA(dst->narrow, src->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7413 | target_is_directory); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7414 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7415 | Py_END_ALLOW_THREADS |
| 7416 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7417 | if (!result) |
| 7418 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7419 | |
| 7420 | #else |
| 7421 | |
| 7422 | Py_BEGIN_ALLOW_THREADS |
| 7423 | #if HAVE_SYMLINKAT |
| 7424 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7425 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7426 | else |
| 7427 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7428 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7429 | Py_END_ALLOW_THREADS |
| 7430 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7431 | if (result) |
| 7432 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7433 | #endif |
| 7434 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7435 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7436 | } |
| 7437 | #endif /* HAVE_SYMLINK */ |
| 7438 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7439 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7440 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7441 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7442 | static PyStructSequence_Field times_result_fields[] = { |
| 7443 | {"user", "user time"}, |
| 7444 | {"system", "system time"}, |
| 7445 | {"children_user", "user time of children"}, |
| 7446 | {"children_system", "system time of children"}, |
| 7447 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 7448 | {NULL} |
| 7449 | }; |
| 7450 | |
| 7451 | PyDoc_STRVAR(times_result__doc__, |
| 7452 | "times_result: Result from os.times().\n\n\ |
| 7453 | This object may be accessed either as a tuple of\n\ |
| 7454 | (user, system, children_user, children_system, elapsed),\n\ |
| 7455 | or via the attributes user, system, children_user, children_system,\n\ |
| 7456 | and elapsed.\n\ |
| 7457 | \n\ |
| 7458 | See os.times for more information."); |
| 7459 | |
| 7460 | static PyStructSequence_Desc times_result_desc = { |
| 7461 | "times_result", /* name */ |
| 7462 | times_result__doc__, /* doc */ |
| 7463 | times_result_fields, |
| 7464 | 5 |
| 7465 | }; |
| 7466 | |
| 7467 | static PyTypeObject TimesResultType; |
| 7468 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7469 | #ifdef MS_WINDOWS |
| 7470 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 7471 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7472 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7473 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7474 | |
| 7475 | static PyObject * |
| 7476 | build_times_result(double user, double system, |
| 7477 | double children_user, double children_system, |
| 7478 | double elapsed) |
| 7479 | { |
| 7480 | PyObject *value = PyStructSequence_New(&TimesResultType); |
| 7481 | if (value == NULL) |
| 7482 | return NULL; |
| 7483 | |
| 7484 | #define SET(i, field) \ |
| 7485 | { \ |
| 7486 | PyObject *o = PyFloat_FromDouble(field); \ |
| 7487 | if (!o) { \ |
| 7488 | Py_DECREF(value); \ |
| 7489 | return NULL; \ |
| 7490 | } \ |
| 7491 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 7492 | } \ |
| 7493 | |
| 7494 | SET(0, user); |
| 7495 | SET(1, system); |
| 7496 | SET(2, children_user); |
| 7497 | SET(3, children_system); |
| 7498 | SET(4, elapsed); |
| 7499 | |
| 7500 | #undef SET |
| 7501 | |
| 7502 | return value; |
| 7503 | } |
| 7504 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7505 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7506 | #ifndef MS_WINDOWS |
| 7507 | #define NEED_TICKS_PER_SECOND |
| 7508 | static long ticks_per_second = -1; |
| 7509 | #endif /* MS_WINDOWS */ |
| 7510 | |
| 7511 | /*[clinic input] |
| 7512 | os.times |
| 7513 | |
| 7514 | Return a collection containing process timing information. |
| 7515 | |
| 7516 | The object returned behaves like a named tuple with these fields: |
| 7517 | (utime, stime, cutime, cstime, elapsed_time) |
| 7518 | All fields are floating point numbers. |
| 7519 | [clinic start generated code]*/ |
| 7520 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7521 | static PyObject * |
| 7522 | os_times_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7523 | /*[clinic end generated code: output=df0a63ebe6e6f091 input=2bf9df3d6ab2e48b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7524 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7525 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7526 | FILETIME create, exit, kernel, user; |
| 7527 | HANDLE hProc; |
| 7528 | hProc = GetCurrentProcess(); |
| 7529 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 7530 | /* The fields of a FILETIME structure are the hi and lo part |
| 7531 | of a 64-bit value expressed in 100 nanosecond units. |
| 7532 | 1e7 is one second in such units; 1e-7 the inverse. |
| 7533 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 7534 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7535 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7536 | (double)(user.dwHighDateTime*429.4967296 + |
| 7537 | user.dwLowDateTime*1e-7), |
| 7538 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 7539 | kernel.dwLowDateTime*1e-7), |
| 7540 | (double)0, |
| 7541 | (double)0, |
| 7542 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7543 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7544 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7545 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7546 | |
| 7547 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7548 | struct tms t; |
| 7549 | clock_t c; |
| 7550 | errno = 0; |
| 7551 | c = times(&t); |
| 7552 | if (c == (clock_t) -1) |
| 7553 | return posix_error(); |
| 7554 | return build_times_result( |
| 7555 | (double)t.tms_utime / ticks_per_second, |
| 7556 | (double)t.tms_stime / ticks_per_second, |
| 7557 | (double)t.tms_cutime / ticks_per_second, |
| 7558 | (double)t.tms_cstime / ticks_per_second, |
| 7559 | (double)c / ticks_per_second); |
| 7560 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7561 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7562 | #endif /* HAVE_TIMES */ |
| 7563 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7564 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7565 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7566 | /*[clinic input] |
| 7567 | os.getsid |
| 7568 | |
| 7569 | pid: pid_t |
| 7570 | / |
| 7571 | |
| 7572 | Call the system call getsid(pid) and return the result. |
| 7573 | [clinic start generated code]*/ |
| 7574 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7575 | static PyObject * |
| 7576 | os_getsid_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7577 | /*[clinic end generated code: output=a074f80c0e6bfb38 input=eeb2b923a30ce04e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7578 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7579 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7580 | sid = getsid(pid); |
| 7581 | if (sid < 0) |
| 7582 | return posix_error(); |
| 7583 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7584 | } |
| 7585 | #endif /* HAVE_GETSID */ |
| 7586 | |
| 7587 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7588 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7589 | /*[clinic input] |
| 7590 | os.setsid |
| 7591 | |
| 7592 | Call the system call setsid(). |
| 7593 | [clinic start generated code]*/ |
| 7594 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7595 | static PyObject * |
| 7596 | os_setsid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7597 | /*[clinic end generated code: output=398fc152ae327330 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7598 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7599 | if (setsid() < 0) |
| 7600 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7601 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7602 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7603 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7604 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7605 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7606 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7607 | /*[clinic input] |
| 7608 | os.setpgid |
| 7609 | |
| 7610 | pid: pid_t |
| 7611 | pgrp: pid_t |
| 7612 | / |
| 7613 | |
| 7614 | Call the system call setpgid(pid, pgrp). |
| 7615 | [clinic start generated code]*/ |
| 7616 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7617 | static PyObject * |
| 7618 | os_setpgid_impl(PyModuleDef *module, pid_t pid, pid_t pgrp) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7619 | /*[clinic end generated code: output=7079a8e932912841 input=fceb395eca572e1a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7620 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7621 | if (setpgid(pid, pgrp) < 0) |
| 7622 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7623 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7624 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7625 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7626 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7627 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7628 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7629 | /*[clinic input] |
| 7630 | os.tcgetpgrp |
| 7631 | |
| 7632 | fd: int |
| 7633 | / |
| 7634 | |
| 7635 | Return the process group associated with the terminal specified by fd. |
| 7636 | [clinic start generated code]*/ |
| 7637 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7638 | static PyObject * |
| 7639 | os_tcgetpgrp_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7640 | /*[clinic end generated code: output=ebb6dc5f111c7dc0 input=7f6c18eac10ada86]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7641 | { |
| 7642 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7643 | if (pgid < 0) |
| 7644 | return posix_error(); |
| 7645 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7646 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7647 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7648 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7649 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7650 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7651 | /*[clinic input] |
| 7652 | os.tcsetpgrp |
| 7653 | |
| 7654 | fd: int |
| 7655 | pgid: pid_t |
| 7656 | / |
| 7657 | |
| 7658 | Set the process group associated with the terminal specified by fd. |
| 7659 | [clinic start generated code]*/ |
| 7660 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7661 | static PyObject * |
| 7662 | os_tcsetpgrp_impl(PyModuleDef *module, int fd, pid_t pgid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7663 | /*[clinic end generated code: output=3e4b05177462cd22 input=5bdc997c6a619020]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7664 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7665 | if (tcsetpgrp(fd, pgid) < 0) |
| 7666 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7667 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7668 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7669 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7670 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7671 | /* Functions acting on file descriptors */ |
| 7672 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7673 | #ifdef O_CLOEXEC |
| 7674 | extern int _Py_open_cloexec_works; |
| 7675 | #endif |
| 7676 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7677 | |
| 7678 | /*[clinic input] |
| 7679 | os.open -> int |
| 7680 | path: path_t |
| 7681 | flags: int |
| 7682 | mode: int = 0o777 |
| 7683 | * |
| 7684 | dir_fd: dir_fd(requires='openat') = None |
| 7685 | |
| 7686 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 7687 | |
| 7688 | Open a file for low level IO. Returns a file descriptor (integer). |
| 7689 | |
| 7690 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7691 | and path should be relative; path will then be relative to that directory. |
| 7692 | dir_fd may not be implemented on your platform. |
| 7693 | If it is unavailable, using it will raise a NotImplementedError. |
| 7694 | [clinic start generated code]*/ |
| 7695 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7696 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7697 | os_open_impl(PyModuleDef *module, path_t *path, int flags, int mode, |
| 7698 | int dir_fd) |
| 7699 | /*[clinic end generated code: output=47e8cc63559f5ddd input=ad8623b29acd2934]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7700 | { |
| 7701 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7702 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7703 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7704 | #ifdef O_CLOEXEC |
| 7705 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 7706 | #elif !defined(MS_WINDOWS) |
| 7707 | int *atomic_flag_works = NULL; |
| 7708 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7709 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7710 | #ifdef MS_WINDOWS |
| 7711 | flags |= O_NOINHERIT; |
| 7712 | #elif defined(O_CLOEXEC) |
| 7713 | flags |= O_CLOEXEC; |
| 7714 | #endif |
| 7715 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7716 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7717 | do { |
| 7718 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7719 | #ifdef MS_WINDOWS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7720 | if (path->wide) |
| 7721 | fd = _wopen(path->wide, flags, mode); |
| 7722 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7723 | #endif |
| 7724 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7725 | if (dir_fd != DEFAULT_DIR_FD) |
| 7726 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 7727 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7728 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7729 | fd = open(path->narrow, flags, mode); |
| 7730 | Py_END_ALLOW_THREADS |
| 7731 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7732 | _Py_END_SUPPRESS_IPH |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7733 | |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 7734 | if (fd < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7735 | if (!async_err) |
| 7736 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7737 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7738 | } |
| 7739 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7740 | #ifndef MS_WINDOWS |
| 7741 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 7742 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7743 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7744 | } |
| 7745 | #endif |
| 7746 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7747 | return fd; |
| 7748 | } |
| 7749 | |
| 7750 | |
| 7751 | /*[clinic input] |
| 7752 | os.close |
| 7753 | |
| 7754 | fd: int |
| 7755 | |
| 7756 | Close a file descriptor. |
| 7757 | [clinic start generated code]*/ |
| 7758 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7759 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7760 | os_close_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7761 | /*[clinic end generated code: output=47bf2ea536445a26 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7762 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7763 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7764 | if (!_PyVerify_fd(fd)) |
| 7765 | return posix_error(); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7766 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 7767 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 7768 | * for more details. |
| 7769 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7770 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7771 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7772 | res = close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7773 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7774 | Py_END_ALLOW_THREADS |
| 7775 | if (res < 0) |
| 7776 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7777 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7778 | } |
| 7779 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7780 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7781 | /*[clinic input] |
| 7782 | os.closerange |
| 7783 | |
| 7784 | fd_low: int |
| 7785 | fd_high: int |
| 7786 | / |
| 7787 | |
| 7788 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 7789 | [clinic start generated code]*/ |
| 7790 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7791 | static PyObject * |
| 7792 | os_closerange_impl(PyModuleDef *module, int fd_low, int fd_high) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7793 | /*[clinic end generated code: output=70e6adb95220ba96 input=5855a3d053ebd4ec]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7794 | { |
| 7795 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7796 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7797 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7798 | for (i = fd_low; i < fd_high; i++) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7799 | if (_PyVerify_fd(i)) |
| 7800 | close(i); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7801 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7802 | Py_END_ALLOW_THREADS |
| 7803 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7804 | } |
| 7805 | |
| 7806 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7807 | /*[clinic input] |
| 7808 | os.dup -> int |
| 7809 | |
| 7810 | fd: int |
| 7811 | / |
| 7812 | |
| 7813 | Return a duplicate of a file descriptor. |
| 7814 | [clinic start generated code]*/ |
| 7815 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7816 | static int |
| 7817 | os_dup_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7818 | /*[clinic end generated code: output=f4bbac8c7652d05e input=6f10f7ea97f7852a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7819 | { |
| 7820 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7821 | } |
| 7822 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7823 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7824 | /*[clinic input] |
| 7825 | os.dup2 |
| 7826 | fd: int |
| 7827 | fd2: int |
| 7828 | inheritable: bool=True |
| 7829 | |
| 7830 | Duplicate file descriptor. |
| 7831 | [clinic start generated code]*/ |
| 7832 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7833 | static PyObject * |
| 7834 | os_dup2_impl(PyModuleDef *module, int fd, int fd2, int inheritable) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7835 | /*[clinic end generated code: output=9a099d95881a7923 input=76e96f511be0352f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7836 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7837 | int res; |
| 7838 | #if defined(HAVE_DUP3) && \ |
| 7839 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 7840 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
| 7841 | int dup3_works = -1; |
| 7842 | #endif |
| 7843 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7844 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 7845 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7846 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7847 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 7848 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 7849 | * upon close(), and therefore below. |
| 7850 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7851 | #ifdef MS_WINDOWS |
| 7852 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7853 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7854 | res = dup2(fd, fd2); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7855 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7856 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7857 | if (res < 0) |
| 7858 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7859 | |
| 7860 | /* Character files like console cannot be make non-inheritable */ |
| 7861 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7862 | close(fd2); |
| 7863 | return NULL; |
| 7864 | } |
| 7865 | |
| 7866 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 7867 | Py_BEGIN_ALLOW_THREADS |
| 7868 | if (!inheritable) |
| 7869 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 7870 | else |
| 7871 | res = dup2(fd, fd2); |
| 7872 | Py_END_ALLOW_THREADS |
| 7873 | if (res < 0) |
| 7874 | return posix_error(); |
| 7875 | |
| 7876 | #else |
| 7877 | |
| 7878 | #ifdef HAVE_DUP3 |
| 7879 | if (!inheritable && dup3_works != 0) { |
| 7880 | Py_BEGIN_ALLOW_THREADS |
| 7881 | res = dup3(fd, fd2, O_CLOEXEC); |
| 7882 | Py_END_ALLOW_THREADS |
| 7883 | if (res < 0) { |
| 7884 | if (dup3_works == -1) |
| 7885 | dup3_works = (errno != ENOSYS); |
| 7886 | if (dup3_works) |
| 7887 | return posix_error(); |
| 7888 | } |
| 7889 | } |
| 7890 | |
| 7891 | if (inheritable || dup3_works == 0) |
| 7892 | { |
| 7893 | #endif |
| 7894 | Py_BEGIN_ALLOW_THREADS |
| 7895 | res = dup2(fd, fd2); |
| 7896 | Py_END_ALLOW_THREADS |
| 7897 | if (res < 0) |
| 7898 | return posix_error(); |
| 7899 | |
| 7900 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7901 | close(fd2); |
| 7902 | return NULL; |
| 7903 | } |
| 7904 | #ifdef HAVE_DUP3 |
| 7905 | } |
| 7906 | #endif |
| 7907 | |
| 7908 | #endif |
| 7909 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7910 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7911 | } |
| 7912 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7913 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7914 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7915 | /*[clinic input] |
| 7916 | os.lockf |
| 7917 | |
| 7918 | fd: int |
| 7919 | An open file descriptor. |
| 7920 | command: int |
| 7921 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 7922 | length: Py_off_t |
| 7923 | The number of bytes to lock, starting at the current position. |
| 7924 | / |
| 7925 | |
| 7926 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 7927 | |
| 7928 | [clinic start generated code]*/ |
| 7929 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7930 | static PyObject * |
| 7931 | 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] | 7932 | /*[clinic end generated code: output=25ff778f9e2fbf1b input=65da41d2106e9b79]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7933 | { |
| 7934 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7935 | |
| 7936 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7937 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7938 | Py_END_ALLOW_THREADS |
| 7939 | |
| 7940 | if (res < 0) |
| 7941 | return posix_error(); |
| 7942 | |
| 7943 | Py_RETURN_NONE; |
| 7944 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7945 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7946 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7947 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7948 | /*[clinic input] |
| 7949 | os.lseek -> Py_off_t |
| 7950 | |
| 7951 | fd: int |
| 7952 | position: Py_off_t |
| 7953 | how: int |
| 7954 | / |
| 7955 | |
| 7956 | Set the position of a file descriptor. Return the new position. |
| 7957 | |
| 7958 | Return the new cursor position in number of bytes |
| 7959 | relative to the beginning of the file. |
| 7960 | [clinic start generated code]*/ |
| 7961 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7962 | static Py_off_t |
| 7963 | 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] | 7964 | /*[clinic end generated code: output=65d4ab96d664998c input=902654ad3f96a6d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7965 | { |
| 7966 | Py_off_t result; |
| 7967 | |
| 7968 | if (!_PyVerify_fd(fd)) { |
| 7969 | posix_error(); |
| 7970 | return -1; |
| 7971 | } |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7972 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7973 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 7974 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7975 | case 0: how = SEEK_SET; break; |
| 7976 | case 1: how = SEEK_CUR; break; |
| 7977 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7978 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7979 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7980 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7981 | if (PyErr_Occurred()) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7982 | return -1; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7983 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7984 | if (!_PyVerify_fd(fd)) { |
| 7985 | posix_error(); |
| 7986 | return -1; |
| 7987 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7988 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7989 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 7990 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7991 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7992 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7993 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7994 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 7995 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7996 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7997 | if (result < 0) |
| 7998 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7999 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8000 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8001 | } |
| 8002 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8003 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8004 | /*[clinic input] |
| 8005 | os.read |
| 8006 | fd: int |
| 8007 | length: Py_ssize_t |
| 8008 | / |
| 8009 | |
| 8010 | Read from a file descriptor. Returns a bytes object. |
| 8011 | [clinic start generated code]*/ |
| 8012 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8013 | static PyObject * |
| 8014 | os_read_impl(PyModuleDef *module, int fd, Py_ssize_t length) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8015 | /*[clinic end generated code: output=be24f44178455e8b input=1df2eaa27c0bf1d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8016 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8017 | Py_ssize_t n; |
| 8018 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8019 | |
| 8020 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8021 | errno = EINVAL; |
| 8022 | return posix_error(); |
| 8023 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8024 | |
| 8025 | #ifdef MS_WINDOWS |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8026 | /* On Windows, the count parameter of read() is an int */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8027 | if (length > INT_MAX) |
| 8028 | length = INT_MAX; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8029 | #endif |
| 8030 | |
| 8031 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8032 | if (buffer == NULL) |
| 8033 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8034 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8035 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 8036 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8037 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8038 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8039 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8040 | |
| 8041 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8042 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8043 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8044 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8045 | } |
| 8046 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8047 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 8048 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8049 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8050 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 8051 | { |
| 8052 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8053 | Py_ssize_t blen, total = 0; |
| 8054 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8055 | *iov = PyMem_New(struct iovec, cnt); |
| 8056 | if (*iov == NULL) { |
| 8057 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8058 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8059 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8060 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8061 | *buf = PyMem_New(Py_buffer, cnt); |
| 8062 | if (*buf == NULL) { |
| 8063 | PyMem_Del(*iov); |
| 8064 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8065 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8066 | } |
| 8067 | |
| 8068 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8069 | PyObject *item = PySequence_GetItem(seq, i); |
| 8070 | if (item == NULL) |
| 8071 | goto fail; |
| 8072 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 8073 | Py_DECREF(item); |
| 8074 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8075 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8076 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8077 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8078 | blen = (*buf)[i].len; |
| 8079 | (*iov)[i].iov_len = blen; |
| 8080 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8081 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8082 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8083 | |
| 8084 | fail: |
| 8085 | PyMem_Del(*iov); |
| 8086 | for (j = 0; j < i; j++) { |
| 8087 | PyBuffer_Release(&(*buf)[j]); |
| 8088 | } |
| 8089 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8090 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8091 | } |
| 8092 | |
| 8093 | static void |
| 8094 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 8095 | { |
| 8096 | int i; |
| 8097 | PyMem_Del(iov); |
| 8098 | for (i = 0; i < cnt; i++) { |
| 8099 | PyBuffer_Release(&buf[i]); |
| 8100 | } |
| 8101 | PyMem_Del(buf); |
| 8102 | } |
| 8103 | #endif |
| 8104 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8105 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8106 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8107 | /*[clinic input] |
| 8108 | os.readv -> Py_ssize_t |
| 8109 | |
| 8110 | fd: int |
| 8111 | buffers: object |
| 8112 | / |
| 8113 | |
| 8114 | Read from a file descriptor fd into an iterable of buffers. |
| 8115 | |
| 8116 | The buffers should be mutable buffers accepting bytes. |
| 8117 | readv will transfer data into each buffer until it is full |
| 8118 | and then move on to the next buffer in the sequence to hold |
| 8119 | the rest of the data. |
| 8120 | |
| 8121 | readv returns the total number of bytes read, |
| 8122 | which may be less than the total capacity of all the buffers. |
| 8123 | [clinic start generated code]*/ |
| 8124 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8125 | static Py_ssize_t |
| 8126 | os_readv_impl(PyModuleDef *module, int fd, PyObject *buffers) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8127 | /*[clinic end generated code: output=00fc56ff1800059f input=e679eb5dbfa0357d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8128 | { |
| 8129 | int cnt; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8130 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8131 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8132 | struct iovec *iov; |
| 8133 | Py_buffer *buf; |
| 8134 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8135 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8136 | PyErr_SetString(PyExc_TypeError, |
| 8137 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8138 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8139 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8140 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8141 | cnt = PySequence_Size(buffers); |
| 8142 | |
| 8143 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 8144 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8145 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8146 | do { |
| 8147 | Py_BEGIN_ALLOW_THREADS |
| 8148 | n = readv(fd, iov, cnt); |
| 8149 | Py_END_ALLOW_THREADS |
| 8150 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8151 | |
| 8152 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8153 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8154 | if (!async_err) |
| 8155 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8156 | return -1; |
| 8157 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8158 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8159 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8160 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8161 | #endif /* HAVE_READV */ |
| 8162 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8163 | |
| 8164 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8165 | /*[clinic input] |
| 8166 | # TODO length should be size_t! but Python doesn't support parsing size_t yet. |
| 8167 | os.pread |
| 8168 | |
| 8169 | fd: int |
| 8170 | length: int |
| 8171 | offset: Py_off_t |
| 8172 | / |
| 8173 | |
| 8174 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 8175 | |
| 8176 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 8177 | the beginning of the file. The file offset remains unchanged. |
| 8178 | [clinic start generated code]*/ |
| 8179 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8180 | static PyObject * |
| 8181 | 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] | 8182 | /*[clinic end generated code: output=90d1fed87f68fa33 input=084948dcbaa35d4c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8183 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8184 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8185 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8186 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8187 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8188 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8189 | errno = EINVAL; |
| 8190 | return posix_error(); |
| 8191 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8192 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8193 | if (buffer == NULL) |
| 8194 | return NULL; |
| 8195 | if (!_PyVerify_fd(fd)) { |
| 8196 | Py_DECREF(buffer); |
| 8197 | return posix_error(); |
| 8198 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8199 | |
| 8200 | do { |
| 8201 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8202 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8203 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8204 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8205 | Py_END_ALLOW_THREADS |
| 8206 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8207 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8208 | if (n < 0) { |
| 8209 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8210 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8211 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8212 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8213 | _PyBytes_Resize(&buffer, n); |
| 8214 | return buffer; |
| 8215 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8216 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8217 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8218 | |
| 8219 | /*[clinic input] |
| 8220 | os.write -> Py_ssize_t |
| 8221 | |
| 8222 | fd: int |
| 8223 | data: Py_buffer |
| 8224 | / |
| 8225 | |
| 8226 | Write a bytes object to a file descriptor. |
| 8227 | [clinic start generated code]*/ |
| 8228 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8229 | static Py_ssize_t |
| 8230 | os_write_impl(PyModuleDef *module, int fd, Py_buffer *data) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8231 | /*[clinic end generated code: output=58845c93c9ee1dda input=3207e28963234f3c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8232 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8233 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8234 | } |
| 8235 | |
| 8236 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8237 | PyDoc_STRVAR(posix_sendfile__doc__, |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8238 | "sendfile(out, in, offset, count) -> byteswritten\n\ |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8239 | sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8240 | -> byteswritten\n\ |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8241 | Copy count bytes from file descriptor in to file descriptor out."); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8242 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8243 | /* AC 3.5: don't bother converting, has optional group*/ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8244 | static PyObject * |
| 8245 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 8246 | { |
| 8247 | int in, out; |
| 8248 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8249 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8250 | off_t offset; |
| 8251 | |
| 8252 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 8253 | #ifndef __APPLE__ |
| 8254 | Py_ssize_t len; |
| 8255 | #endif |
| 8256 | PyObject *headers = NULL, *trailers = NULL; |
| 8257 | Py_buffer *hbuf, *tbuf; |
| 8258 | off_t sbytes; |
| 8259 | struct sf_hdtr sf; |
| 8260 | int flags = 0; |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 8261 | /* Beware that "in" clashes with Python's own "in" operator keyword */ |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 8262 | static char *keywords[] = {"out", "in", |
| 8263 | "offset", "count", |
| 8264 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8265 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 8266 | sf.headers = NULL; |
| 8267 | sf.trailers = NULL; |
| 8268 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8269 | #ifdef __APPLE__ |
| 8270 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8271 | 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] | 8272 | #else |
| 8273 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8274 | keywords, &out, &in, Py_off_t_converter, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8275 | #endif |
| 8276 | &headers, &trailers, &flags)) |
| 8277 | return NULL; |
| 8278 | if (headers != NULL) { |
| 8279 | if (!PySequence_Check(headers)) { |
| 8280 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8281 | "sendfile() headers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8282 | return NULL; |
| 8283 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8284 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8285 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8286 | if (sf.hdr_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8287 | (i = iov_setup(&(sf.headers), &hbuf, |
| 8288 | headers, sf.hdr_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8289 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8290 | #ifdef __APPLE__ |
| 8291 | sbytes += i; |
| 8292 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8293 | } |
| 8294 | } |
| 8295 | if (trailers != NULL) { |
| 8296 | if (!PySequence_Check(trailers)) { |
| 8297 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 8298 | "sendfile() trailers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8299 | return NULL; |
| 8300 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8301 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8302 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8303 | if (sf.trl_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8304 | (i = iov_setup(&(sf.trailers), &tbuf, |
| 8305 | trailers, sf.trl_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8306 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8307 | #ifdef __APPLE__ |
| 8308 | sbytes += i; |
| 8309 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8310 | } |
| 8311 | } |
| 8312 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8313 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8314 | do { |
| 8315 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8316 | #ifdef __APPLE__ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8317 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8318 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8319 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8320 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8321 | Py_END_ALLOW_THREADS |
| 8322 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8323 | _Py_END_SUPPRESS_IPH |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8324 | |
| 8325 | if (sf.headers != NULL) |
| 8326 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 8327 | if (sf.trailers != NULL) |
| 8328 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 8329 | |
| 8330 | if (ret < 0) { |
| 8331 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 8332 | if (sbytes != 0) { |
| 8333 | // some data has been sent |
| 8334 | goto done; |
| 8335 | } |
| 8336 | else { |
| 8337 | // no data has been sent; upper application is supposed |
| 8338 | // to retry on EAGAIN or EBUSY |
| 8339 | return posix_error(); |
| 8340 | } |
| 8341 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8342 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8343 | } |
| 8344 | goto done; |
| 8345 | |
| 8346 | done: |
| 8347 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 8348 | return Py_BuildValue("l", sbytes); |
| 8349 | #else |
| 8350 | return Py_BuildValue("L", sbytes); |
| 8351 | #endif |
| 8352 | |
| 8353 | #else |
| 8354 | Py_ssize_t count; |
| 8355 | PyObject *offobj; |
| 8356 | static char *keywords[] = {"out", "in", |
| 8357 | "offset", "count", NULL}; |
| 8358 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 8359 | keywords, &out, &in, &offobj, &count)) |
| 8360 | return NULL; |
| 8361 | #ifdef linux |
| 8362 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8363 | do { |
| 8364 | Py_BEGIN_ALLOW_THREADS |
| 8365 | ret = sendfile(out, in, NULL, count); |
| 8366 | Py_END_ALLOW_THREADS |
| 8367 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8368 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8369 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 8370 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8371 | } |
| 8372 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8373 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 8374 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8375 | |
| 8376 | do { |
| 8377 | Py_BEGIN_ALLOW_THREADS |
| 8378 | ret = sendfile(out, in, &offset, count); |
| 8379 | Py_END_ALLOW_THREADS |
| 8380 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8381 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8382 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8383 | return Py_BuildValue("n", ret); |
| 8384 | #endif |
| 8385 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8386 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8387 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8388 | |
| 8389 | /*[clinic input] |
| 8390 | os.fstat |
| 8391 | |
| 8392 | fd : int |
| 8393 | |
| 8394 | Perform a stat system call on the given file descriptor. |
| 8395 | |
| 8396 | Like stat(), but for an open file descriptor. |
| 8397 | Equivalent to os.stat(fd). |
| 8398 | [clinic start generated code]*/ |
| 8399 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8400 | static PyObject * |
| 8401 | os_fstat_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8402 | /*[clinic end generated code: output=d71fe98bf042b626 input=27e0e0ebbe5600c9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8403 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8404 | STRUCT_STAT st; |
| 8405 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8406 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8407 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8408 | do { |
| 8409 | Py_BEGIN_ALLOW_THREADS |
| 8410 | res = FSTAT(fd, &st); |
| 8411 | Py_END_ALLOW_THREADS |
| 8412 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8413 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8414 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8415 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8416 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8417 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8418 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8419 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8420 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8421 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8422 | } |
| 8423 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8424 | |
| 8425 | /*[clinic input] |
| 8426 | os.isatty -> bool |
| 8427 | fd: int |
| 8428 | / |
| 8429 | |
| 8430 | Return True if the fd is connected to a terminal. |
| 8431 | |
| 8432 | Return True if the file descriptor is an open file descriptor |
| 8433 | connected to the slave end of a terminal. |
| 8434 | [clinic start generated code]*/ |
| 8435 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8436 | static int |
| 8437 | os_isatty_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8438 | /*[clinic end generated code: output=acec9d3c29d16d33 input=08ce94aa1eaf7b5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8439 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8440 | int return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8441 | if (!_PyVerify_fd(fd)) |
| 8442 | return 0; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8443 | _Py_BEGIN_SUPPRESS_IPH |
| 8444 | return_value = isatty(fd); |
| 8445 | _Py_END_SUPPRESS_IPH |
| 8446 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8447 | } |
| 8448 | |
| 8449 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8450 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8451 | /*[clinic input] |
| 8452 | os.pipe |
| 8453 | |
| 8454 | Create a pipe. |
| 8455 | |
| 8456 | Returns a tuple of two file descriptors: |
| 8457 | (read_fd, write_fd) |
| 8458 | [clinic start generated code]*/ |
| 8459 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8460 | static PyObject * |
| 8461 | os_pipe_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8462 | /*[clinic end generated code: output=6b0cd3f868ec3c40 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8463 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8464 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8465 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8466 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8467 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8468 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8469 | #else |
| 8470 | int res; |
| 8471 | #endif |
| 8472 | |
| 8473 | #ifdef MS_WINDOWS |
| 8474 | attr.nLength = sizeof(attr); |
| 8475 | attr.lpSecurityDescriptor = NULL; |
| 8476 | attr.bInheritHandle = FALSE; |
| 8477 | |
| 8478 | Py_BEGIN_ALLOW_THREADS |
| 8479 | ok = CreatePipe(&read, &write, &attr, 0); |
| 8480 | if (ok) { |
| 8481 | fds[0] = _open_osfhandle((Py_intptr_t)read, _O_RDONLY); |
| 8482 | fds[1] = _open_osfhandle((Py_intptr_t)write, _O_WRONLY); |
| 8483 | if (fds[0] == -1 || fds[1] == -1) { |
| 8484 | CloseHandle(read); |
| 8485 | CloseHandle(write); |
| 8486 | ok = 0; |
| 8487 | } |
| 8488 | } |
| 8489 | Py_END_ALLOW_THREADS |
| 8490 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8491 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8492 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8493 | #else |
| 8494 | |
| 8495 | #ifdef HAVE_PIPE2 |
| 8496 | Py_BEGIN_ALLOW_THREADS |
| 8497 | res = pipe2(fds, O_CLOEXEC); |
| 8498 | Py_END_ALLOW_THREADS |
| 8499 | |
| 8500 | if (res != 0 && errno == ENOSYS) |
| 8501 | { |
| 8502 | #endif |
| 8503 | Py_BEGIN_ALLOW_THREADS |
| 8504 | res = pipe(fds); |
| 8505 | Py_END_ALLOW_THREADS |
| 8506 | |
| 8507 | if (res == 0) { |
| 8508 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 8509 | close(fds[0]); |
| 8510 | close(fds[1]); |
| 8511 | return NULL; |
| 8512 | } |
| 8513 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 8514 | close(fds[0]); |
| 8515 | close(fds[1]); |
| 8516 | return NULL; |
| 8517 | } |
| 8518 | } |
| 8519 | #ifdef HAVE_PIPE2 |
| 8520 | } |
| 8521 | #endif |
| 8522 | |
| 8523 | if (res != 0) |
| 8524 | return PyErr_SetFromErrno(PyExc_OSError); |
| 8525 | #endif /* !MS_WINDOWS */ |
| 8526 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8527 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8528 | #endif /* HAVE_PIPE */ |
| 8529 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8530 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8531 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8532 | /*[clinic input] |
| 8533 | os.pipe2 |
| 8534 | |
| 8535 | flags: int |
| 8536 | / |
| 8537 | |
| 8538 | Create a pipe with flags set atomically. |
| 8539 | |
| 8540 | Returns a tuple of two file descriptors: |
| 8541 | (read_fd, write_fd) |
| 8542 | |
| 8543 | flags can be constructed by ORing together one or more of these values: |
| 8544 | O_NONBLOCK, O_CLOEXEC. |
| 8545 | [clinic start generated code]*/ |
| 8546 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8547 | static PyObject * |
| 8548 | os_pipe2_impl(PyModuleDef *module, int flags) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8549 | /*[clinic end generated code: output=c15b6075d0c6b2e7 input=f261b6e7e63c6817]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8550 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8551 | int fds[2]; |
| 8552 | int res; |
| 8553 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8554 | res = pipe2(fds, flags); |
| 8555 | if (res != 0) |
| 8556 | return posix_error(); |
| 8557 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 8558 | } |
| 8559 | #endif /* HAVE_PIPE2 */ |
| 8560 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8561 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8562 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8563 | /*[clinic input] |
| 8564 | os.writev -> Py_ssize_t |
| 8565 | fd: int |
| 8566 | buffers: object |
| 8567 | / |
| 8568 | |
| 8569 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 8570 | |
| 8571 | Returns the total number of bytes written. |
| 8572 | buffers must be a sequence of bytes-like objects. |
| 8573 | [clinic start generated code]*/ |
| 8574 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8575 | static Py_ssize_t |
| 8576 | os_writev_impl(PyModuleDef *module, int fd, PyObject *buffers) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8577 | /*[clinic end generated code: output=a48925dbf2d5c238 input=5b8d17fe4189d2fe]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8578 | { |
| 8579 | int cnt; |
| 8580 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8581 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8582 | struct iovec *iov; |
| 8583 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8584 | |
| 8585 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8586 | PyErr_SetString(PyExc_TypeError, |
| 8587 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8588 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8589 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8590 | cnt = PySequence_Size(buffers); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8591 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8592 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 8593 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8594 | } |
| 8595 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8596 | do { |
| 8597 | Py_BEGIN_ALLOW_THREADS |
| 8598 | result = writev(fd, iov, cnt); |
| 8599 | Py_END_ALLOW_THREADS |
| 8600 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8601 | |
| 8602 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8603 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8604 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8605 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8606 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8607 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8608 | #endif /* HAVE_WRITEV */ |
| 8609 | |
| 8610 | |
| 8611 | #ifdef HAVE_PWRITE |
| 8612 | /*[clinic input] |
| 8613 | os.pwrite -> Py_ssize_t |
| 8614 | |
| 8615 | fd: int |
| 8616 | buffer: Py_buffer |
| 8617 | offset: Py_off_t |
| 8618 | / |
| 8619 | |
| 8620 | Write bytes to a file descriptor starting at a particular offset. |
| 8621 | |
| 8622 | Write buffer to fd, starting at offset bytes from the beginning of |
| 8623 | the file. Returns the number of bytes writte. Does not change the |
| 8624 | current file offset. |
| 8625 | [clinic start generated code]*/ |
| 8626 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8627 | static Py_ssize_t |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8628 | os_pwrite_impl(PyModuleDef *module, int fd, Py_buffer *buffer, |
| 8629 | Py_off_t offset) |
| 8630 | /*[clinic end generated code: output=93aabdb40e17d325 input=19903f1b3dd26377]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8631 | { |
| 8632 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8633 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8634 | |
| 8635 | if (!_PyVerify_fd(fd)) { |
| 8636 | posix_error(); |
| 8637 | return -1; |
| 8638 | } |
| 8639 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8640 | do { |
| 8641 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8642 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8643 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8644 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8645 | Py_END_ALLOW_THREADS |
| 8646 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8647 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8648 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8649 | posix_error(); |
| 8650 | return size; |
| 8651 | } |
| 8652 | #endif /* HAVE_PWRITE */ |
| 8653 | |
| 8654 | |
| 8655 | #ifdef HAVE_MKFIFO |
| 8656 | /*[clinic input] |
| 8657 | os.mkfifo |
| 8658 | |
| 8659 | path: path_t |
| 8660 | mode: int=0o666 |
| 8661 | * |
| 8662 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 8663 | |
| 8664 | Create a "fifo" (a POSIX named pipe). |
| 8665 | |
| 8666 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8667 | and path should be relative; path will then be relative to that directory. |
| 8668 | dir_fd may not be implemented on your platform. |
| 8669 | If it is unavailable, using it will raise a NotImplementedError. |
| 8670 | [clinic start generated code]*/ |
| 8671 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8672 | static PyObject * |
| 8673 | 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] | 8674 | /*[clinic end generated code: output=8f5f5e72c630049a input=73032e98a36e0e19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8675 | { |
| 8676 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8677 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8678 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8679 | do { |
| 8680 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8681 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8682 | if (dir_fd != DEFAULT_DIR_FD) |
| 8683 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 8684 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8685 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8686 | result = mkfifo(path->narrow, mode); |
| 8687 | Py_END_ALLOW_THREADS |
| 8688 | } while (result != 0 && errno == EINTR && |
| 8689 | !(async_err = PyErr_CheckSignals())); |
| 8690 | if (result != 0) |
| 8691 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8692 | |
| 8693 | Py_RETURN_NONE; |
| 8694 | } |
| 8695 | #endif /* HAVE_MKFIFO */ |
| 8696 | |
| 8697 | |
| 8698 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 8699 | /*[clinic input] |
| 8700 | os.mknod |
| 8701 | |
| 8702 | path: path_t |
| 8703 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8704 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8705 | * |
| 8706 | dir_fd: dir_fd(requires='mknodat')=None |
| 8707 | |
| 8708 | Create a node in the file system. |
| 8709 | |
| 8710 | Create a node in the file system (file, device special file or named pipe) |
| 8711 | at path. mode specifies both the permissions to use and the |
| 8712 | type of node to be created, being combined (bitwise OR) with one of |
| 8713 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 8714 | device defines the newly created device special file (probably using |
| 8715 | os.makedev()). Otherwise device is ignored. |
| 8716 | |
| 8717 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8718 | and path should be relative; path will then be relative to that directory. |
| 8719 | dir_fd may not be implemented on your platform. |
| 8720 | If it is unavailable, using it will raise a NotImplementedError. |
| 8721 | [clinic start generated code]*/ |
| 8722 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8723 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8724 | os_mknod_impl(PyModuleDef *module, path_t *path, int mode, dev_t device, |
| 8725 | int dir_fd) |
| 8726 | /*[clinic end generated code: output=5151a8a9f754d272 input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8727 | { |
| 8728 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8729 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8730 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8731 | do { |
| 8732 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8733 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8734 | if (dir_fd != DEFAULT_DIR_FD) |
| 8735 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 8736 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8737 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8738 | result = mknod(path->narrow, mode, device); |
| 8739 | Py_END_ALLOW_THREADS |
| 8740 | } while (result != 0 && errno == EINTR && |
| 8741 | !(async_err = PyErr_CheckSignals())); |
| 8742 | if (result != 0) |
| 8743 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8744 | |
| 8745 | Py_RETURN_NONE; |
| 8746 | } |
| 8747 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 8748 | |
| 8749 | |
| 8750 | #ifdef HAVE_DEVICE_MACROS |
| 8751 | /*[clinic input] |
| 8752 | os.major -> unsigned_int |
| 8753 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8754 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8755 | / |
| 8756 | |
| 8757 | Extracts a device major number from a raw device number. |
| 8758 | [clinic start generated code]*/ |
| 8759 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8760 | static unsigned int |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8761 | os_major_impl(PyModuleDef *module, dev_t device) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8762 | /*[clinic end generated code: output=ba55693ab49bac34 input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8763 | { |
| 8764 | return major(device); |
| 8765 | } |
| 8766 | |
| 8767 | |
| 8768 | /*[clinic input] |
| 8769 | os.minor -> unsigned_int |
| 8770 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8771 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8772 | / |
| 8773 | |
| 8774 | Extracts a device minor number from a raw device number. |
| 8775 | [clinic start generated code]*/ |
| 8776 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8777 | static unsigned int |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8778 | os_minor_impl(PyModuleDef *module, dev_t device) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8779 | /*[clinic end generated code: output=2867219ebf274e27 input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8780 | { |
| 8781 | return minor(device); |
| 8782 | } |
| 8783 | |
| 8784 | |
| 8785 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8786 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8787 | |
| 8788 | major: int |
| 8789 | minor: int |
| 8790 | / |
| 8791 | |
| 8792 | Composes a raw device number from the major and minor device numbers. |
| 8793 | [clinic start generated code]*/ |
| 8794 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8795 | static dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8796 | os_makedev_impl(PyModuleDef *module, int major, int minor) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8797 | /*[clinic end generated code: output=7cb6264352437660 input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8798 | { |
| 8799 | return makedev(major, minor); |
| 8800 | } |
| 8801 | #endif /* HAVE_DEVICE_MACROS */ |
| 8802 | |
| 8803 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8804 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8805 | /*[clinic input] |
| 8806 | os.ftruncate |
| 8807 | |
| 8808 | fd: int |
| 8809 | length: Py_off_t |
| 8810 | / |
| 8811 | |
| 8812 | Truncate a file, specified by file descriptor, to a specific length. |
| 8813 | [clinic start generated code]*/ |
| 8814 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8815 | static PyObject * |
| 8816 | os_ftruncate_impl(PyModuleDef *module, int fd, Py_off_t length) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8817 | /*[clinic end generated code: output=3666f401d76bf834 input=63b43641e52818f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8818 | { |
| 8819 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8820 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8821 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8822 | if (!_PyVerify_fd(fd)) |
| 8823 | return posix_error(); |
| 8824 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8825 | do { |
| 8826 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8827 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8828 | #ifdef MS_WINDOWS |
| 8829 | result = _chsize_s(fd, length); |
| 8830 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8831 | result = ftruncate(fd, length); |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8832 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8833 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8834 | Py_END_ALLOW_THREADS |
| 8835 | } while (result != 0 && errno == EINTR && |
| 8836 | !(async_err = PyErr_CheckSignals())); |
| 8837 | if (result != 0) |
| 8838 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8839 | Py_RETURN_NONE; |
| 8840 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8841 | #endif /* HAVE_FTRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8842 | |
| 8843 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8844 | #if defined HAVE_TRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8845 | /*[clinic input] |
| 8846 | os.truncate |
| 8847 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 8848 | length: Py_off_t |
| 8849 | |
| 8850 | Truncate a file, specified by path, to a specific length. |
| 8851 | |
| 8852 | On some platforms, path may also be specified as an open file descriptor. |
| 8853 | If this functionality is unavailable, using it raises an exception. |
| 8854 | [clinic start generated code]*/ |
| 8855 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8856 | static PyObject * |
| 8857 | os_truncate_impl(PyModuleDef *module, path_t *path, Py_off_t length) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8858 | /*[clinic end generated code: output=f60a9e08370e9e2e input=77229cf0b50a9b77]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8859 | { |
| 8860 | int result; |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8861 | #ifdef MS_WINDOWS |
| 8862 | int fd; |
| 8863 | #endif |
| 8864 | |
| 8865 | if (path->fd != -1) |
| 8866 | return os_ftruncate_impl(module, path->fd, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8867 | |
| 8868 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8869 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8870 | #ifdef MS_WINDOWS |
| 8871 | if (path->wide) |
| 8872 | fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8873 | else |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8874 | fd = _open(path->narrow, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Victor Stinner | cc0bbbc | 2015-04-25 00:21:52 +0200 | [diff] [blame] | 8875 | if (fd < 0) |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8876 | result = -1; |
| 8877 | else { |
| 8878 | result = _chsize_s(fd, length); |
| 8879 | close(fd); |
| 8880 | if (result < 0) |
| 8881 | errno = result; |
| 8882 | } |
| 8883 | #else |
| 8884 | result = truncate(path->narrow, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8885 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 8886 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8887 | Py_END_ALLOW_THREADS |
| 8888 | if (result < 0) |
| 8889 | return path_error(path); |
| 8890 | |
| 8891 | Py_RETURN_NONE; |
| 8892 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 8893 | #endif /* HAVE_TRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8894 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8895 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 8896 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 8897 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 8898 | defined, which is the case in Python on AIX. AIX bug report: |
| 8899 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 8900 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 8901 | # define POSIX_FADVISE_AIX_BUG |
| 8902 | #endif |
| 8903 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 8904 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 8905 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8906 | /*[clinic input] |
| 8907 | os.posix_fallocate |
| 8908 | |
| 8909 | fd: int |
| 8910 | offset: Py_off_t |
| 8911 | length: Py_off_t |
| 8912 | / |
| 8913 | |
| 8914 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 8915 | |
| 8916 | Ensure that the file specified by fd encompasses a range of bytes |
| 8917 | starting at offset bytes from the beginning and continuing for length bytes. |
| 8918 | [clinic start generated code]*/ |
| 8919 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8920 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8921 | os_posix_fallocate_impl(PyModuleDef *module, int fd, Py_off_t offset, |
| 8922 | Py_off_t length) |
| 8923 | /*[clinic end generated code: output=7f6f87a8c751e1b4 input=d7a2ef0ab2ca52fb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8924 | { |
| 8925 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8926 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8927 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8928 | do { |
| 8929 | Py_BEGIN_ALLOW_THREADS |
| 8930 | result = posix_fallocate(fd, offset, length); |
| 8931 | Py_END_ALLOW_THREADS |
| 8932 | } while (result != 0 && errno == EINTR && |
| 8933 | !(async_err = PyErr_CheckSignals())); |
| 8934 | if (result != 0) |
| 8935 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8936 | Py_RETURN_NONE; |
| 8937 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 8938 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8939 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8940 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 8941 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8942 | /*[clinic input] |
| 8943 | os.posix_fadvise |
| 8944 | |
| 8945 | fd: int |
| 8946 | offset: Py_off_t |
| 8947 | length: Py_off_t |
| 8948 | advice: int |
| 8949 | / |
| 8950 | |
| 8951 | Announce an intention to access data in a specific pattern. |
| 8952 | |
| 8953 | Announce an intention to access data in a specific pattern, thus allowing |
| 8954 | the kernel to make optimizations. |
| 8955 | The advice applies to the region of the file specified by fd starting at |
| 8956 | offset and continuing for length bytes. |
| 8957 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 8958 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 8959 | POSIX_FADV_DONTNEED. |
| 8960 | [clinic start generated code]*/ |
| 8961 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8962 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8963 | os_posix_fadvise_impl(PyModuleDef *module, int fd, Py_off_t offset, |
| 8964 | Py_off_t length, int advice) |
| 8965 | /*[clinic end generated code: output=457ce6a67189e10d input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8966 | { |
| 8967 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8968 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8969 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8970 | do { |
| 8971 | Py_BEGIN_ALLOW_THREADS |
| 8972 | result = posix_fadvise(fd, offset, length, advice); |
| 8973 | Py_END_ALLOW_THREADS |
| 8974 | } while (result != 0 && errno == EINTR && |
| 8975 | !(async_err = PyErr_CheckSignals())); |
| 8976 | if (result != 0) |
| 8977 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8978 | Py_RETURN_NONE; |
| 8979 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 8980 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8981 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8982 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8983 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8984 | /* Save putenv() parameters as values here, so we can collect them when they |
| 8985 | * get re-set with another call for the same key. */ |
| 8986 | static PyObject *posix_putenv_garbage; |
| 8987 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8988 | static void |
| 8989 | posix_putenv_garbage_setitem(PyObject *name, PyObject *value) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8990 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8991 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 8992 | * this will cause previous value to be collected. This has to |
| 8993 | * happen after the real putenv() call because the old value |
| 8994 | * was still accessible until then. */ |
| 8995 | if (PyDict_SetItem(posix_putenv_garbage, name, value)) |
| 8996 | /* really not much we can do; just leak */ |
| 8997 | PyErr_Clear(); |
| 8998 | else |
| 8999 | Py_DECREF(value); |
| 9000 | } |
| 9001 | |
| 9002 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 9003 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9004 | /*[clinic input] |
| 9005 | os.putenv |
| 9006 | |
| 9007 | name: unicode |
| 9008 | value: unicode |
| 9009 | / |
| 9010 | |
| 9011 | Change or add an environment variable. |
| 9012 | [clinic start generated code]*/ |
| 9013 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9014 | static PyObject * |
| 9015 | os_putenv_impl(PyModuleDef *module, PyObject *name, PyObject *value) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9016 | /*[clinic end generated code: output=a2438cf95e5a0c1c input=ba586581c2e6105f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9017 | { |
| 9018 | wchar_t *env; |
| 9019 | |
| 9020 | PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 9021 | if (unicode == NULL) { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9022 | PyErr_NoMemory(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9023 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9024 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9025 | if (_MAX_ENV < PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9026 | PyErr_Format(PyExc_ValueError, |
| 9027 | "the environment variable is longer than %u characters", |
| 9028 | _MAX_ENV); |
| 9029 | goto error; |
| 9030 | } |
| 9031 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9032 | env = PyUnicode_AsUnicode(unicode); |
| 9033 | if (env == NULL) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9034 | goto error; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9035 | if (_wputenv(env)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9036 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9037 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9038 | } |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9039 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9040 | posix_putenv_garbage_setitem(name, unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9041 | Py_RETURN_NONE; |
| 9042 | |
| 9043 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9044 | Py_DECREF(unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9045 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9046 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9047 | #else /* MS_WINDOWS */ |
| 9048 | /*[clinic input] |
| 9049 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9050 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9051 | name: FSConverter |
| 9052 | value: FSConverter |
| 9053 | / |
| 9054 | |
| 9055 | Change or add an environment variable. |
| 9056 | [clinic start generated code]*/ |
| 9057 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9058 | static PyObject * |
| 9059 | os_putenv_impl(PyModuleDef *module, PyObject *name, PyObject *value) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9060 | /*[clinic end generated code: output=a2438cf95e5a0c1c input=a97bc6152f688d31]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9061 | { |
| 9062 | PyObject *bytes = NULL; |
| 9063 | char *env; |
| 9064 | char *name_string = PyBytes_AsString(name); |
| 9065 | char *value_string = PyBytes_AsString(value); |
| 9066 | |
| 9067 | bytes = PyBytes_FromFormat("%s=%s", name_string, value_string); |
| 9068 | if (bytes == NULL) { |
| 9069 | PyErr_NoMemory(); |
| 9070 | return NULL; |
| 9071 | } |
| 9072 | |
| 9073 | env = PyBytes_AS_STRING(bytes); |
| 9074 | if (putenv(env)) { |
| 9075 | Py_DECREF(bytes); |
| 9076 | return posix_error(); |
| 9077 | } |
| 9078 | |
| 9079 | posix_putenv_garbage_setitem(name, bytes); |
| 9080 | Py_RETURN_NONE; |
| 9081 | } |
| 9082 | #endif /* MS_WINDOWS */ |
| 9083 | #endif /* HAVE_PUTENV */ |
| 9084 | |
| 9085 | |
| 9086 | #ifdef HAVE_UNSETENV |
| 9087 | /*[clinic input] |
| 9088 | os.unsetenv |
| 9089 | name: FSConverter |
| 9090 | / |
| 9091 | |
| 9092 | Delete an environment variable. |
| 9093 | [clinic start generated code]*/ |
| 9094 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9095 | static PyObject * |
| 9096 | os_unsetenv_impl(PyModuleDef *module, PyObject *name) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9097 | /*[clinic end generated code: output=25994b57016a2dc9 input=2bb5288a599c7107]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9098 | { |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9099 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 9100 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9101 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9102 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9103 | #ifdef HAVE_BROKEN_UNSETENV |
| 9104 | unsetenv(PyBytes_AS_STRING(name)); |
| 9105 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9106 | err = unsetenv(PyBytes_AS_STRING(name)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9107 | if (err) |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 9108 | return posix_error(); |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9109 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9110 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9111 | /* Remove the key from posix_putenv_garbage; |
| 9112 | * this will cause it to be collected. This has to |
| 9113 | * happen after the real unsetenv() call because the |
| 9114 | * old value was still accessible until then. |
| 9115 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9116 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9117 | /* really not much we can do; just leak */ |
| 9118 | PyErr_Clear(); |
| 9119 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9120 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9121 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9122 | #endif /* HAVE_UNSETENV */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9123 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9124 | |
| 9125 | /*[clinic input] |
| 9126 | os.strerror |
| 9127 | |
| 9128 | code: int |
| 9129 | / |
| 9130 | |
| 9131 | Translate an error code to a message string. |
| 9132 | [clinic start generated code]*/ |
| 9133 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9134 | static PyObject * |
| 9135 | os_strerror_impl(PyModuleDef *module, int code) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9136 | /*[clinic end generated code: output=0280c6af51e5c9fe input=75a8673d97915a91]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9137 | { |
| 9138 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9139 | if (message == NULL) { |
| 9140 | PyErr_SetString(PyExc_ValueError, |
| 9141 | "strerror() argument out of range"); |
| 9142 | return NULL; |
| 9143 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 9144 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9145 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9146 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9147 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9148 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9149 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9150 | /*[clinic input] |
| 9151 | os.WCOREDUMP -> bool |
| 9152 | |
| 9153 | status: int |
| 9154 | / |
| 9155 | |
| 9156 | Return True if the process returning status was dumped to a core file. |
| 9157 | [clinic start generated code]*/ |
| 9158 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9159 | static int |
| 9160 | os_WCOREDUMP_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9161 | /*[clinic end generated code: output=134f70bbe63fbf41 input=8b05e7ab38528d04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9162 | { |
| 9163 | WAIT_TYPE wait_status; |
| 9164 | WAIT_STATUS_INT(wait_status) = status; |
| 9165 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9166 | } |
| 9167 | #endif /* WCOREDUMP */ |
| 9168 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9169 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9170 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9171 | /*[clinic input] |
| 9172 | os.WIFCONTINUED -> bool |
| 9173 | |
| 9174 | status: int |
| 9175 | |
| 9176 | Return True if a particular process was continued from a job control stop. |
| 9177 | |
| 9178 | Return True if the process returning status was continued from a |
| 9179 | job control stop. |
| 9180 | [clinic start generated code]*/ |
| 9181 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9182 | static int |
| 9183 | os_WIFCONTINUED_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9184 | /*[clinic end generated code: output=9cdd26543ebb6dcd input=e777e7d38eb25bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9185 | { |
| 9186 | WAIT_TYPE wait_status; |
| 9187 | WAIT_STATUS_INT(wait_status) = status; |
| 9188 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9189 | } |
| 9190 | #endif /* WIFCONTINUED */ |
| 9191 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9192 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9193 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9194 | /*[clinic input] |
| 9195 | os.WIFSTOPPED -> bool |
| 9196 | |
| 9197 | status: int |
| 9198 | |
| 9199 | Return True if the process returning status was stopped. |
| 9200 | [clinic start generated code]*/ |
| 9201 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9202 | static int |
| 9203 | os_WIFSTOPPED_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9204 | /*[clinic end generated code: output=73bf35e44994a724 input=043cb7f1289ef904]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9205 | { |
| 9206 | WAIT_TYPE wait_status; |
| 9207 | WAIT_STATUS_INT(wait_status) = status; |
| 9208 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9209 | } |
| 9210 | #endif /* WIFSTOPPED */ |
| 9211 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9212 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9213 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9214 | /*[clinic input] |
| 9215 | os.WIFSIGNALED -> bool |
| 9216 | |
| 9217 | status: int |
| 9218 | |
| 9219 | Return True if the process returning status was terminated by a signal. |
| 9220 | [clinic start generated code]*/ |
| 9221 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9222 | static int |
| 9223 | os_WIFSIGNALED_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9224 | /*[clinic end generated code: output=2697975771872420 input=d55ba7cc9ce5dc43]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9225 | { |
| 9226 | WAIT_TYPE wait_status; |
| 9227 | WAIT_STATUS_INT(wait_status) = status; |
| 9228 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9229 | } |
| 9230 | #endif /* WIFSIGNALED */ |
| 9231 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9232 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9233 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9234 | /*[clinic input] |
| 9235 | os.WIFEXITED -> bool |
| 9236 | |
| 9237 | status: int |
| 9238 | |
| 9239 | Return True if the process returning status exited via the exit() system call. |
| 9240 | [clinic start generated code]*/ |
| 9241 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9242 | static int |
| 9243 | os_WIFEXITED_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9244 | /*[clinic end generated code: output=ca8f8c61f0b8532e input=d63775a6791586c0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9245 | { |
| 9246 | WAIT_TYPE wait_status; |
| 9247 | WAIT_STATUS_INT(wait_status) = status; |
| 9248 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9249 | } |
| 9250 | #endif /* WIFEXITED */ |
| 9251 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9252 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 9253 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9254 | /*[clinic input] |
| 9255 | os.WEXITSTATUS -> int |
| 9256 | |
| 9257 | status: int |
| 9258 | |
| 9259 | Return the process return code from status. |
| 9260 | [clinic start generated code]*/ |
| 9261 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9262 | static int |
| 9263 | os_WEXITSTATUS_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9264 | /*[clinic end generated code: output=ea54da23d9e0f6af input=e1fb4944e377585b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9265 | { |
| 9266 | WAIT_TYPE wait_status; |
| 9267 | WAIT_STATUS_INT(wait_status) = status; |
| 9268 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9269 | } |
| 9270 | #endif /* WEXITSTATUS */ |
| 9271 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9272 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9273 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9274 | /*[clinic input] |
| 9275 | os.WTERMSIG -> int |
| 9276 | |
| 9277 | status: int |
| 9278 | |
| 9279 | Return the signal that terminated the process that provided the status value. |
| 9280 | [clinic start generated code]*/ |
| 9281 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9282 | static int |
| 9283 | os_WTERMSIG_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9284 | /*[clinic end generated code: output=4d25367026cb852c input=727fd7f84ec3f243]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9285 | { |
| 9286 | WAIT_TYPE wait_status; |
| 9287 | WAIT_STATUS_INT(wait_status) = status; |
| 9288 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9289 | } |
| 9290 | #endif /* WTERMSIG */ |
| 9291 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9292 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9293 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9294 | /*[clinic input] |
| 9295 | os.WSTOPSIG -> int |
| 9296 | |
| 9297 | status: int |
| 9298 | |
| 9299 | Return the signal that stopped the process that provided the status value. |
| 9300 | [clinic start generated code]*/ |
| 9301 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9302 | static int |
| 9303 | os_WSTOPSIG_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9304 | /*[clinic end generated code: output=54eb9c13b001adb4 input=46ebf1d1b293c5c1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9305 | { |
| 9306 | WAIT_TYPE wait_status; |
| 9307 | WAIT_STATUS_INT(wait_status) = status; |
| 9308 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9309 | } |
| 9310 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9311 | #endif /* HAVE_SYS_WAIT_H */ |
| 9312 | |
| 9313 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9314 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 9315 | #ifdef _SCO_DS |
| 9316 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 9317 | needed definitions in sys/statvfs.h */ |
| 9318 | #define _SVID3 |
| 9319 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9320 | #include <sys/statvfs.h> |
| 9321 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9322 | static PyObject* |
| 9323 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9324 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 9325 | if (v == NULL) |
| 9326 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9327 | |
| 9328 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9329 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9330 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9331 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 9332 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 9333 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 9334 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 9335 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 9336 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 9337 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9338 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9339 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9340 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9341 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9342 | PyStructSequence_SET_ITEM(v, 2, |
| 9343 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 9344 | PyStructSequence_SET_ITEM(v, 3, |
| 9345 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 9346 | PyStructSequence_SET_ITEM(v, 4, |
| 9347 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 9348 | PyStructSequence_SET_ITEM(v, 5, |
| 9349 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 9350 | PyStructSequence_SET_ITEM(v, 6, |
| 9351 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 9352 | PyStructSequence_SET_ITEM(v, 7, |
| 9353 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 9354 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9355 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9356 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 9357 | if (PyErr_Occurred()) { |
| 9358 | Py_DECREF(v); |
| 9359 | return NULL; |
| 9360 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9361 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9362 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9363 | } |
| 9364 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9365 | |
| 9366 | /*[clinic input] |
| 9367 | os.fstatvfs |
| 9368 | fd: int |
| 9369 | / |
| 9370 | |
| 9371 | Perform an fstatvfs system call on the given fd. |
| 9372 | |
| 9373 | Equivalent to statvfs(fd). |
| 9374 | [clinic start generated code]*/ |
| 9375 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9376 | static PyObject * |
| 9377 | os_fstatvfs_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9378 | /*[clinic end generated code: output=584a94a754497ac0 input=d8122243ac50975e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9379 | { |
| 9380 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9381 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9382 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9383 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9384 | do { |
| 9385 | Py_BEGIN_ALLOW_THREADS |
| 9386 | result = fstatvfs(fd, &st); |
| 9387 | Py_END_ALLOW_THREADS |
| 9388 | } while (result != 0 && errno == EINTR && |
| 9389 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9390 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9391 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9392 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9393 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9394 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9395 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9396 | |
| 9397 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9398 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9399 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9400 | /*[clinic input] |
| 9401 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9402 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9403 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 9404 | |
| 9405 | Perform a statvfs system call on the given path. |
| 9406 | |
| 9407 | path may always be specified as a string. |
| 9408 | On some platforms, path may also be specified as an open file descriptor. |
| 9409 | If this functionality is unavailable, using it raises an exception. |
| 9410 | [clinic start generated code]*/ |
| 9411 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9412 | static PyObject * |
| 9413 | os_statvfs_impl(PyModuleDef *module, path_t *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9414 | /*[clinic end generated code: output=5ced07a2cf931f41 input=3f5c35791c669bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9415 | { |
| 9416 | int result; |
| 9417 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9418 | |
| 9419 | Py_BEGIN_ALLOW_THREADS |
| 9420 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9421 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9422 | #ifdef __APPLE__ |
| 9423 | /* handle weak-linking on Mac OS X 10.3 */ |
| 9424 | if (fstatvfs == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9425 | fd_specified("statvfs", path->fd); |
| 9426 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9427 | } |
| 9428 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9429 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9430 | } |
| 9431 | else |
| 9432 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9433 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9434 | Py_END_ALLOW_THREADS |
| 9435 | |
| 9436 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9437 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9438 | } |
| 9439 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9440 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9441 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9442 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 9443 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9444 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9445 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9446 | /*[clinic input] |
| 9447 | os._getdiskusage |
| 9448 | |
| 9449 | path: Py_UNICODE |
| 9450 | |
| 9451 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 9452 | [clinic start generated code]*/ |
| 9453 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9454 | static PyObject * |
| 9455 | os__getdiskusage_impl(PyModuleDef *module, Py_UNICODE *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9456 | /*[clinic end generated code: output=60a9cf33449db1dd input=6458133aed893c78]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9457 | { |
| 9458 | BOOL retval; |
| 9459 | ULARGE_INTEGER _, total, free; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9460 | |
| 9461 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 9462 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9463 | Py_END_ALLOW_THREADS |
| 9464 | if (retval == 0) |
| 9465 | return PyErr_SetFromWindowsErr(0); |
| 9466 | |
| 9467 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 9468 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9469 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9470 | |
| 9471 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9472 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 9473 | * It maps strings representing configuration variable names to |
| 9474 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 9475 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9476 | * rarely-used constants. There are three separate tables that use |
| 9477 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9478 | * |
| 9479 | * This code is always included, even if none of the interfaces that |
| 9480 | * need it are included. The #if hackery needed to avoid it would be |
| 9481 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9482 | */ |
| 9483 | struct constdef { |
| 9484 | char *name; |
| 9485 | long value; |
| 9486 | }; |
| 9487 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9488 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9489 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 9490 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9491 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9492 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9493 | *valuep = PyLong_AS_LONG(arg); |
| 9494 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9495 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 9496 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9497 | /* look up the value in the table using a binary search */ |
| 9498 | size_t lo = 0; |
| 9499 | size_t mid; |
| 9500 | size_t hi = tablesize; |
| 9501 | int cmp; |
| 9502 | const char *confname; |
| 9503 | if (!PyUnicode_Check(arg)) { |
| 9504 | PyErr_SetString(PyExc_TypeError, |
| 9505 | "configuration names must be strings or integers"); |
| 9506 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9507 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9508 | confname = _PyUnicode_AsString(arg); |
| 9509 | if (confname == NULL) |
| 9510 | return 0; |
| 9511 | while (lo < hi) { |
| 9512 | mid = (lo + hi) / 2; |
| 9513 | cmp = strcmp(confname, table[mid].name); |
| 9514 | if (cmp < 0) |
| 9515 | hi = mid; |
| 9516 | else if (cmp > 0) |
| 9517 | lo = mid + 1; |
| 9518 | else { |
| 9519 | *valuep = table[mid].value; |
| 9520 | return 1; |
| 9521 | } |
| 9522 | } |
| 9523 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 9524 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9525 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9526 | } |
| 9527 | |
| 9528 | |
| 9529 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 9530 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9531 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9532 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9533 | #endif |
| 9534 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9535 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9536 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9537 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9538 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9539 | #endif |
| 9540 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9541 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9542 | #endif |
| 9543 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9544 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9545 | #endif |
| 9546 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9547 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9548 | #endif |
| 9549 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9550 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9551 | #endif |
| 9552 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9553 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9554 | #endif |
| 9555 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9556 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9557 | #endif |
| 9558 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9559 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9560 | #endif |
| 9561 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9562 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9563 | #endif |
| 9564 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9565 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9566 | #endif |
| 9567 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9568 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9569 | #endif |
| 9570 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9571 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9572 | #endif |
| 9573 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9574 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9575 | #endif |
| 9576 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9577 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9578 | #endif |
| 9579 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9580 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9581 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 9582 | #ifdef _PC_ACL_ENABLED |
| 9583 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 9584 | #endif |
| 9585 | #ifdef _PC_MIN_HOLE_SIZE |
| 9586 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 9587 | #endif |
| 9588 | #ifdef _PC_ALLOC_SIZE_MIN |
| 9589 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 9590 | #endif |
| 9591 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 9592 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 9593 | #endif |
| 9594 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 9595 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 9596 | #endif |
| 9597 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 9598 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 9599 | #endif |
| 9600 | #ifdef _PC_REC_XFER_ALIGN |
| 9601 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 9602 | #endif |
| 9603 | #ifdef _PC_SYMLINK_MAX |
| 9604 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 9605 | #endif |
| 9606 | #ifdef _PC_XATTR_ENABLED |
| 9607 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 9608 | #endif |
| 9609 | #ifdef _PC_XATTR_EXISTS |
| 9610 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 9611 | #endif |
| 9612 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 9613 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 9614 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9615 | }; |
| 9616 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9617 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9618 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9619 | { |
| 9620 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 9621 | sizeof(posix_constants_pathconf) |
| 9622 | / sizeof(struct constdef)); |
| 9623 | } |
| 9624 | #endif |
| 9625 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9626 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9627 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9628 | /*[clinic input] |
| 9629 | os.fpathconf -> long |
| 9630 | |
| 9631 | fd: int |
| 9632 | name: path_confname |
| 9633 | / |
| 9634 | |
| 9635 | Return the configuration limit name for the file descriptor fd. |
| 9636 | |
| 9637 | If there is no limit, return -1. |
| 9638 | [clinic start generated code]*/ |
| 9639 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9640 | static long |
| 9641 | os_fpathconf_impl(PyModuleDef *module, int fd, int name) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9642 | /*[clinic end generated code: output=082b2922d4441de7 input=5942a024d3777810]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9643 | { |
| 9644 | long limit; |
| 9645 | |
| 9646 | errno = 0; |
| 9647 | limit = fpathconf(fd, name); |
| 9648 | if (limit == -1 && errno != 0) |
| 9649 | posix_error(); |
| 9650 | |
| 9651 | return limit; |
| 9652 | } |
| 9653 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9654 | |
| 9655 | |
| 9656 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9657 | /*[clinic input] |
| 9658 | os.pathconf -> long |
| 9659 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 9660 | name: path_confname |
| 9661 | |
| 9662 | Return the configuration limit name for the file or directory path. |
| 9663 | |
| 9664 | If there is no limit, return -1. |
| 9665 | On some platforms, path may also be specified as an open file descriptor. |
| 9666 | If this functionality is unavailable, using it raises an exception. |
| 9667 | [clinic start generated code]*/ |
| 9668 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9669 | static long |
| 9670 | os_pathconf_impl(PyModuleDef *module, path_t *path, int name) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9671 | /*[clinic end generated code: output=3713029e9501f5ab input=bc3e2a985af27e5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9672 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9673 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9674 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9675 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9676 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9677 | if (path->fd != -1) |
| 9678 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9679 | else |
| 9680 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9681 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9682 | if (limit == -1 && errno != 0) { |
| 9683 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 9684 | /* could be a path or name problem */ |
| 9685 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9686 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9687 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9688 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9689 | |
| 9690 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9691 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9692 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9693 | |
| 9694 | #ifdef HAVE_CONFSTR |
| 9695 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9696 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9697 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9698 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9699 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9700 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9701 | #endif |
| 9702 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9703 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9704 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9705 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9706 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9707 | #endif |
| 9708 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9709 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9710 | #endif |
| 9711 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9712 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9713 | #endif |
| 9714 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9715 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9716 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9717 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9718 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9719 | #endif |
| 9720 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9721 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9722 | #endif |
| 9723 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9724 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9725 | #endif |
| 9726 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9727 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9728 | #endif |
| 9729 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9730 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9731 | #endif |
| 9732 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9733 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9734 | #endif |
| 9735 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9736 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9737 | #endif |
| 9738 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9739 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9740 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9741 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9742 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9743 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9744 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9745 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9746 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9747 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9748 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9749 | #endif |
| 9750 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9751 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9752 | #endif |
| 9753 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9754 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9755 | #endif |
| 9756 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9757 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9758 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9759 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9760 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9761 | #endif |
| 9762 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9763 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9764 | #endif |
| 9765 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9766 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9767 | #endif |
| 9768 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9769 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9770 | #endif |
| 9771 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9772 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9773 | #endif |
| 9774 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9775 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9776 | #endif |
| 9777 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9778 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9779 | #endif |
| 9780 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9781 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9782 | #endif |
| 9783 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9784 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9785 | #endif |
| 9786 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9787 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9788 | #endif |
| 9789 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9790 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9791 | #endif |
| 9792 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9793 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9794 | #endif |
| 9795 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9796 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9797 | #endif |
| 9798 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9799 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9800 | #endif |
| 9801 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9802 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9803 | #endif |
| 9804 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9805 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9806 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9807 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9808 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9809 | #endif |
| 9810 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9811 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9812 | #endif |
| 9813 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9814 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9815 | #endif |
| 9816 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9817 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9818 | #endif |
| 9819 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9820 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9821 | #endif |
| 9822 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9823 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9824 | #endif |
| 9825 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9826 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9827 | #endif |
| 9828 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9829 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9830 | #endif |
| 9831 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9832 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9833 | #endif |
| 9834 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9835 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9836 | #endif |
| 9837 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9838 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9839 | #endif |
| 9840 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9841 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9842 | #endif |
| 9843 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9844 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9845 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9846 | }; |
| 9847 | |
| 9848 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9849 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9850 | { |
| 9851 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 9852 | sizeof(posix_constants_confstr) |
| 9853 | / sizeof(struct constdef)); |
| 9854 | } |
| 9855 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9856 | |
| 9857 | /*[clinic input] |
| 9858 | os.confstr |
| 9859 | |
| 9860 | name: confstr_confname |
| 9861 | / |
| 9862 | |
| 9863 | Return a string-valued system configuration variable. |
| 9864 | [clinic start generated code]*/ |
| 9865 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9866 | static PyObject * |
| 9867 | os_confstr_impl(PyModuleDef *module, int name) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9868 | /*[clinic end generated code: output=6ff79c9eed8c2daf input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9869 | { |
| 9870 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9871 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9872 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9873 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9874 | errno = 0; |
| 9875 | len = confstr(name, buffer, sizeof(buffer)); |
| 9876 | if (len == 0) { |
| 9877 | if (errno) { |
| 9878 | posix_error(); |
| 9879 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9880 | } |
| 9881 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9882 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9883 | } |
| 9884 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9885 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9886 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 9887 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9888 | char *buf = PyMem_Malloc(len); |
| 9889 | if (buf == NULL) |
| 9890 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 9891 | len2 = confstr(name, buf, len); |
| 9892 | assert(len == len2); |
Christian Heimes | 8714cfd | 2015-04-21 10:57:41 +0200 | [diff] [blame] | 9893 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9894 | PyMem_Free(buf); |
| 9895 | } |
| 9896 | else |
| 9897 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9898 | return result; |
| 9899 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9900 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9901 | |
| 9902 | |
| 9903 | #ifdef HAVE_SYSCONF |
| 9904 | static struct constdef posix_constants_sysconf[] = { |
| 9905 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9906 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9907 | #endif |
| 9908 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9909 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9910 | #endif |
| 9911 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9912 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9913 | #endif |
| 9914 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9915 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9916 | #endif |
| 9917 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9918 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9919 | #endif |
| 9920 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9921 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9922 | #endif |
| 9923 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9924 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9925 | #endif |
| 9926 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9927 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9928 | #endif |
| 9929 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9930 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9931 | #endif |
| 9932 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9933 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9934 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9935 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9936 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9937 | #endif |
| 9938 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9939 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9940 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9941 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9942 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9943 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9944 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9945 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9946 | #endif |
| 9947 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9948 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9949 | #endif |
| 9950 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9951 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9952 | #endif |
| 9953 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9954 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9955 | #endif |
| 9956 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9957 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9958 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9959 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9960 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9961 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9962 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9963 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9964 | #endif |
| 9965 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9966 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9967 | #endif |
| 9968 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9969 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9970 | #endif |
| 9971 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9972 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9973 | #endif |
| 9974 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9975 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9976 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9977 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9978 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9979 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9980 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9981 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9982 | #endif |
| 9983 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9984 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9985 | #endif |
| 9986 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9987 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9988 | #endif |
| 9989 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9990 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9991 | #endif |
| 9992 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9993 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9994 | #endif |
| 9995 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9996 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9997 | #endif |
| 9998 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9999 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10000 | #endif |
| 10001 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10002 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10003 | #endif |
| 10004 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10005 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10006 | #endif |
| 10007 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10008 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10009 | #endif |
| 10010 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10011 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10012 | #endif |
| 10013 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10014 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10015 | #endif |
| 10016 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10017 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10018 | #endif |
| 10019 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10020 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10021 | #endif |
| 10022 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10023 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10024 | #endif |
| 10025 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10026 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10027 | #endif |
| 10028 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10029 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10030 | #endif |
| 10031 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10032 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10033 | #endif |
| 10034 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10035 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10036 | #endif |
| 10037 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10038 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10039 | #endif |
| 10040 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10041 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10042 | #endif |
| 10043 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10044 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10045 | #endif |
| 10046 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10047 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10048 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10049 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10050 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10051 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10052 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10053 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10054 | #endif |
| 10055 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10056 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10057 | #endif |
| 10058 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10059 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10060 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10061 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10062 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10063 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10064 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10065 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10066 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10067 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10068 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10069 | #endif |
| 10070 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10071 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10072 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10073 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10074 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10075 | #endif |
| 10076 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10077 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10078 | #endif |
| 10079 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10080 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10081 | #endif |
| 10082 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10083 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10084 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10085 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10086 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10087 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10088 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10089 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10090 | #endif |
| 10091 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10092 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10093 | #endif |
| 10094 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10095 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10096 | #endif |
| 10097 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10098 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10099 | #endif |
| 10100 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10101 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10102 | #endif |
| 10103 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10104 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10105 | #endif |
| 10106 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10107 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10108 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10109 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10110 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10111 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10112 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10113 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10114 | #endif |
| 10115 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10116 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10117 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10118 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10119 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10120 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10121 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10122 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10123 | #endif |
| 10124 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10125 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10126 | #endif |
| 10127 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10128 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10129 | #endif |
| 10130 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10131 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10132 | #endif |
| 10133 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10134 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10135 | #endif |
| 10136 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10137 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10138 | #endif |
| 10139 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10140 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10141 | #endif |
| 10142 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10143 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10144 | #endif |
| 10145 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10146 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10147 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10148 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10149 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10150 | #endif |
| 10151 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10152 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10153 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10154 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10155 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10156 | #endif |
| 10157 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10158 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10159 | #endif |
| 10160 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10161 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10162 | #endif |
| 10163 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10164 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10165 | #endif |
| 10166 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10167 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10168 | #endif |
| 10169 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10170 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10171 | #endif |
| 10172 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10173 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10174 | #endif |
| 10175 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10176 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10177 | #endif |
| 10178 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10179 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10180 | #endif |
| 10181 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10182 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10183 | #endif |
| 10184 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10185 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10186 | #endif |
| 10187 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10188 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10189 | #endif |
| 10190 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10191 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10192 | #endif |
| 10193 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10194 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10195 | #endif |
| 10196 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10197 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10198 | #endif |
| 10199 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10200 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10201 | #endif |
| 10202 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10203 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10204 | #endif |
| 10205 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10206 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10207 | #endif |
| 10208 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10209 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10210 | #endif |
| 10211 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10212 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10213 | #endif |
| 10214 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10215 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10216 | #endif |
| 10217 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10218 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10219 | #endif |
| 10220 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10221 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10222 | #endif |
| 10223 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10224 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10225 | #endif |
| 10226 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10227 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10228 | #endif |
| 10229 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10230 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10231 | #endif |
| 10232 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10233 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10234 | #endif |
| 10235 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10236 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10237 | #endif |
| 10238 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10239 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10240 | #endif |
| 10241 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10242 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10243 | #endif |
| 10244 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10245 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10246 | #endif |
| 10247 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10248 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10249 | #endif |
| 10250 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10251 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10252 | #endif |
| 10253 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10254 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10255 | #endif |
| 10256 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10257 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10258 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10259 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10260 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10261 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10262 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10263 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10264 | #endif |
| 10265 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10266 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10267 | #endif |
| 10268 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10269 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10270 | #endif |
| 10271 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10272 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10273 | #endif |
| 10274 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10275 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10276 | #endif |
| 10277 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10278 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10279 | #endif |
| 10280 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10281 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10282 | #endif |
| 10283 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10284 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10285 | #endif |
| 10286 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10287 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10288 | #endif |
| 10289 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10290 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10291 | #endif |
| 10292 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10293 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10294 | #endif |
| 10295 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10296 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10297 | #endif |
| 10298 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10299 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10300 | #endif |
| 10301 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10302 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10303 | #endif |
| 10304 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10305 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10306 | #endif |
| 10307 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10308 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10309 | #endif |
| 10310 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10311 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10312 | #endif |
| 10313 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10314 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10315 | #endif |
| 10316 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10317 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10318 | #endif |
| 10319 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10320 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10321 | #endif |
| 10322 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10323 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10324 | #endif |
| 10325 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10326 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10327 | #endif |
| 10328 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10329 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10330 | #endif |
| 10331 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10332 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10333 | #endif |
| 10334 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10335 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10336 | #endif |
| 10337 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10338 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10339 | #endif |
| 10340 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10341 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10342 | #endif |
| 10343 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10344 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10345 | #endif |
| 10346 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10347 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10348 | #endif |
| 10349 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10350 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10351 | #endif |
| 10352 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10353 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10354 | #endif |
| 10355 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10356 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10357 | #endif |
| 10358 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10359 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10360 | #endif |
| 10361 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10362 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10363 | #endif |
| 10364 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10365 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10366 | #endif |
| 10367 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10368 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10369 | #endif |
| 10370 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10371 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10372 | #endif |
| 10373 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10374 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10375 | #endif |
| 10376 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10377 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10378 | #endif |
| 10379 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10380 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10381 | #endif |
| 10382 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10383 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10384 | #endif |
| 10385 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10386 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10387 | #endif |
| 10388 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10389 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10390 | #endif |
| 10391 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10392 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10393 | #endif |
| 10394 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10395 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10396 | #endif |
| 10397 | }; |
| 10398 | |
| 10399 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10400 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10401 | { |
| 10402 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 10403 | sizeof(posix_constants_sysconf) |
| 10404 | / sizeof(struct constdef)); |
| 10405 | } |
| 10406 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10407 | |
| 10408 | /*[clinic input] |
| 10409 | os.sysconf -> long |
| 10410 | name: sysconf_confname |
| 10411 | / |
| 10412 | |
| 10413 | Return an integer-valued system configuration variable. |
| 10414 | [clinic start generated code]*/ |
| 10415 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10416 | static long |
| 10417 | os_sysconf_impl(PyModuleDef *module, int name) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10418 | /*[clinic end generated code: output=ed567306f58d69c4 input=279e3430a33f29e4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10419 | { |
| 10420 | long value; |
| 10421 | |
| 10422 | errno = 0; |
| 10423 | value = sysconf(name); |
| 10424 | if (value == -1 && errno != 0) |
| 10425 | posix_error(); |
| 10426 | return value; |
| 10427 | } |
| 10428 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10429 | |
| 10430 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10431 | /* 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] | 10432 | * 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] | 10433 | * the exported dictionaries that are used to publish information about the |
| 10434 | * names available on the host platform. |
| 10435 | * |
| 10436 | * Sorting the table at runtime ensures that the table is properly ordered |
| 10437 | * when used, even for platforms we're not able to test on. It also makes |
| 10438 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10439 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10440 | |
| 10441 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10442 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10443 | { |
| 10444 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10445 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10446 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10447 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10448 | |
| 10449 | return strcmp(c1->name, c2->name); |
| 10450 | } |
| 10451 | |
| 10452 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10453 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10454 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10455 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10456 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10457 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10458 | |
| 10459 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 10460 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10461 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10462 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10463 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10464 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10465 | PyObject *o = PyLong_FromLong(table[i].value); |
| 10466 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 10467 | Py_XDECREF(o); |
| 10468 | Py_DECREF(d); |
| 10469 | return -1; |
| 10470 | } |
| 10471 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10472 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10473 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10474 | } |
| 10475 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10476 | /* Return -1 on failure, 0 on success. */ |
| 10477 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10478 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10479 | { |
| 10480 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10481 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10482 | sizeof(posix_constants_pathconf) |
| 10483 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10484 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10485 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10486 | #endif |
| 10487 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10488 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10489 | sizeof(posix_constants_confstr) |
| 10490 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10491 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10492 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10493 | #endif |
| 10494 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10495 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10496 | sizeof(posix_constants_sysconf) |
| 10497 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10498 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10499 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10500 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10501 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10502 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10503 | |
| 10504 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10505 | /*[clinic input] |
| 10506 | os.abort |
| 10507 | |
| 10508 | Abort the interpreter immediately. |
| 10509 | |
| 10510 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 10511 | on the hosting operating system. This function never returns. |
| 10512 | [clinic start generated code]*/ |
| 10513 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10514 | static PyObject * |
| 10515 | os_abort_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10516 | /*[clinic end generated code: output=486bb96647c299b3 input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10517 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10518 | abort(); |
| 10519 | /*NOTREACHED*/ |
| 10520 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 10521 | return NULL; |
| 10522 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10523 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10524 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10525 | /* 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] | 10526 | PyDoc_STRVAR(win32_startfile__doc__, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10527 | "startfile(filepath [, operation])\n\ |
| 10528 | \n\ |
| 10529 | Start a file with its associated application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10530 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 10531 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 10532 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 10533 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 10534 | application (if any) its extension is associated.\n\ |
| 10535 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 10536 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10537 | \n\ |
| 10538 | startfile returns as soon as the associated application is launched.\n\ |
| 10539 | There is no option to wait for the application to close, and no way\n\ |
| 10540 | to retrieve the application's exit status.\n\ |
| 10541 | \n\ |
| 10542 | The filepath is relative to the current directory. If you want to use\n\ |
| 10543 | 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] | 10544 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10545 | |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10546 | /* Grab ShellExecute dynamically from shell32 */ |
| 10547 | static int has_ShellExecute = -1; |
| 10548 | static HINSTANCE (CALLBACK *Py_ShellExecuteA)(HWND, LPCSTR, LPCSTR, LPCSTR, |
| 10549 | LPCSTR, INT); |
| 10550 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 10551 | LPCWSTR, INT); |
| 10552 | static int |
| 10553 | check_ShellExecute() |
| 10554 | { |
| 10555 | HINSTANCE hShell32; |
| 10556 | |
| 10557 | /* only recheck */ |
| 10558 | if (-1 == has_ShellExecute) { |
| 10559 | Py_BEGIN_ALLOW_THREADS |
| 10560 | hShell32 = LoadLibraryW(L"SHELL32"); |
| 10561 | Py_END_ALLOW_THREADS |
| 10562 | if (hShell32) { |
| 10563 | *(FARPROC*)&Py_ShellExecuteA = GetProcAddress(hShell32, |
| 10564 | "ShellExecuteA"); |
| 10565 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 10566 | "ShellExecuteW"); |
| 10567 | has_ShellExecute = Py_ShellExecuteA && |
| 10568 | Py_ShellExecuteW; |
| 10569 | } else { |
| 10570 | has_ShellExecute = 0; |
| 10571 | } |
| 10572 | } |
| 10573 | return has_ShellExecute; |
| 10574 | } |
| 10575 | |
| 10576 | |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10577 | static PyObject * |
| 10578 | win32_startfile(PyObject *self, PyObject *args) |
| 10579 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10580 | PyObject *ofilepath; |
| 10581 | char *filepath; |
| 10582 | char *operation = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10583 | wchar_t *wpath, *woperation; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10584 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 10585 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10586 | PyObject *unipath, *uoperation = NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10587 | |
| 10588 | if(!check_ShellExecute()) { |
| 10589 | /* If the OS doesn't have ShellExecute, return a |
| 10590 | NotImplementedError. */ |
| 10591 | return PyErr_Format(PyExc_NotImplementedError, |
| 10592 | "startfile not available on this platform"); |
| 10593 | } |
| 10594 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10595 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 10596 | &unipath, &operation)) { |
| 10597 | PyErr_Clear(); |
| 10598 | goto normal; |
| 10599 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10600 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10601 | if (operation) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10602 | uoperation = PyUnicode_DecodeASCII(operation, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10603 | strlen(operation), NULL); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10604 | if (!uoperation) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10605 | PyErr_Clear(); |
| 10606 | operation = NULL; |
| 10607 | goto normal; |
| 10608 | } |
| 10609 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10610 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10611 | wpath = PyUnicode_AsUnicode(unipath); |
| 10612 | if (wpath == NULL) |
| 10613 | goto normal; |
| 10614 | if (uoperation) { |
| 10615 | woperation = PyUnicode_AsUnicode(uoperation); |
| 10616 | if (woperation == NULL) |
| 10617 | goto normal; |
| 10618 | } |
| 10619 | else |
| 10620 | woperation = NULL; |
| 10621 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10622 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10623 | rc = Py_ShellExecuteW((HWND)0, woperation, wpath, |
| 10624 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10625 | Py_END_ALLOW_THREADS |
| 10626 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10627 | Py_XDECREF(uoperation); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10628 | if (rc <= (HINSTANCE)32) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10629 | win32_error_object("startfile", unipath); |
| 10630 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10631 | } |
| 10632 | Py_INCREF(Py_None); |
| 10633 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10634 | |
| 10635 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10636 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 10637 | PyUnicode_FSConverter, &ofilepath, |
| 10638 | &operation)) |
| 10639 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 10640 | if (win32_warn_bytes_api()) { |
| 10641 | Py_DECREF(ofilepath); |
| 10642 | return NULL; |
| 10643 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10644 | filepath = PyBytes_AsString(ofilepath); |
| 10645 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10646 | rc = Py_ShellExecuteA((HWND)0, operation, filepath, |
| 10647 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10648 | Py_END_ALLOW_THREADS |
| 10649 | if (rc <= (HINSTANCE)32) { |
| 10650 | PyObject *errval = win32_error("startfile", filepath); |
| 10651 | Py_DECREF(ofilepath); |
| 10652 | return errval; |
| 10653 | } |
| 10654 | Py_DECREF(ofilepath); |
| 10655 | Py_INCREF(Py_None); |
| 10656 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10657 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10658 | #endif /* MS_WINDOWS */ |
| 10659 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10660 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10661 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10662 | /*[clinic input] |
| 10663 | os.getloadavg |
| 10664 | |
| 10665 | Return average recent system load information. |
| 10666 | |
| 10667 | Return the number of processes in the system run queue averaged over |
| 10668 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 10669 | Raises OSError if the load average was unobtainable. |
| 10670 | [clinic start generated code]*/ |
| 10671 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10672 | static PyObject * |
| 10673 | os_getloadavg_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10674 | /*[clinic end generated code: output=2b64c5b675d74c14 input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10675 | { |
| 10676 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10677 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10678 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 10679 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10680 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10681 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10682 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10683 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10684 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10685 | |
| 10686 | /*[clinic input] |
| 10687 | os.device_encoding |
| 10688 | fd: int |
| 10689 | |
| 10690 | Return a string describing the encoding of a terminal's file descriptor. |
| 10691 | |
| 10692 | The file descriptor must be attached to a terminal. |
| 10693 | If the device is not a terminal, return None. |
| 10694 | [clinic start generated code]*/ |
| 10695 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10696 | static PyObject * |
| 10697 | os_device_encoding_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10698 | /*[clinic end generated code: output=34f14e33468419c1 input=9e1d4a42b66df312]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10699 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10700 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 10701 | } |
| 10702 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10703 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10704 | #ifdef HAVE_SETRESUID |
| 10705 | /*[clinic input] |
| 10706 | os.setresuid |
| 10707 | |
| 10708 | ruid: uid_t |
| 10709 | euid: uid_t |
| 10710 | suid: uid_t |
| 10711 | / |
| 10712 | |
| 10713 | Set the current process's real, effective, and saved user ids. |
| 10714 | [clinic start generated code]*/ |
| 10715 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10716 | static PyObject * |
| 10717 | 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] | 10718 | /*[clinic end generated code: output=92cc330812c6ed0f input=9e33cb79a82792f3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10719 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10720 | if (setresuid(ruid, euid, suid) < 0) |
| 10721 | return posix_error(); |
| 10722 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10723 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10724 | #endif /* HAVE_SETRESUID */ |
| 10725 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10726 | |
| 10727 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10728 | /*[clinic input] |
| 10729 | os.setresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10730 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10731 | rgid: gid_t |
| 10732 | egid: gid_t |
| 10733 | sgid: gid_t |
| 10734 | / |
| 10735 | |
| 10736 | Set the current process's real, effective, and saved group ids. |
| 10737 | [clinic start generated code]*/ |
| 10738 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10739 | static PyObject * |
| 10740 | 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] | 10741 | /*[clinic end generated code: output=e91dc4842a604429 input=33e9e0785ef426b1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10742 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10743 | if (setresgid(rgid, egid, sgid) < 0) |
| 10744 | return posix_error(); |
| 10745 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10746 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10747 | #endif /* HAVE_SETRESGID */ |
| 10748 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10749 | |
| 10750 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10751 | /*[clinic input] |
| 10752 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10753 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10754 | Return a tuple of the current process's real, effective, and saved user ids. |
| 10755 | [clinic start generated code]*/ |
| 10756 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10757 | static PyObject * |
| 10758 | os_getresuid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10759 | /*[clinic end generated code: output=9ddef62faae8e477 input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10760 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10761 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10762 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 10763 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10764 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 10765 | _PyLong_FromUid(euid), |
| 10766 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10767 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10768 | #endif /* HAVE_GETRESUID */ |
| 10769 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10770 | |
| 10771 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10772 | /*[clinic input] |
| 10773 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10774 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10775 | Return a tuple of the current process's real, effective, and saved group ids. |
| 10776 | [clinic start generated code]*/ |
| 10777 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10778 | static PyObject * |
| 10779 | os_getresgid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10780 | /*[clinic end generated code: output=e1a553cbcf16234c input=517e68db9ca32df6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10781 | { |
| 10782 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10783 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 10784 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10785 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 10786 | _PyLong_FromGid(egid), |
| 10787 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10788 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10789 | #endif /* HAVE_GETRESGID */ |
| 10790 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10791 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10792 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10793 | /*[clinic input] |
| 10794 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10795 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10796 | path: path_t(allow_fd=True) |
| 10797 | attribute: path_t |
| 10798 | * |
| 10799 | follow_symlinks: bool = True |
| 10800 | |
| 10801 | Return the value of extended attribute attribute on path. |
| 10802 | |
| 10803 | path may be either a string or an open file descriptor. |
| 10804 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10805 | link, getxattr will examine the symbolic link itself instead of the file |
| 10806 | the link points to. |
| 10807 | |
| 10808 | [clinic start generated code]*/ |
| 10809 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10810 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10811 | os_getxattr_impl(PyModuleDef *module, path_t *path, path_t *attribute, |
| 10812 | int follow_symlinks) |
| 10813 | /*[clinic end generated code: output=cf2cede74bd5d412 input=8c8ea3bab78d89c2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10814 | { |
| 10815 | Py_ssize_t i; |
| 10816 | PyObject *buffer = NULL; |
| 10817 | |
| 10818 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 10819 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10820 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10821 | for (i = 0; ; i++) { |
| 10822 | void *ptr; |
| 10823 | ssize_t result; |
| 10824 | static Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
| 10825 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10826 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10827 | path_error(path); |
| 10828 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10829 | } |
| 10830 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 10831 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10832 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10833 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10834 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10835 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10836 | if (path->fd >= 0) |
| 10837 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10838 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10839 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10840 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10841 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10842 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10843 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10844 | if (result < 0) { |
| 10845 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10846 | if (errno == ERANGE) |
| 10847 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10848 | path_error(path); |
| 10849 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10850 | } |
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 != buffer_size) { |
| 10853 | /* Can only shrink. */ |
| 10854 | _PyBytes_Resize(&buffer, result); |
| 10855 | } |
| 10856 | break; |
| 10857 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10858 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10859 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10860 | } |
| 10861 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10862 | |
| 10863 | /*[clinic input] |
| 10864 | os.setxattr |
| 10865 | |
| 10866 | path: path_t(allow_fd=True) |
| 10867 | attribute: path_t |
| 10868 | value: Py_buffer |
| 10869 | flags: int = 0 |
| 10870 | * |
| 10871 | follow_symlinks: bool = True |
| 10872 | |
| 10873 | Set extended attribute attribute on path to value. |
| 10874 | |
| 10875 | path may be either a string or an open file descriptor. |
| 10876 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10877 | link, setxattr will modify the symbolic link itself instead of the file |
| 10878 | the link points to. |
| 10879 | |
| 10880 | [clinic start generated code]*/ |
| 10881 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10882 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10883 | os_setxattr_impl(PyModuleDef *module, path_t *path, path_t *attribute, |
| 10884 | Py_buffer *value, int flags, int follow_symlinks) |
| 10885 | /*[clinic end generated code: output=1b395ef82880fea0 input=f0d26833992015c2]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10886 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10887 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10888 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10889 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10890 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10891 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10892 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10893 | if (path->fd > -1) |
| 10894 | result = fsetxattr(path->fd, attribute->narrow, |
| 10895 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10896 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10897 | result = setxattr(path->narrow, attribute->narrow, |
| 10898 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10899 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10900 | result = lsetxattr(path->narrow, attribute->narrow, |
| 10901 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10902 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10903 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10904 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10905 | path_error(path); |
| 10906 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10907 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10908 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10909 | Py_RETURN_NONE; |
| 10910 | } |
| 10911 | |
| 10912 | |
| 10913 | /*[clinic input] |
| 10914 | os.removexattr |
| 10915 | |
| 10916 | path: path_t(allow_fd=True) |
| 10917 | attribute: path_t |
| 10918 | * |
| 10919 | follow_symlinks: bool = True |
| 10920 | |
| 10921 | Remove extended attribute attribute on path. |
| 10922 | |
| 10923 | path may be either a string or an open file descriptor. |
| 10924 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10925 | link, removexattr will modify the symbolic link itself instead of the file |
| 10926 | the link points to. |
| 10927 | |
| 10928 | [clinic start generated code]*/ |
| 10929 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10930 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10931 | os_removexattr_impl(PyModuleDef *module, path_t *path, path_t *attribute, |
| 10932 | int follow_symlinks) |
| 10933 | /*[clinic end generated code: output=f92bb39ab992650d input=cdb54834161e3329]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10934 | { |
| 10935 | ssize_t result; |
| 10936 | |
| 10937 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 10938 | return NULL; |
| 10939 | |
| 10940 | Py_BEGIN_ALLOW_THREADS; |
| 10941 | if (path->fd > -1) |
| 10942 | result = fremovexattr(path->fd, attribute->narrow); |
| 10943 | else if (follow_symlinks) |
| 10944 | result = removexattr(path->narrow, attribute->narrow); |
| 10945 | else |
| 10946 | result = lremovexattr(path->narrow, attribute->narrow); |
| 10947 | Py_END_ALLOW_THREADS; |
| 10948 | |
| 10949 | if (result) { |
| 10950 | return path_error(path); |
| 10951 | } |
| 10952 | |
| 10953 | Py_RETURN_NONE; |
| 10954 | } |
| 10955 | |
| 10956 | |
| 10957 | /*[clinic input] |
| 10958 | os.listxattr |
| 10959 | |
| 10960 | path: path_t(allow_fd=True, nullable=True) = None |
| 10961 | * |
| 10962 | follow_symlinks: bool = True |
| 10963 | |
| 10964 | Return a list of extended attributes on path. |
| 10965 | |
| 10966 | path may be either None, a string, or an open file descriptor. |
| 10967 | if path is None, listxattr will examine the current directory. |
| 10968 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10969 | link, listxattr will examine the symbolic link itself instead of the file |
| 10970 | the link points to. |
| 10971 | [clinic start generated code]*/ |
| 10972 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10973 | static PyObject * |
| 10974 | os_listxattr_impl(PyModuleDef *module, path_t *path, int follow_symlinks) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10975 | /*[clinic end generated code: output=a87ad6ce56e42a4f input=08cca53ac0b07c13]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10976 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10977 | Py_ssize_t i; |
| 10978 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10979 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10980 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10981 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10982 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10983 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10984 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10985 | name = path->narrow ? path->narrow : "."; |
| 10986 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10987 | for (i = 0; ; i++) { |
| 10988 | char *start, *trace, *end; |
| 10989 | ssize_t length; |
| 10990 | static Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
| 10991 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10992 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 10993 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10994 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10995 | break; |
| 10996 | } |
| 10997 | buffer = PyMem_MALLOC(buffer_size); |
| 10998 | if (!buffer) { |
| 10999 | PyErr_NoMemory(); |
| 11000 | break; |
| 11001 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11002 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11003 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11004 | if (path->fd > -1) |
| 11005 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11006 | else if (follow_symlinks) |
| 11007 | length = listxattr(name, buffer, buffer_size); |
| 11008 | else |
| 11009 | length = llistxattr(name, buffer, buffer_size); |
| 11010 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11011 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11012 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 11013 | if (errno == ERANGE) { |
| 11014 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 11015 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11016 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 11017 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11018 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11019 | break; |
| 11020 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11021 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11022 | result = PyList_New(0); |
| 11023 | if (!result) { |
| 11024 | goto exit; |
| 11025 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11026 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11027 | end = buffer + length; |
| 11028 | for (trace = start = buffer; trace != end; trace++) { |
| 11029 | if (!*trace) { |
| 11030 | int error; |
| 11031 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 11032 | trace - start); |
| 11033 | if (!attribute) { |
| 11034 | Py_DECREF(result); |
| 11035 | result = NULL; |
| 11036 | goto exit; |
| 11037 | } |
| 11038 | error = PyList_Append(result, attribute); |
| 11039 | Py_DECREF(attribute); |
| 11040 | if (error) { |
| 11041 | Py_DECREF(result); |
| 11042 | result = NULL; |
| 11043 | goto exit; |
| 11044 | } |
| 11045 | start = trace + 1; |
| 11046 | } |
| 11047 | } |
| 11048 | break; |
| 11049 | } |
| 11050 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11051 | if (buffer) |
| 11052 | PyMem_FREE(buffer); |
| 11053 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11054 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11055 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11056 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11057 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11058 | /*[clinic input] |
| 11059 | os.urandom |
| 11060 | |
| 11061 | size: Py_ssize_t |
| 11062 | / |
| 11063 | |
| 11064 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 11065 | [clinic start generated code]*/ |
| 11066 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11067 | static PyObject * |
| 11068 | os_urandom_impl(PyModuleDef *module, Py_ssize_t size) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 11069 | /*[clinic end generated code: output=e0011f021501f03b input=4067cdb1b6776c29]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11070 | { |
| 11071 | PyObject *bytes; |
| 11072 | int result; |
| 11073 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11074 | if (size < 0) |
| 11075 | return PyErr_Format(PyExc_ValueError, |
| 11076 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11077 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 11078 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11079 | return NULL; |
| 11080 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11081 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), |
| 11082 | PyBytes_GET_SIZE(bytes)); |
| 11083 | if (result == -1) { |
| 11084 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11085 | return NULL; |
| 11086 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11087 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11088 | } |
| 11089 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11090 | /* Terminal size querying */ |
| 11091 | |
| 11092 | static PyTypeObject TerminalSizeType; |
| 11093 | |
| 11094 | PyDoc_STRVAR(TerminalSize_docstring, |
| 11095 | "A tuple of (columns, lines) for holding terminal window size"); |
| 11096 | |
| 11097 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 11098 | {"columns", "width of the terminal window in characters"}, |
| 11099 | {"lines", "height of the terminal window in characters"}, |
| 11100 | {NULL, NULL} |
| 11101 | }; |
| 11102 | |
| 11103 | static PyStructSequence_Desc TerminalSize_desc = { |
| 11104 | "os.terminal_size", |
| 11105 | TerminalSize_docstring, |
| 11106 | TerminalSize_fields, |
| 11107 | 2, |
| 11108 | }; |
| 11109 | |
| 11110 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11111 | /* AC 3.5: fd should accept None */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11112 | PyDoc_STRVAR(termsize__doc__, |
| 11113 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 11114 | "\n" \ |
| 11115 | "The optional argument fd (default standard output) specifies\n" \ |
| 11116 | "which file descriptor should be queried.\n" \ |
| 11117 | "\n" \ |
| 11118 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 11119 | "is thrown.\n" \ |
| 11120 | "\n" \ |
| 11121 | "This function will only be defined if an implementation is\n" \ |
| 11122 | "available for this system.\n" \ |
| 11123 | "\n" \ |
| 11124 | "shutil.get_terminal_size is the high-level function which should \n" \ |
| 11125 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 11126 | |
| 11127 | static PyObject* |
| 11128 | get_terminal_size(PyObject *self, PyObject *args) |
| 11129 | { |
| 11130 | int columns, lines; |
| 11131 | PyObject *termsize; |
| 11132 | |
| 11133 | int fd = fileno(stdout); |
| 11134 | /* Under some conditions stdout may not be connected and |
| 11135 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 11136 | * GUI apps don't have valid standard streams by default. |
| 11137 | * |
| 11138 | * If this happens, and the optional fd argument is not present, |
| 11139 | * the ioctl below will fail returning EBADF. This is what we want. |
| 11140 | */ |
| 11141 | |
| 11142 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 11143 | return NULL; |
| 11144 | |
| 11145 | #ifdef TERMSIZE_USE_IOCTL |
| 11146 | { |
| 11147 | struct winsize w; |
| 11148 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 11149 | return PyErr_SetFromErrno(PyExc_OSError); |
| 11150 | columns = w.ws_col; |
| 11151 | lines = w.ws_row; |
| 11152 | } |
| 11153 | #endif /* TERMSIZE_USE_IOCTL */ |
| 11154 | |
| 11155 | #ifdef TERMSIZE_USE_CONIO |
| 11156 | { |
| 11157 | DWORD nhandle; |
| 11158 | HANDLE handle; |
| 11159 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 11160 | switch (fd) { |
| 11161 | case 0: nhandle = STD_INPUT_HANDLE; |
| 11162 | break; |
| 11163 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 11164 | break; |
| 11165 | case 2: nhandle = STD_ERROR_HANDLE; |
| 11166 | break; |
| 11167 | default: |
| 11168 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 11169 | } |
| 11170 | handle = GetStdHandle(nhandle); |
| 11171 | if (handle == NULL) |
| 11172 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 11173 | if (handle == INVALID_HANDLE_VALUE) |
| 11174 | return PyErr_SetFromWindowsErr(0); |
| 11175 | |
| 11176 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 11177 | return PyErr_SetFromWindowsErr(0); |
| 11178 | |
| 11179 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 11180 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 11181 | } |
| 11182 | #endif /* TERMSIZE_USE_CONIO */ |
| 11183 | |
| 11184 | termsize = PyStructSequence_New(&TerminalSizeType); |
| 11185 | if (termsize == NULL) |
| 11186 | return NULL; |
| 11187 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 11188 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 11189 | if (PyErr_Occurred()) { |
| 11190 | Py_DECREF(termsize); |
| 11191 | return NULL; |
| 11192 | } |
| 11193 | return termsize; |
| 11194 | } |
| 11195 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 11196 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11197 | |
| 11198 | /*[clinic input] |
| 11199 | os.cpu_count |
| 11200 | |
| 11201 | Return the number of CPUs in the system; return None if indeterminable. |
| 11202 | [clinic start generated code]*/ |
| 11203 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11204 | static PyObject * |
| 11205 | os_cpu_count_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 11206 | /*[clinic end generated code: output=c59ee7f6bce832b8 input=d55e2f8f3823a628]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11207 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11208 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11209 | #ifdef MS_WINDOWS |
| 11210 | SYSTEM_INFO sysinfo; |
| 11211 | GetSystemInfo(&sysinfo); |
| 11212 | ncpu = sysinfo.dwNumberOfProcessors; |
| 11213 | #elif defined(__hpux) |
| 11214 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 11215 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 11216 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11217 | #elif defined(__DragonFly__) || \ |
| 11218 | defined(__OpenBSD__) || \ |
| 11219 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11220 | defined(__NetBSD__) || \ |
| 11221 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 11222 | int mib[2]; |
| 11223 | size_t len = sizeof(ncpu); |
| 11224 | mib[0] = CTL_HW; |
| 11225 | mib[1] = HW_NCPU; |
| 11226 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 11227 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11228 | #endif |
| 11229 | if (ncpu >= 1) |
| 11230 | return PyLong_FromLong(ncpu); |
| 11231 | else |
| 11232 | Py_RETURN_NONE; |
| 11233 | } |
| 11234 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11235 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11236 | /*[clinic input] |
| 11237 | os.get_inheritable -> bool |
| 11238 | |
| 11239 | fd: int |
| 11240 | / |
| 11241 | |
| 11242 | Get the close-on-exe flag of the specified file descriptor. |
| 11243 | [clinic start generated code]*/ |
| 11244 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11245 | static int |
| 11246 | os_get_inheritable_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 11247 | /*[clinic end generated code: output=36110bb36efaa21e input=89ac008dc9ab6b95]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11248 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11249 | int return_value; |
| 11250 | if (!_PyVerify_fd(fd)) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11251 | posix_error(); |
| 11252 | return -1; |
| 11253 | } |
| 11254 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11255 | _Py_BEGIN_SUPPRESS_IPH |
| 11256 | return_value = _Py_get_inheritable(fd); |
| 11257 | _Py_END_SUPPRESS_IPH |
| 11258 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11259 | } |
| 11260 | |
| 11261 | |
| 11262 | /*[clinic input] |
| 11263 | os.set_inheritable |
| 11264 | fd: int |
| 11265 | inheritable: int |
| 11266 | / |
| 11267 | |
| 11268 | Set the inheritable flag of the specified file descriptor. |
| 11269 | [clinic start generated code]*/ |
| 11270 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11271 | static PyObject * |
| 11272 | os_set_inheritable_impl(PyModuleDef *module, int fd, int inheritable) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 11273 | /*[clinic end generated code: output=2ac5c6ce8623f045 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11274 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11275 | int result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11276 | if (!_PyVerify_fd(fd)) |
| 11277 | return posix_error(); |
| 11278 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11279 | _Py_BEGIN_SUPPRESS_IPH |
| 11280 | result = _Py_set_inheritable(fd, inheritable, NULL); |
| 11281 | _Py_END_SUPPRESS_IPH |
| 11282 | if (result < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11283 | return NULL; |
| 11284 | Py_RETURN_NONE; |
| 11285 | } |
| 11286 | |
| 11287 | |
| 11288 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11289 | /*[clinic input] |
| 11290 | os.get_handle_inheritable -> bool |
| 11291 | handle: Py_intptr_t |
| 11292 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11293 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11294 | Get the close-on-exe flag of the specified file descriptor. |
| 11295 | [clinic start generated code]*/ |
| 11296 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11297 | static int |
| 11298 | os_get_handle_inheritable_impl(PyModuleDef *module, Py_intptr_t handle) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 11299 | /*[clinic end generated code: output=3b7b3e1b43f312b6 input=5f7759443aae3dc5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11300 | { |
| 11301 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11302 | |
| 11303 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 11304 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11305 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11306 | } |
| 11307 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11308 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11309 | } |
| 11310 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11311 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11312 | /*[clinic input] |
| 11313 | os.set_handle_inheritable |
| 11314 | handle: Py_intptr_t |
| 11315 | inheritable: bool |
| 11316 | / |
| 11317 | |
| 11318 | Set the inheritable flag of the specified handle. |
| 11319 | [clinic start generated code]*/ |
| 11320 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11321 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11322 | os_set_handle_inheritable_impl(PyModuleDef *module, Py_intptr_t handle, |
| 11323 | int inheritable) |
| 11324 | /*[clinic end generated code: output=d2e111a96c9eb296 input=e64b2b2730469def]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11325 | { |
| 11326 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11327 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 11328 | PyErr_SetFromWindowsErr(0); |
| 11329 | return NULL; |
| 11330 | } |
| 11331 | Py_RETURN_NONE; |
| 11332 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11333 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11334 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11335 | #ifndef MS_WINDOWS |
| 11336 | PyDoc_STRVAR(get_blocking__doc__, |
| 11337 | "get_blocking(fd) -> bool\n" \ |
| 11338 | "\n" \ |
| 11339 | "Get the blocking mode of the file descriptor:\n" \ |
| 11340 | "False if the O_NONBLOCK flag is set, True if the flag is cleared."); |
| 11341 | |
| 11342 | static PyObject* |
| 11343 | posix_get_blocking(PyObject *self, PyObject *args) |
| 11344 | { |
| 11345 | int fd; |
| 11346 | int blocking; |
| 11347 | |
| 11348 | if (!PyArg_ParseTuple(args, "i:get_blocking", &fd)) |
| 11349 | return NULL; |
| 11350 | |
| 11351 | if (!_PyVerify_fd(fd)) |
| 11352 | return posix_error(); |
| 11353 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11354 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11355 | blocking = _Py_get_blocking(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11356 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11357 | if (blocking < 0) |
| 11358 | return NULL; |
| 11359 | return PyBool_FromLong(blocking); |
| 11360 | } |
| 11361 | |
| 11362 | PyDoc_STRVAR(set_blocking__doc__, |
| 11363 | "set_blocking(fd, blocking)\n" \ |
| 11364 | "\n" \ |
| 11365 | "Set the blocking mode of the specified file descriptor.\n" \ |
| 11366 | "Set the O_NONBLOCK flag if blocking is False,\n" \ |
| 11367 | "clear the O_NONBLOCK flag otherwise."); |
| 11368 | |
| 11369 | static PyObject* |
| 11370 | posix_set_blocking(PyObject *self, PyObject *args) |
| 11371 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11372 | int fd, blocking, result; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11373 | |
| 11374 | if (!PyArg_ParseTuple(args, "ii:set_blocking", &fd, &blocking)) |
| 11375 | return NULL; |
| 11376 | |
| 11377 | if (!_PyVerify_fd(fd)) |
| 11378 | return posix_error(); |
| 11379 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 11380 | _Py_BEGIN_SUPPRESS_IPH |
| 11381 | result = _Py_set_blocking(fd, blocking); |
| 11382 | _Py_END_SUPPRESS_IPH |
| 11383 | if (result < 0) |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11384 | return NULL; |
| 11385 | Py_RETURN_NONE; |
| 11386 | } |
| 11387 | #endif /* !MS_WINDOWS */ |
| 11388 | |
| 11389 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11390 | PyDoc_STRVAR(posix_scandir__doc__, |
| 11391 | "scandir(path='.') -> iterator of DirEntry objects for given path"); |
| 11392 | |
| 11393 | static char *follow_symlinks_keywords[] = {"follow_symlinks", NULL}; |
| 11394 | |
| 11395 | typedef struct { |
| 11396 | PyObject_HEAD |
| 11397 | PyObject *name; |
| 11398 | PyObject *path; |
| 11399 | PyObject *stat; |
| 11400 | PyObject *lstat; |
| 11401 | #ifdef MS_WINDOWS |
| 11402 | struct _Py_stat_struct win32_lstat; |
| 11403 | __int64 win32_file_index; |
| 11404 | int got_file_index; |
| 11405 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11406 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11407 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11408 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11409 | ino_t d_ino; |
| 11410 | #endif |
| 11411 | } DirEntry; |
| 11412 | |
| 11413 | static void |
| 11414 | DirEntry_dealloc(DirEntry *entry) |
| 11415 | { |
| 11416 | Py_XDECREF(entry->name); |
| 11417 | Py_XDECREF(entry->path); |
| 11418 | Py_XDECREF(entry->stat); |
| 11419 | Py_XDECREF(entry->lstat); |
| 11420 | Py_TYPE(entry)->tp_free((PyObject *)entry); |
| 11421 | } |
| 11422 | |
| 11423 | /* Forward reference */ |
| 11424 | static int |
| 11425 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits); |
| 11426 | |
| 11427 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 11428 | static int |
| 11429 | DirEntry_is_symlink(DirEntry *self) |
| 11430 | { |
| 11431 | #ifdef MS_WINDOWS |
| 11432 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11433 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 11434 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11435 | if (self->d_type != DT_UNKNOWN) |
| 11436 | return self->d_type == DT_LNK; |
| 11437 | else |
| 11438 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11439 | #else |
| 11440 | /* POSIX without d_type */ |
| 11441 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11442 | #endif |
| 11443 | } |
| 11444 | |
| 11445 | static PyObject * |
| 11446 | DirEntry_py_is_symlink(DirEntry *self) |
| 11447 | { |
| 11448 | int result; |
| 11449 | |
| 11450 | result = DirEntry_is_symlink(self); |
| 11451 | if (result == -1) |
| 11452 | return NULL; |
| 11453 | return PyBool_FromLong(result); |
| 11454 | } |
| 11455 | |
| 11456 | static PyObject * |
| 11457 | DirEntry_fetch_stat(DirEntry *self, int follow_symlinks) |
| 11458 | { |
| 11459 | int result; |
| 11460 | struct _Py_stat_struct st; |
| 11461 | |
| 11462 | #ifdef MS_WINDOWS |
| 11463 | wchar_t *path; |
| 11464 | |
| 11465 | path = PyUnicode_AsUnicode(self->path); |
| 11466 | if (!path) |
| 11467 | return NULL; |
| 11468 | |
| 11469 | if (follow_symlinks) |
| 11470 | result = win32_stat_w(path, &st); |
| 11471 | else |
| 11472 | result = win32_lstat_w(path, &st); |
| 11473 | |
| 11474 | if (result != 0) { |
| 11475 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 11476 | 0, self->path); |
| 11477 | } |
| 11478 | #else /* POSIX */ |
| 11479 | PyObject *bytes; |
| 11480 | char *path; |
| 11481 | |
| 11482 | if (!PyUnicode_FSConverter(self->path, &bytes)) |
| 11483 | return NULL; |
| 11484 | path = PyBytes_AS_STRING(bytes); |
| 11485 | |
| 11486 | if (follow_symlinks) |
| 11487 | result = STAT(path, &st); |
| 11488 | else |
| 11489 | result = LSTAT(path, &st); |
| 11490 | Py_DECREF(bytes); |
| 11491 | |
| 11492 | if (result != 0) |
| 11493 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, self->path); |
| 11494 | #endif |
| 11495 | |
| 11496 | return _pystat_fromstructstat(&st); |
| 11497 | } |
| 11498 | |
| 11499 | static PyObject * |
| 11500 | DirEntry_get_lstat(DirEntry *self) |
| 11501 | { |
| 11502 | if (!self->lstat) { |
| 11503 | #ifdef MS_WINDOWS |
| 11504 | self->lstat = _pystat_fromstructstat(&self->win32_lstat); |
| 11505 | #else /* POSIX */ |
| 11506 | self->lstat = DirEntry_fetch_stat(self, 0); |
| 11507 | #endif |
| 11508 | } |
| 11509 | Py_XINCREF(self->lstat); |
| 11510 | return self->lstat; |
| 11511 | } |
| 11512 | |
| 11513 | static PyObject * |
| 11514 | DirEntry_get_stat(DirEntry *self, int follow_symlinks) |
| 11515 | { |
| 11516 | if (!follow_symlinks) |
| 11517 | return DirEntry_get_lstat(self); |
| 11518 | |
| 11519 | if (!self->stat) { |
| 11520 | int result = DirEntry_is_symlink(self); |
| 11521 | if (result == -1) |
| 11522 | return NULL; |
| 11523 | else if (result) |
| 11524 | self->stat = DirEntry_fetch_stat(self, 1); |
| 11525 | else |
| 11526 | self->stat = DirEntry_get_lstat(self); |
| 11527 | } |
| 11528 | |
| 11529 | Py_XINCREF(self->stat); |
| 11530 | return self->stat; |
| 11531 | } |
| 11532 | |
| 11533 | static PyObject * |
| 11534 | DirEntry_stat(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 11535 | { |
| 11536 | int follow_symlinks = 1; |
| 11537 | |
| 11538 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.stat", |
| 11539 | follow_symlinks_keywords, &follow_symlinks)) |
| 11540 | return NULL; |
| 11541 | |
| 11542 | return DirEntry_get_stat(self, follow_symlinks); |
| 11543 | } |
| 11544 | |
| 11545 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 11546 | static int |
| 11547 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 11548 | { |
| 11549 | PyObject *stat = NULL; |
| 11550 | PyObject *st_mode = NULL; |
| 11551 | long mode; |
| 11552 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11553 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11554 | int is_symlink; |
| 11555 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11556 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11557 | #ifdef MS_WINDOWS |
| 11558 | unsigned long dir_bits; |
| 11559 | #endif |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11560 | _Py_IDENTIFIER(st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11561 | |
| 11562 | #ifdef MS_WINDOWS |
| 11563 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 11564 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11565 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11566 | is_symlink = self->d_type == DT_LNK; |
| 11567 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 11568 | #endif |
| 11569 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11570 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11571 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11572 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11573 | stat = DirEntry_get_stat(self, follow_symlinks); |
| 11574 | if (!stat) { |
| 11575 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 11576 | /* If file doesn't exist (anymore), then return False |
| 11577 | (i.e., say it's not a file/directory) */ |
| 11578 | PyErr_Clear(); |
| 11579 | return 0; |
| 11580 | } |
| 11581 | goto error; |
| 11582 | } |
| 11583 | st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode); |
| 11584 | if (!st_mode) |
| 11585 | goto error; |
| 11586 | |
| 11587 | mode = PyLong_AsLong(st_mode); |
| 11588 | if (mode == -1 && PyErr_Occurred()) |
| 11589 | goto error; |
| 11590 | Py_CLEAR(st_mode); |
| 11591 | Py_CLEAR(stat); |
| 11592 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11593 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11594 | } |
| 11595 | else if (is_symlink) { |
| 11596 | assert(mode_bits != S_IFLNK); |
| 11597 | result = 0; |
| 11598 | } |
| 11599 | else { |
| 11600 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 11601 | #ifdef MS_WINDOWS |
| 11602 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 11603 | if (mode_bits == S_IFDIR) |
| 11604 | result = dir_bits != 0; |
| 11605 | else |
| 11606 | result = dir_bits == 0; |
| 11607 | #else /* POSIX */ |
| 11608 | if (mode_bits == S_IFDIR) |
| 11609 | result = self->d_type == DT_DIR; |
| 11610 | else |
| 11611 | result = self->d_type == DT_REG; |
| 11612 | #endif |
| 11613 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11614 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11615 | |
| 11616 | return result; |
| 11617 | |
| 11618 | error: |
| 11619 | Py_XDECREF(st_mode); |
| 11620 | Py_XDECREF(stat); |
| 11621 | return -1; |
| 11622 | } |
| 11623 | |
| 11624 | static PyObject * |
| 11625 | DirEntry_py_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 11626 | { |
| 11627 | int result; |
| 11628 | |
| 11629 | result = DirEntry_test_mode(self, follow_symlinks, mode_bits); |
| 11630 | if (result == -1) |
| 11631 | return NULL; |
| 11632 | return PyBool_FromLong(result); |
| 11633 | } |
| 11634 | |
| 11635 | static PyObject * |
| 11636 | DirEntry_is_dir(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 11637 | { |
| 11638 | int follow_symlinks = 1; |
| 11639 | |
| 11640 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_dir", |
| 11641 | follow_symlinks_keywords, &follow_symlinks)) |
| 11642 | return NULL; |
| 11643 | |
| 11644 | return DirEntry_py_test_mode(self, follow_symlinks, S_IFDIR); |
| 11645 | } |
| 11646 | |
| 11647 | static PyObject * |
| 11648 | DirEntry_is_file(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 11649 | { |
| 11650 | int follow_symlinks = 1; |
| 11651 | |
| 11652 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_file", |
| 11653 | follow_symlinks_keywords, &follow_symlinks)) |
| 11654 | return NULL; |
| 11655 | |
| 11656 | return DirEntry_py_test_mode(self, follow_symlinks, S_IFREG); |
| 11657 | } |
| 11658 | |
| 11659 | static PyObject * |
| 11660 | DirEntry_inode(DirEntry *self) |
| 11661 | { |
| 11662 | #ifdef MS_WINDOWS |
| 11663 | if (!self->got_file_index) { |
| 11664 | wchar_t *path; |
| 11665 | struct _Py_stat_struct stat; |
| 11666 | |
| 11667 | path = PyUnicode_AsUnicode(self->path); |
| 11668 | if (!path) |
| 11669 | return NULL; |
| 11670 | |
| 11671 | if (win32_lstat_w(path, &stat) != 0) { |
| 11672 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 11673 | 0, self->path); |
| 11674 | } |
| 11675 | |
| 11676 | self->win32_file_index = stat.st_ino; |
| 11677 | self->got_file_index = 1; |
| 11678 | } |
| 11679 | return PyLong_FromLongLong((PY_LONG_LONG)self->win32_file_index); |
| 11680 | #else /* POSIX */ |
| 11681 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 11682 | return PyLong_FromLongLong((PY_LONG_LONG)self->d_ino); |
| 11683 | #else |
| 11684 | return PyLong_FromLong((long)self->d_ino); |
| 11685 | #endif |
| 11686 | #endif |
| 11687 | } |
| 11688 | |
| 11689 | static PyObject * |
| 11690 | DirEntry_repr(DirEntry *self) |
| 11691 | { |
| 11692 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 11693 | } |
| 11694 | |
| 11695 | static PyMemberDef DirEntry_members[] = { |
| 11696 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 11697 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 11698 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 11699 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 11700 | {NULL} |
| 11701 | }; |
| 11702 | |
| 11703 | static PyMethodDef DirEntry_methods[] = { |
| 11704 | {"is_dir", (PyCFunction)DirEntry_is_dir, METH_VARARGS | METH_KEYWORDS, |
| 11705 | "return True if the entry is a directory; cached per entry" |
| 11706 | }, |
| 11707 | {"is_file", (PyCFunction)DirEntry_is_file, METH_VARARGS | METH_KEYWORDS, |
| 11708 | "return True if the entry is a file; cached per entry" |
| 11709 | }, |
| 11710 | {"is_symlink", (PyCFunction)DirEntry_py_is_symlink, METH_NOARGS, |
| 11711 | "return True if the entry is a symbolic link; cached per entry" |
| 11712 | }, |
| 11713 | {"stat", (PyCFunction)DirEntry_stat, METH_VARARGS | METH_KEYWORDS, |
| 11714 | "return stat_result object for the entry; cached per entry" |
| 11715 | }, |
| 11716 | {"inode", (PyCFunction)DirEntry_inode, METH_NOARGS, |
| 11717 | "return inode of the entry; cached per entry", |
| 11718 | }, |
| 11719 | {NULL} |
| 11720 | }; |
| 11721 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 11722 | static PyTypeObject DirEntryType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11723 | PyVarObject_HEAD_INIT(NULL, 0) |
| 11724 | MODNAME ".DirEntry", /* tp_name */ |
| 11725 | sizeof(DirEntry), /* tp_basicsize */ |
| 11726 | 0, /* tp_itemsize */ |
| 11727 | /* methods */ |
| 11728 | (destructor)DirEntry_dealloc, /* tp_dealloc */ |
| 11729 | 0, /* tp_print */ |
| 11730 | 0, /* tp_getattr */ |
| 11731 | 0, /* tp_setattr */ |
| 11732 | 0, /* tp_compare */ |
| 11733 | (reprfunc)DirEntry_repr, /* tp_repr */ |
| 11734 | 0, /* tp_as_number */ |
| 11735 | 0, /* tp_as_sequence */ |
| 11736 | 0, /* tp_as_mapping */ |
| 11737 | 0, /* tp_hash */ |
| 11738 | 0, /* tp_call */ |
| 11739 | 0, /* tp_str */ |
| 11740 | 0, /* tp_getattro */ |
| 11741 | 0, /* tp_setattro */ |
| 11742 | 0, /* tp_as_buffer */ |
| 11743 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 11744 | 0, /* tp_doc */ |
| 11745 | 0, /* tp_traverse */ |
| 11746 | 0, /* tp_clear */ |
| 11747 | 0, /* tp_richcompare */ |
| 11748 | 0, /* tp_weaklistoffset */ |
| 11749 | 0, /* tp_iter */ |
| 11750 | 0, /* tp_iternext */ |
| 11751 | DirEntry_methods, /* tp_methods */ |
| 11752 | DirEntry_members, /* tp_members */ |
| 11753 | }; |
| 11754 | |
| 11755 | #ifdef MS_WINDOWS |
| 11756 | |
| 11757 | static wchar_t * |
| 11758 | join_path_filenameW(wchar_t *path_wide, wchar_t* filename) |
| 11759 | { |
| 11760 | Py_ssize_t path_len; |
| 11761 | Py_ssize_t size; |
| 11762 | wchar_t *result; |
| 11763 | wchar_t ch; |
| 11764 | |
| 11765 | if (!path_wide) { /* Default arg: "." */ |
| 11766 | path_wide = L"."; |
| 11767 | path_len = 1; |
| 11768 | } |
| 11769 | else { |
| 11770 | path_len = wcslen(path_wide); |
| 11771 | } |
| 11772 | |
| 11773 | /* The +1's are for the path separator and the NUL */ |
| 11774 | size = path_len + 1 + wcslen(filename) + 1; |
| 11775 | result = PyMem_New(wchar_t, size); |
| 11776 | if (!result) { |
| 11777 | PyErr_NoMemory(); |
| 11778 | return NULL; |
| 11779 | } |
| 11780 | wcscpy(result, path_wide); |
| 11781 | if (path_len > 0) { |
| 11782 | ch = result[path_len - 1]; |
| 11783 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 11784 | result[path_len++] = SEP; |
| 11785 | wcscpy(result + path_len, filename); |
| 11786 | } |
| 11787 | return result; |
| 11788 | } |
| 11789 | |
| 11790 | static PyObject * |
| 11791 | DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW) |
| 11792 | { |
| 11793 | DirEntry *entry; |
| 11794 | BY_HANDLE_FILE_INFORMATION file_info; |
| 11795 | ULONG reparse_tag; |
| 11796 | wchar_t *joined_path; |
| 11797 | |
| 11798 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 11799 | if (!entry) |
| 11800 | return NULL; |
| 11801 | entry->name = NULL; |
| 11802 | entry->path = NULL; |
| 11803 | entry->stat = NULL; |
| 11804 | entry->lstat = NULL; |
| 11805 | entry->got_file_index = 0; |
| 11806 | |
| 11807 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 11808 | if (!entry->name) |
| 11809 | goto error; |
| 11810 | |
| 11811 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 11812 | if (!joined_path) |
| 11813 | goto error; |
| 11814 | |
| 11815 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 11816 | PyMem_Free(joined_path); |
| 11817 | if (!entry->path) |
| 11818 | goto error; |
| 11819 | |
| 11820 | find_data_to_file_info_w(dataW, &file_info, &reparse_tag); |
| 11821 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 11822 | |
| 11823 | return (PyObject *)entry; |
| 11824 | |
| 11825 | error: |
| 11826 | Py_DECREF(entry); |
| 11827 | return NULL; |
| 11828 | } |
| 11829 | |
| 11830 | #else /* POSIX */ |
| 11831 | |
| 11832 | static char * |
| 11833 | join_path_filename(char *path_narrow, char* filename, Py_ssize_t filename_len) |
| 11834 | { |
| 11835 | Py_ssize_t path_len; |
| 11836 | Py_ssize_t size; |
| 11837 | char *result; |
| 11838 | |
| 11839 | if (!path_narrow) { /* Default arg: "." */ |
| 11840 | path_narrow = "."; |
| 11841 | path_len = 1; |
| 11842 | } |
| 11843 | else { |
| 11844 | path_len = strlen(path_narrow); |
| 11845 | } |
| 11846 | |
| 11847 | if (filename_len == -1) |
| 11848 | filename_len = strlen(filename); |
| 11849 | |
| 11850 | /* The +1's are for the path separator and the NUL */ |
| 11851 | size = path_len + 1 + filename_len + 1; |
| 11852 | result = PyMem_New(char, size); |
| 11853 | if (!result) { |
| 11854 | PyErr_NoMemory(); |
| 11855 | return NULL; |
| 11856 | } |
| 11857 | strcpy(result, path_narrow); |
| 11858 | if (path_len > 0 && result[path_len - 1] != '/') |
| 11859 | result[path_len++] = '/'; |
| 11860 | strcpy(result + path_len, filename); |
| 11861 | return result; |
| 11862 | } |
| 11863 | |
| 11864 | static PyObject * |
| 11865 | DirEntry_from_posix_info(path_t *path, char *name, Py_ssize_t name_len, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11866 | ino_t d_ino |
| 11867 | #ifdef HAVE_DIRENT_D_TYPE |
| 11868 | , unsigned char d_type |
| 11869 | #endif |
| 11870 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11871 | { |
| 11872 | DirEntry *entry; |
| 11873 | char *joined_path; |
| 11874 | |
| 11875 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 11876 | if (!entry) |
| 11877 | return NULL; |
| 11878 | entry->name = NULL; |
| 11879 | entry->path = NULL; |
| 11880 | entry->stat = NULL; |
| 11881 | entry->lstat = NULL; |
| 11882 | |
| 11883 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 11884 | if (!joined_path) |
| 11885 | goto error; |
| 11886 | |
| 11887 | if (!path->narrow || !PyBytes_Check(path->object)) { |
| 11888 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
| 11889 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
| 11890 | } |
| 11891 | else { |
| 11892 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
| 11893 | entry->path = PyBytes_FromString(joined_path); |
| 11894 | } |
| 11895 | PyMem_Free(joined_path); |
| 11896 | if (!entry->name || !entry->path) |
| 11897 | goto error; |
| 11898 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11899 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11900 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11901 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11902 | entry->d_ino = d_ino; |
| 11903 | |
| 11904 | return (PyObject *)entry; |
| 11905 | |
| 11906 | error: |
| 11907 | Py_XDECREF(entry); |
| 11908 | return NULL; |
| 11909 | } |
| 11910 | |
| 11911 | #endif |
| 11912 | |
| 11913 | |
| 11914 | typedef struct { |
| 11915 | PyObject_HEAD |
| 11916 | path_t path; |
| 11917 | #ifdef MS_WINDOWS |
| 11918 | HANDLE handle; |
| 11919 | WIN32_FIND_DATAW file_data; |
| 11920 | int first_time; |
| 11921 | #else /* POSIX */ |
| 11922 | DIR *dirp; |
| 11923 | #endif |
| 11924 | } ScandirIterator; |
| 11925 | |
| 11926 | #ifdef MS_WINDOWS |
| 11927 | |
| 11928 | static void |
| 11929 | ScandirIterator_close(ScandirIterator *iterator) |
| 11930 | { |
| 11931 | if (iterator->handle == INVALID_HANDLE_VALUE) |
| 11932 | return; |
| 11933 | |
| 11934 | Py_BEGIN_ALLOW_THREADS |
| 11935 | FindClose(iterator->handle); |
| 11936 | Py_END_ALLOW_THREADS |
| 11937 | iterator->handle = INVALID_HANDLE_VALUE; |
| 11938 | } |
| 11939 | |
| 11940 | static PyObject * |
| 11941 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 11942 | { |
| 11943 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 11944 | BOOL success; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11945 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11946 | |
| 11947 | /* Happens if the iterator is iterated twice */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11948 | if (iterator->handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11949 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11950 | |
| 11951 | while (1) { |
| 11952 | if (!iterator->first_time) { |
| 11953 | Py_BEGIN_ALLOW_THREADS |
| 11954 | success = FindNextFileW(iterator->handle, file_data); |
| 11955 | Py_END_ALLOW_THREADS |
| 11956 | if (!success) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11957 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11958 | if (GetLastError() != ERROR_NO_MORE_FILES) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11959 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11960 | break; |
| 11961 | } |
| 11962 | } |
| 11963 | iterator->first_time = 0; |
| 11964 | |
| 11965 | /* Skip over . and .. */ |
| 11966 | if (wcscmp(file_data->cFileName, L".") != 0 && |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11967 | wcscmp(file_data->cFileName, L"..") != 0) { |
| 11968 | entry = DirEntry_from_find_data(&iterator->path, file_data); |
| 11969 | if (!entry) |
| 11970 | break; |
| 11971 | return entry; |
| 11972 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11973 | |
| 11974 | /* Loop till we get a non-dot directory or finish iterating */ |
| 11975 | } |
| 11976 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 11977 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11978 | ScandirIterator_close(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11979 | return NULL; |
| 11980 | } |
| 11981 | |
| 11982 | #else /* POSIX */ |
| 11983 | |
| 11984 | static void |
| 11985 | ScandirIterator_close(ScandirIterator *iterator) |
| 11986 | { |
| 11987 | if (!iterator->dirp) |
| 11988 | return; |
| 11989 | |
| 11990 | Py_BEGIN_ALLOW_THREADS |
| 11991 | closedir(iterator->dirp); |
| 11992 | Py_END_ALLOW_THREADS |
| 11993 | iterator->dirp = NULL; |
| 11994 | return; |
| 11995 | } |
| 11996 | |
| 11997 | static PyObject * |
| 11998 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 11999 | { |
| 12000 | struct dirent *direntp; |
| 12001 | Py_ssize_t name_len; |
| 12002 | int is_dot; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12003 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12004 | |
| 12005 | /* Happens if the iterator is iterated twice */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12006 | if (!iterator->dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12007 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12008 | |
| 12009 | while (1) { |
| 12010 | errno = 0; |
| 12011 | Py_BEGIN_ALLOW_THREADS |
| 12012 | direntp = readdir(iterator->dirp); |
| 12013 | Py_END_ALLOW_THREADS |
| 12014 | |
| 12015 | if (!direntp) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12016 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12017 | if (errno != 0) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12018 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12019 | break; |
| 12020 | } |
| 12021 | |
| 12022 | /* Skip over . and .. */ |
| 12023 | name_len = NAMLEN(direntp); |
| 12024 | is_dot = direntp->d_name[0] == '.' && |
| 12025 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 12026 | if (!is_dot) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12027 | entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12028 | name_len, direntp->d_ino |
| 12029 | #ifdef HAVE_DIRENT_D_TYPE |
| 12030 | , direntp->d_type |
| 12031 | #endif |
| 12032 | ); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12033 | if (!entry) |
| 12034 | break; |
| 12035 | return entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12036 | } |
| 12037 | |
| 12038 | /* Loop till we get a non-dot directory or finish iterating */ |
| 12039 | } |
| 12040 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 12041 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12042 | ScandirIterator_close(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12043 | return NULL; |
| 12044 | } |
| 12045 | |
| 12046 | #endif |
| 12047 | |
| 12048 | static void |
| 12049 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 12050 | { |
| 12051 | ScandirIterator_close(iterator); |
| 12052 | Py_XDECREF(iterator->path.object); |
| 12053 | path_cleanup(&iterator->path); |
| 12054 | Py_TYPE(iterator)->tp_free((PyObject *)iterator); |
| 12055 | } |
| 12056 | |
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 12057 | static PyTypeObject ScandirIteratorType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12058 | PyVarObject_HEAD_INIT(NULL, 0) |
| 12059 | MODNAME ".ScandirIterator", /* tp_name */ |
| 12060 | sizeof(ScandirIterator), /* tp_basicsize */ |
| 12061 | 0, /* tp_itemsize */ |
| 12062 | /* methods */ |
| 12063 | (destructor)ScandirIterator_dealloc, /* tp_dealloc */ |
| 12064 | 0, /* tp_print */ |
| 12065 | 0, /* tp_getattr */ |
| 12066 | 0, /* tp_setattr */ |
| 12067 | 0, /* tp_compare */ |
| 12068 | 0, /* tp_repr */ |
| 12069 | 0, /* tp_as_number */ |
| 12070 | 0, /* tp_as_sequence */ |
| 12071 | 0, /* tp_as_mapping */ |
| 12072 | 0, /* tp_hash */ |
| 12073 | 0, /* tp_call */ |
| 12074 | 0, /* tp_str */ |
| 12075 | 0, /* tp_getattro */ |
| 12076 | 0, /* tp_setattro */ |
| 12077 | 0, /* tp_as_buffer */ |
| 12078 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 12079 | 0, /* tp_doc */ |
| 12080 | 0, /* tp_traverse */ |
| 12081 | 0, /* tp_clear */ |
| 12082 | 0, /* tp_richcompare */ |
| 12083 | 0, /* tp_weaklistoffset */ |
| 12084 | PyObject_SelfIter, /* tp_iter */ |
| 12085 | (iternextfunc)ScandirIterator_iternext, /* tp_iternext */ |
| 12086 | }; |
| 12087 | |
| 12088 | static PyObject * |
| 12089 | posix_scandir(PyObject *self, PyObject *args, PyObject *kwargs) |
| 12090 | { |
| 12091 | ScandirIterator *iterator; |
| 12092 | static char *keywords[] = {"path", NULL}; |
| 12093 | #ifdef MS_WINDOWS |
| 12094 | wchar_t *path_strW; |
| 12095 | #else |
| 12096 | char *path; |
| 12097 | #endif |
| 12098 | |
| 12099 | iterator = PyObject_New(ScandirIterator, &ScandirIteratorType); |
| 12100 | if (!iterator) |
| 12101 | return NULL; |
| 12102 | memset(&iterator->path, 0, sizeof(path_t)); |
| 12103 | iterator->path.function_name = "scandir"; |
| 12104 | iterator->path.nullable = 1; |
| 12105 | |
| 12106 | #ifdef MS_WINDOWS |
| 12107 | iterator->handle = INVALID_HANDLE_VALUE; |
| 12108 | #else |
| 12109 | iterator->dirp = NULL; |
| 12110 | #endif |
| 12111 | |
| 12112 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:scandir", keywords, |
| 12113 | path_converter, &iterator->path)) |
| 12114 | goto error; |
| 12115 | |
| 12116 | /* path_converter doesn't keep path.object around, so do it |
| 12117 | manually for the lifetime of the iterator here (the refcount |
| 12118 | is decremented in ScandirIterator_dealloc) |
| 12119 | */ |
| 12120 | Py_XINCREF(iterator->path.object); |
| 12121 | |
| 12122 | #ifdef MS_WINDOWS |
| 12123 | if (iterator->path.narrow) { |
| 12124 | PyErr_SetString(PyExc_TypeError, |
| 12125 | "os.scandir() doesn't support bytes path on Windows, use Unicode instead"); |
| 12126 | goto error; |
| 12127 | } |
| 12128 | iterator->first_time = 1; |
| 12129 | |
| 12130 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 12131 | if (!path_strW) |
| 12132 | goto error; |
| 12133 | |
| 12134 | Py_BEGIN_ALLOW_THREADS |
| 12135 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 12136 | Py_END_ALLOW_THREADS |
| 12137 | |
| 12138 | PyMem_Free(path_strW); |
| 12139 | |
| 12140 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 12141 | path_error(&iterator->path); |
| 12142 | goto error; |
| 12143 | } |
| 12144 | #else /* POSIX */ |
| 12145 | if (iterator->path.narrow) |
| 12146 | path = iterator->path.narrow; |
| 12147 | else |
| 12148 | path = "."; |
| 12149 | |
| 12150 | errno = 0; |
| 12151 | Py_BEGIN_ALLOW_THREADS |
| 12152 | iterator->dirp = opendir(path); |
| 12153 | Py_END_ALLOW_THREADS |
| 12154 | |
| 12155 | if (!iterator->dirp) { |
| 12156 | path_error(&iterator->path); |
| 12157 | goto error; |
| 12158 | } |
| 12159 | #endif |
| 12160 | |
| 12161 | return (PyObject *)iterator; |
| 12162 | |
| 12163 | error: |
| 12164 | Py_DECREF(iterator); |
| 12165 | return NULL; |
| 12166 | } |
| 12167 | |
| 12168 | |
Serhiy Storchaka | a4c6bad | 2015-04-04 23:35:52 +0300 | [diff] [blame] | 12169 | #include "clinic/posixmodule.c.h" |
| 12170 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 12171 | /*[clinic input] |
| 12172 | dump buffer |
| 12173 | [clinic start generated code]*/ |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 12174 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/ |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 12175 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12176 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12177 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12178 | |
| 12179 | OS_STAT_METHODDEF |
| 12180 | OS_ACCESS_METHODDEF |
| 12181 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12182 | OS_CHDIR_METHODDEF |
| 12183 | OS_CHFLAGS_METHODDEF |
| 12184 | OS_CHMOD_METHODDEF |
| 12185 | OS_FCHMOD_METHODDEF |
| 12186 | OS_LCHMOD_METHODDEF |
| 12187 | OS_CHOWN_METHODDEF |
| 12188 | OS_FCHOWN_METHODDEF |
| 12189 | OS_LCHOWN_METHODDEF |
| 12190 | OS_LCHFLAGS_METHODDEF |
| 12191 | OS_CHROOT_METHODDEF |
| 12192 | OS_CTERMID_METHODDEF |
| 12193 | OS_GETCWD_METHODDEF |
| 12194 | OS_GETCWDB_METHODDEF |
| 12195 | OS_LINK_METHODDEF |
| 12196 | OS_LISTDIR_METHODDEF |
| 12197 | OS_LSTAT_METHODDEF |
| 12198 | OS_MKDIR_METHODDEF |
| 12199 | OS_NICE_METHODDEF |
| 12200 | OS_GETPRIORITY_METHODDEF |
| 12201 | OS_SETPRIORITY_METHODDEF |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12202 | #ifdef HAVE_READLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12203 | {"readlink", (PyCFunction)posix_readlink, |
| 12204 | METH_VARARGS | METH_KEYWORDS, |
| 12205 | readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 12206 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 12207 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12208 | {"readlink", (PyCFunction)win_readlink, |
| 12209 | METH_VARARGS | METH_KEYWORDS, |
| 12210 | readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 12211 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12212 | OS_RENAME_METHODDEF |
| 12213 | OS_REPLACE_METHODDEF |
| 12214 | OS_RMDIR_METHODDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12215 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12216 | OS_SYMLINK_METHODDEF |
| 12217 | OS_SYSTEM_METHODDEF |
| 12218 | OS_UMASK_METHODDEF |
| 12219 | OS_UNAME_METHODDEF |
| 12220 | OS_UNLINK_METHODDEF |
| 12221 | OS_REMOVE_METHODDEF |
| 12222 | OS_UTIME_METHODDEF |
| 12223 | OS_TIMES_METHODDEF |
| 12224 | OS__EXIT_METHODDEF |
| 12225 | OS_EXECV_METHODDEF |
| 12226 | OS_EXECVE_METHODDEF |
| 12227 | OS_SPAWNV_METHODDEF |
| 12228 | OS_SPAWNVE_METHODDEF |
| 12229 | OS_FORK1_METHODDEF |
| 12230 | OS_FORK_METHODDEF |
| 12231 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 12232 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 12233 | OS_SCHED_GETPARAM_METHODDEF |
| 12234 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 12235 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 12236 | OS_SCHED_SETPARAM_METHODDEF |
| 12237 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 12238 | OS_SCHED_YIELD_METHODDEF |
| 12239 | OS_SCHED_SETAFFINITY_METHODDEF |
| 12240 | OS_SCHED_GETAFFINITY_METHODDEF |
| 12241 | OS_OPENPTY_METHODDEF |
| 12242 | OS_FORKPTY_METHODDEF |
| 12243 | OS_GETEGID_METHODDEF |
| 12244 | OS_GETEUID_METHODDEF |
| 12245 | OS_GETGID_METHODDEF |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 12246 | #ifdef HAVE_GETGROUPLIST |
| 12247 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 12248 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12249 | OS_GETGROUPS_METHODDEF |
| 12250 | OS_GETPID_METHODDEF |
| 12251 | OS_GETPGRP_METHODDEF |
| 12252 | OS_GETPPID_METHODDEF |
| 12253 | OS_GETUID_METHODDEF |
| 12254 | OS_GETLOGIN_METHODDEF |
| 12255 | OS_KILL_METHODDEF |
| 12256 | OS_KILLPG_METHODDEF |
| 12257 | OS_PLOCK_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 12258 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12259 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 12260 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12261 | OS_SETUID_METHODDEF |
| 12262 | OS_SETEUID_METHODDEF |
| 12263 | OS_SETREUID_METHODDEF |
| 12264 | OS_SETGID_METHODDEF |
| 12265 | OS_SETEGID_METHODDEF |
| 12266 | OS_SETREGID_METHODDEF |
| 12267 | OS_SETGROUPS_METHODDEF |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 12268 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12269 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 12270 | #endif /* HAVE_INITGROUPS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12271 | OS_GETPGID_METHODDEF |
| 12272 | OS_SETPGRP_METHODDEF |
| 12273 | OS_WAIT_METHODDEF |
| 12274 | OS_WAIT3_METHODDEF |
| 12275 | OS_WAIT4_METHODDEF |
| 12276 | OS_WAITID_METHODDEF |
| 12277 | OS_WAITPID_METHODDEF |
| 12278 | OS_GETSID_METHODDEF |
| 12279 | OS_SETSID_METHODDEF |
| 12280 | OS_SETPGID_METHODDEF |
| 12281 | OS_TCGETPGRP_METHODDEF |
| 12282 | OS_TCSETPGRP_METHODDEF |
| 12283 | OS_OPEN_METHODDEF |
| 12284 | OS_CLOSE_METHODDEF |
| 12285 | OS_CLOSERANGE_METHODDEF |
| 12286 | OS_DEVICE_ENCODING_METHODDEF |
| 12287 | OS_DUP_METHODDEF |
| 12288 | OS_DUP2_METHODDEF |
| 12289 | OS_LOCKF_METHODDEF |
| 12290 | OS_LSEEK_METHODDEF |
| 12291 | OS_READ_METHODDEF |
| 12292 | OS_READV_METHODDEF |
| 12293 | OS_PREAD_METHODDEF |
| 12294 | OS_WRITE_METHODDEF |
| 12295 | OS_WRITEV_METHODDEF |
| 12296 | OS_PWRITE_METHODDEF |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12297 | #ifdef HAVE_SENDFILE |
| 12298 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 12299 | posix_sendfile__doc__}, |
| 12300 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12301 | OS_FSTAT_METHODDEF |
| 12302 | OS_ISATTY_METHODDEF |
| 12303 | OS_PIPE_METHODDEF |
| 12304 | OS_PIPE2_METHODDEF |
| 12305 | OS_MKFIFO_METHODDEF |
| 12306 | OS_MKNOD_METHODDEF |
| 12307 | OS_MAJOR_METHODDEF |
| 12308 | OS_MINOR_METHODDEF |
| 12309 | OS_MAKEDEV_METHODDEF |
| 12310 | OS_FTRUNCATE_METHODDEF |
| 12311 | OS_TRUNCATE_METHODDEF |
| 12312 | OS_POSIX_FALLOCATE_METHODDEF |
| 12313 | OS_POSIX_FADVISE_METHODDEF |
| 12314 | OS_PUTENV_METHODDEF |
| 12315 | OS_UNSETENV_METHODDEF |
| 12316 | OS_STRERROR_METHODDEF |
| 12317 | OS_FCHDIR_METHODDEF |
| 12318 | OS_FSYNC_METHODDEF |
| 12319 | OS_SYNC_METHODDEF |
| 12320 | OS_FDATASYNC_METHODDEF |
| 12321 | OS_WCOREDUMP_METHODDEF |
| 12322 | OS_WIFCONTINUED_METHODDEF |
| 12323 | OS_WIFSTOPPED_METHODDEF |
| 12324 | OS_WIFSIGNALED_METHODDEF |
| 12325 | OS_WIFEXITED_METHODDEF |
| 12326 | OS_WEXITSTATUS_METHODDEF |
| 12327 | OS_WTERMSIG_METHODDEF |
| 12328 | OS_WSTOPSIG_METHODDEF |
| 12329 | OS_FSTATVFS_METHODDEF |
| 12330 | OS_STATVFS_METHODDEF |
| 12331 | OS_CONFSTR_METHODDEF |
| 12332 | OS_SYSCONF_METHODDEF |
| 12333 | OS_FPATHCONF_METHODDEF |
| 12334 | OS_PATHCONF_METHODDEF |
| 12335 | OS_ABORT_METHODDEF |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 12336 | OS__GETFULLPATHNAME_METHODDEF |
| 12337 | OS__ISDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12338 | OS__GETDISKUSAGE_METHODDEF |
| 12339 | OS__GETFINALPATHNAME_METHODDEF |
| 12340 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 12341 | OS_GETLOADAVG_METHODDEF |
| 12342 | OS_URANDOM_METHODDEF |
| 12343 | OS_SETRESUID_METHODDEF |
| 12344 | OS_SETRESGID_METHODDEF |
| 12345 | OS_GETRESUID_METHODDEF |
| 12346 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12347 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12348 | OS_GETXATTR_METHODDEF |
| 12349 | OS_SETXATTR_METHODDEF |
| 12350 | OS_REMOVEXATTR_METHODDEF |
| 12351 | OS_LISTXATTR_METHODDEF |
| 12352 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12353 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 12354 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 12355 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12356 | OS_CPU_COUNT_METHODDEF |
| 12357 | OS_GET_INHERITABLE_METHODDEF |
| 12358 | OS_SET_INHERITABLE_METHODDEF |
| 12359 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 12360 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12361 | #ifndef MS_WINDOWS |
| 12362 | {"get_blocking", posix_get_blocking, METH_VARARGS, get_blocking__doc__}, |
| 12363 | {"set_blocking", posix_set_blocking, METH_VARARGS, set_blocking__doc__}, |
| 12364 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12365 | {"scandir", (PyCFunction)posix_scandir, |
| 12366 | METH_VARARGS | METH_KEYWORDS, |
| 12367 | posix_scandir__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12368 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 12369 | }; |
| 12370 | |
| 12371 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12372 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12373 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12374 | enable_symlink() |
| 12375 | { |
| 12376 | HANDLE tok; |
| 12377 | TOKEN_PRIVILEGES tok_priv; |
| 12378 | LUID luid; |
| 12379 | int meth_idx = 0; |
| 12380 | |
| 12381 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12382 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12383 | |
| 12384 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12385 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12386 | |
| 12387 | tok_priv.PrivilegeCount = 1; |
| 12388 | tok_priv.Privileges[0].Luid = luid; |
| 12389 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 12390 | |
| 12391 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 12392 | sizeof(TOKEN_PRIVILEGES), |
| 12393 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12394 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12395 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12396 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 12397 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12398 | } |
| 12399 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 12400 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12401 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12402 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12403 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12404 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12405 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12406 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12407 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12408 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12409 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12410 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12411 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12412 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12413 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12414 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12415 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12416 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12417 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12418 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12419 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12420 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12421 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12422 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12423 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12424 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12425 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12426 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12427 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12428 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12429 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12430 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12431 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12432 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12433 | #endif |
| 12434 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12435 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12436 | #endif |
| 12437 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12438 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12439 | #endif |
| 12440 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12441 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12442 | #endif |
| 12443 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12444 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12445 | #endif |
| 12446 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12447 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12448 | #endif |
| 12449 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12450 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12451 | #endif |
| 12452 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12453 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12454 | #endif |
| 12455 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12456 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12457 | #endif |
| 12458 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12459 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12460 | #endif |
| 12461 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12462 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12463 | #endif |
| 12464 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12465 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12466 | #endif |
| 12467 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12468 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12469 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 12470 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12471 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 12472 | #endif |
| 12473 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12474 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 12475 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12476 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12477 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12478 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12479 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12480 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12481 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 12482 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12483 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 12484 | #endif |
| 12485 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12486 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 12487 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12488 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12489 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12490 | #endif |
| 12491 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12492 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12493 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 12494 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12495 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 12496 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12497 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12498 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12499 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 12500 | #ifdef O_TMPFILE |
| 12501 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 12502 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12503 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12504 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12505 | #endif |
| 12506 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12507 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12508 | #endif |
| 12509 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12510 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12511 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 12512 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12513 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 12514 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12515 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12516 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12517 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12518 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12519 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 12520 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12521 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 12522 | #endif |
| 12523 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12524 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 12525 | #endif |
| 12526 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12527 | /* MS Windows */ |
| 12528 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12529 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12530 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12531 | #endif |
| 12532 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12533 | /* Optimize for short life (keep in memory). */ |
| 12534 | /* 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] | 12535 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12536 | #endif |
| 12537 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12538 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12539 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12540 | #endif |
| 12541 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12542 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12543 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12544 | #endif |
| 12545 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12546 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12547 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12548 | #endif |
| 12549 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12550 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 12551 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12552 | /* Send a SIGIO signal whenever input or output |
| 12553 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12554 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 12555 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12556 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12557 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12558 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12559 | #endif |
| 12560 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12561 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12562 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12563 | #endif |
| 12564 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12565 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12566 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12567 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12568 | #ifdef O_NOLINKS |
| 12569 | /* 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] | 12570 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12571 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 12572 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12573 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12574 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 12575 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 12576 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12577 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12578 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12579 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12580 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12581 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12582 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12583 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12584 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12585 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12586 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12587 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12588 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12589 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12590 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12591 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12592 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12593 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12594 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12595 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12596 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12597 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12598 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12599 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12600 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12601 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12602 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12603 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12604 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12605 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12606 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12607 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12608 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12609 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12610 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12611 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12612 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12613 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12614 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12615 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12616 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12617 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12618 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12619 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12620 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12621 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12622 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12623 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12624 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12625 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12626 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12627 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12628 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12629 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 12630 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 12631 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12632 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 12633 | #endif /* ST_RDONLY */ |
| 12634 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12635 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 12636 | #endif /* ST_NOSUID */ |
| 12637 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 12638 | /* GNU extensions */ |
| 12639 | #ifdef ST_NODEV |
| 12640 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 12641 | #endif /* ST_NODEV */ |
| 12642 | #ifdef ST_NOEXEC |
| 12643 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 12644 | #endif /* ST_NOEXEC */ |
| 12645 | #ifdef ST_SYNCHRONOUS |
| 12646 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 12647 | #endif /* ST_SYNCHRONOUS */ |
| 12648 | #ifdef ST_MANDLOCK |
| 12649 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 12650 | #endif /* ST_MANDLOCK */ |
| 12651 | #ifdef ST_WRITE |
| 12652 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 12653 | #endif /* ST_WRITE */ |
| 12654 | #ifdef ST_APPEND |
| 12655 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 12656 | #endif /* ST_APPEND */ |
| 12657 | #ifdef ST_NOATIME |
| 12658 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 12659 | #endif /* ST_NOATIME */ |
| 12660 | #ifdef ST_NODIRATIME |
| 12661 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 12662 | #endif /* ST_NODIRATIME */ |
| 12663 | #ifdef ST_RELATIME |
| 12664 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 12665 | #endif /* ST_RELATIME */ |
| 12666 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12667 | /* FreeBSD sendfile() constants */ |
| 12668 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12669 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12670 | #endif |
| 12671 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12672 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12673 | #endif |
| 12674 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12675 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12676 | #endif |
| 12677 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12678 | /* constants for posix_fadvise */ |
| 12679 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12680 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12681 | #endif |
| 12682 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12683 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12684 | #endif |
| 12685 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12686 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12687 | #endif |
| 12688 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12689 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12690 | #endif |
| 12691 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12692 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12693 | #endif |
| 12694 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12695 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12696 | #endif |
| 12697 | |
| 12698 | /* constants for waitid */ |
| 12699 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12700 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 12701 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 12702 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12703 | #endif |
| 12704 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12705 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12706 | #endif |
| 12707 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12708 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12709 | #endif |
| 12710 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12711 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12712 | #endif |
| 12713 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12714 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12715 | #endif |
| 12716 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12717 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12718 | #endif |
| 12719 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12720 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12721 | #endif |
| 12722 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12723 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12724 | #endif |
| 12725 | |
| 12726 | /* constants for lockf */ |
| 12727 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12728 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12729 | #endif |
| 12730 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12731 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12732 | #endif |
| 12733 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12734 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12735 | #endif |
| 12736 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12737 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12738 | #endif |
| 12739 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 12740 | #ifdef HAVE_SPAWNV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12741 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 12742 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
| 12743 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
| 12744 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
| 12745 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 12746 | #endif |
| 12747 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12748 | #ifdef HAVE_SCHED_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12749 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
| 12750 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
| 12751 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12752 | #ifdef SCHED_SPORADIC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12753 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12754 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12755 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12756 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12757 | #endif |
| 12758 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12759 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12760 | #endif |
| 12761 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12762 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12763 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12764 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12765 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12766 | #endif |
| 12767 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12768 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12769 | #endif |
| 12770 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12771 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12772 | #endif |
| 12773 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12774 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12775 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12776 | #endif |
| 12777 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12778 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12779 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 12780 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 12781 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12782 | #endif |
| 12783 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12784 | #ifdef RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12785 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12786 | #endif |
| 12787 | #ifdef RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12788 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12789 | #endif |
| 12790 | #ifdef RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12791 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12792 | #endif |
| 12793 | #ifdef RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12794 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12795 | #endif |
| 12796 | #ifdef RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12797 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12798 | #endif |
| 12799 | #ifdef RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12800 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12801 | #endif |
| 12802 | #ifdef RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12803 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12804 | #endif |
| 12805 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12806 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12807 | } |
| 12808 | |
| 12809 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12810 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12811 | PyModuleDef_HEAD_INIT, |
| 12812 | MODNAME, |
| 12813 | posix__doc__, |
| 12814 | -1, |
| 12815 | posix_methods, |
| 12816 | NULL, |
| 12817 | NULL, |
| 12818 | NULL, |
| 12819 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12820 | }; |
| 12821 | |
| 12822 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12823 | static char *have_functions[] = { |
| 12824 | |
| 12825 | #ifdef HAVE_FACCESSAT |
| 12826 | "HAVE_FACCESSAT", |
| 12827 | #endif |
| 12828 | |
| 12829 | #ifdef HAVE_FCHDIR |
| 12830 | "HAVE_FCHDIR", |
| 12831 | #endif |
| 12832 | |
| 12833 | #ifdef HAVE_FCHMOD |
| 12834 | "HAVE_FCHMOD", |
| 12835 | #endif |
| 12836 | |
| 12837 | #ifdef HAVE_FCHMODAT |
| 12838 | "HAVE_FCHMODAT", |
| 12839 | #endif |
| 12840 | |
| 12841 | #ifdef HAVE_FCHOWN |
| 12842 | "HAVE_FCHOWN", |
| 12843 | #endif |
| 12844 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 12845 | #ifdef HAVE_FCHOWNAT |
| 12846 | "HAVE_FCHOWNAT", |
| 12847 | #endif |
| 12848 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12849 | #ifdef HAVE_FEXECVE |
| 12850 | "HAVE_FEXECVE", |
| 12851 | #endif |
| 12852 | |
| 12853 | #ifdef HAVE_FDOPENDIR |
| 12854 | "HAVE_FDOPENDIR", |
| 12855 | #endif |
| 12856 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12857 | #ifdef HAVE_FPATHCONF |
| 12858 | "HAVE_FPATHCONF", |
| 12859 | #endif |
| 12860 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12861 | #ifdef HAVE_FSTATAT |
| 12862 | "HAVE_FSTATAT", |
| 12863 | #endif |
| 12864 | |
| 12865 | #ifdef HAVE_FSTATVFS |
| 12866 | "HAVE_FSTATVFS", |
| 12867 | #endif |
| 12868 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 12869 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12870 | "HAVE_FTRUNCATE", |
| 12871 | #endif |
| 12872 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12873 | #ifdef HAVE_FUTIMENS |
| 12874 | "HAVE_FUTIMENS", |
| 12875 | #endif |
| 12876 | |
| 12877 | #ifdef HAVE_FUTIMES |
| 12878 | "HAVE_FUTIMES", |
| 12879 | #endif |
| 12880 | |
| 12881 | #ifdef HAVE_FUTIMESAT |
| 12882 | "HAVE_FUTIMESAT", |
| 12883 | #endif |
| 12884 | |
| 12885 | #ifdef HAVE_LINKAT |
| 12886 | "HAVE_LINKAT", |
| 12887 | #endif |
| 12888 | |
| 12889 | #ifdef HAVE_LCHFLAGS |
| 12890 | "HAVE_LCHFLAGS", |
| 12891 | #endif |
| 12892 | |
| 12893 | #ifdef HAVE_LCHMOD |
| 12894 | "HAVE_LCHMOD", |
| 12895 | #endif |
| 12896 | |
| 12897 | #ifdef HAVE_LCHOWN |
| 12898 | "HAVE_LCHOWN", |
| 12899 | #endif |
| 12900 | |
| 12901 | #ifdef HAVE_LSTAT |
| 12902 | "HAVE_LSTAT", |
| 12903 | #endif |
| 12904 | |
| 12905 | #ifdef HAVE_LUTIMES |
| 12906 | "HAVE_LUTIMES", |
| 12907 | #endif |
| 12908 | |
| 12909 | #ifdef HAVE_MKDIRAT |
| 12910 | "HAVE_MKDIRAT", |
| 12911 | #endif |
| 12912 | |
| 12913 | #ifdef HAVE_MKFIFOAT |
| 12914 | "HAVE_MKFIFOAT", |
| 12915 | #endif |
| 12916 | |
| 12917 | #ifdef HAVE_MKNODAT |
| 12918 | "HAVE_MKNODAT", |
| 12919 | #endif |
| 12920 | |
| 12921 | #ifdef HAVE_OPENAT |
| 12922 | "HAVE_OPENAT", |
| 12923 | #endif |
| 12924 | |
| 12925 | #ifdef HAVE_READLINKAT |
| 12926 | "HAVE_READLINKAT", |
| 12927 | #endif |
| 12928 | |
| 12929 | #ifdef HAVE_RENAMEAT |
| 12930 | "HAVE_RENAMEAT", |
| 12931 | #endif |
| 12932 | |
| 12933 | #ifdef HAVE_SYMLINKAT |
| 12934 | "HAVE_SYMLINKAT", |
| 12935 | #endif |
| 12936 | |
| 12937 | #ifdef HAVE_UNLINKAT |
| 12938 | "HAVE_UNLINKAT", |
| 12939 | #endif |
| 12940 | |
| 12941 | #ifdef HAVE_UTIMENSAT |
| 12942 | "HAVE_UTIMENSAT", |
| 12943 | #endif |
| 12944 | |
| 12945 | #ifdef MS_WINDOWS |
| 12946 | "MS_WINDOWS", |
| 12947 | #endif |
| 12948 | |
| 12949 | NULL |
| 12950 | }; |
| 12951 | |
| 12952 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 12953 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 12954 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12955 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12956 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12957 | PyObject *list; |
| 12958 | char **trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12959 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12960 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12961 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12962 | #endif |
| 12963 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12964 | m = PyModule_Create(&posixmodule); |
| 12965 | if (m == NULL) |
| 12966 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12967 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12968 | /* Initialize environ dictionary */ |
| 12969 | v = convertenviron(); |
| 12970 | Py_XINCREF(v); |
| 12971 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 12972 | return NULL; |
| 12973 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12974 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12975 | if (all_ins(m)) |
| 12976 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12977 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12978 | if (setup_confname_tables(m)) |
| 12979 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12980 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12981 | Py_INCREF(PyExc_OSError); |
| 12982 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 12983 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12984 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12985 | if (posix_putenv_garbage == NULL) |
| 12986 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12987 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 12988 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12989 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12990 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 12991 | waitid_result_desc.name = MODNAME ".waitid_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12992 | if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0) |
| 12993 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12994 | #endif |
| 12995 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 12996 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12997 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 12998 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 12999 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13000 | if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0) |
| 13001 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13002 | structseq_new = StatResultType.tp_new; |
| 13003 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13004 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 13005 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13006 | if (PyStructSequence_InitType2(&StatVFSResultType, |
| 13007 | &statvfs_result_desc) < 0) |
| 13008 | return NULL; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 13009 | #ifdef NEED_TICKS_PER_SECOND |
| 13010 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13011 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 13012 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13013 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 13014 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13015 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 13016 | # endif |
| 13017 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13018 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 13019 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13020 | sched_param_desc.name = MODNAME ".sched_param"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13021 | if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0) |
| 13022 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13023 | SchedParamType.tp_new = os_sched_param; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13024 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13025 | |
| 13026 | /* initialize TerminalSize_info */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13027 | if (PyStructSequence_InitType2(&TerminalSizeType, |
| 13028 | &TerminalSize_desc) < 0) |
| 13029 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13030 | |
| 13031 | /* initialize scandir types */ |
| 13032 | if (PyType_Ready(&ScandirIteratorType) < 0) |
| 13033 | return NULL; |
| 13034 | if (PyType_Ready(&DirEntryType) < 0) |
| 13035 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13036 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 13037 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 13038 | Py_INCREF((PyObject*) &WaitidResultType); |
| 13039 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 13040 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13041 | Py_INCREF((PyObject*) &StatResultType); |
| 13042 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 13043 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 13044 | PyModule_AddObject(m, "statvfs_result", |
| 13045 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 13046 | |
| 13047 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 13048 | Py_INCREF(&SchedParamType); |
| 13049 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 13050 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13051 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 13052 | times_result_desc.name = MODNAME ".times_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13053 | if (PyStructSequence_InitType2(&TimesResultType, ×_result_desc) < 0) |
| 13054 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 13055 | PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType); |
| 13056 | |
| 13057 | uname_result_desc.name = MODNAME ".uname_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 13058 | if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0) |
| 13059 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 13060 | PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType); |
| 13061 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13062 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13063 | /* |
| 13064 | * Step 2 of weak-linking support on Mac OS X. |
| 13065 | * |
| 13066 | * The code below removes functions that are not available on the |
| 13067 | * currently active platform. |
| 13068 | * |
| 13069 | * 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] | 13070 | * 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] | 13071 | * OSX 10.4. |
| 13072 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13073 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13074 | if (fstatvfs == NULL) { |
| 13075 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 13076 | return NULL; |
| 13077 | } |
| 13078 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13079 | #endif /* HAVE_FSTATVFS */ |
| 13080 | |
| 13081 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13082 | if (statvfs == NULL) { |
| 13083 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 13084 | return NULL; |
| 13085 | } |
| 13086 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13087 | #endif /* HAVE_STATVFS */ |
| 13088 | |
| 13089 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13090 | if (lchown == NULL) { |
| 13091 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 13092 | return NULL; |
| 13093 | } |
| 13094 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13095 | #endif /* HAVE_LCHOWN */ |
| 13096 | |
| 13097 | |
| 13098 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13099 | |
Antoine Pitrou | 9d20e0e | 2012-09-12 18:01:36 +0200 | [diff] [blame] | 13100 | Py_INCREF(&TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13101 | PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); |
| 13102 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 13103 | billion = PyLong_FromLong(1000000000); |
| 13104 | if (!billion) |
| 13105 | return NULL; |
| 13106 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13107 | /* suppress "function not used" warnings */ |
| 13108 | { |
| 13109 | int ignored; |
| 13110 | fd_specified("", -1); |
| 13111 | follow_symlinks_specified("", 1); |
| 13112 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 13113 | dir_fd_converter(Py_None, &ignored); |
| 13114 | dir_fd_unavailable(Py_None, &ignored); |
| 13115 | } |
| 13116 | |
| 13117 | /* |
| 13118 | * provide list of locally available functions |
| 13119 | * so os.py can populate support_* lists |
| 13120 | */ |
| 13121 | list = PyList_New(0); |
| 13122 | if (!list) |
| 13123 | return NULL; |
| 13124 | for (trace = have_functions; *trace; trace++) { |
| 13125 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 13126 | if (!unicode) |
| 13127 | return NULL; |
| 13128 | if (PyList_Append(list, unicode)) |
| 13129 | return NULL; |
| 13130 | Py_DECREF(unicode); |
| 13131 | } |
| 13132 | PyModule_AddObject(m, "_have_functions", list); |
| 13133 | |
| 13134 | initialized = 1; |
| 13135 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13136 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 13137 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13138 | |
| 13139 | #ifdef __cplusplus |
| 13140 | } |
| 13141 | #endif |