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 |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_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" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 28 | #ifndef MS_WINDOWS |
| 29 | #include "posixmodule.h" |
| 30 | #endif |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 31 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 36 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 37 | "This module provides access to operating system functionality that is\n\ |
| 38 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 39 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 40 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 41 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 42 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 43 | #ifdef HAVE_SYS_UIO_H |
| 44 | #include <sys/uio.h> |
| 45 | #endif |
| 46 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 47 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 48 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 49 | #endif /* HAVE_SYS_TYPES_H */ |
| 50 | |
| 51 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 52 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 53 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 55 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 56 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 57 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 58 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 59 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 60 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 61 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 63 | #ifdef HAVE_FCNTL_H |
| 64 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 65 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 67 | #ifdef HAVE_GRP_H |
| 68 | #include <grp.h> |
| 69 | #endif |
| 70 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 71 | #ifdef HAVE_SYSEXITS_H |
| 72 | #include <sysexits.h> |
| 73 | #endif /* HAVE_SYSEXITS_H */ |
| 74 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 75 | #ifdef HAVE_SYS_LOADAVG_H |
| 76 | #include <sys/loadavg.h> |
| 77 | #endif |
| 78 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 79 | #ifdef HAVE_LANGINFO_H |
| 80 | #include <langinfo.h> |
| 81 | #endif |
| 82 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 83 | #ifdef HAVE_SYS_SENDFILE_H |
| 84 | #include <sys/sendfile.h> |
| 85 | #endif |
| 86 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 87 | #ifdef HAVE_SCHED_H |
| 88 | #include <sched.h> |
| 89 | #endif |
| 90 | |
Benjamin Peterson | 2dbda07 | 2012-03-16 10:12:55 -0500 | [diff] [blame] | 91 | #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) |
Benjamin Peterson | 7b51b8d | 2012-03-14 22:28:25 -0500 | [diff] [blame] | 92 | #undef HAVE_SCHED_SETAFFINITY |
| 93 | #endif |
| 94 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 95 | #if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) |
| 96 | #define USE_XATTRS |
| 97 | #endif |
| 98 | |
| 99 | #ifdef USE_XATTRS |
Benjamin Peterson | b77fe17 | 2011-09-13 17:20:47 -0400 | [diff] [blame] | 100 | #include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 101 | #endif |
| 102 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 103 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 104 | #ifdef HAVE_SYS_SOCKET_H |
| 105 | #include <sys/socket.h> |
| 106 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 107 | #endif |
| 108 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 109 | #ifdef HAVE_DLFCN_H |
| 110 | #include <dlfcn.h> |
| 111 | #endif |
| 112 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 113 | #ifdef __hpux |
| 114 | #include <sys/mpctl.h> |
| 115 | #endif |
| 116 | |
| 117 | #if defined(__DragonFly__) || \ |
| 118 | defined(__OpenBSD__) || \ |
| 119 | defined(__FreeBSD__) || \ |
| 120 | defined(__NetBSD__) || \ |
| 121 | defined(__APPLE__) |
| 122 | #include <sys/sysctl.h> |
| 123 | #endif |
| 124 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 125 | #if defined(MS_WINDOWS) |
| 126 | # define TERMSIZE_USE_CONIO |
| 127 | #elif defined(HAVE_SYS_IOCTL_H) |
| 128 | # include <sys/ioctl.h> |
| 129 | # if defined(HAVE_TERMIOS_H) |
| 130 | # include <termios.h> |
| 131 | # endif |
| 132 | # if defined(TIOCGWINSZ) |
| 133 | # define TERMSIZE_USE_IOCTL |
| 134 | # endif |
| 135 | #endif /* MS_WINDOWS */ |
| 136 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 137 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 138 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 139 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 140 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 141 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 142 | #include <process.h> |
| 143 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 144 | #ifdef __BORLANDC__ /* Borland compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 145 | #define HAVE_EXECV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 146 | #define HAVE_OPENDIR 1 |
| 147 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 148 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 149 | #define HAVE_WAIT 1 |
| 150 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 151 | #ifdef _MSC_VER /* Microsoft compiler */ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 152 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 153 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 154 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 155 | #define HAVE_EXECV 1 |
| 156 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 157 | #define HAVE_SYSTEM 1 |
| 158 | #define HAVE_CWAIT 1 |
| 159 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 160 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 161 | #else |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 162 | /* Unix functions that the configure script doesn't check for */ |
| 163 | #define HAVE_EXECV 1 |
| 164 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 165 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 166 | #define HAVE_FORK1 1 |
| 167 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 168 | #define HAVE_GETEGID 1 |
| 169 | #define HAVE_GETEUID 1 |
| 170 | #define HAVE_GETGID 1 |
| 171 | #define HAVE_GETPPID 1 |
| 172 | #define HAVE_GETUID 1 |
| 173 | #define HAVE_KILL 1 |
| 174 | #define HAVE_OPENDIR 1 |
| 175 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 176 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 177 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 178 | #define HAVE_TTYNAME 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 179 | #endif /* _MSC_VER */ |
| 180 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 181 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 182 | |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 183 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 184 | /*[clinic input] |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 185 | module os |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 186 | [clinic start generated code]*/ |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 187 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=8cff096d1133288f]*/ |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 188 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 189 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 190 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 191 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 192 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 193 | (default) */ |
| 194 | extern char *ctermid_r(char *); |
| 195 | #endif |
| 196 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 197 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 198 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 199 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 200 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 201 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 202 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 203 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 204 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 205 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 206 | #endif |
| 207 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 208 | extern int chdir(char *); |
| 209 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 210 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 211 | extern int chdir(const char *); |
| 212 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 213 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 214 | #ifdef __BORLANDC__ |
| 215 | extern int chmod(const char *, int); |
| 216 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 217 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 218 | #endif |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 219 | /*#ifdef HAVE_FCHMOD |
| 220 | extern int fchmod(int, mode_t); |
| 221 | #endif*/ |
| 222 | /*#ifdef HAVE_LCHMOD |
| 223 | extern int lchmod(const char *, mode_t); |
| 224 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 225 | extern int chown(const char *, uid_t, gid_t); |
| 226 | extern char *getcwd(char *, int); |
| 227 | extern char *strerror(int); |
| 228 | extern int link(const char *, const char *); |
| 229 | extern int rename(const char *, const char *); |
| 230 | extern int stat(const char *, struct stat *); |
| 231 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 232 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 233 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 234 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 235 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 236 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 237 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 238 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 239 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 240 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 241 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 242 | #ifdef HAVE_UTIME_H |
| 243 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 244 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 245 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 246 | #ifdef HAVE_SYS_UTIME_H |
| 247 | #include <sys/utime.h> |
| 248 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 249 | #endif /* HAVE_SYS_UTIME_H */ |
| 250 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | #ifdef HAVE_SYS_TIMES_H |
| 252 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 253 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 254 | |
| 255 | #ifdef HAVE_SYS_PARAM_H |
| 256 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 257 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 258 | |
| 259 | #ifdef HAVE_SYS_UTSNAME_H |
| 260 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 261 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 262 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 263 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 264 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 265 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 266 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 267 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 268 | #include <direct.h> |
| 269 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 270 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 271 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 272 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 273 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 274 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 275 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 276 | #endif |
| 277 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 278 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 279 | #endif |
| 280 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 281 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 282 | #endif |
| 283 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 284 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 285 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 286 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 287 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 288 | #endif |
| 289 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 290 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 291 | #endif |
| 292 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 293 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 294 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 295 | #ifndef VOLUME_NAME_DOS |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 296 | #define VOLUME_NAME_DOS 0x0 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 297 | #endif |
| 298 | #ifndef VOLUME_NAME_NT |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 299 | #define VOLUME_NAME_NT 0x2 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 300 | #endif |
| 301 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 302 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 303 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 304 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 305 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 306 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 307 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 308 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 309 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 310 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 311 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 312 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 313 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 314 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 315 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 316 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 317 | #define MAXPATHLEN PATH_MAX |
| 318 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 319 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 320 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 321 | #endif /* MAXPATHLEN */ |
| 322 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 323 | #ifdef UNION_WAIT |
| 324 | /* Emulate some macros on systems that have a union instead of macros */ |
| 325 | |
| 326 | #ifndef WIFEXITED |
| 327 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 328 | #endif |
| 329 | |
| 330 | #ifndef WEXITSTATUS |
| 331 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 332 | #endif |
| 333 | |
| 334 | #ifndef WTERMSIG |
| 335 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 336 | #endif |
| 337 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 338 | #define WAIT_TYPE union wait |
| 339 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 340 | |
| 341 | #else /* !UNION_WAIT */ |
| 342 | #define WAIT_TYPE int |
| 343 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 344 | #endif /* UNION_WAIT */ |
| 345 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 346 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 347 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 348 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 349 | #define USE_CTERMID_R |
| 350 | #endif |
| 351 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 352 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 353 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 354 | #undef FSTAT |
| 355 | #undef STRUCT_STAT |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 356 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 357 | # define STAT win32_stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 358 | # define LSTAT win32_lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 359 | # define FSTAT win32_fstat |
| 360 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 361 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 362 | # define STAT stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 363 | # define LSTAT lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 364 | # define FSTAT fstat |
| 365 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 366 | #endif |
| 367 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 368 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 369 | #include <sys/mkdev.h> |
| 370 | #else |
| 371 | #if defined(MAJOR_IN_SYSMACROS) |
| 372 | #include <sys/sysmacros.h> |
| 373 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 374 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 375 | #include <sys/mkdev.h> |
| 376 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 377 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 378 | |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 379 | #define DWORD_MAX 4294967295U |
| 380 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 381 | |
| 382 | #ifdef MS_WINDOWS |
| 383 | static int |
| 384 | win32_warn_bytes_api() |
| 385 | { |
| 386 | return PyErr_WarnEx(PyExc_DeprecationWarning, |
| 387 | "The Windows bytes API has been deprecated, " |
| 388 | "use Unicode filenames instead", |
| 389 | 1); |
| 390 | } |
| 391 | #endif |
| 392 | |
| 393 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 394 | #ifndef MS_WINDOWS |
| 395 | PyObject * |
| 396 | _PyLong_FromUid(uid_t uid) |
| 397 | { |
| 398 | if (uid == (uid_t)-1) |
| 399 | return PyLong_FromLong(-1); |
| 400 | return PyLong_FromUnsignedLong(uid); |
| 401 | } |
| 402 | |
| 403 | PyObject * |
| 404 | _PyLong_FromGid(gid_t gid) |
| 405 | { |
| 406 | if (gid == (gid_t)-1) |
| 407 | return PyLong_FromLong(-1); |
| 408 | return PyLong_FromUnsignedLong(gid); |
| 409 | } |
| 410 | |
| 411 | int |
| 412 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 413 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 414 | uid_t uid; |
| 415 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 416 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 417 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 418 | unsigned long uresult; |
| 419 | |
| 420 | index = PyNumber_Index(obj); |
| 421 | if (index == NULL) { |
| 422 | PyErr_Format(PyExc_TypeError, |
| 423 | "uid should be integer, not %.200s", |
| 424 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 425 | return 0; |
| 426 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 427 | |
| 428 | /* |
| 429 | * Handling uid_t is complicated for two reasons: |
| 430 | * * Although uid_t is (always?) unsigned, it still |
| 431 | * accepts -1. |
| 432 | * * We don't know its size in advance--it may be |
| 433 | * bigger than an int, or it may be smaller than |
| 434 | * a long. |
| 435 | * |
| 436 | * So a bit of defensive programming is in order. |
| 437 | * Start with interpreting the value passed |
| 438 | * in as a signed long and see if it works. |
| 439 | */ |
| 440 | |
| 441 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 442 | |
| 443 | if (!overflow) { |
| 444 | uid = (uid_t)result; |
| 445 | |
| 446 | if (result == -1) { |
| 447 | if (PyErr_Occurred()) |
| 448 | goto fail; |
| 449 | /* It's a legitimate -1, we're done. */ |
| 450 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 451 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 452 | |
| 453 | /* Any other negative number is disallowed. */ |
| 454 | if (result < 0) |
| 455 | goto underflow; |
| 456 | |
| 457 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 458 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 459 | (long)uid != result) |
| 460 | goto underflow; |
| 461 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 462 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 463 | |
| 464 | if (overflow < 0) |
| 465 | goto underflow; |
| 466 | |
| 467 | /* |
| 468 | * Okay, the value overflowed a signed long. If it |
| 469 | * fits in an *unsigned* long, it may still be okay, |
| 470 | * as uid_t may be unsigned long on this platform. |
| 471 | */ |
| 472 | uresult = PyLong_AsUnsignedLong(index); |
| 473 | if (PyErr_Occurred()) { |
| 474 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 475 | goto overflow; |
| 476 | goto fail; |
| 477 | } |
| 478 | |
| 479 | uid = (uid_t)uresult; |
| 480 | |
| 481 | /* |
| 482 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, |
| 483 | * but this value would get interpreted as (uid_t)-1 by chown |
| 484 | * and its siblings. That's not what the user meant! So we |
| 485 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 486 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 487 | */ |
| 488 | if (uid == (uid_t)-1) |
| 489 | goto overflow; |
| 490 | |
| 491 | /* Ensure the value wasn't truncated. */ |
| 492 | if (sizeof(uid_t) < sizeof(long) && |
| 493 | (unsigned long)uid != uresult) |
| 494 | goto overflow; |
| 495 | /* fallthrough */ |
| 496 | |
| 497 | success: |
| 498 | Py_DECREF(index); |
| 499 | *(uid_t *)p = uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 500 | return 1; |
| 501 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 502 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 503 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 504 | "uid is less than minimum"); |
| 505 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 506 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 507 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 508 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 509 | "uid is greater than maximum"); |
| 510 | /* fallthrough */ |
| 511 | |
| 512 | fail: |
| 513 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | int |
| 518 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 519 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 520 | gid_t gid; |
| 521 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 522 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 523 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 524 | unsigned long uresult; |
| 525 | |
| 526 | index = PyNumber_Index(obj); |
| 527 | if (index == NULL) { |
| 528 | PyErr_Format(PyExc_TypeError, |
| 529 | "gid should be integer, not %.200s", |
| 530 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 531 | return 0; |
| 532 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 533 | |
| 534 | /* |
| 535 | * Handling gid_t is complicated for two reasons: |
| 536 | * * Although gid_t is (always?) unsigned, it still |
| 537 | * accepts -1. |
| 538 | * * We don't know its size in advance--it may be |
| 539 | * bigger than an int, or it may be smaller than |
| 540 | * a long. |
| 541 | * |
| 542 | * So a bit of defensive programming is in order. |
| 543 | * Start with interpreting the value passed |
| 544 | * in as a signed long and see if it works. |
| 545 | */ |
| 546 | |
| 547 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 548 | |
| 549 | if (!overflow) { |
| 550 | gid = (gid_t)result; |
| 551 | |
| 552 | if (result == -1) { |
| 553 | if (PyErr_Occurred()) |
| 554 | goto fail; |
| 555 | /* It's a legitimate -1, we're done. */ |
| 556 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 557 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 558 | |
| 559 | /* Any other negative number is disallowed. */ |
| 560 | if (result < 0) { |
| 561 | goto underflow; |
| 562 | } |
| 563 | |
| 564 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 565 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 566 | (long)gid != result) |
| 567 | goto underflow; |
| 568 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 569 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 570 | |
| 571 | if (overflow < 0) |
| 572 | goto underflow; |
| 573 | |
| 574 | /* |
| 575 | * Okay, the value overflowed a signed long. If it |
| 576 | * fits in an *unsigned* long, it may still be okay, |
| 577 | * as gid_t may be unsigned long on this platform. |
| 578 | */ |
| 579 | uresult = PyLong_AsUnsignedLong(index); |
| 580 | if (PyErr_Occurred()) { |
| 581 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 582 | goto overflow; |
| 583 | goto fail; |
| 584 | } |
| 585 | |
| 586 | gid = (gid_t)uresult; |
| 587 | |
| 588 | /* |
| 589 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, |
| 590 | * but this value would get interpreted as (gid_t)-1 by chown |
| 591 | * and its siblings. That's not what the user meant! So we |
| 592 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 593 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 594 | */ |
| 595 | if (gid == (gid_t)-1) |
| 596 | goto overflow; |
| 597 | |
| 598 | /* Ensure the value wasn't truncated. */ |
| 599 | if (sizeof(gid_t) < sizeof(long) && |
| 600 | (unsigned long)gid != uresult) |
| 601 | goto overflow; |
| 602 | /* fallthrough */ |
| 603 | |
| 604 | success: |
| 605 | Py_DECREF(index); |
| 606 | *(gid_t *)p = gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 607 | return 1; |
| 608 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 609 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 610 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 611 | "gid is less than minimum"); |
| 612 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 613 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 614 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 615 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 616 | "gid is greater than maximum"); |
| 617 | /* fallthrough */ |
| 618 | |
| 619 | fail: |
| 620 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 621 | return 0; |
| 622 | } |
| 623 | #endif /* MS_WINDOWS */ |
| 624 | |
| 625 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 626 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 627 | /* |
| 628 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 629 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 630 | * which doesn't play nicely with all the initializer lines in this file that |
| 631 | * look like this: |
| 632 | * int dir_fd = DEFAULT_DIR_FD; |
| 633 | */ |
| 634 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 635 | #else |
| 636 | #define DEFAULT_DIR_FD (-100) |
| 637 | #endif |
| 638 | |
| 639 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 640 | _fd_converter(PyObject *o, int *p, const char *allowed) |
| 641 | { |
| 642 | int overflow; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 643 | long long_value; |
| 644 | |
| 645 | PyObject *index = PyNumber_Index(o); |
| 646 | if (index == NULL) { |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 647 | PyErr_Format(PyExc_TypeError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 648 | "argument should be %s, not %.200s", |
| 649 | allowed, Py_TYPE(o)->tp_name); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 650 | return 0; |
| 651 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 652 | |
| 653 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
| 654 | Py_DECREF(index); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 655 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 656 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 657 | "fd is greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 658 | return 0; |
| 659 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 660 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 661 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 662 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 663 | return 0; |
| 664 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 665 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 666 | *p = (int)long_value; |
| 667 | return 1; |
| 668 | } |
| 669 | |
| 670 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 671 | dir_fd_converter(PyObject *o, void *p) |
| 672 | { |
| 673 | if (o == Py_None) { |
| 674 | *(int *)p = DEFAULT_DIR_FD; |
| 675 | return 1; |
| 676 | } |
| 677 | return _fd_converter(o, (int *)p, "integer"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | |
| 681 | |
| 682 | /* |
| 683 | * A PyArg_ParseTuple "converter" function |
| 684 | * that handles filesystem paths in the manner |
| 685 | * preferred by the os module. |
| 686 | * |
| 687 | * path_converter accepts (Unicode) strings and their |
| 688 | * subclasses, and bytes and their subclasses. What |
| 689 | * it does with the argument depends on the platform: |
| 690 | * |
| 691 | * * On Windows, if we get a (Unicode) string we |
| 692 | * extract the wchar_t * and return it; if we get |
| 693 | * bytes we extract the char * and return that. |
| 694 | * |
| 695 | * * On all other platforms, strings are encoded |
| 696 | * to bytes using PyUnicode_FSConverter, then we |
| 697 | * extract the char * from the bytes object and |
| 698 | * return that. |
| 699 | * |
| 700 | * path_converter also optionally accepts signed |
| 701 | * integers (representing open file descriptors) instead |
| 702 | * of path strings. |
| 703 | * |
| 704 | * Input fields: |
| 705 | * path.nullable |
| 706 | * If nonzero, the path is permitted to be None. |
| 707 | * path.allow_fd |
| 708 | * If nonzero, the path is permitted to be a file handle |
| 709 | * (a signed int) instead of a string. |
| 710 | * path.function_name |
| 711 | * If non-NULL, path_converter will use that as the name |
| 712 | * of the function in error messages. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 713 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 714 | * path.argument_name |
| 715 | * If non-NULL, path_converter will use that as the name |
| 716 | * of the parameter in error messages. |
| 717 | * (If path.argument_name is NULL it uses "path".) |
| 718 | * |
| 719 | * Output fields: |
| 720 | * path.wide |
| 721 | * Points to the path if it was expressed as Unicode |
| 722 | * and was not encoded. (Only used on Windows.) |
| 723 | * path.narrow |
| 724 | * Points to the path if it was expressed as bytes, |
| 725 | * or it was Unicode and was encoded to bytes. |
| 726 | * path.fd |
| 727 | * Contains a file descriptor if path.accept_fd was true |
| 728 | * and the caller provided a signed integer instead of any |
| 729 | * sort of string. |
| 730 | * |
| 731 | * WARNING: if your "path" parameter is optional, and is |
| 732 | * unspecified, path_converter will never get called. |
| 733 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 734 | * yourself! |
| 735 | * path.length |
| 736 | * The length of the path in characters, if specified as |
| 737 | * a string. |
| 738 | * path.object |
| 739 | * The original object passed in. |
| 740 | * path.cleanup |
| 741 | * For internal use only. May point to a temporary object. |
| 742 | * (Pay no attention to the man behind the curtain.) |
| 743 | * |
| 744 | * At most one of path.wide or path.narrow will be non-NULL. |
| 745 | * If path was None and path.nullable was set, |
| 746 | * or if path was an integer and path.allow_fd was set, |
| 747 | * both path.wide and path.narrow will be NULL |
| 748 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 749 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 750 | * path_converter takes care to not write to the path_t |
| 751 | * unless it's successful. However it must reset the |
| 752 | * "cleanup" field each time it's called. |
| 753 | * |
| 754 | * Use as follows: |
| 755 | * path_t path; |
| 756 | * memset(&path, 0, sizeof(path)); |
| 757 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 758 | * // ... use values from path ... |
| 759 | * path_cleanup(&path); |
| 760 | * |
| 761 | * (Note that if PyArg_Parse fails you don't need to call |
| 762 | * path_cleanup(). However it is safe to do so.) |
| 763 | */ |
| 764 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 765 | const char *function_name; |
| 766 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 767 | int nullable; |
| 768 | int allow_fd; |
| 769 | wchar_t *wide; |
| 770 | char *narrow; |
| 771 | int fd; |
| 772 | Py_ssize_t length; |
| 773 | PyObject *object; |
| 774 | PyObject *cleanup; |
| 775 | } path_t; |
| 776 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 777 | #define PATH_T_INITIALIZE(function_name, nullable, allow_fd) \ |
| 778 | {function_name, NULL, nullable, allow_fd, NULL, NULL, 0, 0, NULL, NULL} |
| 779 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 780 | static void |
| 781 | path_cleanup(path_t *path) { |
| 782 | if (path->cleanup) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 783 | Py_CLEAR(path->cleanup); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | |
| 787 | static int |
| 788 | path_converter(PyObject *o, void *p) { |
| 789 | path_t *path = (path_t *)p; |
| 790 | PyObject *unicode, *bytes; |
| 791 | Py_ssize_t length; |
| 792 | char *narrow; |
| 793 | |
| 794 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 795 | PyErr_Format(exc, "%s%s" fmt, \ |
| 796 | path->function_name ? path->function_name : "", \ |
| 797 | path->function_name ? ": " : "", \ |
| 798 | path->argument_name ? path->argument_name : "path") |
| 799 | |
| 800 | /* Py_CLEANUP_SUPPORTED support */ |
| 801 | if (o == NULL) { |
| 802 | path_cleanup(path); |
| 803 | return 1; |
| 804 | } |
| 805 | |
| 806 | /* ensure it's always safe to call path_cleanup() */ |
| 807 | path->cleanup = NULL; |
| 808 | |
| 809 | if (o == Py_None) { |
| 810 | if (!path->nullable) { |
| 811 | FORMAT_EXCEPTION(PyExc_TypeError, |
| 812 | "can't specify None for %s argument"); |
| 813 | return 0; |
| 814 | } |
| 815 | path->wide = NULL; |
| 816 | path->narrow = NULL; |
| 817 | path->length = 0; |
| 818 | path->object = o; |
| 819 | path->fd = -1; |
| 820 | return 1; |
| 821 | } |
| 822 | |
| 823 | unicode = PyUnicode_FromObject(o); |
| 824 | if (unicode) { |
| 825 | #ifdef MS_WINDOWS |
| 826 | wchar_t *wide; |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 827 | |
| 828 | wide = PyUnicode_AsUnicodeAndSize(unicode, &length); |
| 829 | if (!wide) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 830 | Py_DECREF(unicode); |
| 831 | return 0; |
| 832 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 833 | if (length > 32767) { |
| 834 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 835 | Py_DECREF(unicode); |
| 836 | return 0; |
| 837 | } |
| 838 | |
| 839 | path->wide = wide; |
| 840 | path->narrow = NULL; |
| 841 | path->length = length; |
| 842 | path->object = o; |
| 843 | path->fd = -1; |
| 844 | path->cleanup = unicode; |
| 845 | return Py_CLEANUP_SUPPORTED; |
| 846 | #else |
| 847 | int converted = PyUnicode_FSConverter(unicode, &bytes); |
| 848 | Py_DECREF(unicode); |
| 849 | if (!converted) |
| 850 | bytes = NULL; |
| 851 | #endif |
| 852 | } |
| 853 | else { |
| 854 | PyErr_Clear(); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 855 | if (PyObject_CheckBuffer(o)) |
| 856 | bytes = PyBytes_FromObject(o); |
| 857 | else |
| 858 | bytes = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 859 | if (!bytes) { |
| 860 | PyErr_Clear(); |
| 861 | if (path->allow_fd) { |
| 862 | int fd; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 863 | int result = _fd_converter(o, &fd, |
| 864 | "string, bytes or integer"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 865 | if (result) { |
| 866 | path->wide = NULL; |
| 867 | path->narrow = NULL; |
| 868 | path->length = 0; |
| 869 | path->object = o; |
| 870 | path->fd = fd; |
| 871 | return result; |
| 872 | } |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | if (!bytes) { |
| 878 | if (!PyErr_Occurred()) |
| 879 | FORMAT_EXCEPTION(PyExc_TypeError, "illegal type for %s parameter"); |
| 880 | return 0; |
| 881 | } |
| 882 | |
| 883 | #ifdef MS_WINDOWS |
| 884 | if (win32_warn_bytes_api()) { |
| 885 | Py_DECREF(bytes); |
| 886 | return 0; |
| 887 | } |
| 888 | #endif |
| 889 | |
| 890 | length = PyBytes_GET_SIZE(bytes); |
| 891 | #ifdef MS_WINDOWS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 892 | if (length > MAX_PATH-1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 893 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
| 894 | Py_DECREF(bytes); |
| 895 | return 0; |
| 896 | } |
| 897 | #endif |
| 898 | |
| 899 | narrow = PyBytes_AS_STRING(bytes); |
| 900 | if (length != strlen(narrow)) { |
| 901 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded NUL character in %s"); |
| 902 | Py_DECREF(bytes); |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | path->wide = NULL; |
| 907 | path->narrow = narrow; |
| 908 | path->length = length; |
| 909 | path->object = o; |
| 910 | path->fd = -1; |
| 911 | path->cleanup = bytes; |
| 912 | return Py_CLEANUP_SUPPORTED; |
| 913 | } |
| 914 | |
| 915 | static void |
| 916 | argument_unavailable_error(char *function_name, char *argument_name) { |
| 917 | PyErr_Format(PyExc_NotImplementedError, |
| 918 | "%s%s%s unavailable on this platform", |
| 919 | (function_name != NULL) ? function_name : "", |
| 920 | (function_name != NULL) ? ": ": "", |
| 921 | argument_name); |
| 922 | } |
| 923 | |
| 924 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 925 | dir_fd_unavailable(PyObject *o, void *p) |
| 926 | { |
| 927 | int dir_fd; |
| 928 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 929 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 930 | if (dir_fd != DEFAULT_DIR_FD) { |
| 931 | argument_unavailable_error(NULL, "dir_fd"); |
| 932 | return 0; |
| 933 | } |
| 934 | *(int *)p = dir_fd; |
| 935 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | static int |
| 939 | fd_specified(char *function_name, int fd) { |
| 940 | if (fd == -1) |
| 941 | return 0; |
| 942 | |
| 943 | argument_unavailable_error(function_name, "fd"); |
| 944 | return 1; |
| 945 | } |
| 946 | |
| 947 | static int |
| 948 | follow_symlinks_specified(char *function_name, int follow_symlinks) { |
| 949 | if (follow_symlinks) |
| 950 | return 0; |
| 951 | |
| 952 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 953 | return 1; |
| 954 | } |
| 955 | |
| 956 | static int |
| 957 | path_and_dir_fd_invalid(char *function_name, path_t *path, int dir_fd) { |
| 958 | if (!path->narrow && !path->wide && (dir_fd != DEFAULT_DIR_FD)) { |
| 959 | PyErr_Format(PyExc_ValueError, |
| 960 | "%s: can't specify dir_fd without matching path", |
| 961 | function_name); |
| 962 | return 1; |
| 963 | } |
| 964 | return 0; |
| 965 | } |
| 966 | |
| 967 | static int |
| 968 | dir_fd_and_fd_invalid(char *function_name, int dir_fd, int fd) { |
| 969 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 970 | PyErr_Format(PyExc_ValueError, |
| 971 | "%s: can't specify both dir_fd and fd", |
| 972 | function_name); |
| 973 | return 1; |
| 974 | } |
| 975 | return 0; |
| 976 | } |
| 977 | |
| 978 | static int |
| 979 | fd_and_follow_symlinks_invalid(char *function_name, int fd, |
| 980 | int follow_symlinks) { |
| 981 | if ((fd > 0) && (!follow_symlinks)) { |
| 982 | PyErr_Format(PyExc_ValueError, |
| 983 | "%s: cannot use fd and follow_symlinks together", |
| 984 | function_name); |
| 985 | return 1; |
| 986 | } |
| 987 | return 0; |
| 988 | } |
| 989 | |
| 990 | static int |
| 991 | dir_fd_and_follow_symlinks_invalid(char *function_name, int dir_fd, |
| 992 | int follow_symlinks) { |
| 993 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 994 | PyErr_Format(PyExc_ValueError, |
| 995 | "%s: cannot use dir_fd and follow_symlinks together", |
| 996 | function_name); |
| 997 | return 1; |
| 998 | } |
| 999 | return 0; |
| 1000 | } |
| 1001 | |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1002 | /* A helper used by a number of POSIX-only functions */ |
| 1003 | #ifndef MS_WINDOWS |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 1004 | static int |
| 1005 | _parse_off_t(PyObject* arg, void* addr) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1006 | { |
| 1007 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 1008 | *((off_t*)addr) = PyLong_AsLong(arg); |
| 1009 | #else |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 1010 | *((off_t*)addr) = PyLong_AsLongLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1011 | #endif |
| 1012 | if (PyErr_Occurred()) |
| 1013 | return 0; |
| 1014 | return 1; |
| 1015 | } |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1016 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1017 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1018 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 1019 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 1020 | * valid and raise an assertion if it isn't. |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1021 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 1022 | * an assertion can be useful, but it does contradict the POSIX standard |
| 1023 | * which for write(2) states: |
| 1024 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 1025 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 1026 | * writing." |
| 1027 | * Furthermore, python allows the user to enter any old integer |
| 1028 | * as a fd and should merely raise a python exception on error. |
| 1029 | * The Microsoft CRT doesn't provide an official way to check for the |
| 1030 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1031 | * by using the exported __pinfo data member and knowledge of the |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1032 | * internal structures involved. |
| 1033 | * The structures below must be updated for each version of visual studio |
| 1034 | * according to the file internal.h in the CRT source, until MS comes |
| 1035 | * up with a less hacky way to do this. |
| 1036 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 1037 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 1038 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 1039 | /* The actual size of the structure is determined at runtime. |
| 1040 | * Only the first items must be present. |
| 1041 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1042 | typedef struct { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1043 | intptr_t osfhnd; |
| 1044 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 1045 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1046 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 1047 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1048 | #define IOINFO_L2E 5 |
| 1049 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 1050 | #define IOINFO_ARRAYS 64 |
| 1051 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 1052 | #define FOPEN 0x01 |
| 1053 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 1054 | |
| 1055 | /* This function emulates what the windows CRT does to validate file handles */ |
| 1056 | int |
| 1057 | _PyVerify_fd(int fd) |
| 1058 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1059 | const int i1 = fd >> IOINFO_L2E; |
| 1060 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 1061 | |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 1062 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1063 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1064 | /* Determine the actual size of the ioinfo structure, |
| 1065 | * as used by the CRT loaded in memory |
| 1066 | */ |
| 1067 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 1068 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 1069 | } |
| 1070 | if (sizeof_ioinfo == 0) { |
| 1071 | /* This should not happen... */ |
| 1072 | goto fail; |
| 1073 | } |
| 1074 | |
| 1075 | /* See that it isn't a special CLEAR fileno */ |
| 1076 | if (fd != _NO_CONSOLE_FILENO) { |
| 1077 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 1078 | * we check pointer validity and other info |
| 1079 | */ |
| 1080 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 1081 | /* finally, check that the file is open */ |
| 1082 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 1083 | if (info->osfile & FOPEN) { |
| 1084 | return 1; |
| 1085 | } |
| 1086 | } |
| 1087 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 1088 | fail: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1089 | errno = EBADF; |
| 1090 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 1094 | static int |
| 1095 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 1096 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1097 | if (!_PyVerify_fd(fd1)) |
| 1098 | return 0; |
| 1099 | if (fd2 == _NO_CONSOLE_FILENO) |
| 1100 | return 0; |
| 1101 | if ((unsigned)fd2 < _NHANDLE_) |
| 1102 | return 1; |
| 1103 | else |
| 1104 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1105 | } |
| 1106 | #else |
| 1107 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 1108 | #define _PyVerify_fd_dup2(A, B) (1) |
| 1109 | #endif |
| 1110 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1111 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1112 | /* The following structure was copied from |
| 1113 | http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required |
| 1114 | include doesn't seem to be present in the Windows SDK (at least as included |
| 1115 | with Visual Studio Express). */ |
| 1116 | typedef struct _REPARSE_DATA_BUFFER { |
| 1117 | ULONG ReparseTag; |
| 1118 | USHORT ReparseDataLength; |
| 1119 | USHORT Reserved; |
| 1120 | union { |
| 1121 | struct { |
| 1122 | USHORT SubstituteNameOffset; |
| 1123 | USHORT SubstituteNameLength; |
| 1124 | USHORT PrintNameOffset; |
| 1125 | USHORT PrintNameLength; |
| 1126 | ULONG Flags; |
| 1127 | WCHAR PathBuffer[1]; |
| 1128 | } SymbolicLinkReparseBuffer; |
| 1129 | |
| 1130 | struct { |
| 1131 | USHORT SubstituteNameOffset; |
| 1132 | USHORT SubstituteNameLength; |
| 1133 | USHORT PrintNameOffset; |
| 1134 | USHORT PrintNameLength; |
| 1135 | WCHAR PathBuffer[1]; |
| 1136 | } MountPointReparseBuffer; |
| 1137 | |
| 1138 | struct { |
| 1139 | UCHAR DataBuffer[1]; |
| 1140 | } GenericReparseBuffer; |
| 1141 | }; |
| 1142 | } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 1143 | |
| 1144 | #define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ |
| 1145 | GenericReparseBuffer) |
| 1146 | #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) |
| 1147 | |
| 1148 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1149 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1150 | { |
| 1151 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1152 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 1153 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1154 | |
| 1155 | if (0 == DeviceIoControl( |
| 1156 | reparse_point_handle, |
| 1157 | FSCTL_GET_REPARSE_POINT, |
| 1158 | NULL, 0, /* in buffer */ |
| 1159 | target_buffer, sizeof(target_buffer), |
| 1160 | &n_bytes_returned, |
| 1161 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1162 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1163 | |
| 1164 | if (reparse_tag) |
| 1165 | *reparse_tag = rdb->ReparseTag; |
| 1166 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1167 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1168 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1169 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1170 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1171 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1172 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1173 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1174 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1175 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1176 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1177 | */ |
| 1178 | #include <crt_externs.h> |
| 1179 | static char **environ; |
| 1180 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1181 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1182 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1183 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1184 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1185 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1186 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1187 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1188 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1189 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1190 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1191 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1192 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1193 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1194 | d = PyDict_New(); |
| 1195 | if (d == NULL) |
| 1196 | return NULL; |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1197 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1198 | if (environ == NULL) |
| 1199 | environ = *_NSGetEnviron(); |
| 1200 | #endif |
| 1201 | #ifdef MS_WINDOWS |
| 1202 | /* _wenviron must be initialized in this way if the program is started |
| 1203 | through main() instead of wmain(). */ |
| 1204 | _wgetenv(L""); |
| 1205 | if (_wenviron == NULL) |
| 1206 | return d; |
| 1207 | /* This part ignores errors */ |
| 1208 | for (e = _wenviron; *e != NULL; e++) { |
| 1209 | PyObject *k; |
| 1210 | PyObject *v; |
| 1211 | wchar_t *p = wcschr(*e, L'='); |
| 1212 | if (p == NULL) |
| 1213 | continue; |
| 1214 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1215 | if (k == NULL) { |
| 1216 | PyErr_Clear(); |
| 1217 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1218 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1219 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1220 | if (v == NULL) { |
| 1221 | PyErr_Clear(); |
| 1222 | Py_DECREF(k); |
| 1223 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1224 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1225 | if (PyDict_GetItem(d, k) == NULL) { |
| 1226 | if (PyDict_SetItem(d, k, v) != 0) |
| 1227 | PyErr_Clear(); |
| 1228 | } |
| 1229 | Py_DECREF(k); |
| 1230 | Py_DECREF(v); |
| 1231 | } |
| 1232 | #else |
| 1233 | if (environ == NULL) |
| 1234 | return d; |
| 1235 | /* This part ignores errors */ |
| 1236 | for (e = environ; *e != NULL; e++) { |
| 1237 | PyObject *k; |
| 1238 | PyObject *v; |
| 1239 | char *p = strchr(*e, '='); |
| 1240 | if (p == NULL) |
| 1241 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1242 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1243 | if (k == NULL) { |
| 1244 | PyErr_Clear(); |
| 1245 | continue; |
| 1246 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1247 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1248 | if (v == NULL) { |
| 1249 | PyErr_Clear(); |
| 1250 | Py_DECREF(k); |
| 1251 | continue; |
| 1252 | } |
| 1253 | if (PyDict_GetItem(d, k) == NULL) { |
| 1254 | if (PyDict_SetItem(d, k, v) != 0) |
| 1255 | PyErr_Clear(); |
| 1256 | } |
| 1257 | Py_DECREF(k); |
| 1258 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1259 | } |
| 1260 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1261 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1264 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1265 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1266 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1267 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1268 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1269 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1270 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1271 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1272 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1273 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1274 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1275 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1276 | /* XXX We should pass the function name along in the future. |
| 1277 | (winreg.c also wants to pass the function name.) |
| 1278 | This would however require an additional param to the |
| 1279 | Windows error object, which is non-trivial. |
| 1280 | */ |
| 1281 | errno = GetLastError(); |
| 1282 | if (filename) |
| 1283 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1284 | else |
| 1285 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1286 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1287 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1288 | static PyObject * |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1289 | win32_error_object(char* function, PyObject* filename) |
| 1290 | { |
| 1291 | /* XXX - see win32_error for comments on 'function' */ |
| 1292 | errno = GetLastError(); |
| 1293 | if (filename) |
| 1294 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1295 | PyExc_OSError, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1296 | errno, |
| 1297 | filename); |
| 1298 | else |
| 1299 | return PyErr_SetFromWindowsErr(errno); |
| 1300 | } |
| 1301 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1302 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1303 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1304 | static PyObject * |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1305 | path_error(path_t *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1306 | { |
| 1307 | #ifdef MS_WINDOWS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1308 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 1309 | 0, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1310 | #else |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1311 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1312 | #endif |
| 1313 | } |
| 1314 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1315 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1316 | static PyObject * |
| 1317 | path_error2(path_t *path, path_t *path2) |
| 1318 | { |
| 1319 | #ifdef MS_WINDOWS |
| 1320 | return PyErr_SetExcFromWindowsErrWithFilenameObjects(PyExc_OSError, |
| 1321 | 0, path->object, path2->object); |
| 1322 | #else |
| 1323 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, |
| 1324 | path->object, path2->object); |
| 1325 | #endif |
| 1326 | } |
| 1327 | |
| 1328 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1329 | /* POSIX generic methods */ |
| 1330 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1331 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1332 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 1333 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1334 | int fd; |
| 1335 | int res; |
| 1336 | fd = PyObject_AsFileDescriptor(fdobj); |
| 1337 | if (fd < 0) |
| 1338 | return NULL; |
| 1339 | if (!_PyVerify_fd(fd)) |
| 1340 | return posix_error(); |
| 1341 | Py_BEGIN_ALLOW_THREADS |
| 1342 | res = (*func)(fd); |
| 1343 | Py_END_ALLOW_THREADS |
| 1344 | if (res < 0) |
| 1345 | return posix_error(); |
| 1346 | Py_INCREF(Py_None); |
| 1347 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1348 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1349 | |
| 1350 | static PyObject * |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1351 | posix_1str(const char *func_name, PyObject *args, char *format, |
| 1352 | int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1353 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1354 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1355 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1356 | memset(&path, 0, sizeof(path)); |
| 1357 | path.function_name = func_name; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1358 | if (!PyArg_ParseTuple(args, format, |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1359 | path_converter, &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1360 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1361 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1362 | res = (*func)(path.narrow); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1363 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1364 | if (res < 0) { |
| 1365 | path_error(&path); |
| 1366 | path_cleanup(&path); |
| 1367 | return NULL; |
| 1368 | } |
| 1369 | path_cleanup(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1370 | Py_INCREF(Py_None); |
| 1371 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1374 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1375 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1376 | /* This is a reimplementation of the C library's chdir function, |
| 1377 | but one that produces Win32 errors instead of DOS error codes. |
| 1378 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1379 | it also needs to set "magic" environment variables indicating |
| 1380 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1381 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1382 | win32_chdir(LPCSTR path) |
| 1383 | { |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1384 | char new_path[MAX_PATH]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1385 | int result; |
| 1386 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1387 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1388 | if(!SetCurrentDirectoryA(path)) |
| 1389 | return FALSE; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1390 | result = GetCurrentDirectoryA(Py_ARRAY_LENGTH(new_path), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1391 | if (!result) |
| 1392 | return FALSE; |
| 1393 | /* In the ANSI API, there should not be any paths longer |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1394 | than MAX_PATH-1 (not including the final null character). */ |
| 1395 | assert(result < Py_ARRAY_LENGTH(new_path)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1396 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 1397 | strncmp(new_path, "//", 2) == 0) |
| 1398 | /* UNC path, nothing to do. */ |
| 1399 | return TRUE; |
| 1400 | env[1] = new_path[0]; |
| 1401 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | /* The Unicode version differs from the ANSI version |
| 1405 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1406 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1407 | win32_wchdir(LPCWSTR path) |
| 1408 | { |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1409 | wchar_t _new_path[MAX_PATH], *new_path = _new_path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1410 | int result; |
| 1411 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1412 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1413 | if(!SetCurrentDirectoryW(path)) |
| 1414 | return FALSE; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1415 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(new_path), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1416 | if (!result) |
| 1417 | return FALSE; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1418 | if (result > Py_ARRAY_LENGTH(new_path)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1419 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1420 | if (!new_path) { |
| 1421 | SetLastError(ERROR_OUTOFMEMORY); |
| 1422 | return FALSE; |
| 1423 | } |
| 1424 | result = GetCurrentDirectoryW(result, new_path); |
| 1425 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1426 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1427 | return FALSE; |
| 1428 | } |
| 1429 | } |
| 1430 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1431 | wcsncmp(new_path, L"//", 2) == 0) |
| 1432 | /* UNC path, nothing to do. */ |
| 1433 | return TRUE; |
| 1434 | env[1] = new_path[0]; |
| 1435 | result = SetEnvironmentVariableW(env, new_path); |
| 1436 | if (new_path != _new_path) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1437 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1438 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1439 | } |
| 1440 | #endif |
| 1441 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1442 | #ifdef MS_WINDOWS |
| 1443 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1444 | - time stamps are restricted to second resolution |
| 1445 | - file modification times suffer from forth-and-back conversions between |
| 1446 | UTC and local time |
| 1447 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1448 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1449 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1450 | |
| 1451 | struct win32_stat{ |
Brian Curtin | 87e63a2 | 2012-12-31 11:59:48 -0600 | [diff] [blame] | 1452 | unsigned long st_dev; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1453 | __int64 st_ino; |
| 1454 | unsigned short st_mode; |
| 1455 | int st_nlink; |
| 1456 | int st_uid; |
| 1457 | int st_gid; |
Brian Curtin | 87e63a2 | 2012-12-31 11:59:48 -0600 | [diff] [blame] | 1458 | unsigned long st_rdev; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1459 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1460 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1461 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1462 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1463 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1464 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1465 | int st_ctime_nsec; |
| 1466 | }; |
| 1467 | |
| 1468 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 1469 | |
| 1470 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1471 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1472 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1473 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 1474 | /* Cannot simply cast and dereference in_ptr, |
| 1475 | since it might not be aligned properly */ |
| 1476 | __int64 in; |
| 1477 | memcpy(&in, in_ptr, sizeof(in)); |
| 1478 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1479 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1482 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1483 | time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1484 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1485 | /* XXX endianness */ |
| 1486 | __int64 out; |
| 1487 | out = time_in + secs_between_epochs; |
| 1488 | out = out * 10000000 + nsec_in / 100; |
| 1489 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1492 | /* Below, we *know* that ugo+r is 0444 */ |
| 1493 | #if _S_IREAD != 0400 |
| 1494 | #error Unsupported C library |
| 1495 | #endif |
| 1496 | static int |
| 1497 | attributes_to_mode(DWORD attr) |
| 1498 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1499 | int m = 0; |
| 1500 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1501 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1502 | else |
| 1503 | m |= _S_IFREG; |
| 1504 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1505 | m |= 0444; |
| 1506 | else |
| 1507 | m |= 0666; |
| 1508 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1512 | attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1513 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1514 | memset(result, 0, sizeof(*result)); |
| 1515 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1516 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
Brian Curtin | 490b32a | 2012-12-26 07:03:03 -0600 | [diff] [blame] | 1517 | result->st_dev = info->dwVolumeSerialNumber; |
| 1518 | result->st_rdev = result->st_dev; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1519 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1520 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1521 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1522 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1523 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1524 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1525 | /* first clear the S_IFMT bits */ |
Christian Heimes | 99d6135 | 2013-06-23 23:56:05 +0200 | [diff] [blame] | 1526 | result->st_mode ^= (result->st_mode & S_IFMT); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1527 | /* now set the bits that make this a symlink */ |
Christian Heimes | 99d6135 | 2013-06-23 23:56:05 +0200 | [diff] [blame] | 1528 | result->st_mode |= S_IFLNK; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1529 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1530 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1531 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1534 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1535 | 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] | 1536 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1537 | HANDLE hFindFile; |
| 1538 | WIN32_FIND_DATAA FileData; |
| 1539 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1540 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1541 | return FALSE; |
| 1542 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1543 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1544 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1545 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1546 | info->ftCreationTime = FileData.ftCreationTime; |
| 1547 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1548 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1549 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1550 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1551 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1552 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1553 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1554 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1558 | 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] | 1559 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1560 | HANDLE hFindFile; |
| 1561 | WIN32_FIND_DATAW FileData; |
| 1562 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1563 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1564 | return FALSE; |
| 1565 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1566 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1567 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1568 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1569 | info->ftCreationTime = FileData.ftCreationTime; |
| 1570 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1571 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1572 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1573 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1574 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1575 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1576 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1577 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1580 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
Antoine Pitrou | 06eecea | 2012-10-21 16:33:33 +0200 | [diff] [blame] | 1581 | static int has_GetFinalPathNameByHandle = -1; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1582 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 1583 | DWORD); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1584 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1585 | check_GetFinalPathNameByHandle() |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1586 | { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1587 | HINSTANCE hKernel32; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1588 | DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 1589 | DWORD); |
| 1590 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1591 | /* only recheck */ |
Antoine Pitrou | 06eecea | 2012-10-21 16:33:33 +0200 | [diff] [blame] | 1592 | if (-1 == has_GetFinalPathNameByHandle) |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1593 | { |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1594 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1595 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 1596 | "GetFinalPathNameByHandleA"); |
| 1597 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 1598 | "GetFinalPathNameByHandleW"); |
| 1599 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 1600 | Py_GetFinalPathNameByHandleW; |
| 1601 | } |
| 1602 | return has_GetFinalPathNameByHandle; |
| 1603 | } |
| 1604 | |
| 1605 | static BOOL |
| 1606 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1607 | { |
| 1608 | int buf_size, result_length; |
| 1609 | wchar_t *buf; |
| 1610 | |
| 1611 | /* We have a good handle to the target, use it to determine |
| 1612 | the target path name (then we'll call lstat on it). */ |
| 1613 | buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1614 | VOLUME_NAME_DOS); |
| 1615 | if(!buf_size) |
| 1616 | return FALSE; |
| 1617 | |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1618 | buf = (wchar_t *)PyMem_Malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1619 | if (!buf) { |
| 1620 | SetLastError(ERROR_OUTOFMEMORY); |
| 1621 | return FALSE; |
| 1622 | } |
| 1623 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1624 | result_length = Py_GetFinalPathNameByHandleW(hdl, |
| 1625 | buf, buf_size, VOLUME_NAME_DOS); |
| 1626 | |
| 1627 | if(!result_length) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1628 | PyMem_Free(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1629 | return FALSE; |
| 1630 | } |
| 1631 | |
| 1632 | if(!CloseHandle(hdl)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1633 | PyMem_Free(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1634 | return FALSE; |
| 1635 | } |
| 1636 | |
| 1637 | buf[result_length] = 0; |
| 1638 | |
| 1639 | *target_path = buf; |
| 1640 | return TRUE; |
| 1641 | } |
| 1642 | |
| 1643 | static int |
| 1644 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1645 | BOOL traverse); |
| 1646 | static int |
| 1647 | win32_xstat_impl(const char *path, struct win32_stat *result, |
| 1648 | BOOL traverse) |
| 1649 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1650 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1651 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1652 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1653 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1654 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1655 | const char *dot; |
| 1656 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1657 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1658 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1659 | traverse reparse point. */ |
| 1660 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1663 | hFile = CreateFileA( |
| 1664 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1665 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1666 | 0, /* share mode */ |
| 1667 | NULL, /* security attributes */ |
| 1668 | OPEN_EXISTING, |
| 1669 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1670 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1671 | Because of this, calls like GetFinalPathNameByHandle will return |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 1672 | the symlink path again and not the actual final path. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1673 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1674 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1675 | NULL); |
| 1676 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1677 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1678 | /* Either the target doesn't exist, or we don't have access to |
| 1679 | get a handle to it. If the former, we need to return an error. |
| 1680 | If the latter, we can use attributes_from_dir. */ |
| 1681 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1682 | return -1; |
| 1683 | /* Could not get attributes on open file. Fall back to |
| 1684 | reading the directory. */ |
| 1685 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1686 | /* Very strange. This should not fail now */ |
| 1687 | return -1; |
| 1688 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1689 | if (traverse) { |
| 1690 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1691 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1692 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1693 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1694 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1695 | } else { |
| 1696 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1697 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1698 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1699 | } |
| 1700 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1701 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1702 | return -1; |
| 1703 | |
| 1704 | /* Close the outer open file handle now that we're about to |
| 1705 | reopen it with different flags. */ |
| 1706 | if (!CloseHandle(hFile)) |
| 1707 | return -1; |
| 1708 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1709 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1710 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1711 | the file without the reparse handling flag set. */ |
| 1712 | hFile2 = CreateFileA( |
| 1713 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1714 | NULL, OPEN_EXISTING, |
| 1715 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1716 | NULL); |
| 1717 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1718 | return -1; |
| 1719 | |
| 1720 | if (!get_target_path(hFile2, &target_path)) |
| 1721 | return -1; |
| 1722 | |
| 1723 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1724 | PyMem_Free(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1725 | return code; |
| 1726 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1727 | } else |
| 1728 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1729 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1730 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1731 | |
| 1732 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1733 | dot = strrchr(path, '.'); |
| 1734 | if (dot) { |
| 1735 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1736 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1737 | result->st_mode |= 0111; |
| 1738 | } |
| 1739 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1743 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1744 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1745 | { |
| 1746 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1747 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1748 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1749 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1750 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1751 | const wchar_t *dot; |
| 1752 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1753 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1754 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1755 | traverse reparse point. */ |
| 1756 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1759 | hFile = CreateFileW( |
| 1760 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1761 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1762 | 0, /* share mode */ |
| 1763 | NULL, /* security attributes */ |
| 1764 | OPEN_EXISTING, |
| 1765 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1766 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1767 | Because of this, calls like GetFinalPathNameByHandle will return |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 1768 | the symlink path again and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1769 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1770 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1771 | NULL); |
| 1772 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1773 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1774 | /* Either the target doesn't exist, or we don't have access to |
| 1775 | get a handle to it. If the former, we need to return an error. |
| 1776 | If the latter, we can use attributes_from_dir. */ |
| 1777 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1778 | return -1; |
| 1779 | /* Could not get attributes on open file. Fall back to |
| 1780 | reading the directory. */ |
| 1781 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1782 | /* Very strange. This should not fail now */ |
| 1783 | return -1; |
| 1784 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1785 | if (traverse) { |
| 1786 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1787 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1788 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1789 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1790 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1791 | } else { |
| 1792 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1793 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1794 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1795 | } |
| 1796 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1797 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1798 | return -1; |
| 1799 | |
| 1800 | /* Close the outer open file handle now that we're about to |
| 1801 | reopen it with different flags. */ |
| 1802 | if (!CloseHandle(hFile)) |
| 1803 | return -1; |
| 1804 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1805 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1806 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1807 | the file without the reparse handling flag set. */ |
| 1808 | hFile2 = CreateFileW( |
| 1809 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1810 | NULL, OPEN_EXISTING, |
| 1811 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1812 | NULL); |
| 1813 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1814 | return -1; |
| 1815 | |
| 1816 | if (!get_target_path(hFile2, &target_path)) |
| 1817 | return -1; |
| 1818 | |
| 1819 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1820 | PyMem_Free(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1821 | return code; |
| 1822 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1823 | } else |
| 1824 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1825 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1826 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1827 | |
| 1828 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1829 | dot = wcsrchr(path, '.'); |
| 1830 | if (dot) { |
| 1831 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1832 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1833 | result->st_mode |= 0111; |
| 1834 | } |
| 1835 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1839 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1840 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1841 | /* Protocol violation: we explicitly clear errno, instead of |
| 1842 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1843 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1844 | errno = 0; |
| 1845 | return code; |
| 1846 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1847 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1848 | static int |
| 1849 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1850 | { |
| 1851 | /* Protocol violation: we explicitly clear errno, instead of |
| 1852 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1853 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1854 | errno = 0; |
| 1855 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1856 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1857 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1858 | |
| 1859 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1860 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1861 | default does not traverse symlinks and instead returns attributes for |
| 1862 | the symlink. |
| 1863 | |
| 1864 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1865 | win32_stat will first explicitly resolve the symlink target and then will |
| 1866 | call win32_lstat on that result. |
| 1867 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1868 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1869 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1870 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1871 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1872 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1873 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1876 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1877 | win32_lstat_w(const wchar_t* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1878 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1879 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | static int |
| 1883 | win32_stat(const char* path, struct win32_stat *result) |
| 1884 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1885 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1888 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1889 | win32_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1890 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1891 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
| 1894 | static int |
| 1895 | win32_fstat(int file_number, struct win32_stat *result) |
| 1896 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1897 | BY_HANDLE_FILE_INFORMATION info; |
| 1898 | HANDLE h; |
| 1899 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1900 | |
Richard Oudkerk | 2240ac1 | 2012-07-06 12:05:32 +0100 | [diff] [blame] | 1901 | if (!_PyVerify_fd(file_number)) |
| 1902 | h = INVALID_HANDLE_VALUE; |
| 1903 | else |
| 1904 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1905 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1906 | /* Protocol violation: we explicitly clear errno, instead of |
| 1907 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1908 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1909 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1910 | if (h == INVALID_HANDLE_VALUE) { |
| 1911 | /* This is really a C library error (invalid file handle). |
| 1912 | We set the Win32 error to the closes one matching. */ |
| 1913 | SetLastError(ERROR_INVALID_HANDLE); |
| 1914 | return -1; |
| 1915 | } |
| 1916 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1917 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1918 | type = GetFileType(h); |
| 1919 | if (type == FILE_TYPE_UNKNOWN) { |
| 1920 | DWORD error = GetLastError(); |
| 1921 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1922 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1923 | } |
| 1924 | /* else: valid but unknown file */ |
| 1925 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1926 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1927 | if (type != FILE_TYPE_DISK) { |
| 1928 | if (type == FILE_TYPE_CHAR) |
| 1929 | result->st_mode = _S_IFCHR; |
| 1930 | else if (type == FILE_TYPE_PIPE) |
| 1931 | result->st_mode = _S_IFIFO; |
| 1932 | return 0; |
| 1933 | } |
| 1934 | |
| 1935 | if (!GetFileInformationByHandle(h, &info)) { |
| 1936 | return -1; |
| 1937 | } |
| 1938 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1939 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1940 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1941 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1942 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | #endif /* MS_WINDOWS */ |
| 1946 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1947 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1948 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1949 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1950 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1951 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1952 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1953 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1954 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1955 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1956 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1957 | |
| 1958 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1959 | {"st_mode", "protection bits"}, |
| 1960 | {"st_ino", "inode"}, |
| 1961 | {"st_dev", "device"}, |
| 1962 | {"st_nlink", "number of hard links"}, |
| 1963 | {"st_uid", "user ID of owner"}, |
| 1964 | {"st_gid", "group ID of owner"}, |
| 1965 | {"st_size", "total size, in bytes"}, |
| 1966 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1967 | {NULL, "integer time of last access"}, |
| 1968 | {NULL, "integer time of last modification"}, |
| 1969 | {NULL, "integer time of last change"}, |
| 1970 | {"st_atime", "time of last access"}, |
| 1971 | {"st_mtime", "time of last modification"}, |
| 1972 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1973 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1974 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1975 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1976 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1977 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1978 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1979 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1980 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1981 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1982 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1983 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1984 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1985 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1986 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1987 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1988 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1989 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1990 | #endif |
| 1991 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1992 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1993 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1994 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1995 | }; |
| 1996 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1997 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1998 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1999 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2000 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2001 | #endif |
| 2002 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2003 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2004 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 2005 | #else |
| 2006 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 2007 | #endif |
| 2008 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2009 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2010 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 2011 | #else |
| 2012 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 2013 | #endif |
| 2014 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2015 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 2016 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 2017 | #else |
| 2018 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 2019 | #endif |
| 2020 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2021 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 2022 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2023 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 2024 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2025 | #endif |
| 2026 | |
| 2027 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 2028 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 2029 | #else |
| 2030 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 2031 | #endif |
| 2032 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2033 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2034 | "stat_result", /* name */ |
| 2035 | stat_result__doc__, /* doc */ |
| 2036 | stat_result_fields, |
| 2037 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2038 | }; |
| 2039 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2040 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2041 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 2042 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2043 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 2044 | 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] | 2045 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2046 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2047 | |
| 2048 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2049 | {"f_bsize", }, |
| 2050 | {"f_frsize", }, |
| 2051 | {"f_blocks", }, |
| 2052 | {"f_bfree", }, |
| 2053 | {"f_bavail", }, |
| 2054 | {"f_files", }, |
| 2055 | {"f_ffree", }, |
| 2056 | {"f_favail", }, |
| 2057 | {"f_flag", }, |
| 2058 | {"f_namemax",}, |
| 2059 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2060 | }; |
| 2061 | |
| 2062 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2063 | "statvfs_result", /* name */ |
| 2064 | statvfs_result__doc__, /* doc */ |
| 2065 | statvfs_result_fields, |
| 2066 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2067 | }; |
| 2068 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2069 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 2070 | PyDoc_STRVAR(waitid_result__doc__, |
| 2071 | "waitid_result: Result from waitid.\n\n\ |
| 2072 | This object may be accessed either as a tuple of\n\ |
| 2073 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 2074 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 2075 | \n\ |
| 2076 | See os.waitid for more information."); |
| 2077 | |
| 2078 | static PyStructSequence_Field waitid_result_fields[] = { |
| 2079 | {"si_pid", }, |
| 2080 | {"si_uid", }, |
| 2081 | {"si_signo", }, |
| 2082 | {"si_status", }, |
| 2083 | {"si_code", }, |
| 2084 | {0} |
| 2085 | }; |
| 2086 | |
| 2087 | static PyStructSequence_Desc waitid_result_desc = { |
| 2088 | "waitid_result", /* name */ |
| 2089 | waitid_result__doc__, /* doc */ |
| 2090 | waitid_result_fields, |
| 2091 | 5 |
| 2092 | }; |
| 2093 | static PyTypeObject WaitidResultType; |
| 2094 | #endif |
| 2095 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2096 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2097 | static PyTypeObject StatResultType; |
| 2098 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2099 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 2100 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2101 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2102 | static newfunc structseq_new; |
| 2103 | |
| 2104 | static PyObject * |
| 2105 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2106 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2107 | PyStructSequence *result; |
| 2108 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2109 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2110 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 2111 | if (!result) |
| 2112 | return NULL; |
| 2113 | /* If we have been initialized from a tuple, |
| 2114 | st_?time might be set to None. Initialize it |
| 2115 | from the int slots. */ |
| 2116 | for (i = 7; i <= 9; i++) { |
| 2117 | if (result->ob_item[i+3] == Py_None) { |
| 2118 | Py_DECREF(Py_None); |
| 2119 | Py_INCREF(result->ob_item[i]); |
| 2120 | result->ob_item[i+3] = result->ob_item[i]; |
| 2121 | } |
| 2122 | } |
| 2123 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
| 2126 | |
| 2127 | |
| 2128 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 2129 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2130 | |
| 2131 | PyDoc_STRVAR(stat_float_times__doc__, |
| 2132 | "stat_float_times([newval]) -> oldval\n\n\ |
| 2133 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 2134 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 2135 | future calls return ints. \n\ |
| 2136 | If newval is omitted, return the current setting.\n"); |
| 2137 | |
| 2138 | static PyObject* |
| 2139 | stat_float_times(PyObject* self, PyObject *args) |
| 2140 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2141 | int newval = -1; |
| 2142 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 2143 | return NULL; |
Victor Stinner | 034d0aa | 2012-06-05 01:22:15 +0200 | [diff] [blame] | 2144 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2145 | "stat_float_times() is deprecated", |
| 2146 | 1)) |
| 2147 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2148 | if (newval == -1) |
| 2149 | /* Return old value */ |
| 2150 | return PyBool_FromLong(_stat_float_times); |
| 2151 | _stat_float_times = newval; |
| 2152 | Py_INCREF(Py_None); |
| 2153 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2154 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2155 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2156 | static PyObject *billion = NULL; |
| 2157 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2158 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2159 | 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] | 2160 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2161 | PyObject *s = _PyLong_FromTime_t(sec); |
| 2162 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 2163 | PyObject *s_in_ns = NULL; |
| 2164 | PyObject *ns_total = NULL; |
| 2165 | PyObject *float_s = NULL; |
| 2166 | |
| 2167 | if (!(s && ns_fractional)) |
| 2168 | goto exit; |
| 2169 | |
| 2170 | s_in_ns = PyNumber_Multiply(s, billion); |
| 2171 | if (!s_in_ns) |
| 2172 | goto exit; |
| 2173 | |
| 2174 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 2175 | if (!ns_total) |
| 2176 | goto exit; |
| 2177 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2178 | if (_stat_float_times) { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2179 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2180 | if (!float_s) |
| 2181 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2182 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2183 | else { |
| 2184 | float_s = s; |
| 2185 | Py_INCREF(float_s); |
| 2186 | } |
| 2187 | |
| 2188 | PyStructSequence_SET_ITEM(v, index, s); |
| 2189 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2190 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2191 | s = NULL; |
| 2192 | float_s = NULL; |
| 2193 | ns_total = NULL; |
| 2194 | exit: |
| 2195 | Py_XDECREF(s); |
| 2196 | Py_XDECREF(ns_fractional); |
| 2197 | Py_XDECREF(s_in_ns); |
| 2198 | Py_XDECREF(ns_total); |
| 2199 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2202 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2203 | (used by posix_stat() and posix_fstat()) */ |
| 2204 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2205 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2206 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2207 | unsigned long ansec, mnsec, cnsec; |
| 2208 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 2209 | if (v == NULL) |
| 2210 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2211 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2212 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2213 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2214 | PyStructSequence_SET_ITEM(v, 1, |
| 2215 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2216 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2217 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2218 | #endif |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2219 | #ifdef MS_WINDOWS |
| 2220 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
| 2221 | #elif defined(HAVE_LONG_LONG) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2222 | PyStructSequence_SET_ITEM(v, 2, |
| 2223 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2224 | #else |
Brian Curtin | 9cc4321 | 2013-01-01 12:31:06 -0600 | [diff] [blame] | 2225 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2226 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2227 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2228 | #if defined(MS_WINDOWS) |
| 2229 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2230 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2231 | #else |
| 2232 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2233 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2234 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2235 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2236 | PyStructSequence_SET_ITEM(v, 6, |
| 2237 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2238 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2239 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2240 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2241 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2242 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2243 | ansec = st->st_atim.tv_nsec; |
| 2244 | mnsec = st->st_mtim.tv_nsec; |
| 2245 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2246 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2247 | ansec = st->st_atimespec.tv_nsec; |
| 2248 | mnsec = st->st_mtimespec.tv_nsec; |
| 2249 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2250 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2251 | ansec = st->st_atime_nsec; |
| 2252 | mnsec = st->st_mtime_nsec; |
| 2253 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2254 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2255 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2256 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2257 | fill_time(v, 7, st->st_atime, ansec); |
| 2258 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2259 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2260 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2261 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2262 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2263 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2264 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2265 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2266 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2267 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2268 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2269 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2270 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2271 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2272 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2273 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2274 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2275 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2276 | #endif |
| 2277 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2278 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2279 | PyObject *val; |
| 2280 | unsigned long bsec,bnsec; |
| 2281 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2282 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2283 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2284 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2285 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2286 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2287 | if (_stat_float_times) { |
| 2288 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 2289 | } else { |
| 2290 | val = PyLong_FromLong((long)bsec); |
| 2291 | } |
| 2292 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2293 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2294 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2295 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2296 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2297 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2298 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2299 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2300 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2301 | if (PyErr_Occurred()) { |
| 2302 | Py_DECREF(v); |
| 2303 | return NULL; |
| 2304 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2305 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2306 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2309 | /* POSIX methods */ |
| 2310 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2311 | |
| 2312 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2313 | posix_do_stat(char *function_name, path_t *path, |
| 2314 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2315 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2316 | STRUCT_STAT st; |
| 2317 | int result; |
| 2318 | |
| 2319 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2320 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2321 | return NULL; |
| 2322 | #endif |
| 2323 | |
| 2324 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2325 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2326 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2327 | return NULL; |
| 2328 | |
| 2329 | Py_BEGIN_ALLOW_THREADS |
| 2330 | if (path->fd != -1) |
| 2331 | result = FSTAT(path->fd, &st); |
| 2332 | else |
| 2333 | #ifdef MS_WINDOWS |
| 2334 | if (path->wide) { |
| 2335 | if (follow_symlinks) |
| 2336 | result = win32_stat_w(path->wide, &st); |
| 2337 | else |
| 2338 | result = win32_lstat_w(path->wide, &st); |
| 2339 | } |
| 2340 | else |
| 2341 | #endif |
| 2342 | #if defined(HAVE_LSTAT) || defined(MS_WINDOWS) |
| 2343 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2344 | result = LSTAT(path->narrow, &st); |
| 2345 | else |
| 2346 | #endif |
| 2347 | #ifdef HAVE_FSTATAT |
| 2348 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2349 | result = fstatat(dir_fd, path->narrow, &st, |
| 2350 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2351 | else |
| 2352 | #endif |
| 2353 | result = STAT(path->narrow, &st); |
| 2354 | Py_END_ALLOW_THREADS |
| 2355 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2356 | if (result != 0) { |
| 2357 | return path_error(path); |
| 2358 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2359 | |
| 2360 | return _pystat_fromstructstat(&st); |
| 2361 | } |
| 2362 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2363 | #ifdef HAVE_FSTATAT |
| 2364 | #define OS_STAT_DIR_FD_CONVERTER dir_fd_converter |
| 2365 | #else |
| 2366 | #define OS_STAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2367 | #endif |
| 2368 | |
| 2369 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2370 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2371 | |
| 2372 | class path_t_converter(CConverter): |
| 2373 | |
| 2374 | type = "path_t" |
| 2375 | impl_by_reference = True |
| 2376 | parse_by_reference = True |
| 2377 | |
| 2378 | converter = 'path_converter' |
| 2379 | |
| 2380 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2381 | # right now path_t doesn't support default values. |
| 2382 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2383 | if self.default is not unspecified: |
| 2384 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2385 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2386 | if self.c_default is not None: |
| 2387 | fail("Can't specify a c_default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2388 | |
| 2389 | self.nullable = nullable |
| 2390 | self.allow_fd = allow_fd |
| 2391 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2392 | def pre_render(self): |
| 2393 | def strify(value): |
| 2394 | return str(int(bool(value))) |
| 2395 | |
| 2396 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2397 | self.c_default = 'PATH_T_INITIALIZE("{}", {}, {})'.format( |
| 2398 | self.function.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2399 | strify(self.nullable), |
| 2400 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2401 | ) |
| 2402 | |
| 2403 | def cleanup(self): |
| 2404 | return "path_cleanup(&" + self.name + ");\n" |
| 2405 | |
| 2406 | |
| 2407 | class dir_fd_converter(CConverter): |
| 2408 | type = 'int' |
| 2409 | converter = 'OS_STAT_DIR_FD_CONVERTER' |
| 2410 | |
| 2411 | def converter_init(self): |
| 2412 | if self.default in (unspecified, None): |
| 2413 | self.c_default = 'DEFAULT_DIR_FD' |
| 2414 | |
| 2415 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2416 | [python start generated code]*/ |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2417 | /*[python end generated code: output=da39a3ee5e6b4b0d input=5c9f456f53244fc3]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2418 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2419 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2420 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2421 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2422 | |
| 2423 | path : path_t(allow_fd=True) |
| 2424 | Path to be examined; can be string, bytes, or open-file-descriptor int. |
| 2425 | |
| 2426 | * |
| 2427 | |
| 2428 | dir_fd : dir_fd = None |
| 2429 | If not None, it should be a file descriptor open to a directory, |
| 2430 | and path should be a relative string; path will then be relative to |
| 2431 | that directory. |
| 2432 | |
| 2433 | follow_symlinks: bool = True |
| 2434 | If False, and the last element of the path is a symbolic link, |
| 2435 | stat will examine the symbolic link itself instead of the file |
| 2436 | the link points to. |
| 2437 | |
| 2438 | Perform a stat system call on the given path. |
| 2439 | |
| 2440 | dir_fd and follow_symlinks may not be implemented |
| 2441 | on your platform. If they are unavailable, using them will raise a |
| 2442 | NotImplementedError. |
| 2443 | |
| 2444 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2445 | an open file descriptor. |
| 2446 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2447 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2448 | |
| 2449 | PyDoc_STRVAR(os_stat__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2450 | "stat($module, /, path, *, dir_fd=None, follow_symlinks=True)\n" |
| 2451 | "--\n" |
| 2452 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2453 | "Perform a stat system call on the given path.\n" |
| 2454 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2455 | " path\n" |
| 2456 | " Path to be examined; can be string, bytes, or open-file-descriptor int.\n" |
| 2457 | " dir_fd\n" |
| 2458 | " If not None, it should be a file descriptor open to a directory,\n" |
| 2459 | " and path should be a relative string; path will then be relative to\n" |
| 2460 | " that directory.\n" |
| 2461 | " follow_symlinks\n" |
| 2462 | " If False, and the last element of the path is a symbolic link,\n" |
| 2463 | " stat will examine the symbolic link itself instead of the file\n" |
| 2464 | " the link points to.\n" |
| 2465 | "\n" |
| 2466 | "dir_fd and follow_symlinks may not be implemented\n" |
| 2467 | " on your platform. If they are unavailable, using them will raise a\n" |
| 2468 | " NotImplementedError.\n" |
| 2469 | "\n" |
| 2470 | "It\'s an error to use dir_fd or follow_symlinks when specifying path as\n" |
| 2471 | " an open file descriptor."); |
| 2472 | |
| 2473 | #define OS_STAT_METHODDEF \ |
| 2474 | {"stat", (PyCFunction)os_stat, METH_VARARGS|METH_KEYWORDS, os_stat__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2475 | |
| 2476 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2477 | os_stat_impl(PyModuleDef *module, path_t *path, int dir_fd, int follow_symlinks); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2478 | |
| 2479 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2480 | os_stat(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2481 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2482 | PyObject *return_value = NULL; |
| 2483 | static char *_keywords[] = {"path", "dir_fd", "follow_symlinks", NULL}; |
| 2484 | path_t path = PATH_T_INITIALIZE("stat", 0, 1); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2485 | int dir_fd = DEFAULT_DIR_FD; |
| 2486 | int follow_symlinks = 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2487 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2488 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2489 | "O&|$O&p:stat", _keywords, |
| 2490 | path_converter, &path, OS_STAT_DIR_FD_CONVERTER, &dir_fd, &follow_symlinks)) |
| 2491 | goto exit; |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 2492 | return_value = os_stat_impl(module, &path, dir_fd, follow_symlinks); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2493 | |
| 2494 | exit: |
| 2495 | /* Cleanup for path */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2496 | path_cleanup(&path); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2497 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2498 | return return_value; |
| 2499 | } |
| 2500 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2501 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2502 | os_stat_impl(PyModuleDef *module, path_t *path, int dir_fd, int follow_symlinks) |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2503 | /*[clinic end generated code: output=f1dcaa5e24db9882 input=5ae155bd475fd20a]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2504 | { |
| 2505 | return posix_do_stat("stat", path, dir_fd, follow_symlinks); |
| 2506 | } |
| 2507 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2508 | PyDoc_STRVAR(posix_lstat__doc__, |
| 2509 | "lstat(path, *, dir_fd=None) -> stat result\n\n\ |
| 2510 | Like stat(), but do not follow symbolic links.\n\ |
| 2511 | Equivalent to stat(path, follow_symlinks=False)."); |
| 2512 | |
| 2513 | static PyObject * |
| 2514 | posix_lstat(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2515 | { |
| 2516 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 2517 | path_t path; |
| 2518 | int dir_fd = DEFAULT_DIR_FD; |
| 2519 | int follow_symlinks = 0; |
| 2520 | PyObject *return_value; |
| 2521 | |
| 2522 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2523 | path.function_name = "lstat"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2524 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:lstat", keywords, |
| 2525 | path_converter, &path, |
| 2526 | #ifdef HAVE_FSTATAT |
| 2527 | dir_fd_converter, &dir_fd |
| 2528 | #else |
| 2529 | dir_fd_unavailable, &dir_fd |
| 2530 | #endif |
| 2531 | )) |
| 2532 | return NULL; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2533 | return_value = posix_do_stat("lstat", &path, dir_fd, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2534 | path_cleanup(&path); |
| 2535 | return return_value; |
| 2536 | } |
| 2537 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2538 | |
| 2539 | #ifdef HAVE_FACCESSAT |
| 2540 | #define OS_ACCESS_DIR_FD_CONVERTER dir_fd_converter |
| 2541 | #else |
| 2542 | #define OS_ACCESS_DIR_FD_CONVERTER dir_fd_unavailable |
| 2543 | #endif |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2544 | /*[clinic input] |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2545 | os.access |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2546 | |
| 2547 | path: path_t(allow_fd=True) |
| 2548 | Path to be tested; can be string, bytes, or open-file-descriptor int. |
| 2549 | |
| 2550 | mode: int |
| 2551 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2552 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2553 | |
| 2554 | * |
| 2555 | |
| 2556 | dir_fd : dir_fd = None |
| 2557 | If not None, it should be a file descriptor open to a directory, |
| 2558 | and path should be relative; path will then be relative to that |
| 2559 | directory. |
| 2560 | |
| 2561 | effective_ids: bool = False |
| 2562 | If True, access will use the effective uid/gid instead of |
| 2563 | the real uid/gid. |
| 2564 | |
| 2565 | follow_symlinks: bool = True |
| 2566 | If False, and the last element of the path is a symbolic link, |
| 2567 | access will examine the symbolic link itself instead of the file |
| 2568 | the link points to. |
| 2569 | |
| 2570 | Use the real uid/gid to test for access to a path. |
| 2571 | |
| 2572 | {parameters} |
| 2573 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2574 | on your platform. If they are unavailable, using them will raise a |
| 2575 | NotImplementedError. |
| 2576 | |
| 2577 | Note that most operations will use the effective uid/gid, therefore this |
| 2578 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2579 | has the specified access to the path. |
| 2580 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2581 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2582 | |
| 2583 | PyDoc_STRVAR(os_access__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2584 | "access($module, /, path, mode, *, dir_fd=None, effective_ids=False,\n" |
| 2585 | " follow_symlinks=True)\n" |
| 2586 | "--\n" |
| 2587 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2588 | "Use the real uid/gid to test for access to a path.\n" |
| 2589 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2590 | " path\n" |
| 2591 | " Path to be tested; can be string, bytes, or open-file-descriptor int.\n" |
| 2592 | " mode\n" |
| 2593 | " Operating-system mode bitfield. Can be F_OK to test existence,\n" |
| 2594 | " or the inclusive-OR of R_OK, W_OK, and X_OK.\n" |
| 2595 | " dir_fd\n" |
| 2596 | " If not None, it should be a file descriptor open to a directory,\n" |
| 2597 | " and path should be relative; path will then be relative to that\n" |
| 2598 | " directory.\n" |
| 2599 | " effective_ids\n" |
| 2600 | " If True, access will use the effective uid/gid instead of\n" |
| 2601 | " the real uid/gid.\n" |
| 2602 | " follow_symlinks\n" |
| 2603 | " If False, and the last element of the path is a symbolic link,\n" |
| 2604 | " access will examine the symbolic link itself instead of the file\n" |
| 2605 | " the link points to.\n" |
| 2606 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2607 | "dir_fd, effective_ids, and follow_symlinks may not be implemented\n" |
| 2608 | " on your platform. If they are unavailable, using them will raise a\n" |
| 2609 | " NotImplementedError.\n" |
| 2610 | "\n" |
| 2611 | "Note that most operations will use the effective uid/gid, therefore this\n" |
| 2612 | " routine can be used in a suid/sgid environment to test if the invoking user\n" |
| 2613 | " has the specified access to the path."); |
| 2614 | |
| 2615 | #define OS_ACCESS_METHODDEF \ |
| 2616 | {"access", (PyCFunction)os_access, METH_VARARGS|METH_KEYWORDS, os_access__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2617 | |
| 2618 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2619 | os_access_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, int effective_ids, int follow_symlinks); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2620 | |
| 2621 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2622 | os_access(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2623 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2624 | PyObject *return_value = NULL; |
| 2625 | static char *_keywords[] = {"path", "mode", "dir_fd", "effective_ids", "follow_symlinks", NULL}; |
| 2626 | path_t path = PATH_T_INITIALIZE("access", 0, 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2627 | int mode; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2628 | int dir_fd = DEFAULT_DIR_FD; |
| 2629 | int effective_ids = 0; |
| 2630 | int follow_symlinks = 1; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2631 | |
| 2632 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2633 | "O&i|$O&pp:access", _keywords, |
| 2634 | path_converter, &path, &mode, OS_STAT_DIR_FD_CONVERTER, &dir_fd, &effective_ids, &follow_symlinks)) |
| 2635 | goto exit; |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 2636 | return_value = os_access_impl(module, &path, mode, dir_fd, effective_ids, follow_symlinks); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2637 | |
| 2638 | exit: |
| 2639 | /* Cleanup for path */ |
| 2640 | path_cleanup(&path); |
| 2641 | |
| 2642 | return return_value; |
| 2643 | } |
| 2644 | |
| 2645 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2646 | os_access_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, int effective_ids, int follow_symlinks) |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2647 | /*[clinic end generated code: output=a6ed4f151be9df0f input=2e2e7594371f5b7e]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2648 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2649 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2650 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2651 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2652 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2653 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2654 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2655 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2656 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2657 | #ifndef HAVE_FACCESSAT |
| 2658 | if (follow_symlinks_specified("access", follow_symlinks)) |
| 2659 | goto exit; |
| 2660 | |
| 2661 | if (effective_ids) { |
| 2662 | argument_unavailable_error("access", "effective_ids"); |
| 2663 | goto exit; |
| 2664 | } |
| 2665 | #endif |
| 2666 | |
| 2667 | #ifdef MS_WINDOWS |
| 2668 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | ebe83f9 | 2013-10-19 22:36:17 +0200 | [diff] [blame] | 2669 | if (path->wide != NULL) |
| 2670 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2671 | else |
Christian Heimes | ebe83f9 | 2013-10-19 22:36:17 +0200 | [diff] [blame] | 2672 | attr = GetFileAttributesA(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2673 | Py_END_ALLOW_THREADS |
| 2674 | |
| 2675 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2676 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2677 | * * we didn't get a -1, and |
| 2678 | * * write access wasn't requested, |
| 2679 | * * or the file isn't read-only, |
| 2680 | * * or it's a directory. |
| 2681 | * (Directories cannot be read-only on Windows.) |
| 2682 | */ |
| 2683 | return_value = PyBool_FromLong( |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 2684 | (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2685 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2686 | !(attr & FILE_ATTRIBUTE_READONLY) || |
| 2687 | (attr & FILE_ATTRIBUTE_DIRECTORY))); |
| 2688 | #else |
| 2689 | |
| 2690 | Py_BEGIN_ALLOW_THREADS |
| 2691 | #ifdef HAVE_FACCESSAT |
| 2692 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2693 | effective_ids || |
| 2694 | !follow_symlinks) { |
| 2695 | int flags = 0; |
| 2696 | if (!follow_symlinks) |
| 2697 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2698 | if (effective_ids) |
| 2699 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2700 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2701 | } |
| 2702 | else |
| 2703 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2704 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2705 | Py_END_ALLOW_THREADS |
| 2706 | return_value = PyBool_FromLong(!result); |
| 2707 | #endif |
| 2708 | |
| 2709 | #ifndef HAVE_FACCESSAT |
| 2710 | exit: |
| 2711 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2712 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2713 | } |
| 2714 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2715 | #ifndef F_OK |
| 2716 | #define F_OK 0 |
| 2717 | #endif |
| 2718 | #ifndef R_OK |
| 2719 | #define R_OK 4 |
| 2720 | #endif |
| 2721 | #ifndef W_OK |
| 2722 | #define W_OK 2 |
| 2723 | #endif |
| 2724 | #ifndef X_OK |
| 2725 | #define X_OK 1 |
| 2726 | #endif |
| 2727 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2728 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2729 | #ifdef HAVE_TTYNAME |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2730 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2731 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2732 | os.ttyname -> DecodeFSDefault |
| 2733 | |
| 2734 | fd: int |
| 2735 | Integer file descriptor handle. |
| 2736 | |
| 2737 | / |
| 2738 | |
| 2739 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2740 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2741 | |
| 2742 | PyDoc_STRVAR(os_ttyname__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2743 | "ttyname($module, fd, /)\n" |
| 2744 | "--\n" |
| 2745 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2746 | "Return the name of the terminal device connected to \'fd\'.\n" |
| 2747 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2748 | " fd\n" |
| 2749 | " Integer file descriptor handle."); |
| 2750 | |
| 2751 | #define OS_TTYNAME_METHODDEF \ |
| 2752 | {"ttyname", (PyCFunction)os_ttyname, METH_VARARGS, os_ttyname__doc__}, |
| 2753 | |
| 2754 | static char * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2755 | os_ttyname_impl(PyModuleDef *module, int fd); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2756 | |
| 2757 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2758 | os_ttyname(PyModuleDef *module, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2759 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2760 | PyObject *return_value = NULL; |
| 2761 | int fd; |
| 2762 | char *_return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2763 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2764 | if (!PyArg_ParseTuple(args, |
| 2765 | "i:ttyname", |
| 2766 | &fd)) |
| 2767 | goto exit; |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 2768 | _return_value = os_ttyname_impl(module, fd); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2769 | if (_return_value == NULL) |
| 2770 | goto exit; |
| 2771 | return_value = PyUnicode_DecodeFSDefault(_return_value); |
| 2772 | |
| 2773 | exit: |
| 2774 | return return_value; |
| 2775 | } |
| 2776 | |
| 2777 | static char * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2778 | os_ttyname_impl(PyModuleDef *module, int fd) |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2779 | /*[clinic end generated code: output=cee7bc4cffec01a2 input=5f72ca83e76b3b45]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2780 | { |
| 2781 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2782 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2783 | ret = ttyname(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2784 | if (ret == NULL) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2785 | posix_error(); |
| 2786 | return ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2787 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2788 | #else |
| 2789 | #define OS_TTYNAME_METHODDEF |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2790 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2791 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2792 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2793 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2794 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2795 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2796 | |
| 2797 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2798 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2799 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2800 | char *ret; |
| 2801 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2802 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2803 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2804 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2805 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2806 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2807 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2808 | if (ret == NULL) |
| 2809 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2810 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2811 | } |
| 2812 | #endif |
| 2813 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2814 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2815 | "chdir(path)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2816 | Change the current working directory to the specified path.\n\ |
| 2817 | \n\ |
| 2818 | path may always be specified as a string.\n\ |
| 2819 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2820 | If this functionality is unavailable, using it raises an exception."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2821 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2822 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2823 | posix_chdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2824 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2825 | path_t path; |
| 2826 | int result; |
| 2827 | PyObject *return_value = NULL; |
| 2828 | static char *keywords[] = {"path", NULL}; |
| 2829 | |
| 2830 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2831 | path.function_name = "chdir"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2832 | #ifdef HAVE_FCHDIR |
| 2833 | path.allow_fd = 1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2834 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2835 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:chdir", keywords, |
| 2836 | path_converter, &path |
| 2837 | )) |
| 2838 | return NULL; |
| 2839 | |
| 2840 | Py_BEGIN_ALLOW_THREADS |
| 2841 | #ifdef MS_WINDOWS |
| 2842 | if (path.wide) |
| 2843 | result = win32_wchdir(path.wide); |
| 2844 | else |
| 2845 | result = win32_chdir(path.narrow); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 2846 | result = !result; /* on unix, success = 0, on windows, success = !0 */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2847 | #else |
| 2848 | #ifdef HAVE_FCHDIR |
| 2849 | if (path.fd != -1) |
| 2850 | result = fchdir(path.fd); |
| 2851 | else |
| 2852 | #endif |
| 2853 | result = chdir(path.narrow); |
| 2854 | #endif |
| 2855 | Py_END_ALLOW_THREADS |
| 2856 | |
| 2857 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2858 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2859 | goto exit; |
| 2860 | } |
| 2861 | |
| 2862 | return_value = Py_None; |
| 2863 | Py_INCREF(Py_None); |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2864 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2865 | exit: |
| 2866 | path_cleanup(&path); |
| 2867 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2870 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2871 | PyDoc_STRVAR(posix_fchdir__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2872 | "fchdir(fd)\n\n\ |
| 2873 | Change to the directory of the given file descriptor. fd must be\n\ |
| 2874 | opened on a directory, not a file. Equivalent to os.chdir(fd)."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2875 | |
| 2876 | static PyObject * |
| 2877 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 2878 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2879 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2880 | } |
| 2881 | #endif /* HAVE_FCHDIR */ |
| 2882 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2883 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2884 | PyDoc_STRVAR(posix_chmod__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2885 | "chmod(path, mode, *, dir_fd=None, follow_symlinks=True)\n\n\ |
| 2886 | Change the access permissions of a file.\n\ |
| 2887 | \n\ |
| 2888 | path may always be specified as a string.\n\ |
| 2889 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2890 | If this functionality is unavailable, using it raises an exception.\n\ |
| 2891 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2892 | and path should be relative; path will then be relative to that directory.\n\ |
| 2893 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2894 | link, chmod will modify the symbolic link itself instead of the file the\n\ |
| 2895 | link points to.\n\ |
| 2896 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2897 | an open file descriptor.\n\ |
| 2898 | dir_fd and follow_symlinks may not be implemented on your platform.\n\ |
| 2899 | If they are unavailable, using them will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2900 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2901 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2902 | posix_chmod(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2903 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2904 | path_t path; |
| 2905 | int mode; |
| 2906 | int dir_fd = DEFAULT_DIR_FD; |
| 2907 | int follow_symlinks = 1; |
| 2908 | int result; |
| 2909 | PyObject *return_value = NULL; |
| 2910 | static char *keywords[] = {"path", "mode", "dir_fd", |
| 2911 | "follow_symlinks", NULL}; |
| 2912 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2913 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2914 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2915 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2916 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2917 | #ifdef HAVE_FCHMODAT |
| 2918 | int fchmodat_nofollow_unsupported = 0; |
| 2919 | #endif |
| 2920 | |
| 2921 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2922 | path.function_name = "chmod"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2923 | #ifdef HAVE_FCHMOD |
| 2924 | path.allow_fd = 1; |
| 2925 | #endif |
| 2926 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&p:chmod", keywords, |
| 2927 | path_converter, &path, |
| 2928 | &mode, |
| 2929 | #ifdef HAVE_FCHMODAT |
| 2930 | dir_fd_converter, &dir_fd, |
| 2931 | #else |
| 2932 | dir_fd_unavailable, &dir_fd, |
| 2933 | #endif |
| 2934 | &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2935 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2936 | |
| 2937 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2938 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
| 2939 | goto exit; |
| 2940 | #endif |
| 2941 | |
| 2942 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2943 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2944 | if (path.wide) |
| 2945 | attr = GetFileAttributesW(path.wide); |
| 2946 | else |
| 2947 | attr = GetFileAttributesA(path.narrow); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 2948 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2949 | result = 0; |
| 2950 | else { |
| 2951 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2952 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2953 | else |
| 2954 | attr |= FILE_ATTRIBUTE_READONLY; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2955 | if (path.wide) |
| 2956 | result = SetFileAttributesW(path.wide, attr); |
| 2957 | else |
| 2958 | result = SetFileAttributesA(path.narrow, attr); |
| 2959 | } |
| 2960 | Py_END_ALLOW_THREADS |
| 2961 | |
| 2962 | if (!result) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 2963 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2964 | goto exit; |
| 2965 | } |
| 2966 | #else /* MS_WINDOWS */ |
| 2967 | Py_BEGIN_ALLOW_THREADS |
| 2968 | #ifdef HAVE_FCHMOD |
| 2969 | if (path.fd != -1) |
| 2970 | result = fchmod(path.fd, mode); |
| 2971 | else |
| 2972 | #endif |
| 2973 | #ifdef HAVE_LCHMOD |
| 2974 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2975 | result = lchmod(path.narrow, mode); |
| 2976 | else |
| 2977 | #endif |
| 2978 | #ifdef HAVE_FCHMODAT |
| 2979 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2980 | /* |
| 2981 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2982 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2983 | * and then says it isn't implemented yet. |
| 2984 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2985 | * |
| 2986 | * Once it is supported, os.chmod will automatically |
| 2987 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2988 | * Until then, we need to be careful what exception we raise. |
| 2989 | */ |
| 2990 | result = fchmodat(dir_fd, path.narrow, mode, |
| 2991 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2992 | /* |
| 2993 | * But wait! We can't throw the exception without allowing threads, |
| 2994 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2995 | */ |
| 2996 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2997 | result && |
| 2998 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2999 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3000 | } |
| 3001 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3002 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3003 | result = chmod(path.narrow, mode); |
| 3004 | Py_END_ALLOW_THREADS |
| 3005 | |
| 3006 | if (result) { |
| 3007 | #ifdef HAVE_FCHMODAT |
| 3008 | if (fchmodat_nofollow_unsupported) { |
| 3009 | if (dir_fd != DEFAULT_DIR_FD) |
| 3010 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 3011 | dir_fd, follow_symlinks); |
| 3012 | else |
| 3013 | follow_symlinks_specified("chmod", follow_symlinks); |
| 3014 | } |
| 3015 | else |
| 3016 | #endif |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3017 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3018 | goto exit; |
| 3019 | } |
| 3020 | #endif |
| 3021 | |
| 3022 | Py_INCREF(Py_None); |
| 3023 | return_value = Py_None; |
| 3024 | exit: |
| 3025 | path_cleanup(&path); |
| 3026 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3027 | } |
| 3028 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3029 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3030 | #ifdef HAVE_FCHMOD |
| 3031 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 3032 | "fchmod(fd, mode)\n\n\ |
| 3033 | Change the access permissions of the file given by file\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3034 | descriptor fd. Equivalent to os.chmod(fd, mode)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3035 | |
| 3036 | static PyObject * |
| 3037 | posix_fchmod(PyObject *self, PyObject *args) |
| 3038 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3039 | int fd, mode, res; |
| 3040 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 3041 | return NULL; |
| 3042 | Py_BEGIN_ALLOW_THREADS |
| 3043 | res = fchmod(fd, mode); |
| 3044 | Py_END_ALLOW_THREADS |
| 3045 | if (res < 0) |
| 3046 | return posix_error(); |
| 3047 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3048 | } |
| 3049 | #endif /* HAVE_FCHMOD */ |
| 3050 | |
| 3051 | #ifdef HAVE_LCHMOD |
| 3052 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 3053 | "lchmod(path, mode)\n\n\ |
| 3054 | Change the access permissions of a file. If path is a symlink, this\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3055 | affects the link itself rather than the target.\n\ |
| 3056 | Equivalent to chmod(path, mode, follow_symlinks=False)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3057 | |
| 3058 | static PyObject * |
| 3059 | posix_lchmod(PyObject *self, PyObject *args) |
| 3060 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3061 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3062 | int i; |
| 3063 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3064 | memset(&path, 0, sizeof(path)); |
| 3065 | path.function_name = "lchmod"; |
| 3066 | if (!PyArg_ParseTuple(args, "O&i:lchmod", |
| 3067 | path_converter, &path, &i)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3068 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3069 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3070 | res = lchmod(path.narrow, i); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3071 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3072 | if (res < 0) { |
| 3073 | path_error(&path); |
| 3074 | path_cleanup(&path); |
| 3075 | return NULL; |
| 3076 | } |
| 3077 | path_cleanup(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3078 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3079 | } |
| 3080 | #endif /* HAVE_LCHMOD */ |
| 3081 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3082 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3083 | #ifdef HAVE_CHFLAGS |
| 3084 | PyDoc_STRVAR(posix_chflags__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3085 | "chflags(path, flags, *, follow_symlinks=True)\n\n\ |
| 3086 | Set file flags.\n\ |
| 3087 | \n\ |
| 3088 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 3089 | link, chflags will change flags on the symbolic link itself instead of the\n\ |
| 3090 | file the link points to.\n\ |
| 3091 | follow_symlinks may not be implemented on your platform. If it is\n\ |
| 3092 | unavailable, using it will raise a NotImplementedError."); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3093 | |
| 3094 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3095 | posix_chflags(PyObject *self, PyObject *args, PyObject *kwargs) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3096 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3097 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3098 | unsigned long flags; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3099 | int follow_symlinks = 1; |
| 3100 | int result; |
Victor Stinner | 45e9039 | 2013-07-18 23:57:35 +0200 | [diff] [blame] | 3101 | PyObject *return_value = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3102 | static char *keywords[] = {"path", "flags", "follow_symlinks", NULL}; |
| 3103 | |
| 3104 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3105 | path.function_name = "chflags"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3106 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&k|$i:chflags", keywords, |
| 3107 | path_converter, &path, |
| 3108 | &flags, &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3109 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3110 | |
| 3111 | #ifndef HAVE_LCHFLAGS |
| 3112 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
| 3113 | goto exit; |
| 3114 | #endif |
| 3115 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3116 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3117 | #ifdef HAVE_LCHFLAGS |
| 3118 | if (!follow_symlinks) |
| 3119 | result = lchflags(path.narrow, flags); |
| 3120 | else |
| 3121 | #endif |
| 3122 | result = chflags(path.narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3123 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3124 | |
| 3125 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3126 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3127 | goto exit; |
| 3128 | } |
| 3129 | |
| 3130 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3131 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3132 | |
| 3133 | exit: |
| 3134 | path_cleanup(&path); |
| 3135 | return return_value; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3136 | } |
| 3137 | #endif /* HAVE_CHFLAGS */ |
| 3138 | |
| 3139 | #ifdef HAVE_LCHFLAGS |
| 3140 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 3141 | "lchflags(path, flags)\n\n\ |
| 3142 | Set file flags.\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3143 | This function will not follow symbolic links.\n\ |
| 3144 | Equivalent to chflags(path, flags, follow_symlinks=False)."); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3145 | |
| 3146 | static PyObject * |
| 3147 | posix_lchflags(PyObject *self, PyObject *args) |
| 3148 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3149 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3150 | unsigned long flags; |
| 3151 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3152 | memset(&path, 0, sizeof(path)); |
| 3153 | path.function_name = "lchflags"; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3154 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3155 | path_converter, &path, &flags)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3156 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3157 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3158 | res = lchflags(path.narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3159 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3160 | if (res < 0) { |
| 3161 | path_error(&path); |
| 3162 | path_cleanup(&path); |
| 3163 | return NULL; |
| 3164 | } |
| 3165 | path_cleanup(&path); |
| 3166 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3167 | } |
| 3168 | #endif /* HAVE_LCHFLAGS */ |
| 3169 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3170 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3171 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3172 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3173 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3174 | |
| 3175 | static PyObject * |
| 3176 | posix_chroot(PyObject *self, PyObject *args) |
| 3177 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3178 | return posix_1str("chroot", args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3179 | } |
| 3180 | #endif |
| 3181 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3182 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3183 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3184 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3185 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3186 | |
| 3187 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 3188 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3189 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 3190 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3191 | } |
| 3192 | #endif /* HAVE_FSYNC */ |
| 3193 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3194 | #ifdef HAVE_SYNC |
| 3195 | PyDoc_STRVAR(posix_sync__doc__, |
| 3196 | "sync()\n\n\ |
| 3197 | Force write of everything to disk."); |
| 3198 | |
| 3199 | static PyObject * |
| 3200 | posix_sync(PyObject *self, PyObject *noargs) |
| 3201 | { |
| 3202 | Py_BEGIN_ALLOW_THREADS |
| 3203 | sync(); |
| 3204 | Py_END_ALLOW_THREADS |
| 3205 | Py_RETURN_NONE; |
| 3206 | } |
| 3207 | #endif |
| 3208 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3209 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3210 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3211 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3212 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3213 | #endif |
| 3214 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3215 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3216 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3217 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3218 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3219 | |
| 3220 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 3221 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3222 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 3223 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3224 | } |
| 3225 | #endif /* HAVE_FDATASYNC */ |
| 3226 | |
| 3227 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3228 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3229 | PyDoc_STRVAR(posix_chown__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3230 | "chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True)\n\n\ |
| 3231 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 3232 | \n\ |
| 3233 | path may always be specified as a string.\n\ |
| 3234 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 3235 | If this functionality is unavailable, using it raises an exception.\n\ |
| 3236 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 3237 | and path should be relative; path will then be relative to that directory.\n\ |
| 3238 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 3239 | link, chown will modify the symbolic link itself instead of the file the\n\ |
| 3240 | link points to.\n\ |
| 3241 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 3242 | an open file descriptor.\n\ |
| 3243 | dir_fd and follow_symlinks may not be implemented on your platform.\n\ |
| 3244 | If they are unavailable, using them will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3245 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3246 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3247 | posix_chown(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3248 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3249 | path_t path; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3250 | uid_t uid; |
| 3251 | gid_t gid; |
| 3252 | int dir_fd = DEFAULT_DIR_FD; |
| 3253 | int follow_symlinks = 1; |
| 3254 | int result; |
| 3255 | PyObject *return_value = NULL; |
| 3256 | static char *keywords[] = {"path", "uid", "gid", "dir_fd", |
| 3257 | "follow_symlinks", NULL}; |
| 3258 | |
| 3259 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3260 | path.function_name = "chown"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3261 | #ifdef HAVE_FCHOWN |
| 3262 | path.allow_fd = 1; |
| 3263 | #endif |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3264 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&O&|$O&p:chown", keywords, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3265 | path_converter, &path, |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3266 | _Py_Uid_Converter, &uid, |
| 3267 | _Py_Gid_Converter, &gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3268 | #ifdef HAVE_FCHOWNAT |
| 3269 | dir_fd_converter, &dir_fd, |
| 3270 | #else |
| 3271 | dir_fd_unavailable, &dir_fd, |
| 3272 | #endif |
| 3273 | &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3274 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3275 | |
| 3276 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3277 | if (follow_symlinks_specified("chown", follow_symlinks)) |
| 3278 | goto exit; |
| 3279 | #endif |
| 3280 | if (dir_fd_and_fd_invalid("chown", dir_fd, path.fd) || |
| 3281 | fd_and_follow_symlinks_invalid("chown", path.fd, follow_symlinks)) |
| 3282 | goto exit; |
| 3283 | |
| 3284 | #ifdef __APPLE__ |
| 3285 | /* |
| 3286 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3287 | * (But we still have an lchown symbol because of weak-linking.) |
| 3288 | * It doesn't have fchownat either. So there's no possibility |
| 3289 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3290 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3291 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3292 | follow_symlinks_specified("chown", follow_symlinks); |
| 3293 | goto exit; |
| 3294 | } |
| 3295 | #endif |
| 3296 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3297 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3298 | #ifdef HAVE_FCHOWN |
| 3299 | if (path.fd != -1) |
| 3300 | result = fchown(path.fd, uid, gid); |
| 3301 | else |
| 3302 | #endif |
| 3303 | #ifdef HAVE_LCHOWN |
| 3304 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 3305 | result = lchown(path.narrow, uid, gid); |
| 3306 | else |
| 3307 | #endif |
| 3308 | #ifdef HAVE_FCHOWNAT |
| 3309 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
| 3310 | result = fchownat(dir_fd, path.narrow, uid, gid, |
| 3311 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3312 | else |
| 3313 | #endif |
| 3314 | result = chown(path.narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3315 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3316 | |
| 3317 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3318 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3319 | goto exit; |
| 3320 | } |
| 3321 | |
| 3322 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3323 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3324 | |
| 3325 | exit: |
| 3326 | path_cleanup(&path); |
| 3327 | return return_value; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3328 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3329 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3330 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3331 | #ifdef HAVE_FCHOWN |
| 3332 | PyDoc_STRVAR(posix_fchown__doc__, |
| 3333 | "fchown(fd, uid, gid)\n\n\ |
| 3334 | Change the owner and group id of the file given by file descriptor\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3335 | fd to the numeric uid and gid. Equivalent to os.chown(fd, uid, gid)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3336 | |
| 3337 | static PyObject * |
| 3338 | posix_fchown(PyObject *self, PyObject *args) |
| 3339 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3340 | int fd; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3341 | uid_t uid; |
| 3342 | gid_t gid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3343 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3344 | if (!PyArg_ParseTuple(args, "iO&O&:fchown", &fd, |
| 3345 | _Py_Uid_Converter, &uid, |
| 3346 | _Py_Gid_Converter, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3347 | return NULL; |
| 3348 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3349 | res = fchown(fd, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3350 | Py_END_ALLOW_THREADS |
| 3351 | if (res < 0) |
| 3352 | return posix_error(); |
| 3353 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3354 | } |
| 3355 | #endif /* HAVE_FCHOWN */ |
| 3356 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3357 | #ifdef HAVE_LCHOWN |
| 3358 | PyDoc_STRVAR(posix_lchown__doc__, |
| 3359 | "lchown(path, uid, gid)\n\n\ |
| 3360 | Change the owner and group id of path to the numeric uid and gid.\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3361 | This function will not follow symbolic links.\n\ |
| 3362 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False)."); |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3363 | |
| 3364 | static PyObject * |
| 3365 | posix_lchown(PyObject *self, PyObject *args) |
| 3366 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3367 | path_t path; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3368 | uid_t uid; |
| 3369 | gid_t gid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3370 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3371 | memset(&path, 0, sizeof(path)); |
| 3372 | path.function_name = "lchown"; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3373 | if (!PyArg_ParseTuple(args, "O&O&O&:lchown", |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3374 | path_converter, &path, |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3375 | _Py_Uid_Converter, &uid, |
| 3376 | _Py_Gid_Converter, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3377 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3378 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | c2d0200 | 2013-02-10 22:03:08 +0200 | [diff] [blame] | 3379 | res = lchown(path.narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3380 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3381 | if (res < 0) { |
| 3382 | path_error(&path); |
| 3383 | path_cleanup(&path); |
| 3384 | return NULL; |
| 3385 | } |
| 3386 | path_cleanup(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3387 | Py_INCREF(Py_None); |
| 3388 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3389 | } |
| 3390 | #endif /* HAVE_LCHOWN */ |
| 3391 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3392 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3393 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3394 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3395 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3396 | char buf[1026]; |
| 3397 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3398 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3399 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3400 | if (!use_bytes) { |
| 3401 | wchar_t wbuf[1026]; |
| 3402 | wchar_t *wbuf2 = wbuf; |
| 3403 | PyObject *resobj; |
| 3404 | DWORD len; |
| 3405 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3406 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3407 | /* If the buffer is large enough, len does not include the |
| 3408 | terminating \0. If the buffer is too small, len includes |
| 3409 | the space needed for the terminator. */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3410 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3411 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3412 | if (wbuf2) |
| 3413 | len = GetCurrentDirectoryW(len, wbuf2); |
| 3414 | } |
| 3415 | Py_END_ALLOW_THREADS |
| 3416 | if (!wbuf2) { |
| 3417 | PyErr_NoMemory(); |
| 3418 | return NULL; |
| 3419 | } |
| 3420 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3421 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3422 | PyMem_RawFree(wbuf2); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3423 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3424 | } |
| 3425 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3426 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3427 | PyMem_RawFree(wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3428 | return resobj; |
| 3429 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3430 | |
| 3431 | if (win32_warn_bytes_api()) |
| 3432 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3433 | #endif |
| 3434 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3435 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3436 | res = getcwd(buf, sizeof buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3437 | Py_END_ALLOW_THREADS |
| 3438 | if (res == NULL) |
| 3439 | return posix_error(); |
| 3440 | if (use_bytes) |
| 3441 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 3442 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3443 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3444 | |
| 3445 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 3446 | "getcwd() -> path\n\n\ |
| 3447 | Return a unicode string representing the current working directory."); |
| 3448 | |
| 3449 | static PyObject * |
| 3450 | posix_getcwd_unicode(PyObject *self) |
| 3451 | { |
| 3452 | return posix_getcwd(0); |
| 3453 | } |
| 3454 | |
| 3455 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 3456 | "getcwdb() -> path\n\n\ |
| 3457 | Return a bytes string representing the current working directory."); |
| 3458 | |
| 3459 | static PyObject * |
| 3460 | posix_getcwd_bytes(PyObject *self) |
| 3461 | { |
| 3462 | return posix_getcwd(1); |
| 3463 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3464 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3465 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3466 | #define HAVE_LINK 1 |
| 3467 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3468 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3469 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3470 | PyDoc_STRVAR(posix_link__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3471 | "link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True)\n\n\ |
| 3472 | Create a hard link to a file.\n\ |
| 3473 | \n\ |
| 3474 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 3475 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 3476 | should be relative; the path will then be relative to that directory.\n\ |
| 3477 | If follow_symlinks is False, and the last element of src is a symbolic\n\ |
| 3478 | link, link will create a link to the symbolic link itself instead of the\n\ |
| 3479 | file the link points to.\n\ |
| 3480 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your\n\ |
| 3481 | platform. If they are unavailable, using them will raise a\n\ |
| 3482 | NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3483 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3484 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3485 | posix_link(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3486 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3487 | path_t src, dst; |
| 3488 | int src_dir_fd = DEFAULT_DIR_FD; |
| 3489 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 3490 | int follow_symlinks = 1; |
| 3491 | PyObject *return_value = NULL; |
| 3492 | static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", |
| 3493 | "follow_symlinks", NULL}; |
| 3494 | #ifdef MS_WINDOWS |
| 3495 | BOOL result; |
| 3496 | #else |
| 3497 | int result; |
| 3498 | #endif |
| 3499 | |
| 3500 | memset(&src, 0, sizeof(src)); |
| 3501 | memset(&dst, 0, sizeof(dst)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3502 | src.function_name = "link"; |
| 3503 | dst.function_name = "link"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3504 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|O&O&p:link", keywords, |
| 3505 | path_converter, &src, |
| 3506 | path_converter, &dst, |
| 3507 | dir_fd_converter, &src_dir_fd, |
| 3508 | dir_fd_converter, &dst_dir_fd, |
| 3509 | &follow_symlinks)) |
| 3510 | return NULL; |
| 3511 | |
| 3512 | #ifndef HAVE_LINKAT |
| 3513 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3514 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
| 3515 | goto exit; |
| 3516 | } |
| 3517 | #endif |
| 3518 | |
| 3519 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 3520 | PyErr_SetString(PyExc_NotImplementedError, |
| 3521 | "link: src and dst must be the same type"); |
| 3522 | goto exit; |
| 3523 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3524 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3525 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3526 | Py_BEGIN_ALLOW_THREADS |
| 3527 | if (src.wide) |
| 3528 | result = CreateHardLinkW(dst.wide, src.wide, NULL); |
| 3529 | else |
| 3530 | result = CreateHardLinkA(dst.narrow, src.narrow, NULL); |
| 3531 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3532 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3533 | if (!result) { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 3534 | return_value = path_error2(&src, &dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3535 | goto exit; |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3536 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3537 | #else |
| 3538 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3539 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3540 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3541 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3542 | (!follow_symlinks)) |
| 3543 | result = linkat(src_dir_fd, src.narrow, |
| 3544 | dst_dir_fd, dst.narrow, |
| 3545 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3546 | else |
| 3547 | #endif |
| 3548 | result = link(src.narrow, dst.narrow); |
| 3549 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3550 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3551 | if (result) { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 3552 | return_value = path_error2(&src, &dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3553 | goto exit; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3554 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3555 | #endif |
| 3556 | |
| 3557 | return_value = Py_None; |
| 3558 | Py_INCREF(Py_None); |
| 3559 | |
| 3560 | exit: |
| 3561 | path_cleanup(&src); |
| 3562 | path_cleanup(&dst); |
| 3563 | return return_value; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3564 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3565 | #endif |
| 3566 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3567 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3568 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3569 | PyDoc_STRVAR(posix_listdir__doc__, |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3570 | "listdir(path='.') -> list_of_filenames\n\n\ |
| 3571 | Return a list containing the names of the files in the directory.\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3572 | The list is in arbitrary order. It does not include the special\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3573 | entries '.' and '..' even if they are present in the directory.\n\ |
| 3574 | \n\ |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3575 | path can be specified as either str or bytes. If path is bytes,\n\ |
| 3576 | the filenames returned will also be bytes; in all other circumstances\n\ |
| 3577 | the filenames returned will be str.\n\ |
| 3578 | On some platforms, path may also be specified as an open file descriptor;\n\ |
| 3579 | the file descriptor must refer to a directory.\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3580 | If this functionality is unavailable, using it raises NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3581 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3582 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3583 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3584 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3585 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3586 | static char *keywords[] = {"path", NULL}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3587 | PyObject *v; |
| 3588 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3589 | BOOL result; |
| 3590 | WIN32_FIND_DATA FileData; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3591 | char namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3592 | char *bufptr = namebuf; |
| 3593 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3594 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3595 | PyObject *po = NULL; |
| 3596 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3597 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3598 | if (!path->narrow) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3599 | WIN32_FIND_DATAW wFileData; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3600 | wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3601 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3602 | if (!path->wide) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3603 | po_wchars = L"."; |
| 3604 | len = 1; |
| 3605 | } else { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3606 | po_wchars = path->wide; |
| 3607 | len = wcslen(path->wide); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3608 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3609 | /* The +5 is so we can append "\\*.*\0" */ |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3610 | wnamebuf = PyMem_Malloc((len + 5) * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3611 | if (!wnamebuf) { |
| 3612 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3613 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3614 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3615 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3616 | if (len > 0) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3617 | wchar_t wch = wnamebuf[len-1]; |
Tim Golden | 781bbeb | 2013-10-25 20:24:06 +0100 | [diff] [blame] | 3618 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3619 | wnamebuf[len++] = SEP; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3620 | wcscpy(wnamebuf + len, L"*.*"); |
| 3621 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3622 | if ((list = PyList_New(0)) == NULL) { |
| 3623 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3624 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3625 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3626 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3627 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3628 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3629 | int error = GetLastError(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3630 | if (error == ERROR_FILE_NOT_FOUND) |
| 3631 | goto exit; |
| 3632 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3633 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3634 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3635 | } |
| 3636 | do { |
| 3637 | /* Skip over . and .. */ |
| 3638 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3639 | wcscmp(wFileData.cFileName, L"..") != 0) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3640 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3641 | wcslen(wFileData.cFileName)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3642 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3643 | Py_DECREF(list); |
| 3644 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3645 | break; |
| 3646 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3647 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3648 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3649 | Py_DECREF(list); |
| 3650 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3651 | break; |
| 3652 | } |
| 3653 | Py_DECREF(v); |
| 3654 | } |
| 3655 | Py_BEGIN_ALLOW_THREADS |
| 3656 | result = FindNextFileW(hFindFile, &wFileData); |
| 3657 | Py_END_ALLOW_THREADS |
| 3658 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3659 | it got to the end of the directory. */ |
| 3660 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3661 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3662 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3663 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3664 | } |
| 3665 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3666 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3667 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3668 | } |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3669 | strcpy(namebuf, path->narrow); |
| 3670 | len = path->length; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3671 | if (len > 0) { |
| 3672 | char ch = namebuf[len-1]; |
Tim Golden | 781bbeb | 2013-10-25 20:24:06 +0100 | [diff] [blame] | 3673 | if (ch != '\\' && ch != '/' && ch != ':') |
| 3674 | namebuf[len++] = '\\'; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3675 | strcpy(namebuf + len, "*.*"); |
| 3676 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3677 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3678 | if ((list = PyList_New(0)) == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3679 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3680 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3681 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3682 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3683 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3684 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3685 | int error = GetLastError(); |
| 3686 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3687 | goto exit; |
| 3688 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3689 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3690 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3691 | } |
| 3692 | do { |
| 3693 | /* Skip over . and .. */ |
| 3694 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 3695 | strcmp(FileData.cFileName, "..") != 0) { |
| 3696 | v = PyBytes_FromString(FileData.cFileName); |
| 3697 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3698 | Py_DECREF(list); |
| 3699 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3700 | break; |
| 3701 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3702 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3703 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3704 | Py_DECREF(list); |
| 3705 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3706 | break; |
| 3707 | } |
| 3708 | Py_DECREF(v); |
| 3709 | } |
| 3710 | Py_BEGIN_ALLOW_THREADS |
| 3711 | result = FindNextFile(hFindFile, &FileData); |
| 3712 | Py_END_ALLOW_THREADS |
| 3713 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3714 | it got to the end of the directory. */ |
| 3715 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3716 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3717 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3718 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3719 | } |
| 3720 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3721 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3722 | exit: |
| 3723 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3724 | if (FindClose(hFindFile) == FALSE) { |
| 3725 | if (list != NULL) { |
| 3726 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3727 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3728 | } |
| 3729 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3730 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3731 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3732 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3733 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3734 | } /* end of _listdir_windows_no_opendir */ |
| 3735 | |
| 3736 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3737 | |
| 3738 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3739 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3740 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3741 | PyObject *v; |
| 3742 | DIR *dirp = NULL; |
| 3743 | struct dirent *ep; |
| 3744 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3745 | #ifdef HAVE_FDOPENDIR |
| 3746 | int fd = -1; |
| 3747 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3748 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3749 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3750 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3751 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3752 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3753 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3754 | if (fd == -1) |
| 3755 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3756 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3757 | return_str = 1; |
| 3758 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3759 | Py_BEGIN_ALLOW_THREADS |
| 3760 | dirp = fdopendir(fd); |
| 3761 | Py_END_ALLOW_THREADS |
| 3762 | } |
| 3763 | else |
| 3764 | #endif |
| 3765 | { |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3766 | char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3767 | if (path->narrow) { |
| 3768 | name = path->narrow; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3769 | /* only return bytes if they specified a bytes object */ |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3770 | return_str = !(PyBytes_Check(path->object)); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3771 | } |
| 3772 | else { |
| 3773 | name = "."; |
| 3774 | return_str = 1; |
| 3775 | } |
| 3776 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3777 | Py_BEGIN_ALLOW_THREADS |
| 3778 | dirp = opendir(name); |
| 3779 | Py_END_ALLOW_THREADS |
| 3780 | } |
| 3781 | |
| 3782 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3783 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3784 | #ifdef HAVE_FDOPENDIR |
| 3785 | if (fd != -1) { |
| 3786 | Py_BEGIN_ALLOW_THREADS |
| 3787 | close(fd); |
| 3788 | Py_END_ALLOW_THREADS |
| 3789 | } |
| 3790 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3791 | goto exit; |
| 3792 | } |
| 3793 | if ((list = PyList_New(0)) == NULL) { |
| 3794 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3795 | } |
| 3796 | for (;;) { |
| 3797 | errno = 0; |
| 3798 | Py_BEGIN_ALLOW_THREADS |
| 3799 | ep = readdir(dirp); |
| 3800 | Py_END_ALLOW_THREADS |
| 3801 | if (ep == NULL) { |
| 3802 | if (errno == 0) { |
| 3803 | break; |
| 3804 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3805 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3806 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3807 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3808 | } |
| 3809 | } |
| 3810 | if (ep->d_name[0] == '.' && |
| 3811 | (NAMLEN(ep) == 1 || |
| 3812 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3813 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3814 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3815 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3816 | else |
| 3817 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3818 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3819 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3820 | break; |
| 3821 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3822 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3823 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3824 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3825 | break; |
| 3826 | } |
| 3827 | Py_DECREF(v); |
| 3828 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3829 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3830 | exit: |
| 3831 | if (dirp != NULL) { |
| 3832 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3833 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3834 | if (fd > -1) |
| 3835 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3836 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3837 | closedir(dirp); |
| 3838 | Py_END_ALLOW_THREADS |
| 3839 | } |
| 3840 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3841 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3842 | } /* end of _posix_listdir */ |
| 3843 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3844 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3845 | static PyObject * |
| 3846 | posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs) |
| 3847 | { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3848 | path_t path; |
| 3849 | PyObject *list = NULL; |
| 3850 | static char *keywords[] = {"path", NULL}; |
| 3851 | PyObject *return_value; |
| 3852 | |
| 3853 | memset(&path, 0, sizeof(path)); |
| 3854 | path.function_name = "listdir"; |
| 3855 | path.nullable = 1; |
| 3856 | #ifdef HAVE_FDOPENDIR |
| 3857 | path.allow_fd = 1; |
| 3858 | path.fd = -1; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3859 | #endif |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3860 | |
| 3861 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:listdir", keywords, |
| 3862 | path_converter, &path)) { |
| 3863 | return NULL; |
| 3864 | } |
| 3865 | |
| 3866 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3867 | return_value = _listdir_windows_no_opendir(&path, list); |
| 3868 | #else |
| 3869 | return_value = _posix_listdir(&path, list); |
| 3870 | #endif |
| 3871 | path_cleanup(&path); |
| 3872 | return return_value; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3873 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3874 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3875 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3876 | /* A helper function for abspath on win32 */ |
| 3877 | static PyObject * |
| 3878 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 3879 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3880 | const char *path; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3881 | char outbuf[MAX_PATH]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3882 | char *temp; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3883 | PyObject *po; |
| 3884 | |
| 3885 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) |
| 3886 | { |
| 3887 | wchar_t *wpath; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3888 | wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3889 | wchar_t *wtemp; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3890 | DWORD result; |
| 3891 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3892 | |
| 3893 | wpath = PyUnicode_AsUnicode(po); |
| 3894 | if (wpath == NULL) |
| 3895 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3896 | result = GetFullPathNameW(wpath, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3897 | Py_ARRAY_LENGTH(woutbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3898 | woutbuf, &wtemp); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3899 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3900 | woutbufp = PyMem_Malloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3901 | if (!woutbufp) |
| 3902 | return PyErr_NoMemory(); |
| 3903 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 3904 | } |
| 3905 | if (result) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3906 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3907 | else |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3908 | v = win32_error_object("GetFullPathNameW", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3909 | if (woutbufp != woutbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3910 | PyMem_Free(woutbufp); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3911 | return v; |
| 3912 | } |
| 3913 | /* Drop the argument parsing error as narrow strings |
| 3914 | are also valid. */ |
| 3915 | PyErr_Clear(); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3916 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3917 | if (!PyArg_ParseTuple (args, "y:_getfullpathname", |
| 3918 | &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3919 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3920 | if (win32_warn_bytes_api()) |
| 3921 | return NULL; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3922 | if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3923 | outbuf, &temp)) { |
| 3924 | win32_error("GetFullPathName", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3925 | return NULL; |
| 3926 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3927 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 3928 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 3929 | Py_FileSystemDefaultEncoding, NULL); |
| 3930 | } |
| 3931 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3932 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3933 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3934 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3935 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3936 | /* A helper function for samepath on windows */ |
| 3937 | static PyObject * |
| 3938 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 3939 | { |
| 3940 | HANDLE hFile; |
| 3941 | int buf_size; |
| 3942 | wchar_t *target_path; |
| 3943 | int result_length; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3944 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3945 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3946 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3947 | if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po)) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3948 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3949 | path = PyUnicode_AsUnicode(po); |
| 3950 | if (path == NULL) |
| 3951 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3952 | |
| 3953 | if(!check_GetFinalPathNameByHandle()) { |
| 3954 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 3955 | NotImplementedError. */ |
| 3956 | return PyErr_Format(PyExc_NotImplementedError, |
| 3957 | "GetFinalPathNameByHandle not available on this platform"); |
| 3958 | } |
| 3959 | |
| 3960 | hFile = CreateFileW( |
| 3961 | path, |
| 3962 | 0, /* desired access */ |
| 3963 | 0, /* share mode */ |
| 3964 | NULL, /* security attributes */ |
| 3965 | OPEN_EXISTING, |
| 3966 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3967 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3968 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3969 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3970 | if(hFile == INVALID_HANDLE_VALUE) |
| 3971 | return win32_error_object("CreateFileW", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3972 | |
| 3973 | /* We have a good handle to the target, use it to determine the |
| 3974 | target path name. */ |
| 3975 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 3976 | |
| 3977 | if(!buf_size) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3978 | return win32_error_object("GetFinalPathNameByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3979 | |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3980 | target_path = (wchar_t *)PyMem_Malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3981 | if(!target_path) |
| 3982 | return PyErr_NoMemory(); |
| 3983 | |
| 3984 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 3985 | buf_size, VOLUME_NAME_DOS); |
| 3986 | if(!result_length) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3987 | return win32_error_object("GetFinalPathNamyByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3988 | |
| 3989 | if(!CloseHandle(hFile)) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3990 | return win32_error_object("CloseHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3991 | |
| 3992 | target_path[result_length] = 0; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3993 | result = PyUnicode_FromWideChar(target_path, result_length); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3994 | PyMem_Free(target_path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3995 | return result; |
| 3996 | |
| 3997 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3998 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3999 | PyDoc_STRVAR(posix__isdir__doc__, |
| 4000 | "Return true if the pathname refers to an existing directory."); |
| 4001 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 4002 | static PyObject * |
| 4003 | posix__isdir(PyObject *self, PyObject *args) |
| 4004 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 4005 | const char *path; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4006 | PyObject *po; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 4007 | DWORD attributes; |
| 4008 | |
| 4009 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4010 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 4011 | if (wpath == NULL) |
| 4012 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 4013 | |
| 4014 | attributes = GetFileAttributesW(wpath); |
| 4015 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 4016 | Py_RETURN_FALSE; |
| 4017 | goto check; |
| 4018 | } |
| 4019 | /* Drop the argument parsing error as narrow strings |
| 4020 | are also valid. */ |
| 4021 | PyErr_Clear(); |
| 4022 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 4023 | if (!PyArg_ParseTuple(args, "y:_isdir", &path)) |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 4024 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 4025 | if (win32_warn_bytes_api()) |
| 4026 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 4027 | attributes = GetFileAttributesA(path); |
| 4028 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 4029 | Py_RETURN_FALSE; |
| 4030 | |
| 4031 | check: |
| 4032 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 4033 | Py_RETURN_TRUE; |
| 4034 | else |
| 4035 | Py_RETURN_FALSE; |
| 4036 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4037 | |
| 4038 | PyDoc_STRVAR(posix__getvolumepathname__doc__, |
| 4039 | "Return volume mount point of the specified path."); |
| 4040 | |
| 4041 | /* A helper function for ismount on windows */ |
| 4042 | static PyObject * |
| 4043 | posix__getvolumepathname(PyObject *self, PyObject *args) |
| 4044 | { |
| 4045 | PyObject *po, *result; |
| 4046 | wchar_t *path, *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4047 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4048 | BOOL ret; |
| 4049 | |
| 4050 | if (!PyArg_ParseTuple(args, "U|:_getvolumepathname", &po)) |
| 4051 | return NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4052 | path = PyUnicode_AsUnicodeAndSize(po, &buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4053 | if (path == NULL) |
| 4054 | return NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4055 | buflen += 1; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4056 | |
| 4057 | /* Volume path should be shorter than entire path */ |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4058 | buflen = Py_MAX(buflen, MAX_PATH); |
| 4059 | |
| 4060 | if (buflen > DWORD_MAX) { |
| 4061 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 4062 | return NULL; |
| 4063 | } |
| 4064 | |
| 4065 | mountpath = (wchar_t *)PyMem_Malloc(buflen * sizeof(wchar_t)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4066 | if (mountpath == NULL) |
| 4067 | return PyErr_NoMemory(); |
| 4068 | |
| 4069 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | 85ba92a | 2013-11-18 10:30:42 +0100 | [diff] [blame] | 4070 | ret = GetVolumePathNameW(path, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4071 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4072 | Py_END_ALLOW_THREADS |
| 4073 | |
| 4074 | if (!ret) { |
| 4075 | result = win32_error_object("_getvolumepathname", po); |
| 4076 | goto exit; |
| 4077 | } |
| 4078 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
| 4079 | |
| 4080 | exit: |
| 4081 | PyMem_Free(mountpath); |
| 4082 | return result; |
| 4083 | } |
| 4084 | /* end of posix__getvolumepathname */ |
| 4085 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4086 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4087 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4088 | PyDoc_STRVAR(posix_mkdir__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4089 | "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 4090 | Create a directory.\n\ |
| 4091 | \n\ |
| 4092 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4093 | and path should be relative; path will then be relative to that directory.\n\ |
| 4094 | dir_fd may not be implemented on your platform.\n\ |
| 4095 | If it is unavailable, using it will raise a NotImplementedError.\n\ |
| 4096 | \n\ |
| 4097 | The mode argument is ignored on Windows."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4098 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4099 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4100 | posix_mkdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4101 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4102 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4103 | int mode = 0777; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4104 | int dir_fd = DEFAULT_DIR_FD; |
| 4105 | static char *keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 4106 | PyObject *return_value = NULL; |
| 4107 | int result; |
| 4108 | |
| 4109 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4110 | path.function_name = "mkdir"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4111 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkdir", keywords, |
| 4112 | path_converter, &path, &mode, |
| 4113 | #ifdef HAVE_MKDIRAT |
| 4114 | dir_fd_converter, &dir_fd |
| 4115 | #else |
| 4116 | dir_fd_unavailable, &dir_fd |
| 4117 | #endif |
| 4118 | )) |
| 4119 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4120 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4121 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4122 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4123 | if (path.wide) |
| 4124 | result = CreateDirectoryW(path.wide, NULL); |
| 4125 | else |
| 4126 | result = CreateDirectoryA(path.narrow, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4127 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4128 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4129 | if (!result) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4130 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4131 | goto exit; |
| 4132 | } |
| 4133 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4134 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4135 | #if HAVE_MKDIRAT |
| 4136 | if (dir_fd != DEFAULT_DIR_FD) |
| 4137 | result = mkdirat(dir_fd, path.narrow, mode); |
| 4138 | else |
| 4139 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4140 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4141 | result = mkdir(path.narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4142 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4143 | result = mkdir(path.narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4144 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4145 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4146 | if (result < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4147 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4148 | goto exit; |
| 4149 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4150 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4151 | return_value = Py_None; |
| 4152 | Py_INCREF(Py_None); |
| 4153 | exit: |
| 4154 | path_cleanup(&path); |
| 4155 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4156 | } |
| 4157 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4158 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4159 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 4160 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4161 | #include <sys/resource.h> |
| 4162 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4163 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4164 | |
| 4165 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4166 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4167 | "nice(inc) -> new_priority\n\n\ |
| 4168 | Decrease the priority of process by inc and return the new priority."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4169 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4170 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4171 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4172 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4173 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4174 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4175 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 4176 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4177 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4178 | /* There are two flavours of 'nice': one that returns the new |
| 4179 | priority (as required by almost all standards out there) and the |
| 4180 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 4181 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4182 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4183 | If we are of the nice family that returns the new priority, we |
| 4184 | need to clear errno before the call, and check if errno is filled |
| 4185 | before calling posix_error() on a returnvalue of -1, because the |
| 4186 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4187 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4188 | errno = 0; |
| 4189 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4190 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4191 | if (value == 0) |
| 4192 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4193 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4194 | if (value == -1 && errno != 0) |
| 4195 | /* either nice() or getpriority() returned an error */ |
| 4196 | return posix_error(); |
| 4197 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4198 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4199 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4200 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4201 | |
| 4202 | #ifdef HAVE_GETPRIORITY |
| 4203 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 4204 | "getpriority(which, who) -> current_priority\n\n\ |
| 4205 | Get program scheduling priority."); |
| 4206 | |
| 4207 | static PyObject * |
| 4208 | posix_getpriority(PyObject *self, PyObject *args) |
| 4209 | { |
| 4210 | int which, who, retval; |
| 4211 | |
| 4212 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 4213 | return NULL; |
| 4214 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4215 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4216 | if (errno != 0) |
| 4217 | return posix_error(); |
| 4218 | return PyLong_FromLong((long)retval); |
| 4219 | } |
| 4220 | #endif /* HAVE_GETPRIORITY */ |
| 4221 | |
| 4222 | |
| 4223 | #ifdef HAVE_SETPRIORITY |
| 4224 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 4225 | "setpriority(which, who, prio) -> None\n\n\ |
| 4226 | Set program scheduling priority."); |
| 4227 | |
| 4228 | static PyObject * |
| 4229 | posix_setpriority(PyObject *self, PyObject *args) |
| 4230 | { |
| 4231 | int which, who, prio, retval; |
| 4232 | |
| 4233 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 4234 | return NULL; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4235 | retval = setpriority(which, who, prio); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4236 | if (retval == -1) |
| 4237 | return posix_error(); |
| 4238 | Py_RETURN_NONE; |
| 4239 | } |
| 4240 | #endif /* HAVE_SETPRIORITY */ |
| 4241 | |
| 4242 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4243 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4244 | internal_rename(PyObject *args, PyObject *kwargs, int is_replace) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4245 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4246 | char *function_name = is_replace ? "replace" : "rename"; |
| 4247 | path_t src; |
| 4248 | path_t dst; |
| 4249 | int src_dir_fd = DEFAULT_DIR_FD; |
| 4250 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 4251 | int dir_fd_specified; |
| 4252 | PyObject *return_value = NULL; |
| 4253 | char format[24]; |
| 4254 | static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL}; |
| 4255 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4256 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4257 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4258 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4259 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4260 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4261 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4262 | |
| 4263 | memset(&src, 0, sizeof(src)); |
| 4264 | memset(&dst, 0, sizeof(dst)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4265 | src.function_name = function_name; |
| 4266 | dst.function_name = function_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4267 | strcpy(format, "O&O&|$O&O&:"); |
| 4268 | strcat(format, function_name); |
| 4269 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, keywords, |
| 4270 | path_converter, &src, |
| 4271 | path_converter, &dst, |
| 4272 | dir_fd_converter, &src_dir_fd, |
| 4273 | dir_fd_converter, &dst_dir_fd)) |
| 4274 | return NULL; |
| 4275 | |
| 4276 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4277 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4278 | #ifndef HAVE_RENAMEAT |
| 4279 | if (dir_fd_specified) { |
| 4280 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
| 4281 | goto exit; |
| 4282 | } |
| 4283 | #endif |
| 4284 | |
| 4285 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 4286 | PyErr_Format(PyExc_ValueError, |
| 4287 | "%s: src and dst must be the same type", function_name); |
| 4288 | goto exit; |
| 4289 | } |
| 4290 | |
| 4291 | #ifdef MS_WINDOWS |
| 4292 | Py_BEGIN_ALLOW_THREADS |
| 4293 | if (src.wide) |
| 4294 | result = MoveFileExW(src.wide, dst.wide, flags); |
| 4295 | else |
| 4296 | result = MoveFileExA(src.narrow, dst.narrow, flags); |
| 4297 | Py_END_ALLOW_THREADS |
| 4298 | |
| 4299 | if (!result) { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 4300 | return_value = path_error2(&src, &dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4301 | goto exit; |
| 4302 | } |
| 4303 | |
| 4304 | #else |
| 4305 | Py_BEGIN_ALLOW_THREADS |
| 4306 | #ifdef HAVE_RENAMEAT |
| 4307 | if (dir_fd_specified) |
| 4308 | result = renameat(src_dir_fd, src.narrow, dst_dir_fd, dst.narrow); |
| 4309 | else |
| 4310 | #endif |
| 4311 | result = rename(src.narrow, dst.narrow); |
| 4312 | Py_END_ALLOW_THREADS |
| 4313 | |
| 4314 | if (result) { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 4315 | return_value = path_error2(&src, &dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4316 | goto exit; |
| 4317 | } |
| 4318 | #endif |
| 4319 | |
| 4320 | Py_INCREF(Py_None); |
| 4321 | return_value = Py_None; |
| 4322 | exit: |
| 4323 | path_cleanup(&src); |
| 4324 | path_cleanup(&dst); |
| 4325 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4326 | } |
| 4327 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4328 | PyDoc_STRVAR(posix_rename__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4329 | "rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\ |
| 4330 | Rename a file or directory.\n\ |
| 4331 | \n\ |
| 4332 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 4333 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 4334 | should be relative; the path will then be relative to that directory.\n\ |
| 4335 | src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\ |
| 4336 | If they are unavailable, using them will raise a NotImplementedError."); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4337 | |
| 4338 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4339 | posix_rename(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4340 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4341 | return internal_rename(args, kwargs, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4342 | } |
| 4343 | |
| 4344 | PyDoc_STRVAR(posix_replace__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4345 | "replace(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\ |
| 4346 | Rename a file or directory, overwriting the destination.\n\ |
| 4347 | \n\ |
| 4348 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 4349 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 4350 | should be relative; the path will then be relative to that directory.\n\ |
| 4351 | src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\ |
| 4352 | If they are unavailable, using them will raise a NotImplementedError."); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4353 | |
| 4354 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4355 | posix_replace(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4356 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4357 | return internal_rename(args, kwargs, 1); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4358 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4359 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4360 | PyDoc_STRVAR(posix_rmdir__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4361 | "rmdir(path, *, dir_fd=None)\n\n\ |
| 4362 | Remove a directory.\n\ |
| 4363 | \n\ |
| 4364 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4365 | and path should be relative; path will then be relative to that directory.\n\ |
| 4366 | dir_fd may not be implemented on your platform.\n\ |
| 4367 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4368 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4369 | static PyObject * |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4370 | posix_rmdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4371 | { |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4372 | path_t path; |
| 4373 | int dir_fd = DEFAULT_DIR_FD; |
| 4374 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 4375 | int result; |
| 4376 | PyObject *return_value = NULL; |
| 4377 | |
| 4378 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4379 | path.function_name = "rmdir"; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4380 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:rmdir", keywords, |
| 4381 | path_converter, &path, |
| 4382 | #ifdef HAVE_UNLINKAT |
| 4383 | dir_fd_converter, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4384 | #else |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4385 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4386 | #endif |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4387 | )) |
| 4388 | return NULL; |
| 4389 | |
| 4390 | Py_BEGIN_ALLOW_THREADS |
| 4391 | #ifdef MS_WINDOWS |
| 4392 | if (path.wide) |
| 4393 | result = RemoveDirectoryW(path.wide); |
| 4394 | else |
| 4395 | result = RemoveDirectoryA(path.narrow); |
| 4396 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4397 | #else |
| 4398 | #ifdef HAVE_UNLINKAT |
| 4399 | if (dir_fd != DEFAULT_DIR_FD) |
| 4400 | result = unlinkat(dir_fd, path.narrow, AT_REMOVEDIR); |
| 4401 | else |
| 4402 | #endif |
| 4403 | result = rmdir(path.narrow); |
| 4404 | #endif |
| 4405 | Py_END_ALLOW_THREADS |
| 4406 | |
| 4407 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4408 | return_value = path_error(&path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4409 | goto exit; |
| 4410 | } |
| 4411 | |
| 4412 | return_value = Py_None; |
| 4413 | Py_INCREF(Py_None); |
| 4414 | |
| 4415 | exit: |
| 4416 | path_cleanup(&path); |
| 4417 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4420 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4421 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4422 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4423 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4424 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4425 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4426 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4427 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4428 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4429 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 4430 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4431 | wchar_t *command; |
| 4432 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 4433 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4434 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4435 | Py_BEGIN_ALLOW_THREADS |
| 4436 | sts = _wsystem(command); |
| 4437 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4438 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4439 | PyObject *command_obj; |
| 4440 | char *command; |
| 4441 | if (!PyArg_ParseTuple(args, "O&:system", |
| 4442 | PyUnicode_FSConverter, &command_obj)) |
| 4443 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4444 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4445 | command = PyBytes_AsString(command_obj); |
| 4446 | Py_BEGIN_ALLOW_THREADS |
| 4447 | sts = system(command); |
| 4448 | Py_END_ALLOW_THREADS |
| 4449 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4450 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4451 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4452 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4453 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4454 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4455 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4456 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4457 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4458 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4459 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4460 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4461 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4462 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4463 | int i; |
| 4464 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 4465 | return NULL; |
| 4466 | i = (int)umask(i); |
| 4467 | if (i < 0) |
| 4468 | return posix_error(); |
| 4469 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4470 | } |
| 4471 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4472 | #ifdef MS_WINDOWS |
| 4473 | |
| 4474 | /* override the default DeleteFileW behavior so that directory |
| 4475 | symlinks can be removed with this function, the same as with |
| 4476 | Unix symlinks */ |
| 4477 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4478 | { |
| 4479 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4480 | WIN32_FIND_DATAW find_data; |
| 4481 | HANDLE find_data_handle; |
| 4482 | int is_directory = 0; |
| 4483 | int is_link = 0; |
| 4484 | |
| 4485 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4486 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4487 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4488 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4489 | it is a symlink */ |
| 4490 | if(is_directory && |
| 4491 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4492 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4493 | |
| 4494 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
| 4495 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; |
| 4496 | FindClose(find_data_handle); |
| 4497 | } |
| 4498 | } |
| 4499 | } |
| 4500 | |
| 4501 | if (is_directory && is_link) |
| 4502 | return RemoveDirectoryW(lpFileName); |
| 4503 | |
| 4504 | return DeleteFileW(lpFileName); |
| 4505 | } |
| 4506 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4507 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4508 | PyDoc_STRVAR(posix_unlink__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4509 | "unlink(path, *, dir_fd=None)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4510 | Remove a file (same as remove()).\n\ |
| 4511 | \n\ |
| 4512 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4513 | and path should be relative; path will then be relative to that directory.\n\ |
| 4514 | dir_fd may not be implemented on your platform.\n\ |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4515 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4516 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4517 | PyDoc_STRVAR(posix_remove__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4518 | "remove(path, *, dir_fd=None)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4519 | Remove a file (same as unlink()).\n\ |
| 4520 | \n\ |
| 4521 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4522 | and path should be relative; path will then be relative to that directory.\n\ |
| 4523 | dir_fd may not be implemented on your platform.\n\ |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4524 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4525 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4526 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4527 | posix_unlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4528 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4529 | path_t path; |
| 4530 | int dir_fd = DEFAULT_DIR_FD; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4531 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4532 | int result; |
| 4533 | PyObject *return_value = NULL; |
| 4534 | |
| 4535 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4536 | path.function_name = "unlink"; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4537 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:unlink", keywords, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4538 | path_converter, &path, |
| 4539 | #ifdef HAVE_UNLINKAT |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4540 | dir_fd_converter, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4541 | #else |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4542 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4543 | #endif |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4544 | )) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4545 | return NULL; |
| 4546 | |
| 4547 | Py_BEGIN_ALLOW_THREADS |
| 4548 | #ifdef MS_WINDOWS |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4549 | if (path.wide) |
| 4550 | result = Py_DeleteFileW(path.wide); |
| 4551 | else |
| 4552 | result = DeleteFileA(path.narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4553 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4554 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4555 | #ifdef HAVE_UNLINKAT |
| 4556 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4557 | result = unlinkat(dir_fd, path.narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4558 | else |
| 4559 | #endif /* HAVE_UNLINKAT */ |
| 4560 | result = unlink(path.narrow); |
| 4561 | #endif |
| 4562 | Py_END_ALLOW_THREADS |
| 4563 | |
| 4564 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4565 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4566 | goto exit; |
| 4567 | } |
| 4568 | |
| 4569 | return_value = Py_None; |
| 4570 | Py_INCREF(Py_None); |
| 4571 | |
| 4572 | exit: |
| 4573 | path_cleanup(&path); |
| 4574 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4575 | } |
| 4576 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4577 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4578 | PyDoc_STRVAR(posix_uname__doc__, |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4579 | "uname() -> uname_result\n\n\ |
| 4580 | Return an object identifying the current operating system.\n\ |
| 4581 | The object behaves like a named tuple with the following fields:\n\ |
| 4582 | (sysname, nodename, release, version, machine)"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4583 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4584 | static PyStructSequence_Field uname_result_fields[] = { |
| 4585 | {"sysname", "operating system name"}, |
| 4586 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4587 | {"release", "operating system release"}, |
| 4588 | {"version", "operating system version"}, |
| 4589 | {"machine", "hardware identifier"}, |
| 4590 | {NULL} |
| 4591 | }; |
| 4592 | |
| 4593 | PyDoc_STRVAR(uname_result__doc__, |
| 4594 | "uname_result: Result from os.uname().\n\n\ |
| 4595 | This object may be accessed either as a tuple of\n\ |
| 4596 | (sysname, nodename, release, version, machine),\n\ |
| 4597 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4598 | \n\ |
| 4599 | See os.uname for more information."); |
| 4600 | |
| 4601 | static PyStructSequence_Desc uname_result_desc = { |
| 4602 | "uname_result", /* name */ |
| 4603 | uname_result__doc__, /* doc */ |
| 4604 | uname_result_fields, |
| 4605 | 5 |
| 4606 | }; |
| 4607 | |
| 4608 | static PyTypeObject UnameResultType; |
| 4609 | |
| 4610 | |
| 4611 | #ifdef HAVE_UNAME |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4612 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4613 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4614 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4615 | struct utsname u; |
| 4616 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4617 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4618 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4619 | Py_BEGIN_ALLOW_THREADS |
| 4620 | res = uname(&u); |
| 4621 | Py_END_ALLOW_THREADS |
| 4622 | if (res < 0) |
| 4623 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4624 | |
| 4625 | value = PyStructSequence_New(&UnameResultType); |
| 4626 | if (value == NULL) |
| 4627 | return NULL; |
| 4628 | |
| 4629 | #define SET(i, field) \ |
| 4630 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4631 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4632 | if (!o) { \ |
| 4633 | Py_DECREF(value); \ |
| 4634 | return NULL; \ |
| 4635 | } \ |
| 4636 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4637 | } \ |
| 4638 | |
| 4639 | SET(0, u.sysname); |
| 4640 | SET(1, u.nodename); |
| 4641 | SET(2, u.release); |
| 4642 | SET(3, u.version); |
| 4643 | SET(4, u.machine); |
| 4644 | |
| 4645 | #undef SET |
| 4646 | |
| 4647 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4648 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4649 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4650 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4651 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4652 | PyDoc_STRVAR(posix_utime__doc__, |
| 4653 | "utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True)\n\ |
| 4654 | Set the access and modified time of path.\n\ |
| 4655 | \n\ |
| 4656 | path may always be specified as a string.\n\ |
| 4657 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 4658 | If this functionality is unavailable, using it raises an exception.\n\ |
| 4659 | \n\ |
| 4660 | If times is not None, it must be a tuple (atime, mtime);\n\ |
| 4661 | atime and mtime should be expressed as float seconds since the epoch.\n\ |
| 4662 | If ns is not None, it must be a tuple (atime_ns, mtime_ns);\n\ |
| 4663 | atime_ns and mtime_ns should be expressed as integer nanoseconds\n\ |
| 4664 | since the epoch.\n\ |
| 4665 | If both times and ns are None, utime uses the current time.\n\ |
| 4666 | Specifying tuples for both times and ns is an error.\n\ |
| 4667 | \n\ |
| 4668 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4669 | and path should be relative; path will then be relative to that directory.\n\ |
| 4670 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 4671 | link, utime will modify the symbolic link itself instead of the file the\n\ |
| 4672 | link points to.\n\ |
| 4673 | It is an error to use dir_fd or follow_symlinks when specifying path\n\ |
| 4674 | as an open file descriptor.\n\ |
| 4675 | dir_fd and follow_symlinks may not be available on your platform.\n\ |
| 4676 | If they are unavailable, using them will raise a NotImplementedError."); |
| 4677 | |
| 4678 | typedef struct { |
| 4679 | int now; |
| 4680 | time_t atime_s; |
| 4681 | long atime_ns; |
| 4682 | time_t mtime_s; |
| 4683 | long mtime_ns; |
| 4684 | } utime_t; |
| 4685 | |
| 4686 | /* |
| 4687 | * these macros assume that "utime" is a pointer to a utime_t |
| 4688 | * they also intentionally leak the declaration of a pointer named "time" |
| 4689 | */ |
| 4690 | #define UTIME_TO_TIMESPEC \ |
| 4691 | struct timespec ts[2]; \ |
| 4692 | struct timespec *time; \ |
| 4693 | if (utime->now) \ |
| 4694 | time = NULL; \ |
| 4695 | else { \ |
| 4696 | ts[0].tv_sec = utime->atime_s; \ |
| 4697 | ts[0].tv_nsec = utime->atime_ns; \ |
| 4698 | ts[1].tv_sec = utime->mtime_s; \ |
| 4699 | ts[1].tv_nsec = utime->mtime_ns; \ |
| 4700 | time = ts; \ |
| 4701 | } \ |
| 4702 | |
| 4703 | #define UTIME_TO_TIMEVAL \ |
| 4704 | struct timeval tv[2]; \ |
| 4705 | struct timeval *time; \ |
| 4706 | if (utime->now) \ |
| 4707 | time = NULL; \ |
| 4708 | else { \ |
| 4709 | tv[0].tv_sec = utime->atime_s; \ |
| 4710 | tv[0].tv_usec = utime->atime_ns / 1000; \ |
| 4711 | tv[1].tv_sec = utime->mtime_s; \ |
| 4712 | tv[1].tv_usec = utime->mtime_ns / 1000; \ |
| 4713 | time = tv; \ |
| 4714 | } \ |
| 4715 | |
| 4716 | #define UTIME_TO_UTIMBUF \ |
| 4717 | struct utimbuf u[2]; \ |
| 4718 | struct utimbuf *time; \ |
| 4719 | if (utime->now) \ |
| 4720 | time = NULL; \ |
| 4721 | else { \ |
| 4722 | u.actime = utime->atime_s; \ |
| 4723 | u.modtime = utime->mtime_s; \ |
| 4724 | time = u; \ |
| 4725 | } |
| 4726 | |
| 4727 | #define UTIME_TO_TIME_T \ |
| 4728 | time_t timet[2]; \ |
| 4729 | struct timet time; \ |
| 4730 | if (utime->now) \ |
| 4731 | time = NULL; \ |
| 4732 | else { \ |
| 4733 | timet[0] = utime->atime_s; \ |
| 4734 | timet[1] = utime->mtime_s; \ |
| 4735 | time = &timet; \ |
| 4736 | } \ |
| 4737 | |
| 4738 | |
| 4739 | #define UTIME_HAVE_DIR_FD (defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)) |
| 4740 | |
| 4741 | #if UTIME_HAVE_DIR_FD |
| 4742 | |
| 4743 | static int |
| 4744 | utime_dir_fd(utime_t *utime, int dir_fd, char *path, int follow_symlinks) |
| 4745 | { |
| 4746 | #ifdef HAVE_UTIMENSAT |
| 4747 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4748 | UTIME_TO_TIMESPEC; |
| 4749 | return utimensat(dir_fd, path, time, flags); |
| 4750 | #elif defined(HAVE_FUTIMESAT) |
| 4751 | UTIME_TO_TIMEVAL; |
| 4752 | /* |
| 4753 | * follow_symlinks will never be false here; |
| 4754 | * we only allow !follow_symlinks and dir_fd together |
| 4755 | * if we have utimensat() |
| 4756 | */ |
| 4757 | assert(follow_symlinks); |
| 4758 | return futimesat(dir_fd, path, time); |
| 4759 | #endif |
| 4760 | } |
| 4761 | |
| 4762 | #endif |
| 4763 | |
| 4764 | #define UTIME_HAVE_FD (defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)) |
| 4765 | |
| 4766 | #if UTIME_HAVE_FD |
| 4767 | |
| 4768 | static int |
| 4769 | utime_fd(utime_t *utime, int fd) |
| 4770 | { |
| 4771 | #ifdef HAVE_FUTIMENS |
| 4772 | UTIME_TO_TIMESPEC; |
| 4773 | return futimens(fd, time); |
| 4774 | #else |
| 4775 | UTIME_TO_TIMEVAL; |
| 4776 | return futimes(fd, time); |
| 4777 | #endif |
| 4778 | } |
| 4779 | |
| 4780 | #endif |
| 4781 | |
| 4782 | |
| 4783 | #define UTIME_HAVE_NOFOLLOW_SYMLINKS \ |
| 4784 | (defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)) |
| 4785 | |
| 4786 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4787 | |
| 4788 | static int |
| 4789 | utime_nofollow_symlinks(utime_t *utime, char *path) |
| 4790 | { |
| 4791 | #ifdef HAVE_UTIMENSAT |
| 4792 | UTIME_TO_TIMESPEC; |
| 4793 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4794 | #else |
| 4795 | UTIME_TO_TIMEVAL; |
| 4796 | return lutimes(path, time); |
| 4797 | #endif |
| 4798 | } |
| 4799 | |
| 4800 | #endif |
| 4801 | |
| 4802 | #ifndef MS_WINDOWS |
| 4803 | |
| 4804 | static int |
| 4805 | utime_default(utime_t *utime, char *path) |
| 4806 | { |
| 4807 | #ifdef HAVE_UTIMENSAT |
| 4808 | UTIME_TO_TIMESPEC; |
| 4809 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4810 | #elif defined(HAVE_UTIMES) |
| 4811 | UTIME_TO_TIMEVAL; |
| 4812 | return utimes(path, time); |
| 4813 | #elif defined(HAVE_UTIME_H) |
| 4814 | UTIME_TO_UTIMBUF; |
| 4815 | return utime(path, time); |
| 4816 | #else |
| 4817 | UTIME_TO_TIME_T; |
| 4818 | return utime(path, time); |
| 4819 | #endif |
| 4820 | } |
| 4821 | |
| 4822 | #endif |
| 4823 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4824 | static int |
| 4825 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4826 | { |
| 4827 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4828 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4829 | divmod = PyNumber_Divmod(py_long, billion); |
| 4830 | if (!divmod) |
| 4831 | goto exit; |
| 4832 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4833 | if ((*s == -1) && PyErr_Occurred()) |
| 4834 | goto exit; |
| 4835 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4836 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4837 | goto exit; |
| 4838 | |
| 4839 | result = 1; |
| 4840 | exit: |
| 4841 | Py_XDECREF(divmod); |
| 4842 | return result; |
| 4843 | } |
| 4844 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4845 | static PyObject * |
| 4846 | posix_utime(PyObject *self, PyObject *args, PyObject *kwargs) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4847 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4848 | path_t path; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4849 | PyObject *times = NULL; |
| 4850 | PyObject *ns = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4851 | int dir_fd = DEFAULT_DIR_FD; |
| 4852 | int follow_symlinks = 1; |
| 4853 | char *keywords[] = {"path", "times", "ns", "dir_fd", |
| 4854 | "follow_symlinks", NULL}; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4855 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4856 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4857 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4858 | #ifdef MS_WINDOWS |
| 4859 | HANDLE hFile; |
| 4860 | FILETIME atime, mtime; |
| 4861 | #else |
| 4862 | int result; |
| 4863 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4864 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4865 | PyObject *return_value = NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4866 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4867 | memset(&path, 0, sizeof(path)); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4868 | path.function_name = "utime"; |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4869 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4870 | #if UTIME_HAVE_FD |
| 4871 | path.allow_fd = 1; |
| 4872 | #endif |
| 4873 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 4874 | "O&|O$OO&p:utime", keywords, |
| 4875 | path_converter, &path, |
| 4876 | ×, &ns, |
| 4877 | #if UTIME_HAVE_DIR_FD |
| 4878 | dir_fd_converter, &dir_fd, |
| 4879 | #else |
| 4880 | dir_fd_unavailable, &dir_fd, |
| 4881 | #endif |
| 4882 | &follow_symlinks |
| 4883 | )) |
| 4884 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4885 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4886 | if (times && (times != Py_None) && ns) { |
| 4887 | PyErr_SetString(PyExc_ValueError, |
| 4888 | "utime: you may specify either 'times'" |
| 4889 | " or 'ns' but not both"); |
| 4890 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4891 | } |
| 4892 | |
| 4893 | if (times && (times != Py_None)) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4894 | time_t a_sec, m_sec; |
| 4895 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4896 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4897 | PyErr_SetString(PyExc_TypeError, |
| 4898 | "utime: 'times' must be either" |
| 4899 | " a tuple of two ints or None"); |
| 4900 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4901 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4902 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4903 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 4904 | &a_sec, &a_nsec, _PyTime_ROUND_DOWN) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4905 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 4906 | &m_sec, &m_nsec, _PyTime_ROUND_DOWN) == -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4907 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4908 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4909 | utime.atime_s = a_sec; |
| 4910 | utime.atime_ns = a_nsec; |
| 4911 | utime.mtime_s = m_sec; |
| 4912 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4913 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4914 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4915 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4916 | PyErr_SetString(PyExc_TypeError, |
| 4917 | "utime: 'ns' must be a tuple of two ints"); |
| 4918 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4919 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4920 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4921 | 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] | 4922 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4923 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4924 | &utime.mtime_s, &utime.mtime_ns)) { |
| 4925 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4926 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4927 | } |
| 4928 | else { |
| 4929 | /* times and ns are both None/unspecified. use "now". */ |
| 4930 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4931 | } |
| 4932 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4933 | #if !UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4934 | if (follow_symlinks_specified("utime", follow_symlinks)) |
| 4935 | goto exit; |
| 4936 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4937 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4938 | if (path_and_dir_fd_invalid("utime", &path, dir_fd) || |
| 4939 | dir_fd_and_fd_invalid("utime", dir_fd, path.fd) || |
| 4940 | fd_and_follow_symlinks_invalid("utime", path.fd, follow_symlinks)) |
| 4941 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4942 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4943 | #if !defined(HAVE_UTIMENSAT) |
| 4944 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4945 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4946 | "utime: cannot use dir_fd and follow_symlinks " |
| 4947 | "together on this platform"); |
| 4948 | goto exit; |
| 4949 | } |
| 4950 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4951 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4952 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4953 | Py_BEGIN_ALLOW_THREADS |
| 4954 | if (path.wide) |
| 4955 | hFile = CreateFileW(path.wide, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4956 | NULL, OPEN_EXISTING, |
| 4957 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4958 | else |
| 4959 | hFile = CreateFileA(path.narrow, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4960 | NULL, OPEN_EXISTING, |
| 4961 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4962 | Py_END_ALLOW_THREADS |
| 4963 | if (hFile == INVALID_HANDLE_VALUE) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4964 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4965 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4966 | } |
| 4967 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4968 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 4969 | GetSystemTimeAsFileTime(&mtime); |
| 4970 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4971 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4972 | else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4973 | time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4974 | time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4975 | } |
| 4976 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4977 | /* Avoid putting the file name into the error here, |
| 4978 | as that may confuse the user into believing that |
| 4979 | something is wrong with the file, when it also |
| 4980 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4981 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4982 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4983 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4984 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4985 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4986 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4987 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4988 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 4989 | result = utime_nofollow_symlinks(&utime, path.narrow); |
| 4990 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4991 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4992 | |
| 4993 | #if UTIME_HAVE_DIR_FD |
| 4994 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
| 4995 | result = utime_dir_fd(&utime, dir_fd, path.narrow, follow_symlinks); |
| 4996 | else |
| 4997 | #endif |
| 4998 | |
| 4999 | #if UTIME_HAVE_FD |
| 5000 | if (path.fd != -1) |
| 5001 | result = utime_fd(&utime, path.fd); |
| 5002 | else |
| 5003 | #endif |
| 5004 | |
| 5005 | result = utime_default(&utime, path.narrow); |
| 5006 | |
| 5007 | Py_END_ALLOW_THREADS |
| 5008 | |
| 5009 | if (result < 0) { |
| 5010 | /* see previous comment about not putting filename in error here */ |
| 5011 | return_value = posix_error(); |
| 5012 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5013 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5014 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 5015 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5016 | |
| 5017 | Py_INCREF(Py_None); |
| 5018 | return_value = Py_None; |
| 5019 | |
| 5020 | exit: |
| 5021 | path_cleanup(&path); |
| 5022 | #ifdef MS_WINDOWS |
| 5023 | if (hFile != INVALID_HANDLE_VALUE) |
| 5024 | CloseHandle(hFile); |
| 5025 | #endif |
| 5026 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5027 | } |
| 5028 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5029 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5030 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5031 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5032 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5033 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5034 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5035 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5036 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5037 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5038 | int sts; |
| 5039 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 5040 | return NULL; |
| 5041 | _exit(sts); |
| 5042 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5043 | } |
| 5044 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5045 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 5046 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 5047 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5048 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5049 | Py_ssize_t i; |
| 5050 | for (i = 0; i < count; i++) |
| 5051 | PyMem_Free(array[i]); |
| 5052 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5053 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5054 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 5055 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5056 | int fsconvert_strdup(PyObject *o, char**out) |
| 5057 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5058 | PyObject *bytes; |
| 5059 | Py_ssize_t size; |
| 5060 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 5061 | return 0; |
| 5062 | size = PyBytes_GET_SIZE(bytes); |
| 5063 | *out = PyMem_Malloc(size+1); |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 5064 | if (!*out) { |
| 5065 | PyErr_NoMemory(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5066 | return 0; |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 5067 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5068 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 5069 | Py_DECREF(bytes); |
| 5070 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5071 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5072 | #endif |
| 5073 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5074 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5075 | static char** |
| 5076 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 5077 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5078 | char **envlist; |
| 5079 | Py_ssize_t i, pos, envc; |
| 5080 | PyObject *keys=NULL, *vals=NULL; |
| 5081 | PyObject *key, *val, *key2, *val2; |
| 5082 | char *p, *k, *v; |
| 5083 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5084 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5085 | i = PyMapping_Size(env); |
| 5086 | if (i < 0) |
| 5087 | return NULL; |
| 5088 | envlist = PyMem_NEW(char *, i + 1); |
| 5089 | if (envlist == NULL) { |
| 5090 | PyErr_NoMemory(); |
| 5091 | return NULL; |
| 5092 | } |
| 5093 | envc = 0; |
| 5094 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5095 | if (!keys) |
| 5096 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5097 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5098 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5099 | goto error; |
| 5100 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 5101 | PyErr_Format(PyExc_TypeError, |
| 5102 | "env.keys() or env.values() is not a list"); |
| 5103 | goto error; |
| 5104 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5105 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5106 | for (pos = 0; pos < i; pos++) { |
| 5107 | key = PyList_GetItem(keys, pos); |
| 5108 | val = PyList_GetItem(vals, pos); |
| 5109 | if (!key || !val) |
| 5110 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5111 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5112 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 5113 | goto error; |
| 5114 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 5115 | Py_DECREF(key2); |
| 5116 | goto error; |
| 5117 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5118 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5119 | k = PyBytes_AsString(key2); |
| 5120 | v = PyBytes_AsString(val2); |
| 5121 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5122 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5123 | p = PyMem_NEW(char, len); |
| 5124 | if (p == NULL) { |
| 5125 | PyErr_NoMemory(); |
| 5126 | Py_DECREF(key2); |
| 5127 | Py_DECREF(val2); |
| 5128 | goto error; |
| 5129 | } |
| 5130 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 5131 | envlist[envc++] = p; |
| 5132 | Py_DECREF(key2); |
| 5133 | Py_DECREF(val2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5134 | } |
| 5135 | Py_DECREF(vals); |
| 5136 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5137 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5138 | envlist[envc] = 0; |
| 5139 | *envc_ptr = envc; |
| 5140 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5141 | |
| 5142 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5143 | Py_XDECREF(keys); |
| 5144 | Py_XDECREF(vals); |
| 5145 | while (--envc >= 0) |
| 5146 | PyMem_DEL(envlist[envc]); |
| 5147 | PyMem_DEL(envlist); |
| 5148 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5149 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5150 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5151 | static char** |
| 5152 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 5153 | { |
| 5154 | int i; |
| 5155 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 5156 | if (argvlist == NULL) { |
| 5157 | PyErr_NoMemory(); |
| 5158 | return NULL; |
| 5159 | } |
| 5160 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5161 | PyObject* item = PySequence_ITEM(argv, i); |
| 5162 | if (item == NULL) |
| 5163 | goto fail; |
| 5164 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 5165 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5166 | goto fail; |
| 5167 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5168 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5169 | } |
| 5170 | argvlist[*argc] = NULL; |
| 5171 | return argvlist; |
| 5172 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5173 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5174 | free_string_array(argvlist, *argc); |
| 5175 | return NULL; |
| 5176 | } |
| 5177 | #endif |
| 5178 | |
| 5179 | #ifdef HAVE_EXECV |
| 5180 | PyDoc_STRVAR(posix_execv__doc__, |
| 5181 | "execv(path, args)\n\n\ |
| 5182 | Execute an executable path with arguments, replacing current process.\n\ |
| 5183 | \n\ |
| 5184 | path: path of executable file\n\ |
| 5185 | args: tuple or list of strings"); |
| 5186 | |
| 5187 | static PyObject * |
| 5188 | posix_execv(PyObject *self, PyObject *args) |
| 5189 | { |
| 5190 | PyObject *opath; |
| 5191 | char *path; |
| 5192 | PyObject *argv; |
| 5193 | char **argvlist; |
| 5194 | Py_ssize_t argc; |
| 5195 | |
| 5196 | /* execv has two arguments: (path, argv), where |
| 5197 | argv is a list or tuple of strings. */ |
| 5198 | |
| 5199 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 5200 | PyUnicode_FSConverter, |
| 5201 | &opath, &argv)) |
| 5202 | return NULL; |
| 5203 | path = PyBytes_AsString(opath); |
| 5204 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 5205 | PyErr_SetString(PyExc_TypeError, |
| 5206 | "execv() arg 2 must be a tuple or list"); |
| 5207 | Py_DECREF(opath); |
| 5208 | return NULL; |
| 5209 | } |
| 5210 | argc = PySequence_Size(argv); |
| 5211 | if (argc < 1) { |
| 5212 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 5213 | Py_DECREF(opath); |
| 5214 | return NULL; |
| 5215 | } |
| 5216 | |
| 5217 | argvlist = parse_arglist(argv, &argc); |
| 5218 | if (argvlist == NULL) { |
| 5219 | Py_DECREF(opath); |
| 5220 | return NULL; |
| 5221 | } |
| 5222 | |
| 5223 | execv(path, argvlist); |
| 5224 | |
| 5225 | /* If we get here it's definitely an error */ |
| 5226 | |
| 5227 | free_string_array(argvlist, argc); |
| 5228 | Py_DECREF(opath); |
| 5229 | return posix_error(); |
| 5230 | } |
| 5231 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5232 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5233 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5234 | Execute a path with arguments and environment, replacing current process.\n\ |
| 5235 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5236 | path: path of executable file\n\ |
| 5237 | args: tuple or list of arguments\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5238 | env: dictionary of strings mapping to strings\n\ |
| 5239 | \n\ |
| 5240 | On some platforms, you may specify an open file descriptor for path;\n\ |
| 5241 | execve will execute the program the file descriptor is open to.\n\ |
| 5242 | If this functionality is unavailable, using it raises NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5243 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5244 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5245 | posix_execve(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5246 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5247 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5248 | PyObject *argv, *env; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5249 | char **argvlist = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5250 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5251 | Py_ssize_t argc, envc; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5252 | static char *keywords[] = {"path", "argv", "environment", NULL}; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5253 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5254 | /* execve has three arguments: (path, argv, env), where |
| 5255 | argv is a list or tuple of strings and env is a dictionary |
| 5256 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5257 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5258 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 5259 | path.function_name = "execve"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5260 | #ifdef HAVE_FEXECVE |
| 5261 | path.allow_fd = 1; |
| 5262 | #endif |
| 5263 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&OO:execve", keywords, |
| 5264 | path_converter, &path, |
| 5265 | &argv, &env |
| 5266 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5267 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5268 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5269 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5270 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5271 | "execve: argv must be a tuple or list"); |
| 5272 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5273 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5274 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5275 | if (!PyMapping_Check(env)) { |
| 5276 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5277 | "execve: environment must be a mapping object"); |
| 5278 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5279 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5280 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5281 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5282 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5283 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5284 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5285 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5286 | envlist = parse_envlist(env, &envc); |
| 5287 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5288 | goto fail; |
| 5289 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5290 | #ifdef HAVE_FEXECVE |
| 5291 | if (path.fd > -1) |
| 5292 | fexecve(path.fd, argvlist, envlist); |
| 5293 | else |
| 5294 | #endif |
| 5295 | execve(path.narrow, argvlist, envlist); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5296 | |
| 5297 | /* If we get here it's definitely an error */ |
| 5298 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 5299 | path_error(&path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5300 | |
| 5301 | while (--envc >= 0) |
| 5302 | PyMem_DEL(envlist[envc]); |
| 5303 | PyMem_DEL(envlist); |
| 5304 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5305 | if (argvlist) |
| 5306 | free_string_array(argvlist, argc); |
| 5307 | path_cleanup(&path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5308 | return NULL; |
| 5309 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5310 | #endif /* HAVE_EXECV */ |
| 5311 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5312 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5313 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5314 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5315 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5316 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5317 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5318 | mode: mode of process creation\n\ |
| 5319 | path: path of executable file\n\ |
| 5320 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5321 | |
| 5322 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5323 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5324 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5325 | PyObject *opath; |
| 5326 | char *path; |
| 5327 | PyObject *argv; |
| 5328 | char **argvlist; |
| 5329 | int mode, i; |
| 5330 | Py_ssize_t argc; |
| 5331 | Py_intptr_t spawnval; |
| 5332 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5333 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5334 | /* spawnv has three arguments: (mode, path, argv), where |
| 5335 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5336 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5337 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 5338 | PyUnicode_FSConverter, |
| 5339 | &opath, &argv)) |
| 5340 | return NULL; |
| 5341 | path = PyBytes_AsString(opath); |
| 5342 | if (PyList_Check(argv)) { |
| 5343 | argc = PyList_Size(argv); |
| 5344 | getitem = PyList_GetItem; |
| 5345 | } |
| 5346 | else if (PyTuple_Check(argv)) { |
| 5347 | argc = PyTuple_Size(argv); |
| 5348 | getitem = PyTuple_GetItem; |
| 5349 | } |
| 5350 | else { |
| 5351 | PyErr_SetString(PyExc_TypeError, |
| 5352 | "spawnv() arg 2 must be a tuple or list"); |
| 5353 | Py_DECREF(opath); |
| 5354 | return NULL; |
| 5355 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5356 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5357 | argvlist = PyMem_NEW(char *, argc+1); |
| 5358 | if (argvlist == NULL) { |
| 5359 | Py_DECREF(opath); |
| 5360 | return PyErr_NoMemory(); |
| 5361 | } |
| 5362 | for (i = 0; i < argc; i++) { |
| 5363 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5364 | &argvlist[i])) { |
| 5365 | free_string_array(argvlist, i); |
| 5366 | PyErr_SetString( |
| 5367 | PyExc_TypeError, |
| 5368 | "spawnv() arg 2 must contain only strings"); |
| 5369 | Py_DECREF(opath); |
| 5370 | return NULL; |
| 5371 | } |
| 5372 | } |
| 5373 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5374 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5375 | if (mode == _OLD_P_OVERLAY) |
| 5376 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5377 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5378 | Py_BEGIN_ALLOW_THREADS |
| 5379 | spawnval = _spawnv(mode, path, argvlist); |
| 5380 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5381 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5382 | free_string_array(argvlist, argc); |
| 5383 | Py_DECREF(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5384 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5385 | if (spawnval == -1) |
| 5386 | return posix_error(); |
| 5387 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5388 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5389 | } |
| 5390 | |
| 5391 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5392 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5393 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5394 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5395 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5396 | mode: mode of process creation\n\ |
| 5397 | path: path of executable file\n\ |
| 5398 | args: tuple or list of arguments\n\ |
| 5399 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5400 | |
| 5401 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5402 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5403 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5404 | PyObject *opath; |
| 5405 | char *path; |
| 5406 | PyObject *argv, *env; |
| 5407 | char **argvlist; |
| 5408 | char **envlist; |
| 5409 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5410 | int mode; |
| 5411 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5412 | Py_intptr_t spawnval; |
| 5413 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 5414 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5415 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5416 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5417 | argv is a list or tuple of strings and env is a dictionary |
| 5418 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5419 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5420 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 5421 | PyUnicode_FSConverter, |
| 5422 | &opath, &argv, &env)) |
| 5423 | return NULL; |
| 5424 | path = PyBytes_AsString(opath); |
| 5425 | if (PyList_Check(argv)) { |
| 5426 | argc = PyList_Size(argv); |
| 5427 | getitem = PyList_GetItem; |
| 5428 | } |
| 5429 | else if (PyTuple_Check(argv)) { |
| 5430 | argc = PyTuple_Size(argv); |
| 5431 | getitem = PyTuple_GetItem; |
| 5432 | } |
| 5433 | else { |
| 5434 | PyErr_SetString(PyExc_TypeError, |
| 5435 | "spawnve() arg 2 must be a tuple or list"); |
| 5436 | goto fail_0; |
| 5437 | } |
| 5438 | if (!PyMapping_Check(env)) { |
| 5439 | PyErr_SetString(PyExc_TypeError, |
| 5440 | "spawnve() arg 3 must be a mapping object"); |
| 5441 | goto fail_0; |
| 5442 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5443 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5444 | argvlist = PyMem_NEW(char *, argc+1); |
| 5445 | if (argvlist == NULL) { |
| 5446 | PyErr_NoMemory(); |
| 5447 | goto fail_0; |
| 5448 | } |
| 5449 | for (i = 0; i < argc; i++) { |
| 5450 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5451 | &argvlist[i])) |
| 5452 | { |
| 5453 | lastarg = i; |
| 5454 | goto fail_1; |
| 5455 | } |
| 5456 | } |
| 5457 | lastarg = argc; |
| 5458 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5459 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5460 | envlist = parse_envlist(env, &envc); |
| 5461 | if (envlist == NULL) |
| 5462 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5463 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5464 | if (mode == _OLD_P_OVERLAY) |
| 5465 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5466 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5467 | Py_BEGIN_ALLOW_THREADS |
| 5468 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 5469 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5470 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5471 | if (spawnval == -1) |
| 5472 | (void) posix_error(); |
| 5473 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5474 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5475 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5476 | while (--envc >= 0) |
| 5477 | PyMem_DEL(envlist[envc]); |
| 5478 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5479 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5480 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5481 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5482 | Py_DECREF(opath); |
| 5483 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5484 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5485 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5486 | #endif /* HAVE_SPAWNV */ |
| 5487 | |
| 5488 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5489 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5490 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5491 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5492 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 5493 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5494 | Return 0 to child process and PID of child to parent process."); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5495 | |
| 5496 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5497 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5498 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5499 | pid_t pid; |
| 5500 | int result = 0; |
| 5501 | _PyImport_AcquireLock(); |
| 5502 | pid = fork1(); |
| 5503 | if (pid == 0) { |
| 5504 | /* child: this clobbers and resets the import lock. */ |
| 5505 | PyOS_AfterFork(); |
| 5506 | } else { |
| 5507 | /* parent: release the import lock. */ |
| 5508 | result = _PyImport_ReleaseLock(); |
| 5509 | } |
| 5510 | if (pid == -1) |
| 5511 | return posix_error(); |
| 5512 | if (result < 0) { |
| 5513 | /* Don't clobber the OSError if the fork failed. */ |
| 5514 | PyErr_SetString(PyExc_RuntimeError, |
| 5515 | "not holding the import lock"); |
| 5516 | return NULL; |
| 5517 | } |
| 5518 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5519 | } |
| 5520 | #endif |
| 5521 | |
| 5522 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5523 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5524 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5525 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5526 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5527 | Return 0 to child process and PID of child to parent process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5528 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5529 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5530 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5531 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5532 | pid_t pid; |
| 5533 | int result = 0; |
| 5534 | _PyImport_AcquireLock(); |
| 5535 | pid = fork(); |
| 5536 | if (pid == 0) { |
| 5537 | /* child: this clobbers and resets the import lock. */ |
| 5538 | PyOS_AfterFork(); |
| 5539 | } else { |
| 5540 | /* parent: release the import lock. */ |
| 5541 | result = _PyImport_ReleaseLock(); |
| 5542 | } |
| 5543 | if (pid == -1) |
| 5544 | return posix_error(); |
| 5545 | if (result < 0) { |
| 5546 | /* Don't clobber the OSError if the fork failed. */ |
| 5547 | PyErr_SetString(PyExc_RuntimeError, |
| 5548 | "not holding the import lock"); |
| 5549 | return NULL; |
| 5550 | } |
| 5551 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5552 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5553 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5554 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5555 | #ifdef HAVE_SCHED_H |
| 5556 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5557 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
| 5558 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5559 | PyDoc_STRVAR(posix_sched_get_priority_max__doc__, |
| 5560 | "sched_get_priority_max(policy)\n\n\ |
| 5561 | Get the maximum scheduling priority for *policy*."); |
| 5562 | |
| 5563 | static PyObject * |
| 5564 | posix_sched_get_priority_max(PyObject *self, PyObject *args) |
| 5565 | { |
| 5566 | int policy, max; |
| 5567 | |
| 5568 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy)) |
| 5569 | return NULL; |
| 5570 | max = sched_get_priority_max(policy); |
| 5571 | if (max < 0) |
| 5572 | return posix_error(); |
| 5573 | return PyLong_FromLong(max); |
| 5574 | } |
| 5575 | |
| 5576 | PyDoc_STRVAR(posix_sched_get_priority_min__doc__, |
| 5577 | "sched_get_priority_min(policy)\n\n\ |
| 5578 | Get the minimum scheduling priority for *policy*."); |
| 5579 | |
| 5580 | static PyObject * |
| 5581 | posix_sched_get_priority_min(PyObject *self, PyObject *args) |
| 5582 | { |
| 5583 | int policy, min; |
| 5584 | |
| 5585 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy)) |
| 5586 | return NULL; |
| 5587 | min = sched_get_priority_min(policy); |
| 5588 | if (min < 0) |
| 5589 | return posix_error(); |
| 5590 | return PyLong_FromLong(min); |
| 5591 | } |
| 5592 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5593 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 5594 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5595 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5596 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5597 | PyDoc_STRVAR(posix_sched_getscheduler__doc__, |
| 5598 | "sched_getscheduler(pid)\n\n\ |
| 5599 | Get the scheduling policy for the process with a PID of *pid*.\n\ |
| 5600 | Passing a PID of 0 returns the scheduling policy for the calling process."); |
| 5601 | |
| 5602 | static PyObject * |
| 5603 | posix_sched_getscheduler(PyObject *self, PyObject *args) |
| 5604 | { |
| 5605 | pid_t pid; |
| 5606 | int policy; |
| 5607 | |
| 5608 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid)) |
| 5609 | return NULL; |
| 5610 | policy = sched_getscheduler(pid); |
| 5611 | if (policy < 0) |
| 5612 | return posix_error(); |
| 5613 | return PyLong_FromLong(policy); |
| 5614 | } |
| 5615 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5616 | #endif |
| 5617 | |
| 5618 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
| 5619 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5620 | static PyObject * |
| 5621 | sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 5622 | { |
| 5623 | PyObject *res, *priority; |
Benjamin Peterson | 4e36d5a | 2011-08-02 19:56:11 -0500 | [diff] [blame] | 5624 | static char *kwlist[] = {"sched_priority", NULL}; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5625 | |
| 5626 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority)) |
| 5627 | return NULL; |
| 5628 | res = PyStructSequence_New(type); |
| 5629 | if (!res) |
| 5630 | return NULL; |
| 5631 | Py_INCREF(priority); |
| 5632 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5633 | return res; |
| 5634 | } |
| 5635 | |
| 5636 | PyDoc_STRVAR(sched_param__doc__, |
| 5637 | "sched_param(sched_priority): A scheduling parameter.\n\n\ |
| 5638 | Current has only one field: sched_priority"); |
| 5639 | |
| 5640 | static PyStructSequence_Field sched_param_fields[] = { |
| 5641 | {"sched_priority", "the scheduling priority"}, |
| 5642 | {0} |
| 5643 | }; |
| 5644 | |
| 5645 | static PyStructSequence_Desc sched_param_desc = { |
| 5646 | "sched_param", /* name */ |
| 5647 | sched_param__doc__, /* doc */ |
| 5648 | sched_param_fields, |
| 5649 | 1 |
| 5650 | }; |
| 5651 | |
| 5652 | static int |
| 5653 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 5654 | { |
| 5655 | long priority; |
| 5656 | |
| 5657 | if (Py_TYPE(param) != &SchedParamType) { |
| 5658 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 5659 | return 0; |
| 5660 | } |
| 5661 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 5662 | if (priority == -1 && PyErr_Occurred()) |
| 5663 | return 0; |
| 5664 | if (priority > INT_MAX || priority < INT_MIN) { |
| 5665 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 5666 | return 0; |
| 5667 | } |
| 5668 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 5669 | return 1; |
| 5670 | } |
| 5671 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5672 | #endif |
| 5673 | |
| 5674 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5675 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5676 | PyDoc_STRVAR(posix_sched_setscheduler__doc__, |
| 5677 | "sched_setscheduler(pid, policy, param)\n\n\ |
| 5678 | Set the scheduling policy, *policy*, for *pid*.\n\ |
| 5679 | If *pid* is 0, the calling process is changed.\n\ |
| 5680 | *param* is an instance of sched_param."); |
| 5681 | |
| 5682 | static PyObject * |
| 5683 | posix_sched_setscheduler(PyObject *self, PyObject *args) |
| 5684 | { |
| 5685 | pid_t pid; |
| 5686 | int policy; |
| 5687 | struct sched_param param; |
| 5688 | |
| 5689 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler", |
| 5690 | &pid, &policy, &convert_sched_param, ¶m)) |
| 5691 | return NULL; |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5692 | |
| 5693 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 5694 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 5695 | ** scheduling policy under Solaris/Illumos, and others. |
| 5696 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5697 | */ |
| 5698 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5699 | return posix_error(); |
| 5700 | Py_RETURN_NONE; |
| 5701 | } |
| 5702 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5703 | #endif |
| 5704 | |
| 5705 | #ifdef HAVE_SCHED_SETPARAM |
| 5706 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5707 | PyDoc_STRVAR(posix_sched_getparam__doc__, |
| 5708 | "sched_getparam(pid) -> sched_param\n\n\ |
| 5709 | Returns scheduling parameters for the process with *pid* as an instance of the\n\ |
| 5710 | sched_param class. A PID of 0 means the calling process."); |
| 5711 | |
| 5712 | static PyObject * |
| 5713 | posix_sched_getparam(PyObject *self, PyObject *args) |
| 5714 | { |
| 5715 | pid_t pid; |
| 5716 | struct sched_param param; |
| 5717 | PyObject *res, *priority; |
| 5718 | |
| 5719 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid)) |
| 5720 | return NULL; |
| 5721 | if (sched_getparam(pid, ¶m)) |
| 5722 | return posix_error(); |
| 5723 | res = PyStructSequence_New(&SchedParamType); |
| 5724 | if (!res) |
| 5725 | return NULL; |
| 5726 | priority = PyLong_FromLong(param.sched_priority); |
| 5727 | if (!priority) { |
| 5728 | Py_DECREF(res); |
| 5729 | return NULL; |
| 5730 | } |
| 5731 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5732 | return res; |
| 5733 | } |
| 5734 | |
| 5735 | PyDoc_STRVAR(posix_sched_setparam__doc__, |
| 5736 | "sched_setparam(pid, param)\n\n\ |
| 5737 | Set scheduling parameters for a process with PID *pid*.\n\ |
| 5738 | A PID of 0 means the calling process."); |
| 5739 | |
| 5740 | static PyObject * |
| 5741 | posix_sched_setparam(PyObject *self, PyObject *args) |
| 5742 | { |
| 5743 | pid_t pid; |
| 5744 | struct sched_param param; |
| 5745 | |
| 5746 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam", |
| 5747 | &pid, &convert_sched_param, ¶m)) |
| 5748 | return NULL; |
| 5749 | if (sched_setparam(pid, ¶m)) |
| 5750 | return posix_error(); |
| 5751 | Py_RETURN_NONE; |
| 5752 | } |
| 5753 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5754 | #endif |
| 5755 | |
| 5756 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
| 5757 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5758 | PyDoc_STRVAR(posix_sched_rr_get_interval__doc__, |
| 5759 | "sched_rr_get_interval(pid) -> float\n\n\ |
| 5760 | Return the round-robin quantum for the process with PID *pid* in seconds."); |
| 5761 | |
| 5762 | static PyObject * |
| 5763 | posix_sched_rr_get_interval(PyObject *self, PyObject *args) |
| 5764 | { |
| 5765 | pid_t pid; |
| 5766 | struct timespec interval; |
| 5767 | |
| 5768 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid)) |
| 5769 | return NULL; |
| 5770 | if (sched_rr_get_interval(pid, &interval)) |
| 5771 | return posix_error(); |
| 5772 | return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec); |
| 5773 | } |
| 5774 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5775 | #endif |
| 5776 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5777 | PyDoc_STRVAR(posix_sched_yield__doc__, |
| 5778 | "sched_yield()\n\n\ |
| 5779 | Voluntarily relinquish the CPU."); |
| 5780 | |
| 5781 | static PyObject * |
| 5782 | posix_sched_yield(PyObject *self, PyObject *noargs) |
| 5783 | { |
| 5784 | if (sched_yield()) |
| 5785 | return posix_error(); |
| 5786 | Py_RETURN_NONE; |
| 5787 | } |
| 5788 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5789 | #ifdef HAVE_SCHED_SETAFFINITY |
| 5790 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5791 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 5792 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5793 | |
| 5794 | PyDoc_STRVAR(posix_sched_setaffinity__doc__, |
| 5795 | "sched_setaffinity(pid, cpu_set)\n\n\ |
| 5796 | Set the affinity of the process with PID *pid* to *cpu_set*."); |
| 5797 | |
| 5798 | static PyObject * |
| 5799 | posix_sched_setaffinity(PyObject *self, PyObject *args) |
| 5800 | { |
| 5801 | pid_t pid; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5802 | int ncpus; |
| 5803 | size_t setsize; |
| 5804 | cpu_set_t *mask = NULL; |
| 5805 | PyObject *iterable, *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5806 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5807 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O:sched_setaffinity", |
| 5808 | &pid, &iterable)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5809 | return NULL; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5810 | |
| 5811 | iterator = PyObject_GetIter(iterable); |
| 5812 | if (iterator == NULL) |
| 5813 | return NULL; |
| 5814 | |
| 5815 | ncpus = NCPUS_START; |
| 5816 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5817 | mask = CPU_ALLOC(ncpus); |
| 5818 | if (mask == NULL) { |
| 5819 | PyErr_NoMemory(); |
| 5820 | goto error; |
| 5821 | } |
| 5822 | CPU_ZERO_S(setsize, mask); |
| 5823 | |
| 5824 | while ((item = PyIter_Next(iterator))) { |
| 5825 | long cpu; |
| 5826 | if (!PyLong_Check(item)) { |
| 5827 | PyErr_Format(PyExc_TypeError, |
| 5828 | "expected an iterator of ints, " |
| 5829 | "but iterator yielded %R", |
| 5830 | Py_TYPE(item)); |
| 5831 | Py_DECREF(item); |
| 5832 | goto error; |
| 5833 | } |
| 5834 | cpu = PyLong_AsLong(item); |
| 5835 | Py_DECREF(item); |
| 5836 | if (cpu < 0) { |
| 5837 | if (!PyErr_Occurred()) |
| 5838 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 5839 | goto error; |
| 5840 | } |
| 5841 | if (cpu > INT_MAX - 1) { |
| 5842 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 5843 | goto error; |
| 5844 | } |
| 5845 | if (cpu >= ncpus) { |
| 5846 | /* Grow CPU mask to fit the CPU number */ |
| 5847 | int newncpus = ncpus; |
| 5848 | cpu_set_t *newmask; |
| 5849 | size_t newsetsize; |
| 5850 | while (newncpus <= cpu) { |
| 5851 | if (newncpus > INT_MAX / 2) |
| 5852 | newncpus = cpu + 1; |
| 5853 | else |
| 5854 | newncpus = newncpus * 2; |
| 5855 | } |
| 5856 | newmask = CPU_ALLOC(newncpus); |
| 5857 | if (newmask == NULL) { |
| 5858 | PyErr_NoMemory(); |
| 5859 | goto error; |
| 5860 | } |
| 5861 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 5862 | CPU_ZERO_S(newsetsize, newmask); |
| 5863 | memcpy(newmask, mask, setsize); |
| 5864 | CPU_FREE(mask); |
| 5865 | setsize = newsetsize; |
| 5866 | mask = newmask; |
| 5867 | ncpus = newncpus; |
| 5868 | } |
| 5869 | CPU_SET_S(cpu, setsize, mask); |
| 5870 | } |
| 5871 | Py_CLEAR(iterator); |
| 5872 | |
| 5873 | if (sched_setaffinity(pid, setsize, mask)) { |
| 5874 | posix_error(); |
| 5875 | goto error; |
| 5876 | } |
| 5877 | CPU_FREE(mask); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5878 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5879 | |
| 5880 | error: |
| 5881 | if (mask) |
| 5882 | CPU_FREE(mask); |
| 5883 | Py_XDECREF(iterator); |
| 5884 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5885 | } |
| 5886 | |
| 5887 | PyDoc_STRVAR(posix_sched_getaffinity__doc__, |
| 5888 | "sched_getaffinity(pid, ncpus) -> cpu_set\n\n\ |
| 5889 | Return the affinity of the process with PID *pid*.\n\ |
| 5890 | The returned cpu_set will be of size *ncpus*."); |
| 5891 | |
| 5892 | static PyObject * |
| 5893 | posix_sched_getaffinity(PyObject *self, PyObject *args) |
| 5894 | { |
| 5895 | pid_t pid; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5896 | int cpu, ncpus, count; |
| 5897 | size_t setsize; |
| 5898 | cpu_set_t *mask = NULL; |
| 5899 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5900 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5901 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getaffinity", |
| 5902 | &pid)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5903 | return NULL; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5904 | |
| 5905 | ncpus = NCPUS_START; |
| 5906 | while (1) { |
| 5907 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5908 | mask = CPU_ALLOC(ncpus); |
| 5909 | if (mask == NULL) |
| 5910 | return PyErr_NoMemory(); |
| 5911 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 5912 | break; |
| 5913 | CPU_FREE(mask); |
| 5914 | if (errno != EINVAL) |
| 5915 | return posix_error(); |
| 5916 | if (ncpus > INT_MAX / 2) { |
| 5917 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 5918 | "a large enough CPU set"); |
| 5919 | return NULL; |
| 5920 | } |
| 5921 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5922 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5923 | |
| 5924 | res = PySet_New(NULL); |
| 5925 | if (res == NULL) |
| 5926 | goto error; |
| 5927 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 5928 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 5929 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 5930 | --count; |
| 5931 | if (cpu_num == NULL) |
| 5932 | goto error; |
| 5933 | if (PySet_Add(res, cpu_num)) { |
| 5934 | Py_DECREF(cpu_num); |
| 5935 | goto error; |
| 5936 | } |
| 5937 | Py_DECREF(cpu_num); |
| 5938 | } |
| 5939 | } |
| 5940 | CPU_FREE(mask); |
| 5941 | return res; |
| 5942 | |
| 5943 | error: |
| 5944 | if (mask) |
| 5945 | CPU_FREE(mask); |
| 5946 | Py_XDECREF(res); |
| 5947 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5948 | } |
| 5949 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5950 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5951 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5952 | #endif /* HAVE_SCHED_H */ |
| 5953 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5954 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5955 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5956 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5957 | #define DEV_PTY_FILE "/dev/ptc" |
| 5958 | #define HAVE_DEV_PTMX |
| 5959 | #else |
| 5960 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5961 | #endif |
| 5962 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5963 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5964 | #ifdef HAVE_PTY_H |
| 5965 | #include <pty.h> |
| 5966 | #else |
| 5967 | #ifdef HAVE_LIBUTIL_H |
| 5968 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5969 | #else |
| 5970 | #ifdef HAVE_UTIL_H |
| 5971 | #include <util.h> |
| 5972 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5973 | #endif /* HAVE_LIBUTIL_H */ |
| 5974 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5975 | #ifdef HAVE_STROPTS_H |
| 5976 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5977 | #endif |
| 5978 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5979 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5980 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5981 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5982 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5983 | Open a pseudo-terminal, returning open fd's for both master and slave end.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5984 | |
| 5985 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5986 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5987 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5988 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5989 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5990 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5991 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5992 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5993 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5994 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5995 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5996 | #endif |
| 5997 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5998 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5999 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6000 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6001 | goto posix_error; |
| 6002 | |
| 6003 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6004 | goto error; |
| 6005 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 6006 | goto error; |
| 6007 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6008 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6009 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 6010 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6011 | goto posix_error; |
| 6012 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6013 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6014 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6015 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6016 | if (slave_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6017 | goto posix_error; |
| 6018 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6019 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6020 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6021 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6022 | goto posix_error; |
| 6023 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6024 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6025 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6026 | /* change permission of slave */ |
| 6027 | if (grantpt(master_fd) < 0) { |
| 6028 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6029 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6030 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6031 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6032 | /* unlock slave */ |
| 6033 | if (unlockpt(master_fd) < 0) { |
| 6034 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6035 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6036 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6037 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6038 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6039 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6040 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 6041 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6042 | goto posix_error; |
| 6043 | |
| 6044 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6045 | if (slave_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6046 | goto posix_error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6047 | |
| 6048 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6049 | goto posix_error; |
| 6050 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6051 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6052 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 6053 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6054 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6055 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6056 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6057 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 6058 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6059 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6060 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6061 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6062 | posix_error: |
| 6063 | posix_error(); |
Victor Stinner | b9981ba | 2013-08-28 01:51:06 +0200 | [diff] [blame] | 6064 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6065 | error: |
Victor Stinner | b9981ba | 2013-08-28 01:51:06 +0200 | [diff] [blame] | 6066 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6067 | if (master_fd != -1) |
| 6068 | close(master_fd); |
| 6069 | if (slave_fd != -1) |
| 6070 | close(slave_fd); |
| 6071 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6072 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6073 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6074 | |
| 6075 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6076 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6077 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6078 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 6079 | Like fork(), return 0 as pid to child process, and PID of child to parent.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6080 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6081 | |
| 6082 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6083 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6084 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6085 | int master_fd = -1, result = 0; |
| 6086 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6087 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6088 | _PyImport_AcquireLock(); |
| 6089 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 6090 | if (pid == 0) { |
| 6091 | /* child: this clobbers and resets the import lock. */ |
| 6092 | PyOS_AfterFork(); |
| 6093 | } else { |
| 6094 | /* parent: release the import lock. */ |
| 6095 | result = _PyImport_ReleaseLock(); |
| 6096 | } |
| 6097 | if (pid == -1) |
| 6098 | return posix_error(); |
| 6099 | if (result < 0) { |
| 6100 | /* Don't clobber the OSError if the fork failed. */ |
| 6101 | PyErr_SetString(PyExc_RuntimeError, |
| 6102 | "not holding the import lock"); |
| 6103 | return NULL; |
| 6104 | } |
| 6105 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6106 | } |
| 6107 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6108 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6109 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6110 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6111 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6112 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6113 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6114 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6115 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6116 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6117 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6118 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6119 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6120 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6121 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6122 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6123 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6124 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6125 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6126 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6127 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6128 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6129 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6130 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6131 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6132 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6133 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6134 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6135 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6136 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6137 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6138 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6139 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6140 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6141 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6142 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6143 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6144 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6145 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6146 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6147 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6148 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6149 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6150 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6151 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6152 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6153 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6154 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6155 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6156 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6157 | } |
| 6158 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6159 | #ifdef HAVE_GETGROUPLIST |
| 6160 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 6161 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 6162 | Returns a list of groups to which a user belongs.\n\n\ |
| 6163 | user: username to lookup\n\ |
| 6164 | group: base group id of the user"); |
| 6165 | |
| 6166 | static PyObject * |
| 6167 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 6168 | { |
| 6169 | #ifdef NGROUPS_MAX |
| 6170 | #define MAX_GROUPS NGROUPS_MAX |
| 6171 | #else |
| 6172 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6173 | #define MAX_GROUPS 64 |
| 6174 | #endif |
| 6175 | |
| 6176 | const char *user; |
| 6177 | int i, ngroups; |
| 6178 | PyObject *list; |
| 6179 | #ifdef __APPLE__ |
| 6180 | int *groups, basegid; |
| 6181 | #else |
| 6182 | gid_t *groups, basegid; |
| 6183 | #endif |
| 6184 | ngroups = MAX_GROUPS; |
| 6185 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6186 | #ifdef __APPLE__ |
| 6187 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6188 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6189 | #else |
| 6190 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 6191 | _Py_Gid_Converter, &basegid)) |
| 6192 | return NULL; |
| 6193 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6194 | |
| 6195 | #ifdef __APPLE__ |
| 6196 | groups = PyMem_Malloc(ngroups * sizeof(int)); |
| 6197 | #else |
| 6198 | groups = PyMem_Malloc(ngroups * sizeof(gid_t)); |
| 6199 | #endif |
| 6200 | if (groups == NULL) |
| 6201 | return PyErr_NoMemory(); |
| 6202 | |
| 6203 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 6204 | PyMem_Del(groups); |
| 6205 | return posix_error(); |
| 6206 | } |
| 6207 | |
| 6208 | list = PyList_New(ngroups); |
| 6209 | if (list == NULL) { |
| 6210 | PyMem_Del(groups); |
| 6211 | return NULL; |
| 6212 | } |
| 6213 | |
| 6214 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6215 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6216 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6217 | #else |
| 6218 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 6219 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6220 | if (o == NULL) { |
| 6221 | Py_DECREF(list); |
| 6222 | PyMem_Del(groups); |
| 6223 | return NULL; |
| 6224 | } |
| 6225 | PyList_SET_ITEM(list, i, o); |
| 6226 | } |
| 6227 | |
| 6228 | PyMem_Del(groups); |
| 6229 | |
| 6230 | return list; |
| 6231 | } |
| 6232 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6233 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6234 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6235 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6236 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6237 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6238 | |
| 6239 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6240 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6241 | { |
| 6242 | PyObject *result = NULL; |
| 6243 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6244 | #ifdef NGROUPS_MAX |
| 6245 | #define MAX_GROUPS NGROUPS_MAX |
| 6246 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6247 | /* 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] | 6248 | #define MAX_GROUPS 64 |
| 6249 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6250 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6251 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6252 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6253 | * This is a helper variable to store the intermediate result when |
| 6254 | * that happens. |
| 6255 | * |
| 6256 | * To keep the code readable the OSX behaviour is unconditional, |
| 6257 | * according to the POSIX spec this should be safe on all unix-y |
| 6258 | * systems. |
| 6259 | */ |
| 6260 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6261 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6262 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6263 | #ifdef __APPLE__ |
| 6264 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 6265 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 6266 | * always first call getgroups with length 0 to get the actual number |
| 6267 | * of groups. |
| 6268 | */ |
| 6269 | n = getgroups(0, NULL); |
| 6270 | if (n < 0) { |
| 6271 | return posix_error(); |
| 6272 | } else if (n <= MAX_GROUPS) { |
| 6273 | /* groups will fit in existing array */ |
| 6274 | alt_grouplist = grouplist; |
| 6275 | } else { |
| 6276 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 6277 | if (alt_grouplist == NULL) { |
| 6278 | errno = EINVAL; |
| 6279 | return posix_error(); |
| 6280 | } |
| 6281 | } |
| 6282 | |
| 6283 | n = getgroups(n, alt_grouplist); |
| 6284 | if (n == -1) { |
| 6285 | if (alt_grouplist != grouplist) { |
| 6286 | PyMem_Free(alt_grouplist); |
| 6287 | } |
| 6288 | return posix_error(); |
| 6289 | } |
| 6290 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6291 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6292 | if (n < 0) { |
| 6293 | if (errno == EINVAL) { |
| 6294 | n = getgroups(0, NULL); |
| 6295 | if (n == -1) { |
| 6296 | return posix_error(); |
| 6297 | } |
| 6298 | if (n == 0) { |
| 6299 | /* Avoid malloc(0) */ |
| 6300 | alt_grouplist = grouplist; |
| 6301 | } else { |
| 6302 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 6303 | if (alt_grouplist == NULL) { |
| 6304 | errno = EINVAL; |
| 6305 | return posix_error(); |
| 6306 | } |
| 6307 | n = getgroups(n, alt_grouplist); |
| 6308 | if (n == -1) { |
| 6309 | PyMem_Free(alt_grouplist); |
| 6310 | return posix_error(); |
| 6311 | } |
| 6312 | } |
| 6313 | } else { |
| 6314 | return posix_error(); |
| 6315 | } |
| 6316 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6317 | #endif |
| 6318 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6319 | result = PyList_New(n); |
| 6320 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6321 | int i; |
| 6322 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6323 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6324 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6325 | Py_DECREF(result); |
| 6326 | result = NULL; |
| 6327 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6328 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6329 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6330 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6331 | } |
| 6332 | |
| 6333 | if (alt_grouplist != grouplist) { |
| 6334 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6335 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6336 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6337 | return result; |
| 6338 | } |
| 6339 | #endif |
| 6340 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6341 | #ifdef HAVE_INITGROUPS |
| 6342 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 6343 | "initgroups(username, gid) -> None\n\n\ |
| 6344 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 6345 | the groups of which the specified username is a member, plus the specified\n\ |
| 6346 | group id."); |
| 6347 | |
| 6348 | static PyObject * |
| 6349 | posix_initgroups(PyObject *self, PyObject *args) |
| 6350 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6351 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6352 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6353 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6354 | #ifdef __APPLE__ |
| 6355 | int gid; |
| 6356 | #else |
| 6357 | gid_t gid; |
| 6358 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6359 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6360 | #ifdef __APPLE__ |
| 6361 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 6362 | PyUnicode_FSConverter, &oname, |
| 6363 | &gid)) |
| 6364 | #else |
| 6365 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 6366 | PyUnicode_FSConverter, &oname, |
| 6367 | _Py_Gid_Converter, &gid)) |
| 6368 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6369 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6370 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6371 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6372 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6373 | Py_DECREF(oname); |
| 6374 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6375 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6376 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6377 | Py_INCREF(Py_None); |
| 6378 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6379 | } |
| 6380 | #endif |
| 6381 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6382 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 6383 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6384 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 6385 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6386 | |
| 6387 | static PyObject * |
| 6388 | posix_getpgid(PyObject *self, PyObject *args) |
| 6389 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6390 | pid_t pid, pgid; |
| 6391 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 6392 | return NULL; |
| 6393 | pgid = getpgid(pid); |
| 6394 | if (pgid < 0) |
| 6395 | return posix_error(); |
| 6396 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6397 | } |
| 6398 | #endif /* HAVE_GETPGID */ |
| 6399 | |
| 6400 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6401 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6402 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6403 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6404 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6405 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6406 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6407 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6408 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6409 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6410 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6411 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6412 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6413 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6414 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6415 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6416 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6417 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6418 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6419 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6420 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 6421 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6422 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6423 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6424 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6425 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6426 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6427 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6428 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6429 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6430 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6431 | return posix_error(); |
| 6432 | Py_INCREF(Py_None); |
| 6433 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6434 | } |
| 6435 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6436 | #endif /* HAVE_SETPGRP */ |
| 6437 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6438 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6439 | |
| 6440 | #ifdef MS_WINDOWS |
| 6441 | #include <tlhelp32.h> |
| 6442 | |
| 6443 | static PyObject* |
| 6444 | win32_getppid() |
| 6445 | { |
| 6446 | HANDLE snapshot; |
| 6447 | pid_t mypid; |
| 6448 | PyObject* result = NULL; |
| 6449 | BOOL have_record; |
| 6450 | PROCESSENTRY32 pe; |
| 6451 | |
| 6452 | mypid = getpid(); /* This function never fails */ |
| 6453 | |
| 6454 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 6455 | if (snapshot == INVALID_HANDLE_VALUE) |
| 6456 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 6457 | |
| 6458 | pe.dwSize = sizeof(pe); |
| 6459 | have_record = Process32First(snapshot, &pe); |
| 6460 | while (have_record) { |
| 6461 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 6462 | /* We could cache the ulong value in a static variable. */ |
| 6463 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 6464 | break; |
| 6465 | } |
| 6466 | |
| 6467 | have_record = Process32Next(snapshot, &pe); |
| 6468 | } |
| 6469 | |
| 6470 | /* If our loop exits and our pid was not found (result will be NULL) |
| 6471 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 6472 | * error anyway, so let's raise it. */ |
| 6473 | if (!result) |
| 6474 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6475 | |
| 6476 | CloseHandle(snapshot); |
| 6477 | |
| 6478 | return result; |
| 6479 | } |
| 6480 | #endif /*MS_WINDOWS*/ |
| 6481 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6482 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6483 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6484 | Return the parent's process id. If the parent process has already exited,\n\ |
| 6485 | Windows machines will still return its id; others systems will return the id\n\ |
| 6486 | of the 'init' process (1)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6487 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6488 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6489 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6490 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6491 | #ifdef MS_WINDOWS |
| 6492 | return win32_getppid(); |
| 6493 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6494 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6495 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6496 | } |
| 6497 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6498 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6499 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6500 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6501 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6502 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6503 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6504 | |
| 6505 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6506 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6507 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6508 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6509 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6510 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6511 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6512 | |
| 6513 | if (GetUserNameW(user_name, &num_chars)) { |
| 6514 | /* num_chars is the number of unicode chars plus null terminator */ |
| 6515 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6516 | } |
| 6517 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6518 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6519 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6520 | char *name; |
| 6521 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6522 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6523 | errno = 0; |
| 6524 | name = getlogin(); |
| 6525 | if (name == NULL) { |
| 6526 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6527 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6528 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6529 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6530 | } |
| 6531 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6532 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6533 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6534 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6535 | return result; |
| 6536 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6537 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6538 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6539 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6540 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6541 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6542 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6543 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6544 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6545 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6546 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6547 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6548 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6549 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6550 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6551 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6552 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6553 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6554 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6555 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6556 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6557 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6558 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6559 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6560 | pid_t pid; |
| 6561 | int sig; |
| 6562 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 6563 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6564 | if (kill(pid, sig) == -1) |
| 6565 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6566 | Py_INCREF(Py_None); |
| 6567 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6568 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6569 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6570 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6571 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6572 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6573 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6574 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6575 | |
| 6576 | static PyObject * |
| 6577 | posix_killpg(PyObject *self, PyObject *args) |
| 6578 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6579 | int sig; |
| 6580 | pid_t pgid; |
| 6581 | /* XXX some man pages make the `pgid` parameter an int, others |
| 6582 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 6583 | take the same type. Moreover, pid_t is always at least as wide as |
| 6584 | int (else compilation of this module fails), which is safe. */ |
| 6585 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 6586 | return NULL; |
| 6587 | if (killpg(pgid, sig) == -1) |
| 6588 | return posix_error(); |
| 6589 | Py_INCREF(Py_None); |
| 6590 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6591 | } |
| 6592 | #endif |
| 6593 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6594 | #ifdef MS_WINDOWS |
| 6595 | PyDoc_STRVAR(win32_kill__doc__, |
| 6596 | "kill(pid, sig)\n\n\ |
| 6597 | Kill a process with a signal."); |
| 6598 | |
| 6599 | static PyObject * |
| 6600 | win32_kill(PyObject *self, PyObject *args) |
| 6601 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 6602 | PyObject *result; |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6603 | pid_t pid; |
| 6604 | DWORD sig, err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6605 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6606 | |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6607 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "k:kill", &pid, &sig)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6608 | return NULL; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6609 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6610 | /* Console processes which share a common console can be sent CTRL+C or |
| 6611 | CTRL+BREAK events, provided they handle said events. */ |
| 6612 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6613 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6614 | err = GetLastError(); |
| 6615 | PyErr_SetFromWindowsErr(err); |
| 6616 | } |
| 6617 | else |
| 6618 | Py_RETURN_NONE; |
| 6619 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6620 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6621 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 6622 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6623 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6624 | if (handle == NULL) { |
| 6625 | err = GetLastError(); |
| 6626 | return PyErr_SetFromWindowsErr(err); |
| 6627 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6628 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6629 | if (TerminateProcess(handle, sig) == 0) { |
| 6630 | err = GetLastError(); |
| 6631 | result = PyErr_SetFromWindowsErr(err); |
| 6632 | } else { |
| 6633 | Py_INCREF(Py_None); |
| 6634 | result = Py_None; |
| 6635 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6636 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6637 | CloseHandle(handle); |
| 6638 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6639 | } |
| 6640 | #endif /* MS_WINDOWS */ |
| 6641 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6642 | #ifdef HAVE_PLOCK |
| 6643 | |
| 6644 | #ifdef HAVE_SYS_LOCK_H |
| 6645 | #include <sys/lock.h> |
| 6646 | #endif |
| 6647 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6648 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6649 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6650 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6651 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6652 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6653 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6654 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6655 | int op; |
| 6656 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 6657 | return NULL; |
| 6658 | if (plock(op) == -1) |
| 6659 | return posix_error(); |
| 6660 | Py_INCREF(Py_None); |
| 6661 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6662 | } |
| 6663 | #endif |
| 6664 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6665 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6666 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6667 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6668 | Set the current process's user id."); |
| 6669 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6670 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6671 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6672 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6673 | uid_t uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6674 | if (!PyArg_ParseTuple(args, "O&:setuid", _Py_Uid_Converter, &uid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6675 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6676 | if (setuid(uid) < 0) |
| 6677 | return posix_error(); |
| 6678 | Py_INCREF(Py_None); |
| 6679 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6680 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6681 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6682 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6683 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6684 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6685 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6686 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6687 | Set the current process's effective user id."); |
| 6688 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6689 | static PyObject * |
| 6690 | posix_seteuid (PyObject *self, PyObject *args) |
| 6691 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6692 | uid_t euid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6693 | if (!PyArg_ParseTuple(args, "O&:seteuid", _Py_Uid_Converter, &euid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6694 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6695 | if (seteuid(euid) < 0) { |
| 6696 | return posix_error(); |
| 6697 | } else { |
| 6698 | Py_INCREF(Py_None); |
| 6699 | return Py_None; |
| 6700 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6701 | } |
| 6702 | #endif /* HAVE_SETEUID */ |
| 6703 | |
| 6704 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6705 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6706 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6707 | Set the current process's effective group id."); |
| 6708 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6709 | static PyObject * |
| 6710 | posix_setegid (PyObject *self, PyObject *args) |
| 6711 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6712 | gid_t egid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6713 | if (!PyArg_ParseTuple(args, "O&:setegid", _Py_Gid_Converter, &egid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6714 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6715 | if (setegid(egid) < 0) { |
| 6716 | return posix_error(); |
| 6717 | } else { |
| 6718 | Py_INCREF(Py_None); |
| 6719 | return Py_None; |
| 6720 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6721 | } |
| 6722 | #endif /* HAVE_SETEGID */ |
| 6723 | |
| 6724 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6725 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6726 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6727 | Set the current process's real and effective user ids."); |
| 6728 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6729 | static PyObject * |
| 6730 | posix_setreuid (PyObject *self, PyObject *args) |
| 6731 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6732 | uid_t ruid, euid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6733 | if (!PyArg_ParseTuple(args, "O&O&:setreuid", |
| 6734 | _Py_Uid_Converter, &ruid, |
| 6735 | _Py_Uid_Converter, &euid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6736 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6737 | if (setreuid(ruid, euid) < 0) { |
| 6738 | return posix_error(); |
| 6739 | } else { |
| 6740 | Py_INCREF(Py_None); |
| 6741 | return Py_None; |
| 6742 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6743 | } |
| 6744 | #endif /* HAVE_SETREUID */ |
| 6745 | |
| 6746 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6747 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6748 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6749 | Set the current process's real and effective group ids."); |
| 6750 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6751 | static PyObject * |
| 6752 | posix_setregid (PyObject *self, PyObject *args) |
| 6753 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6754 | gid_t rgid, egid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6755 | if (!PyArg_ParseTuple(args, "O&O&:setregid", |
| 6756 | _Py_Gid_Converter, &rgid, |
| 6757 | _Py_Gid_Converter, &egid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6758 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6759 | if (setregid(rgid, egid) < 0) { |
| 6760 | return posix_error(); |
| 6761 | } else { |
| 6762 | Py_INCREF(Py_None); |
| 6763 | return Py_None; |
| 6764 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6765 | } |
| 6766 | #endif /* HAVE_SETREGID */ |
| 6767 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6768 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6769 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6770 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6771 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6772 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6773 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6774 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6775 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6776 | gid_t gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6777 | if (!PyArg_ParseTuple(args, "O&:setgid", _Py_Gid_Converter, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6778 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6779 | if (setgid(gid) < 0) |
| 6780 | return posix_error(); |
| 6781 | Py_INCREF(Py_None); |
| 6782 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6783 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6784 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6785 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6786 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6787 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6788 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6789 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6790 | |
| 6791 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6792 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6793 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6794 | int i, len; |
| 6795 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6796 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6797 | if (!PySequence_Check(groups)) { |
| 6798 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6799 | return NULL; |
| 6800 | } |
| 6801 | len = PySequence_Size(groups); |
| 6802 | if (len > MAX_GROUPS) { |
| 6803 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6804 | return NULL; |
| 6805 | } |
| 6806 | for(i = 0; i < len; i++) { |
| 6807 | PyObject *elem; |
| 6808 | elem = PySequence_GetItem(groups, i); |
| 6809 | if (!elem) |
| 6810 | return NULL; |
| 6811 | if (!PyLong_Check(elem)) { |
| 6812 | PyErr_SetString(PyExc_TypeError, |
| 6813 | "groups must be integers"); |
| 6814 | Py_DECREF(elem); |
| 6815 | return NULL; |
| 6816 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6817 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6818 | Py_DECREF(elem); |
| 6819 | return NULL; |
| 6820 | } |
| 6821 | } |
| 6822 | Py_DECREF(elem); |
| 6823 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6824 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6825 | if (setgroups(len, grouplist) < 0) |
| 6826 | return posix_error(); |
| 6827 | Py_INCREF(Py_None); |
| 6828 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6829 | } |
| 6830 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6831 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6832 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6833 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6834 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6835 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6836 | PyObject *result; |
| 6837 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6838 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6839 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6840 | if (pid == -1) |
| 6841 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6842 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6843 | if (struct_rusage == NULL) { |
| 6844 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6845 | if (m == NULL) |
| 6846 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6847 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6848 | Py_DECREF(m); |
| 6849 | if (struct_rusage == NULL) |
| 6850 | return NULL; |
| 6851 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6852 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6853 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6854 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6855 | if (!result) |
| 6856 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6857 | |
| 6858 | #ifndef doubletime |
| 6859 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6860 | #endif |
| 6861 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6862 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6863 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6864 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6865 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6866 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6867 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6868 | SET_INT(result, 2, ru->ru_maxrss); |
| 6869 | SET_INT(result, 3, ru->ru_ixrss); |
| 6870 | SET_INT(result, 4, ru->ru_idrss); |
| 6871 | SET_INT(result, 5, ru->ru_isrss); |
| 6872 | SET_INT(result, 6, ru->ru_minflt); |
| 6873 | SET_INT(result, 7, ru->ru_majflt); |
| 6874 | SET_INT(result, 8, ru->ru_nswap); |
| 6875 | SET_INT(result, 9, ru->ru_inblock); |
| 6876 | SET_INT(result, 10, ru->ru_oublock); |
| 6877 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6878 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6879 | SET_INT(result, 13, ru->ru_nsignals); |
| 6880 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6881 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6882 | #undef SET_INT |
| 6883 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6884 | if (PyErr_Occurred()) { |
| 6885 | Py_DECREF(result); |
| 6886 | return NULL; |
| 6887 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6888 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6889 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6890 | } |
| 6891 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6892 | |
| 6893 | #ifdef HAVE_WAIT3 |
| 6894 | PyDoc_STRVAR(posix_wait3__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6895 | "wait3(options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6896 | Wait for completion of a child process."); |
| 6897 | |
| 6898 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6899 | posix_wait3(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6900 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6901 | pid_t pid; |
| 6902 | int options; |
| 6903 | struct rusage ru; |
| 6904 | WAIT_TYPE status; |
| 6905 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6906 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6907 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6908 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6909 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6910 | Py_BEGIN_ALLOW_THREADS |
| 6911 | pid = wait3(&status, options, &ru); |
| 6912 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6913 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6914 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6915 | } |
| 6916 | #endif /* HAVE_WAIT3 */ |
| 6917 | |
| 6918 | #ifdef HAVE_WAIT4 |
| 6919 | PyDoc_STRVAR(posix_wait4__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6920 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6921 | Wait for completion of a given child process."); |
| 6922 | |
| 6923 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6924 | posix_wait4(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6925 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6926 | pid_t pid; |
| 6927 | int options; |
| 6928 | struct rusage ru; |
| 6929 | WAIT_TYPE status; |
| 6930 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6931 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6932 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6933 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6934 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6935 | Py_BEGIN_ALLOW_THREADS |
| 6936 | pid = wait4(pid, &status, options, &ru); |
| 6937 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6938 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6939 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6940 | } |
| 6941 | #endif /* HAVE_WAIT4 */ |
| 6942 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6943 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 6944 | PyDoc_STRVAR(posix_waitid__doc__, |
| 6945 | "waitid(idtype, id, options) -> waitid_result\n\n\ |
| 6946 | Wait for the completion of one or more child processes.\n\n\ |
| 6947 | idtype can be P_PID, P_PGID or P_ALL.\n\ |
| 6948 | id specifies the pid to wait on.\n\ |
| 6949 | options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\ |
| 6950 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\ |
| 6951 | Returns either waitid_result or None if WNOHANG is specified and there are\n\ |
| 6952 | no children in a waitable state."); |
| 6953 | |
| 6954 | static PyObject * |
| 6955 | posix_waitid(PyObject *self, PyObject *args) |
| 6956 | { |
| 6957 | PyObject *result; |
| 6958 | idtype_t idtype; |
| 6959 | id_t id; |
| 6960 | int options, res; |
| 6961 | siginfo_t si; |
| 6962 | si.si_pid = 0; |
| 6963 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options)) |
| 6964 | return NULL; |
| 6965 | Py_BEGIN_ALLOW_THREADS |
| 6966 | res = waitid(idtype, id, &si, options); |
| 6967 | Py_END_ALLOW_THREADS |
| 6968 | if (res == -1) |
| 6969 | return posix_error(); |
| 6970 | |
| 6971 | if (si.si_pid == 0) |
| 6972 | Py_RETURN_NONE; |
| 6973 | |
| 6974 | result = PyStructSequence_New(&WaitidResultType); |
| 6975 | if (!result) |
| 6976 | return NULL; |
| 6977 | |
| 6978 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6979 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6980 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6981 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6982 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6983 | if (PyErr_Occurred()) { |
| 6984 | Py_DECREF(result); |
| 6985 | return NULL; |
| 6986 | } |
| 6987 | |
| 6988 | return result; |
| 6989 | } |
| 6990 | #endif |
| 6991 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6992 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6993 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6994 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6995 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6996 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6997 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6998 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6999 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7000 | pid_t pid; |
| 7001 | int options; |
| 7002 | WAIT_TYPE status; |
| 7003 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7004 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7005 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 7006 | return NULL; |
| 7007 | Py_BEGIN_ALLOW_THREADS |
| 7008 | pid = waitpid(pid, &status, options); |
| 7009 | Py_END_ALLOW_THREADS |
| 7010 | if (pid == -1) |
| 7011 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7012 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7013 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7014 | } |
| 7015 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7016 | #elif defined(HAVE_CWAIT) |
| 7017 | |
| 7018 | /* MS C has a variant of waitpid() that's usable for most purposes. */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7019 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7020 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7021 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7022 | |
| 7023 | static PyObject * |
| 7024 | posix_waitpid(PyObject *self, PyObject *args) |
| 7025 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7026 | Py_intptr_t pid; |
| 7027 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7028 | |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7029 | if (!PyArg_ParseTuple(args, _Py_PARSE_INTPTR "i:waitpid", &pid, &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7030 | return NULL; |
| 7031 | Py_BEGIN_ALLOW_THREADS |
| 7032 | pid = _cwait(&status, pid, options); |
| 7033 | Py_END_ALLOW_THREADS |
| 7034 | if (pid == -1) |
| 7035 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7036 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7037 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7038 | return Py_BuildValue(_Py_PARSE_INTPTR "i", pid, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7039 | } |
| 7040 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7041 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7042 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7043 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7044 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7045 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7046 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7047 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7048 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7049 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7050 | pid_t pid; |
| 7051 | WAIT_TYPE status; |
| 7052 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7053 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7054 | Py_BEGIN_ALLOW_THREADS |
| 7055 | pid = wait(&status); |
| 7056 | Py_END_ALLOW_THREADS |
| 7057 | if (pid == -1) |
| 7058 | return posix_error(); |
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 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7062 | #endif |
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 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7078 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7079 | posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7080 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7081 | path_t path; |
| 7082 | int dir_fd = DEFAULT_DIR_FD; |
| 7083 | char buffer[MAXPATHLEN]; |
| 7084 | ssize_t length; |
| 7085 | PyObject *return_value = NULL; |
| 7086 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7087 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7088 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7089 | path.function_name = "readlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7090 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords, |
| 7091 | path_converter, &path, |
| 7092 | #ifdef HAVE_READLINKAT |
| 7093 | dir_fd_converter, &dir_fd |
| 7094 | #else |
| 7095 | dir_fd_unavailable, &dir_fd |
| 7096 | #endif |
| 7097 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7098 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7099 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7100 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7101 | #ifdef HAVE_READLINKAT |
| 7102 | if (dir_fd != DEFAULT_DIR_FD) |
| 7103 | length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer)); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 7104 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7105 | #endif |
| 7106 | length = readlink(path.narrow, buffer, sizeof(buffer)); |
| 7107 | Py_END_ALLOW_THREADS |
| 7108 | |
| 7109 | if (length < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7110 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7111 | goto exit; |
| 7112 | } |
| 7113 | |
| 7114 | if (PyUnicode_Check(path.object)) |
| 7115 | return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 7116 | else |
| 7117 | return_value = PyBytes_FromStringAndSize(buffer, length); |
| 7118 | exit: |
| 7119 | path_cleanup(&path); |
| 7120 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7121 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7122 | |
| 7123 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7124 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7125 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7126 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7127 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7128 | PyDoc_STRVAR(posix_symlink__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7129 | "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 7130 | Create a symbolic link pointing to src named dst.\n\n\ |
| 7131 | target_is_directory is required on Windows if the target is to be\n\ |
| 7132 | interpreted as a directory. (On Windows, symlink requires\n\ |
| 7133 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.)\n\ |
| 7134 | target_is_directory is ignored on non-Windows platforms.\n\ |
| 7135 | \n\ |
| 7136 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7137 | and path should be relative; path will then be relative to that directory.\n\ |
| 7138 | dir_fd may not be implemented on your platform.\n\ |
| 7139 | If it is unavailable, using it will raise a NotImplementedError."); |
| 7140 | |
| 7141 | #if defined(MS_WINDOWS) |
| 7142 | |
| 7143 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 7144 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD) = NULL; |
| 7145 | static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPSTR, LPSTR, DWORD) = NULL; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7146 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7147 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7148 | check_CreateSymbolicLink(void) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7149 | { |
| 7150 | HINSTANCE hKernel32; |
| 7151 | /* only recheck */ |
| 7152 | if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA) |
| 7153 | return 1; |
| 7154 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 7155 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 7156 | "CreateSymbolicLinkW"); |
| 7157 | *(FARPROC*)&Py_CreateSymbolicLinkA = GetProcAddress(hKernel32, |
| 7158 | "CreateSymbolicLinkA"); |
| 7159 | return (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA); |
| 7160 | } |
| 7161 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7162 | /* Remove the last portion of the path */ |
| 7163 | static void |
| 7164 | _dirnameW(WCHAR *path) |
| 7165 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7166 | WCHAR *ptr; |
| 7167 | |
| 7168 | /* walk the path from the end until a backslash is encountered */ |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7169 | for(ptr = path + wcslen(path); ptr != path; ptr--) { |
Victor Stinner | 072318b | 2013-06-05 02:07:46 +0200 | [diff] [blame] | 7170 | if (*ptr == L'\\' || *ptr == L'/') |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7171 | break; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7172 | } |
| 7173 | *ptr = 0; |
| 7174 | } |
| 7175 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7176 | /* Remove the last portion of the path */ |
| 7177 | static void |
| 7178 | _dirnameA(char *path) |
| 7179 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7180 | char *ptr; |
| 7181 | |
| 7182 | /* walk the path from the end until a backslash is encountered */ |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7183 | for(ptr = path + strlen(path); ptr != path; ptr--) { |
| 7184 | if (*ptr == '\\' || *ptr == '/') |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7185 | break; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7186 | } |
| 7187 | *ptr = 0; |
| 7188 | } |
| 7189 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7190 | /* Is this path absolute? */ |
| 7191 | static int |
| 7192 | _is_absW(const WCHAR *path) |
| 7193 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7194 | return path[0] == L'\\' || path[0] == L'/' || path[1] == L':'; |
| 7195 | |
| 7196 | } |
| 7197 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7198 | /* Is this path absolute? */ |
| 7199 | static int |
| 7200 | _is_absA(const char *path) |
| 7201 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7202 | return path[0] == '\\' || path[0] == '/' || path[1] == ':'; |
| 7203 | |
| 7204 | } |
| 7205 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7206 | /* join root and rest with a backslash */ |
| 7207 | static void |
| 7208 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 7209 | { |
Victor Stinner | e7e7eba | 2013-06-05 00:35:54 +0200 | [diff] [blame] | 7210 | size_t root_len; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7211 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7212 | if (_is_absW(rest)) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7213 | wcscpy(dest_path, rest); |
| 7214 | return; |
| 7215 | } |
| 7216 | |
| 7217 | root_len = wcslen(root); |
| 7218 | |
| 7219 | wcscpy(dest_path, root); |
| 7220 | if(root_len) { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7221 | dest_path[root_len] = L'\\'; |
| 7222 | root_len++; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7223 | } |
| 7224 | wcscpy(dest_path+root_len, rest); |
| 7225 | } |
| 7226 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7227 | /* join root and rest with a backslash */ |
| 7228 | static void |
| 7229 | _joinA(char *dest_path, const char *root, const char *rest) |
| 7230 | { |
Victor Stinner | e7e7eba | 2013-06-05 00:35:54 +0200 | [diff] [blame] | 7231 | size_t root_len; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7232 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7233 | if (_is_absA(rest)) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7234 | strcpy(dest_path, rest); |
| 7235 | return; |
| 7236 | } |
| 7237 | |
| 7238 | root_len = strlen(root); |
| 7239 | |
| 7240 | strcpy(dest_path, root); |
| 7241 | if(root_len) { |
| 7242 | dest_path[root_len] = '\\'; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7243 | root_len++; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7244 | } |
| 7245 | strcpy(dest_path+root_len, rest); |
| 7246 | } |
| 7247 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7248 | /* Return True if the path at src relative to dest is a directory */ |
| 7249 | static int |
| 7250 | _check_dirW(WCHAR *src, WCHAR *dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7251 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7252 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7253 | WCHAR dest_parent[MAX_PATH]; |
| 7254 | WCHAR src_resolved[MAX_PATH] = L""; |
| 7255 | |
| 7256 | /* dest_parent = os.path.dirname(dest) */ |
| 7257 | wcscpy(dest_parent, dest); |
| 7258 | _dirnameW(dest_parent); |
| 7259 | /* src_resolved = os.path.join(dest_parent, src) */ |
| 7260 | _joinW(src_resolved, dest_parent, src); |
| 7261 | return ( |
| 7262 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 7263 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7264 | ); |
| 7265 | } |
| 7266 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7267 | /* Return True if the path at src relative to dest is a directory */ |
| 7268 | static int |
| 7269 | _check_dirA(char *src, char *dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7270 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7271 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7272 | char dest_parent[MAX_PATH]; |
| 7273 | char src_resolved[MAX_PATH] = ""; |
| 7274 | |
| 7275 | /* dest_parent = os.path.dirname(dest) */ |
| 7276 | strcpy(dest_parent, dest); |
Victor Stinner | 5a43676 | 2013-06-05 00:37:12 +0200 | [diff] [blame] | 7277 | _dirnameA(dest_parent); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7278 | /* src_resolved = os.path.join(dest_parent, src) */ |
Victor Stinner | 5a43676 | 2013-06-05 00:37:12 +0200 | [diff] [blame] | 7279 | _joinA(src_resolved, dest_parent, src); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7280 | return ( |
| 7281 | GetFileAttributesExA(src_resolved, GetFileExInfoStandard, &src_info) |
| 7282 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7283 | ); |
| 7284 | } |
| 7285 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7286 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7287 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7288 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7289 | posix_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7290 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7291 | path_t src; |
| 7292 | path_t dst; |
| 7293 | int dir_fd = DEFAULT_DIR_FD; |
| 7294 | int target_is_directory = 0; |
| 7295 | static char *keywords[] = {"src", "dst", "target_is_directory", |
| 7296 | "dir_fd", NULL}; |
| 7297 | PyObject *return_value; |
| 7298 | #ifdef MS_WINDOWS |
| 7299 | DWORD result; |
| 7300 | #else |
| 7301 | int result; |
| 7302 | #endif |
| 7303 | |
| 7304 | memset(&src, 0, sizeof(src)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7305 | src.function_name = "symlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7306 | src.argument_name = "src"; |
| 7307 | memset(&dst, 0, sizeof(dst)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7308 | dst.function_name = "symlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7309 | dst.argument_name = "dst"; |
| 7310 | |
| 7311 | #ifdef MS_WINDOWS |
| 7312 | if (!check_CreateSymbolicLink()) { |
| 7313 | PyErr_SetString(PyExc_NotImplementedError, |
| 7314 | "CreateSymbolicLink functions not found"); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7315 | return NULL; |
| 7316 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7317 | if (!win32_can_symlink) { |
| 7318 | PyErr_SetString(PyExc_OSError, "symbolic link privilege not held"); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7319 | return NULL; |
| 7320 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7321 | #endif |
| 7322 | |
| 7323 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|i$O&:symlink", |
| 7324 | keywords, |
| 7325 | path_converter, &src, |
| 7326 | path_converter, &dst, |
| 7327 | &target_is_directory, |
| 7328 | #ifdef HAVE_SYMLINKAT |
| 7329 | dir_fd_converter, &dir_fd |
| 7330 | #else |
| 7331 | dir_fd_unavailable, &dir_fd |
| 7332 | #endif |
| 7333 | )) |
| 7334 | return NULL; |
| 7335 | |
| 7336 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 7337 | PyErr_SetString(PyExc_ValueError, |
| 7338 | "symlink: src and dst must be the same type"); |
| 7339 | return_value = NULL; |
| 7340 | goto exit; |
| 7341 | } |
| 7342 | |
| 7343 | #ifdef MS_WINDOWS |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7344 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7345 | Py_BEGIN_ALLOW_THREADS |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7346 | if (dst.wide) { |
| 7347 | /* if src is a directory, ensure target_is_directory==1 */ |
| 7348 | target_is_directory |= _check_dirW(src.wide, dst.wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7349 | result = Py_CreateSymbolicLinkW(dst.wide, src.wide, |
| 7350 | target_is_directory); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7351 | } |
| 7352 | else { |
| 7353 | /* if src is a directory, ensure target_is_directory==1 */ |
| 7354 | target_is_directory |= _check_dirA(src.narrow, dst.narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7355 | result = Py_CreateSymbolicLinkA(dst.narrow, src.narrow, |
| 7356 | target_is_directory); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7357 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7358 | Py_END_ALLOW_THREADS |
| 7359 | |
| 7360 | if (!result) { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 7361 | return_value = path_error2(&src, &dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7362 | goto exit; |
| 7363 | } |
| 7364 | |
| 7365 | #else |
| 7366 | |
| 7367 | Py_BEGIN_ALLOW_THREADS |
| 7368 | #if HAVE_SYMLINKAT |
| 7369 | if (dir_fd != DEFAULT_DIR_FD) |
| 7370 | result = symlinkat(src.narrow, dir_fd, dst.narrow); |
| 7371 | else |
| 7372 | #endif |
| 7373 | result = symlink(src.narrow, dst.narrow); |
| 7374 | Py_END_ALLOW_THREADS |
| 7375 | |
| 7376 | if (result) { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 7377 | return_value = path_error2(&src, &dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7378 | goto exit; |
| 7379 | } |
| 7380 | #endif |
| 7381 | |
| 7382 | return_value = Py_None; |
| 7383 | Py_INCREF(Py_None); |
| 7384 | goto exit; /* silence "unused label" warning */ |
| 7385 | exit: |
| 7386 | path_cleanup(&src); |
| 7387 | path_cleanup(&dst); |
| 7388 | return return_value; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7389 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7390 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7391 | #endif /* HAVE_SYMLINK */ |
| 7392 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7393 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7394 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 7395 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7396 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7397 | win_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7398 | { |
| 7399 | wchar_t *path; |
| 7400 | DWORD n_bytes_returned; |
| 7401 | DWORD io_result; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7402 | PyObject *po, *result; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7403 | int dir_fd; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7404 | HANDLE reparse_point_handle; |
| 7405 | |
| 7406 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 7407 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 7408 | wchar_t *print_name; |
| 7409 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7410 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 7411 | |
| 7412 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords, |
| 7413 | &po, |
| 7414 | dir_fd_unavailable, &dir_fd |
| 7415 | )) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7416 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7417 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7418 | path = PyUnicode_AsUnicode(po); |
| 7419 | if (path == NULL) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7420 | return NULL; |
| 7421 | |
| 7422 | /* First get a handle to the reparse point */ |
| 7423 | Py_BEGIN_ALLOW_THREADS |
| 7424 | reparse_point_handle = CreateFileW( |
| 7425 | path, |
| 7426 | 0, |
| 7427 | 0, |
| 7428 | 0, |
| 7429 | OPEN_EXISTING, |
| 7430 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 7431 | 0); |
| 7432 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7433 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7434 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7435 | return win32_error_object("readlink", po); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7436 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7437 | Py_BEGIN_ALLOW_THREADS |
| 7438 | /* New call DeviceIoControl to read the reparse point */ |
| 7439 | io_result = DeviceIoControl( |
| 7440 | reparse_point_handle, |
| 7441 | FSCTL_GET_REPARSE_POINT, |
| 7442 | 0, 0, /* in buffer */ |
| 7443 | target_buffer, sizeof(target_buffer), |
| 7444 | &n_bytes_returned, |
| 7445 | 0 /* we're not using OVERLAPPED_IO */ |
| 7446 | ); |
| 7447 | CloseHandle(reparse_point_handle); |
| 7448 | Py_END_ALLOW_THREADS |
| 7449 | |
| 7450 | if (io_result==0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7451 | return win32_error_object("readlink", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7452 | |
| 7453 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 7454 | { |
| 7455 | PyErr_SetString(PyExc_ValueError, |
| 7456 | "not a symbolic link"); |
| 7457 | return NULL; |
| 7458 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 7459 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7460 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 7461 | |
| 7462 | result = PyUnicode_FromWideChar(print_name, |
| 7463 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7464 | return result; |
| 7465 | } |
| 7466 | |
| 7467 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 7468 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7469 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7470 | static PyStructSequence_Field times_result_fields[] = { |
| 7471 | {"user", "user time"}, |
| 7472 | {"system", "system time"}, |
| 7473 | {"children_user", "user time of children"}, |
| 7474 | {"children_system", "system time of children"}, |
| 7475 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 7476 | {NULL} |
| 7477 | }; |
| 7478 | |
| 7479 | PyDoc_STRVAR(times_result__doc__, |
| 7480 | "times_result: Result from os.times().\n\n\ |
| 7481 | This object may be accessed either as a tuple of\n\ |
| 7482 | (user, system, children_user, children_system, elapsed),\n\ |
| 7483 | or via the attributes user, system, children_user, children_system,\n\ |
| 7484 | and elapsed.\n\ |
| 7485 | \n\ |
| 7486 | See os.times for more information."); |
| 7487 | |
| 7488 | static PyStructSequence_Desc times_result_desc = { |
| 7489 | "times_result", /* name */ |
| 7490 | times_result__doc__, /* doc */ |
| 7491 | times_result_fields, |
| 7492 | 5 |
| 7493 | }; |
| 7494 | |
| 7495 | static PyTypeObject TimesResultType; |
| 7496 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7497 | #ifdef MS_WINDOWS |
| 7498 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 7499 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7500 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7501 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7502 | |
| 7503 | static PyObject * |
| 7504 | build_times_result(double user, double system, |
| 7505 | double children_user, double children_system, |
| 7506 | double elapsed) |
| 7507 | { |
| 7508 | PyObject *value = PyStructSequence_New(&TimesResultType); |
| 7509 | if (value == NULL) |
| 7510 | return NULL; |
| 7511 | |
| 7512 | #define SET(i, field) \ |
| 7513 | { \ |
| 7514 | PyObject *o = PyFloat_FromDouble(field); \ |
| 7515 | if (!o) { \ |
| 7516 | Py_DECREF(value); \ |
| 7517 | return NULL; \ |
| 7518 | } \ |
| 7519 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 7520 | } \ |
| 7521 | |
| 7522 | SET(0, user); |
| 7523 | SET(1, system); |
| 7524 | SET(2, children_user); |
| 7525 | SET(3, children_system); |
| 7526 | SET(4, elapsed); |
| 7527 | |
| 7528 | #undef SET |
| 7529 | |
| 7530 | return value; |
| 7531 | } |
| 7532 | |
| 7533 | PyDoc_STRVAR(posix_times__doc__, |
| 7534 | "times() -> times_result\n\n\ |
| 7535 | Return an object containing floating point numbers indicating process\n\ |
| 7536 | times. The object behaves like a named tuple with these fields:\n\ |
| 7537 | (utime, stime, cutime, cstime, elapsed_time)"); |
| 7538 | |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 7539 | #if defined(MS_WINDOWS) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7540 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7541 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7542 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7543 | FILETIME create, exit, kernel, user; |
| 7544 | HANDLE hProc; |
| 7545 | hProc = GetCurrentProcess(); |
| 7546 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 7547 | /* The fields of a FILETIME structure are the hi and lo part |
| 7548 | of a 64-bit value expressed in 100 nanosecond units. |
| 7549 | 1e7 is one second in such units; 1e-7 the inverse. |
| 7550 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 7551 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7552 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7553 | (double)(user.dwHighDateTime*429.4967296 + |
| 7554 | user.dwLowDateTime*1e-7), |
| 7555 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 7556 | kernel.dwLowDateTime*1e-7), |
| 7557 | (double)0, |
| 7558 | (double)0, |
| 7559 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7560 | } |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 7561 | #else /* Not Windows */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7562 | #define NEED_TICKS_PER_SECOND |
| 7563 | static long ticks_per_second = -1; |
| 7564 | static PyObject * |
| 7565 | posix_times(PyObject *self, PyObject *noargs) |
| 7566 | { |
| 7567 | struct tms t; |
| 7568 | clock_t c; |
| 7569 | errno = 0; |
| 7570 | c = times(&t); |
| 7571 | if (c == (clock_t) -1) |
| 7572 | return posix_error(); |
| 7573 | return build_times_result( |
| 7574 | (double)t.tms_utime / ticks_per_second, |
| 7575 | (double)t.tms_stime / ticks_per_second, |
| 7576 | (double)t.tms_cutime / ticks_per_second, |
| 7577 | (double)t.tms_cstime / ticks_per_second, |
| 7578 | (double)c / ticks_per_second); |
| 7579 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7580 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7581 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7582 | #endif /* HAVE_TIMES */ |
| 7583 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7584 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7585 | #ifdef HAVE_GETSID |
| 7586 | PyDoc_STRVAR(posix_getsid__doc__, |
| 7587 | "getsid(pid) -> sid\n\n\ |
| 7588 | Call the system call getsid()."); |
| 7589 | |
| 7590 | static PyObject * |
| 7591 | posix_getsid(PyObject *self, PyObject *args) |
| 7592 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7593 | pid_t pid; |
| 7594 | int sid; |
| 7595 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 7596 | return NULL; |
| 7597 | sid = getsid(pid); |
| 7598 | if (sid < 0) |
| 7599 | return posix_error(); |
| 7600 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7601 | } |
| 7602 | #endif /* HAVE_GETSID */ |
| 7603 | |
| 7604 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7605 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7606 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7607 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7608 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7609 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7610 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7611 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7612 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7613 | if (setsid() < 0) |
| 7614 | return posix_error(); |
| 7615 | Py_INCREF(Py_None); |
| 7616 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7617 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7618 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7619 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7620 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7621 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7622 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7623 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7624 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7625 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7626 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7627 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7628 | pid_t pid; |
| 7629 | int pgrp; |
| 7630 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 7631 | return NULL; |
| 7632 | if (setpgid(pid, pgrp) < 0) |
| 7633 | return posix_error(); |
| 7634 | Py_INCREF(Py_None); |
| 7635 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7636 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7637 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7638 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7639 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7640 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7641 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7642 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7643 | Return the process group associated with the terminal given by a fd."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7644 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7645 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7646 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7647 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7648 | int fd; |
| 7649 | pid_t pgid; |
| 7650 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 7651 | return NULL; |
| 7652 | pgid = tcgetpgrp(fd); |
| 7653 | if (pgid < 0) |
| 7654 | return posix_error(); |
| 7655 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7656 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7657 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7658 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7659 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7660 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7661 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7662 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7663 | Set the process group associated with the terminal given by a fd."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7664 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7665 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7666 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7667 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7668 | int fd; |
| 7669 | pid_t pgid; |
| 7670 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 7671 | return NULL; |
| 7672 | if (tcsetpgrp(fd, pgid) < 0) |
| 7673 | return posix_error(); |
| 7674 | Py_INCREF(Py_None); |
| 7675 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7676 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7677 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7678 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7679 | /* Functions acting on file descriptors */ |
| 7680 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7681 | #ifdef O_CLOEXEC |
| 7682 | extern int _Py_open_cloexec_works; |
| 7683 | #endif |
| 7684 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7685 | PyDoc_STRVAR(posix_open__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7686 | "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 7687 | Open a file for low level IO. Returns a file handle (integer).\n\ |
| 7688 | \n\ |
| 7689 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7690 | and path should be relative; path will then be relative to that directory.\n\ |
| 7691 | dir_fd may not be implemented on your platform.\n\ |
| 7692 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7693 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7694 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7695 | posix_open(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7696 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7697 | path_t path; |
| 7698 | int flags; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7699 | int mode = 0777; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7700 | int dir_fd = DEFAULT_DIR_FD; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7701 | int fd; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7702 | PyObject *return_value = NULL; |
| 7703 | static char *keywords[] = {"path", "flags", "mode", "dir_fd", NULL}; |
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 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7710 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7711 | path.function_name = "open"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7712 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|i$O&:open", keywords, |
| 7713 | path_converter, &path, |
| 7714 | &flags, &mode, |
| 7715 | #ifdef HAVE_OPENAT |
| 7716 | dir_fd_converter, &dir_fd |
| 7717 | #else |
| 7718 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7719 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7720 | )) |
| 7721 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7722 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7723 | #ifdef MS_WINDOWS |
| 7724 | flags |= O_NOINHERIT; |
| 7725 | #elif defined(O_CLOEXEC) |
| 7726 | flags |= O_CLOEXEC; |
| 7727 | #endif |
| 7728 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7729 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7730 | #ifdef MS_WINDOWS |
| 7731 | if (path.wide) |
| 7732 | fd = _wopen(path.wide, flags, mode); |
| 7733 | else |
| 7734 | #endif |
| 7735 | #ifdef HAVE_OPENAT |
| 7736 | if (dir_fd != DEFAULT_DIR_FD) |
| 7737 | fd = openat(dir_fd, path.narrow, flags, mode); |
| 7738 | else |
| 7739 | #endif |
| 7740 | fd = open(path.narrow, flags, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7741 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7742 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7743 | if (fd == -1) { |
Victor Stinner | 4e7d2d4 | 2012-11-05 01:20:58 +0100 | [diff] [blame] | 7744 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path.object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7745 | goto exit; |
| 7746 | } |
| 7747 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7748 | #ifndef MS_WINDOWS |
| 7749 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 7750 | close(fd); |
| 7751 | goto exit; |
| 7752 | } |
| 7753 | #endif |
| 7754 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7755 | return_value = PyLong_FromLong((long)fd); |
| 7756 | |
| 7757 | exit: |
| 7758 | path_cleanup(&path); |
| 7759 | return return_value; |
| 7760 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7761 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7762 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7763 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7764 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7765 | |
Benjamin Peterson | 932bba3 | 2014-02-11 10:16:16 -0500 | [diff] [blame] | 7766 | /* |
| 7767 | The underscore at end of function name avoids a name clash with the libc |
| 7768 | function posix_close. |
| 7769 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7770 | static PyObject * |
Benjamin Peterson | 932bba3 | 2014-02-11 10:16:16 -0500 | [diff] [blame] | 7771 | posix_close_(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7772 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7773 | int fd, res; |
| 7774 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 7775 | return NULL; |
| 7776 | if (!_PyVerify_fd(fd)) |
| 7777 | return posix_error(); |
| 7778 | Py_BEGIN_ALLOW_THREADS |
| 7779 | res = close(fd); |
| 7780 | Py_END_ALLOW_THREADS |
| 7781 | if (res < 0) |
| 7782 | return posix_error(); |
| 7783 | Py_INCREF(Py_None); |
| 7784 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7785 | } |
| 7786 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7787 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7788 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7789 | "closerange(fd_low, fd_high)\n\n\ |
| 7790 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 7791 | |
| 7792 | static PyObject * |
| 7793 | posix_closerange(PyObject *self, PyObject *args) |
| 7794 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7795 | int fd_from, fd_to, i; |
| 7796 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 7797 | return NULL; |
| 7798 | Py_BEGIN_ALLOW_THREADS |
| 7799 | for (i = fd_from; i < fd_to; i++) |
| 7800 | if (_PyVerify_fd(i)) |
| 7801 | close(i); |
| 7802 | Py_END_ALLOW_THREADS |
| 7803 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7804 | } |
| 7805 | |
| 7806 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7807 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7808 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7809 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7810 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7811 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7812 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7813 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7814 | int fd; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7815 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7816 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 7817 | return NULL; |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 7818 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7819 | fd = _Py_dup(fd); |
| 7820 | if (fd == -1) |
| 7821 | return NULL; |
| 7822 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7823 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7824 | } |
| 7825 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7826 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7827 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 7828 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7829 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7830 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7831 | static PyObject * |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7832 | posix_dup2(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7833 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7834 | static char *keywords[] = {"fd", "fd2", "inheritable", NULL}; |
| 7835 | int fd, fd2; |
| 7836 | int inheritable = 1; |
| 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 | |
| 7844 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|i:dup2", keywords, |
| 7845 | &fd, &fd2, &inheritable)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7846 | return NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7847 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7848 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 7849 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7850 | |
| 7851 | #ifdef MS_WINDOWS |
| 7852 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7853 | res = dup2(fd, fd2); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7854 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7855 | if (res < 0) |
| 7856 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7857 | |
| 7858 | /* Character files like console cannot be make non-inheritable */ |
| 7859 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7860 | close(fd2); |
| 7861 | return NULL; |
| 7862 | } |
| 7863 | |
| 7864 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 7865 | Py_BEGIN_ALLOW_THREADS |
| 7866 | if (!inheritable) |
| 7867 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 7868 | else |
| 7869 | res = dup2(fd, fd2); |
| 7870 | Py_END_ALLOW_THREADS |
| 7871 | if (res < 0) |
| 7872 | return posix_error(); |
| 7873 | |
| 7874 | #else |
| 7875 | |
| 7876 | #ifdef HAVE_DUP3 |
| 7877 | if (!inheritable && dup3_works != 0) { |
| 7878 | Py_BEGIN_ALLOW_THREADS |
| 7879 | res = dup3(fd, fd2, O_CLOEXEC); |
| 7880 | Py_END_ALLOW_THREADS |
| 7881 | if (res < 0) { |
| 7882 | if (dup3_works == -1) |
| 7883 | dup3_works = (errno != ENOSYS); |
| 7884 | if (dup3_works) |
| 7885 | return posix_error(); |
| 7886 | } |
| 7887 | } |
| 7888 | |
| 7889 | if (inheritable || dup3_works == 0) |
| 7890 | { |
| 7891 | #endif |
| 7892 | Py_BEGIN_ALLOW_THREADS |
| 7893 | res = dup2(fd, fd2); |
| 7894 | Py_END_ALLOW_THREADS |
| 7895 | if (res < 0) |
| 7896 | return posix_error(); |
| 7897 | |
| 7898 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7899 | close(fd2); |
| 7900 | return NULL; |
| 7901 | } |
| 7902 | #ifdef HAVE_DUP3 |
| 7903 | } |
| 7904 | #endif |
| 7905 | |
| 7906 | #endif |
| 7907 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7908 | Py_INCREF(Py_None); |
| 7909 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7910 | } |
| 7911 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7912 | #ifdef HAVE_LOCKF |
| 7913 | PyDoc_STRVAR(posix_lockf__doc__, |
| 7914 | "lockf(fd, cmd, len)\n\n\ |
| 7915 | Apply, test or remove a POSIX lock on an open file descriptor.\n\n\ |
| 7916 | fd is an open file descriptor.\n\ |
| 7917 | cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\ |
| 7918 | F_TEST.\n\ |
| 7919 | len specifies the section of the file to lock."); |
| 7920 | |
| 7921 | static PyObject * |
| 7922 | posix_lockf(PyObject *self, PyObject *args) |
| 7923 | { |
| 7924 | int fd, cmd, res; |
| 7925 | off_t len; |
| 7926 | if (!PyArg_ParseTuple(args, "iiO&:lockf", |
| 7927 | &fd, &cmd, _parse_off_t, &len)) |
| 7928 | return NULL; |
| 7929 | |
| 7930 | Py_BEGIN_ALLOW_THREADS |
| 7931 | res = lockf(fd, cmd, len); |
| 7932 | Py_END_ALLOW_THREADS |
| 7933 | |
| 7934 | if (res < 0) |
| 7935 | return posix_error(); |
| 7936 | |
| 7937 | Py_RETURN_NONE; |
| 7938 | } |
| 7939 | #endif |
| 7940 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7941 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7942 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7943 | "lseek(fd, pos, how) -> newpos\n\n\ |
Victor Stinner | e83f899 | 2011-12-17 23:15:09 +0100 | [diff] [blame] | 7944 | Set the current position of a file descriptor.\n\ |
| 7945 | Return the new cursor position in bytes, starting from the beginning."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7946 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7947 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7948 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7949 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7950 | int fd, how; |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 7951 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7952 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7953 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7954 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7955 | #endif |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7956 | PyObject *posobj; |
| 7957 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7958 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7959 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7960 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 7961 | switch (how) { |
| 7962 | case 0: how = SEEK_SET; break; |
| 7963 | case 1: how = SEEK_CUR; break; |
| 7964 | case 2: how = SEEK_END; break; |
| 7965 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7966 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7967 | |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7968 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7969 | pos = PyLong_AsLong(posobj); |
| 7970 | #else |
| 7971 | pos = PyLong_AsLongLong(posobj); |
| 7972 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7973 | if (PyErr_Occurred()) |
| 7974 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7975 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7976 | if (!_PyVerify_fd(fd)) |
| 7977 | return posix_error(); |
| 7978 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 7979 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7980 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7981 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7982 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7983 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7984 | Py_END_ALLOW_THREADS |
| 7985 | if (res < 0) |
| 7986 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7987 | |
| 7988 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7989 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7990 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7991 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7992 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7993 | } |
| 7994 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7995 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7996 | PyDoc_STRVAR(posix_read__doc__, |
Benjamin Peterson | 3b08a29 | 2013-05-24 14:35:57 -0700 | [diff] [blame] | 7997 | "read(fd, buffersize) -> bytes\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7998 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7999 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8000 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8001 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8002 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8003 | int fd, size; |
| 8004 | Py_ssize_t n; |
| 8005 | PyObject *buffer; |
| 8006 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 8007 | return NULL; |
| 8008 | if (size < 0) { |
| 8009 | errno = EINVAL; |
| 8010 | return posix_error(); |
| 8011 | } |
| 8012 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 8013 | if (buffer == NULL) |
| 8014 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8015 | if (!_PyVerify_fd(fd)) { |
| 8016 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8017 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8018 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8019 | Py_BEGIN_ALLOW_THREADS |
| 8020 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 8021 | Py_END_ALLOW_THREADS |
| 8022 | if (n < 0) { |
| 8023 | Py_DECREF(buffer); |
| 8024 | return posix_error(); |
| 8025 | } |
| 8026 | if (n != size) |
| 8027 | _PyBytes_Resize(&buffer, n); |
| 8028 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8029 | } |
| 8030 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8031 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 8032 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8033 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8034 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 8035 | { |
| 8036 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8037 | Py_ssize_t blen, total = 0; |
| 8038 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8039 | *iov = PyMem_New(struct iovec, cnt); |
| 8040 | if (*iov == NULL) { |
| 8041 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8042 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8043 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8044 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8045 | *buf = PyMem_New(Py_buffer, cnt); |
| 8046 | if (*buf == NULL) { |
| 8047 | PyMem_Del(*iov); |
| 8048 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8049 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8050 | } |
| 8051 | |
| 8052 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8053 | PyObject *item = PySequence_GetItem(seq, i); |
| 8054 | if (item == NULL) |
| 8055 | goto fail; |
| 8056 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 8057 | Py_DECREF(item); |
| 8058 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8059 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8060 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8061 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8062 | blen = (*buf)[i].len; |
| 8063 | (*iov)[i].iov_len = blen; |
| 8064 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8065 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8066 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8067 | |
| 8068 | fail: |
| 8069 | PyMem_Del(*iov); |
| 8070 | for (j = 0; j < i; j++) { |
| 8071 | PyBuffer_Release(&(*buf)[j]); |
| 8072 | } |
| 8073 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8074 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8075 | } |
| 8076 | |
| 8077 | static void |
| 8078 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 8079 | { |
| 8080 | int i; |
| 8081 | PyMem_Del(iov); |
| 8082 | for (i = 0; i < cnt; i++) { |
| 8083 | PyBuffer_Release(&buf[i]); |
| 8084 | } |
| 8085 | PyMem_Del(buf); |
| 8086 | } |
| 8087 | #endif |
| 8088 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8089 | #ifdef HAVE_READV |
| 8090 | PyDoc_STRVAR(posix_readv__doc__, |
| 8091 | "readv(fd, buffers) -> bytesread\n\n\ |
Benjamin Peterson | e83ed43 | 2014-01-18 22:54:59 -0500 | [diff] [blame] | 8092 | Read from a file descriptor fd into a number of mutable, bytes-like\n\ |
| 8093 | objects (\"buffers\"). readv will transfer data into each buffer\n\ |
| 8094 | until it is full and then move on to the next buffer in the sequence\n\ |
| 8095 | to hold the rest of the data.\n\n\ |
| 8096 | readv returns the total number of bytes read (which may be less than\n\ |
| 8097 | the total capacity of all the buffers."); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8098 | |
| 8099 | static PyObject * |
| 8100 | posix_readv(PyObject *self, PyObject *args) |
| 8101 | { |
| 8102 | int fd, cnt; |
| 8103 | Py_ssize_t n; |
| 8104 | PyObject *seq; |
| 8105 | struct iovec *iov; |
| 8106 | Py_buffer *buf; |
| 8107 | |
| 8108 | if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq)) |
| 8109 | return NULL; |
| 8110 | if (!PySequence_Check(seq)) { |
| 8111 | PyErr_SetString(PyExc_TypeError, |
| 8112 | "readv() arg 2 must be a sequence"); |
| 8113 | return NULL; |
| 8114 | } |
| 8115 | cnt = PySequence_Size(seq); |
| 8116 | |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8117 | if (iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE) < 0) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8118 | return NULL; |
| 8119 | |
| 8120 | Py_BEGIN_ALLOW_THREADS |
| 8121 | n = readv(fd, iov, cnt); |
| 8122 | Py_END_ALLOW_THREADS |
| 8123 | |
| 8124 | iov_cleanup(iov, buf, cnt); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8125 | if (n < 0) |
| 8126 | return posix_error(); |
| 8127 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8128 | return PyLong_FromSsize_t(n); |
| 8129 | } |
| 8130 | #endif |
| 8131 | |
| 8132 | #ifdef HAVE_PREAD |
| 8133 | PyDoc_STRVAR(posix_pread__doc__, |
| 8134 | "pread(fd, buffersize, offset) -> string\n\n\ |
| 8135 | Read from a file descriptor, fd, at a position of offset. It will read up\n\ |
| 8136 | to buffersize number of bytes. The file offset remains unchanged."); |
| 8137 | |
| 8138 | static PyObject * |
| 8139 | posix_pread(PyObject *self, PyObject *args) |
| 8140 | { |
| 8141 | int fd, size; |
| 8142 | off_t offset; |
| 8143 | Py_ssize_t n; |
| 8144 | PyObject *buffer; |
| 8145 | if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset)) |
| 8146 | return NULL; |
| 8147 | |
| 8148 | if (size < 0) { |
| 8149 | errno = EINVAL; |
| 8150 | return posix_error(); |
| 8151 | } |
| 8152 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 8153 | if (buffer == NULL) |
| 8154 | return NULL; |
| 8155 | if (!_PyVerify_fd(fd)) { |
| 8156 | Py_DECREF(buffer); |
| 8157 | return posix_error(); |
| 8158 | } |
| 8159 | Py_BEGIN_ALLOW_THREADS |
| 8160 | n = pread(fd, PyBytes_AS_STRING(buffer), size, offset); |
| 8161 | Py_END_ALLOW_THREADS |
| 8162 | if (n < 0) { |
| 8163 | Py_DECREF(buffer); |
| 8164 | return posix_error(); |
| 8165 | } |
| 8166 | if (n != size) |
| 8167 | _PyBytes_Resize(&buffer, n); |
| 8168 | return buffer; |
| 8169 | } |
| 8170 | #endif |
| 8171 | |
| 8172 | PyDoc_STRVAR(posix_write__doc__, |
Benjamin Peterson | 3b08a29 | 2013-05-24 14:35:57 -0700 | [diff] [blame] | 8173 | "write(fd, data) -> byteswritten\n\n\ |
| 8174 | Write bytes to a file descriptor."); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8175 | |
| 8176 | static PyObject * |
| 8177 | posix_write(PyObject *self, PyObject *args) |
| 8178 | { |
| 8179 | Py_buffer pbuf; |
| 8180 | int fd; |
| 8181 | Py_ssize_t size, len; |
| 8182 | |
| 8183 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 8184 | return NULL; |
| 8185 | if (!_PyVerify_fd(fd)) { |
| 8186 | PyBuffer_Release(&pbuf); |
| 8187 | return posix_error(); |
| 8188 | } |
| 8189 | len = pbuf.len; |
| 8190 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 8191 | #ifdef MS_WINDOWS |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8192 | if (len > INT_MAX) |
| 8193 | len = INT_MAX; |
| 8194 | size = write(fd, pbuf.buf, (int)len); |
| 8195 | #else |
| 8196 | size = write(fd, pbuf.buf, len); |
| 8197 | #endif |
| 8198 | Py_END_ALLOW_THREADS |
| 8199 | PyBuffer_Release(&pbuf); |
| 8200 | if (size < 0) |
| 8201 | return posix_error(); |
| 8202 | return PyLong_FromSsize_t(size); |
| 8203 | } |
| 8204 | |
| 8205 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8206 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 8207 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 8208 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 8209 | -> byteswritten\n\ |
| 8210 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 8211 | |
| 8212 | static PyObject * |
| 8213 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 8214 | { |
| 8215 | int in, out; |
| 8216 | Py_ssize_t ret; |
| 8217 | off_t offset; |
| 8218 | |
| 8219 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 8220 | #ifndef __APPLE__ |
| 8221 | Py_ssize_t len; |
| 8222 | #endif |
| 8223 | PyObject *headers = NULL, *trailers = NULL; |
| 8224 | Py_buffer *hbuf, *tbuf; |
| 8225 | off_t sbytes; |
| 8226 | struct sf_hdtr sf; |
| 8227 | int flags = 0; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 8228 | static char *keywords[] = {"out", "in", |
| 8229 | "offset", "count", |
| 8230 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8231 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 8232 | sf.headers = NULL; |
| 8233 | sf.trailers = NULL; |
| 8234 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8235 | #ifdef __APPLE__ |
| 8236 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 8237 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8238 | #else |
| 8239 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 8240 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8241 | #endif |
| 8242 | &headers, &trailers, &flags)) |
| 8243 | return NULL; |
| 8244 | if (headers != NULL) { |
| 8245 | if (!PySequence_Check(headers)) { |
| 8246 | PyErr_SetString(PyExc_TypeError, |
| 8247 | "sendfile() headers must be a sequence or None"); |
| 8248 | return NULL; |
| 8249 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8250 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8251 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8252 | if (sf.hdr_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8253 | (i = iov_setup(&(sf.headers), &hbuf, |
| 8254 | headers, sf.hdr_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8255 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8256 | #ifdef __APPLE__ |
| 8257 | sbytes += i; |
| 8258 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8259 | } |
| 8260 | } |
| 8261 | if (trailers != NULL) { |
| 8262 | if (!PySequence_Check(trailers)) { |
| 8263 | PyErr_SetString(PyExc_TypeError, |
| 8264 | "sendfile() trailers must be a sequence or None"); |
| 8265 | return NULL; |
| 8266 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8267 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8268 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8269 | if (sf.trl_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8270 | (i = iov_setup(&(sf.trailers), &tbuf, |
| 8271 | trailers, sf.trl_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8272 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8273 | #ifdef __APPLE__ |
| 8274 | sbytes += i; |
| 8275 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8276 | } |
| 8277 | } |
| 8278 | |
| 8279 | Py_BEGIN_ALLOW_THREADS |
| 8280 | #ifdef __APPLE__ |
| 8281 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 8282 | #else |
| 8283 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 8284 | #endif |
| 8285 | Py_END_ALLOW_THREADS |
| 8286 | |
| 8287 | if (sf.headers != NULL) |
| 8288 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 8289 | if (sf.trailers != NULL) |
| 8290 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 8291 | |
| 8292 | if (ret < 0) { |
| 8293 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 8294 | if (sbytes != 0) { |
| 8295 | // some data has been sent |
| 8296 | goto done; |
| 8297 | } |
| 8298 | else { |
| 8299 | // no data has been sent; upper application is supposed |
| 8300 | // to retry on EAGAIN or EBUSY |
| 8301 | return posix_error(); |
| 8302 | } |
| 8303 | } |
| 8304 | return posix_error(); |
| 8305 | } |
| 8306 | goto done; |
| 8307 | |
| 8308 | done: |
| 8309 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 8310 | return Py_BuildValue("l", sbytes); |
| 8311 | #else |
| 8312 | return Py_BuildValue("L", sbytes); |
| 8313 | #endif |
| 8314 | |
| 8315 | #else |
| 8316 | Py_ssize_t count; |
| 8317 | PyObject *offobj; |
| 8318 | static char *keywords[] = {"out", "in", |
| 8319 | "offset", "count", NULL}; |
| 8320 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 8321 | keywords, &out, &in, &offobj, &count)) |
| 8322 | return NULL; |
| 8323 | #ifdef linux |
| 8324 | if (offobj == Py_None) { |
| 8325 | Py_BEGIN_ALLOW_THREADS |
| 8326 | ret = sendfile(out, in, NULL, count); |
| 8327 | Py_END_ALLOW_THREADS |
| 8328 | if (ret < 0) |
| 8329 | return posix_error(); |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 8330 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8331 | } |
| 8332 | #endif |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 8333 | if (!_parse_off_t(offobj, &offset)) |
| 8334 | return NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8335 | Py_BEGIN_ALLOW_THREADS |
| 8336 | ret = sendfile(out, in, &offset, count); |
| 8337 | Py_END_ALLOW_THREADS |
| 8338 | if (ret < 0) |
| 8339 | return posix_error(); |
| 8340 | return Py_BuildValue("n", ret); |
| 8341 | #endif |
| 8342 | } |
| 8343 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8344 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8345 | PyDoc_STRVAR(posix_fstat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8346 | "fstat(fd) -> stat result\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8347 | Like stat(), but for an open file descriptor.\n\ |
| 8348 | Equivalent to stat(fd=fd)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8349 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8350 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8351 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8352 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8353 | int fd; |
| 8354 | STRUCT_STAT st; |
| 8355 | int res; |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8356 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8357 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8358 | Py_BEGIN_ALLOW_THREADS |
| 8359 | res = FSTAT(fd, &st); |
| 8360 | Py_END_ALLOW_THREADS |
| 8361 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8362 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8363 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8364 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8365 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8366 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8367 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8368 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8369 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8370 | } |
| 8371 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8372 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8373 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8374 | Return True if the file descriptor 'fd' is an open file descriptor\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8375 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8376 | |
| 8377 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 8378 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8379 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8380 | int fd; |
| 8381 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 8382 | return NULL; |
| 8383 | if (!_PyVerify_fd(fd)) |
| 8384 | return PyBool_FromLong(0); |
| 8385 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8386 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8387 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8388 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8389 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8390 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8391 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8392 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8393 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8394 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8395 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8396 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8397 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8398 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8399 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8400 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8401 | #else |
| 8402 | int res; |
| 8403 | #endif |
| 8404 | |
| 8405 | #ifdef MS_WINDOWS |
| 8406 | attr.nLength = sizeof(attr); |
| 8407 | attr.lpSecurityDescriptor = NULL; |
| 8408 | attr.bInheritHandle = FALSE; |
| 8409 | |
| 8410 | Py_BEGIN_ALLOW_THREADS |
| 8411 | ok = CreatePipe(&read, &write, &attr, 0); |
| 8412 | if (ok) { |
| 8413 | fds[0] = _open_osfhandle((Py_intptr_t)read, _O_RDONLY); |
| 8414 | fds[1] = _open_osfhandle((Py_intptr_t)write, _O_WRONLY); |
| 8415 | if (fds[0] == -1 || fds[1] == -1) { |
| 8416 | CloseHandle(read); |
| 8417 | CloseHandle(write); |
| 8418 | ok = 0; |
| 8419 | } |
| 8420 | } |
| 8421 | Py_END_ALLOW_THREADS |
| 8422 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8423 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8424 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8425 | #else |
| 8426 | |
| 8427 | #ifdef HAVE_PIPE2 |
| 8428 | Py_BEGIN_ALLOW_THREADS |
| 8429 | res = pipe2(fds, O_CLOEXEC); |
| 8430 | Py_END_ALLOW_THREADS |
| 8431 | |
| 8432 | if (res != 0 && errno == ENOSYS) |
| 8433 | { |
| 8434 | #endif |
| 8435 | Py_BEGIN_ALLOW_THREADS |
| 8436 | res = pipe(fds); |
| 8437 | Py_END_ALLOW_THREADS |
| 8438 | |
| 8439 | if (res == 0) { |
| 8440 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 8441 | close(fds[0]); |
| 8442 | close(fds[1]); |
| 8443 | return NULL; |
| 8444 | } |
| 8445 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 8446 | close(fds[0]); |
| 8447 | close(fds[1]); |
| 8448 | return NULL; |
| 8449 | } |
| 8450 | } |
| 8451 | #ifdef HAVE_PIPE2 |
| 8452 | } |
| 8453 | #endif |
| 8454 | |
| 8455 | if (res != 0) |
| 8456 | return PyErr_SetFromErrno(PyExc_OSError); |
| 8457 | #endif /* !MS_WINDOWS */ |
| 8458 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8459 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8460 | #endif /* HAVE_PIPE */ |
| 8461 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8462 | #ifdef HAVE_PIPE2 |
| 8463 | PyDoc_STRVAR(posix_pipe2__doc__, |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8464 | "pipe2(flags) -> (read_end, write_end)\n\n\ |
| 8465 | Create a pipe with flags set atomically.\n\ |
| 8466 | flags can be constructed by ORing together one or more of these values:\n\ |
| 8467 | O_NONBLOCK, O_CLOEXEC.\n\ |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8468 | "); |
| 8469 | |
| 8470 | static PyObject * |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8471 | posix_pipe2(PyObject *self, PyObject *arg) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8472 | { |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8473 | int flags; |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8474 | int fds[2]; |
| 8475 | int res; |
| 8476 | |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 8477 | flags = _PyLong_AsInt(arg); |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8478 | if (flags == -1 && PyErr_Occurred()) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8479 | return NULL; |
| 8480 | |
| 8481 | res = pipe2(fds, flags); |
| 8482 | if (res != 0) |
| 8483 | return posix_error(); |
| 8484 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 8485 | } |
| 8486 | #endif /* HAVE_PIPE2 */ |
| 8487 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8488 | #ifdef HAVE_WRITEV |
| 8489 | PyDoc_STRVAR(posix_writev__doc__, |
| 8490 | "writev(fd, buffers) -> byteswritten\n\n\ |
Benjamin Peterson | e83ed43 | 2014-01-18 22:54:59 -0500 | [diff] [blame] | 8491 | Write the contents of *buffers* to file descriptor *fd*. *buffers*\n\ |
| 8492 | must be a sequence of bytes-like objects.\n\n\ |
| 8493 | writev writes the contents of each object to the file descriptor\n\ |
| 8494 | and returns the total number of bytes written."); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8495 | |
| 8496 | static PyObject * |
| 8497 | posix_writev(PyObject *self, PyObject *args) |
| 8498 | { |
| 8499 | int fd, cnt; |
| 8500 | Py_ssize_t res; |
| 8501 | PyObject *seq; |
| 8502 | struct iovec *iov; |
| 8503 | Py_buffer *buf; |
| 8504 | if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq)) |
| 8505 | return NULL; |
| 8506 | if (!PySequence_Check(seq)) { |
| 8507 | PyErr_SetString(PyExc_TypeError, |
| 8508 | "writev() arg 2 must be a sequence"); |
| 8509 | return NULL; |
| 8510 | } |
| 8511 | cnt = PySequence_Size(seq); |
| 8512 | |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8513 | if (iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE) < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8514 | return NULL; |
| 8515 | } |
| 8516 | |
| 8517 | Py_BEGIN_ALLOW_THREADS |
| 8518 | res = writev(fd, iov, cnt); |
| 8519 | Py_END_ALLOW_THREADS |
| 8520 | |
| 8521 | iov_cleanup(iov, buf, cnt); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8522 | if (res < 0) |
| 8523 | return posix_error(); |
| 8524 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8525 | return PyLong_FromSsize_t(res); |
| 8526 | } |
| 8527 | #endif |
| 8528 | |
| 8529 | #ifdef HAVE_PWRITE |
| 8530 | PyDoc_STRVAR(posix_pwrite__doc__, |
| 8531 | "pwrite(fd, string, offset) -> byteswritten\n\n\ |
| 8532 | Write string to a file descriptor, fd, from offset, leaving the file\n\ |
| 8533 | offset unchanged."); |
| 8534 | |
| 8535 | static PyObject * |
| 8536 | posix_pwrite(PyObject *self, PyObject *args) |
| 8537 | { |
| 8538 | Py_buffer pbuf; |
| 8539 | int fd; |
| 8540 | off_t offset; |
| 8541 | Py_ssize_t size; |
| 8542 | |
| 8543 | if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset)) |
| 8544 | return NULL; |
| 8545 | |
| 8546 | if (!_PyVerify_fd(fd)) { |
| 8547 | PyBuffer_Release(&pbuf); |
| 8548 | return posix_error(); |
| 8549 | } |
| 8550 | Py_BEGIN_ALLOW_THREADS |
| 8551 | size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset); |
| 8552 | Py_END_ALLOW_THREADS |
| 8553 | PyBuffer_Release(&pbuf); |
| 8554 | if (size < 0) |
| 8555 | return posix_error(); |
| 8556 | return PyLong_FromSsize_t(size); |
| 8557 | } |
| 8558 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8559 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8560 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8561 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8562 | "mkfifo(path, mode=0o666, *, dir_fd=None)\n\n\ |
| 8563 | Create a FIFO (a POSIX named pipe).\n\ |
| 8564 | \n\ |
| 8565 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 8566 | and path should be relative; path will then be relative to that directory.\n\ |
| 8567 | dir_fd may not be implemented on your platform.\n\ |
| 8568 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8569 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8570 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8571 | posix_mkfifo(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8572 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8573 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8574 | int mode = 0666; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8575 | int dir_fd = DEFAULT_DIR_FD; |
| 8576 | int result; |
| 8577 | PyObject *return_value = NULL; |
| 8578 | static char *keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 8579 | |
| 8580 | memset(&path, 0, sizeof(path)); |
| 8581 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkfifo", keywords, |
| 8582 | path_converter, &path, |
| 8583 | &mode, |
| 8584 | #ifdef HAVE_MKFIFOAT |
| 8585 | dir_fd_converter, &dir_fd |
| 8586 | #else |
| 8587 | dir_fd_unavailable, &dir_fd |
| 8588 | #endif |
| 8589 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8590 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8591 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8592 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8593 | #ifdef HAVE_MKFIFOAT |
| 8594 | if (dir_fd != DEFAULT_DIR_FD) |
| 8595 | result = mkfifoat(dir_fd, path.narrow, mode); |
| 8596 | else |
| 8597 | #endif |
| 8598 | result = mkfifo(path.narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8599 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8600 | |
| 8601 | if (result < 0) { |
| 8602 | return_value = posix_error(); |
| 8603 | goto exit; |
| 8604 | } |
| 8605 | |
| 8606 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8607 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8608 | |
| 8609 | exit: |
| 8610 | path_cleanup(&path); |
| 8611 | return return_value; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8612 | } |
| 8613 | #endif |
| 8614 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 8615 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8616 | PyDoc_STRVAR(posix_mknod__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8617 | "mknod(filename, mode=0o600, device=0, *, dir_fd=None)\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8618 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 8619 | named filename. mode specifies both the permissions to use and the\n\ |
| 8620 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 8621 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\ |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8622 | device defines the newly created device special file (probably using\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8623 | os.makedev()), otherwise it is ignored.\n\ |
| 8624 | \n\ |
| 8625 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 8626 | and path should be relative; path will then be relative to that directory.\n\ |
| 8627 | dir_fd may not be implemented on your platform.\n\ |
| 8628 | If it is unavailable, using it will raise a NotImplementedError."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8629 | |
| 8630 | |
| 8631 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8632 | posix_mknod(PyObject *self, PyObject *args, PyObject *kwargs) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8633 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8634 | path_t path; |
| 8635 | int mode = 0666; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8636 | int device = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8637 | int dir_fd = DEFAULT_DIR_FD; |
| 8638 | int result; |
| 8639 | PyObject *return_value = NULL; |
| 8640 | static char *keywords[] = {"path", "mode", "device", "dir_fd", NULL}; |
| 8641 | |
| 8642 | memset(&path, 0, sizeof(path)); |
| 8643 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|ii$O&:mknod", keywords, |
| 8644 | path_converter, &path, |
| 8645 | &mode, &device, |
| 8646 | #ifdef HAVE_MKNODAT |
| 8647 | dir_fd_converter, &dir_fd |
| 8648 | #else |
| 8649 | dir_fd_unavailable, &dir_fd |
| 8650 | #endif |
| 8651 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8652 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8653 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8654 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8655 | #ifdef HAVE_MKNODAT |
| 8656 | if (dir_fd != DEFAULT_DIR_FD) |
| 8657 | result = mknodat(dir_fd, path.narrow, mode, device); |
| 8658 | else |
| 8659 | #endif |
| 8660 | result = mknod(path.narrow, mode, device); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8661 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8662 | |
| 8663 | if (result < 0) { |
| 8664 | return_value = posix_error(); |
| 8665 | goto exit; |
| 8666 | } |
| 8667 | |
| 8668 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8669 | Py_INCREF(Py_None); |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 8670 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8671 | exit: |
| 8672 | path_cleanup(&path); |
| 8673 | return return_value; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8674 | } |
| 8675 | #endif |
| 8676 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8677 | #ifdef HAVE_DEVICE_MACROS |
| 8678 | PyDoc_STRVAR(posix_major__doc__, |
| 8679 | "major(device) -> major number\n\ |
| 8680 | Extracts a device major number from a raw device number."); |
| 8681 | |
| 8682 | static PyObject * |
| 8683 | posix_major(PyObject *self, PyObject *args) |
| 8684 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8685 | int device; |
| 8686 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 8687 | return NULL; |
| 8688 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8689 | } |
| 8690 | |
| 8691 | PyDoc_STRVAR(posix_minor__doc__, |
| 8692 | "minor(device) -> minor number\n\ |
| 8693 | Extracts a device minor number from a raw device number."); |
| 8694 | |
| 8695 | static PyObject * |
| 8696 | posix_minor(PyObject *self, PyObject *args) |
| 8697 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8698 | int device; |
| 8699 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 8700 | return NULL; |
| 8701 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8702 | } |
| 8703 | |
| 8704 | PyDoc_STRVAR(posix_makedev__doc__, |
| 8705 | "makedev(major, minor) -> device number\n\ |
| 8706 | Composes a raw device number from the major and minor device numbers."); |
| 8707 | |
| 8708 | static PyObject * |
| 8709 | posix_makedev(PyObject *self, PyObject *args) |
| 8710 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8711 | int major, minor; |
| 8712 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 8713 | return NULL; |
| 8714 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8715 | } |
| 8716 | #endif /* device macros */ |
| 8717 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8718 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8719 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8720 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8721 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8722 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8723 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8724 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8725 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8726 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8727 | int fd; |
| 8728 | off_t length; |
| 8729 | int res; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8730 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8731 | if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8732 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8733 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8734 | Py_BEGIN_ALLOW_THREADS |
| 8735 | res = ftruncate(fd, length); |
| 8736 | Py_END_ALLOW_THREADS |
| 8737 | if (res < 0) |
| 8738 | return posix_error(); |
| 8739 | Py_INCREF(Py_None); |
| 8740 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8741 | } |
| 8742 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8743 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8744 | #ifdef HAVE_TRUNCATE |
| 8745 | PyDoc_STRVAR(posix_truncate__doc__, |
| 8746 | "truncate(path, length)\n\n\ |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8747 | Truncate the file given by path to length bytes.\n\ |
| 8748 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 8749 | If this functionality is unavailable, using it raises an exception."); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8750 | |
| 8751 | static PyObject * |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8752 | posix_truncate(PyObject *self, PyObject *args, PyObject *kwargs) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8753 | { |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8754 | path_t path; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8755 | off_t length; |
| 8756 | int res; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8757 | PyObject *result = NULL; |
| 8758 | static char *keywords[] = {"path", "length", NULL}; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8759 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8760 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8761 | path.function_name = "truncate"; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8762 | #ifdef HAVE_FTRUNCATE |
| 8763 | path.allow_fd = 1; |
| 8764 | #endif |
| 8765 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:truncate", keywords, |
| 8766 | path_converter, &path, |
| 8767 | _parse_off_t, &length)) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8768 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8769 | |
| 8770 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8771 | #ifdef HAVE_FTRUNCATE |
| 8772 | if (path.fd != -1) |
| 8773 | res = ftruncate(path.fd, length); |
| 8774 | else |
| 8775 | #endif |
| 8776 | res = truncate(path.narrow, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8777 | Py_END_ALLOW_THREADS |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8778 | if (res < 0) |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8779 | result = path_error(&path); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8780 | else { |
| 8781 | Py_INCREF(Py_None); |
| 8782 | result = Py_None; |
| 8783 | } |
| 8784 | path_cleanup(&path); |
| 8785 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8786 | } |
| 8787 | #endif |
| 8788 | |
| 8789 | #ifdef HAVE_POSIX_FALLOCATE |
| 8790 | PyDoc_STRVAR(posix_posix_fallocate__doc__, |
| 8791 | "posix_fallocate(fd, offset, len)\n\n\ |
| 8792 | Ensures that enough disk space is allocated for the file specified by fd\n\ |
| 8793 | starting from offset and continuing for len bytes."); |
| 8794 | |
| 8795 | static PyObject * |
| 8796 | posix_posix_fallocate(PyObject *self, PyObject *args) |
| 8797 | { |
| 8798 | off_t len, offset; |
| 8799 | int res, fd; |
| 8800 | |
| 8801 | if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate", |
| 8802 | &fd, _parse_off_t, &offset, _parse_off_t, &len)) |
| 8803 | return NULL; |
| 8804 | |
| 8805 | Py_BEGIN_ALLOW_THREADS |
| 8806 | res = posix_fallocate(fd, offset, len); |
| 8807 | Py_END_ALLOW_THREADS |
| 8808 | if (res != 0) { |
| 8809 | errno = res; |
| 8810 | return posix_error(); |
| 8811 | } |
| 8812 | Py_RETURN_NONE; |
| 8813 | } |
| 8814 | #endif |
| 8815 | |
| 8816 | #ifdef HAVE_POSIX_FADVISE |
| 8817 | PyDoc_STRVAR(posix_posix_fadvise__doc__, |
| 8818 | "posix_fadvise(fd, offset, len, advice)\n\n\ |
| 8819 | Announces an intention to access data in a specific pattern thus allowing\n\ |
| 8820 | the kernel to make optimizations.\n\ |
| 8821 | The advice applies to the region of the file specified by fd starting at\n\ |
| 8822 | offset and continuing for len bytes.\n\ |
| 8823 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\ |
| 8824 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\ |
| 8825 | POSIX_FADV_DONTNEED."); |
| 8826 | |
| 8827 | static PyObject * |
| 8828 | posix_posix_fadvise(PyObject *self, PyObject *args) |
| 8829 | { |
| 8830 | off_t len, offset; |
| 8831 | int res, fd, advice; |
| 8832 | |
| 8833 | if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise", |
| 8834 | &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice)) |
| 8835 | return NULL; |
| 8836 | |
| 8837 | Py_BEGIN_ALLOW_THREADS |
| 8838 | res = posix_fadvise(fd, offset, len, advice); |
| 8839 | Py_END_ALLOW_THREADS |
| 8840 | if (res != 0) { |
| 8841 | errno = res; |
| 8842 | return posix_error(); |
| 8843 | } |
| 8844 | Py_RETURN_NONE; |
| 8845 | } |
| 8846 | #endif |
| 8847 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8848 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8849 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8850 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8851 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8852 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8853 | /* Save putenv() parameters as values here, so we can collect them when they |
| 8854 | * get re-set with another call for the same key. */ |
| 8855 | static PyObject *posix_putenv_garbage; |
| 8856 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8857 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8858 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8859 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8860 | PyObject *newstr = NULL; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8861 | #ifdef MS_WINDOWS |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8862 | PyObject *os1, *os2; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8863 | wchar_t *newenv; |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8864 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8865 | if (!PyArg_ParseTuple(args, |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8866 | "UU:putenv", |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8867 | &os1, &os2)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8868 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8869 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8870 | newstr = PyUnicode_FromFormat("%U=%U", os1, os2); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8871 | if (newstr == NULL) { |
| 8872 | PyErr_NoMemory(); |
| 8873 | goto error; |
| 8874 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8875 | if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) { |
| 8876 | PyErr_Format(PyExc_ValueError, |
| 8877 | "the environment variable is longer than %u characters", |
| 8878 | _MAX_ENV); |
| 8879 | goto error; |
| 8880 | } |
| 8881 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8882 | newenv = PyUnicode_AsUnicode(newstr); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 8883 | if (newenv == NULL) |
| 8884 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8885 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8886 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8887 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8888 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8889 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8890 | PyObject *os1, *os2; |
| 8891 | char *s1, *s2; |
| 8892 | char *newenv; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8893 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8894 | if (!PyArg_ParseTuple(args, |
| 8895 | "O&O&:putenv", |
| 8896 | PyUnicode_FSConverter, &os1, |
| 8897 | PyUnicode_FSConverter, &os2)) |
| 8898 | return NULL; |
| 8899 | s1 = PyBytes_AsString(os1); |
| 8900 | s2 = PyBytes_AsString(os2); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8901 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8902 | newstr = PyBytes_FromFormat("%s=%s", s1, s2); |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8903 | if (newstr == NULL) { |
| 8904 | PyErr_NoMemory(); |
| 8905 | goto error; |
| 8906 | } |
| 8907 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8908 | newenv = PyBytes_AS_STRING(newstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8909 | if (putenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8910 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8911 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8912 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8913 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8914 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8915 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 8916 | * this will cause previous value to be collected. This has to |
| 8917 | * happen after the real putenv() call because the old value |
| 8918 | * was still accessible until then. */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8919 | if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8920 | /* really not much we can do; just leak */ |
| 8921 | PyErr_Clear(); |
| 8922 | } |
| 8923 | else { |
| 8924 | Py_DECREF(newstr); |
| 8925 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8926 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8927 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8928 | Py_DECREF(os1); |
| 8929 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8930 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8931 | Py_RETURN_NONE; |
| 8932 | |
| 8933 | error: |
| 8934 | #ifndef MS_WINDOWS |
| 8935 | Py_DECREF(os1); |
| 8936 | Py_DECREF(os2); |
| 8937 | #endif |
| 8938 | Py_XDECREF(newstr); |
| 8939 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8940 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8941 | #endif /* putenv */ |
| 8942 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8943 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8944 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8945 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8946 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8947 | |
| 8948 | static PyObject * |
| 8949 | posix_unsetenv(PyObject *self, PyObject *args) |
| 8950 | { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8951 | PyObject *name; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8952 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8953 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8954 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8955 | |
| 8956 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8957 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8958 | PyUnicode_FSConverter, &name)) |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8959 | return NULL; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8960 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8961 | #ifdef HAVE_BROKEN_UNSETENV |
| 8962 | unsetenv(PyBytes_AS_STRING(name)); |
| 8963 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8964 | err = unsetenv(PyBytes_AS_STRING(name)); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8965 | if (err) { |
Benjamin Peterson | e8eb0e8 | 2011-11-22 23:14:47 -0600 | [diff] [blame] | 8966 | Py_DECREF(name); |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8967 | return posix_error(); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8968 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8969 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8970 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8971 | /* Remove the key from posix_putenv_garbage; |
| 8972 | * this will cause it to be collected. This has to |
| 8973 | * happen after the real unsetenv() call because the |
| 8974 | * old value was still accessible until then. |
| 8975 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8976 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8977 | /* really not much we can do; just leak */ |
| 8978 | PyErr_Clear(); |
| 8979 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8980 | Py_DECREF(name); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8981 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8982 | } |
| 8983 | #endif /* unsetenv */ |
| 8984 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8985 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8986 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8987 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8988 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 8989 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8990 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8991 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8992 | int code; |
| 8993 | char *message; |
| 8994 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 8995 | return NULL; |
| 8996 | message = strerror(code); |
| 8997 | if (message == NULL) { |
| 8998 | PyErr_SetString(PyExc_ValueError, |
| 8999 | "strerror() argument out of range"); |
| 9000 | return NULL; |
| 9001 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 9002 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9003 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9004 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 9005 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9006 | #ifdef HAVE_SYS_WAIT_H |
| 9007 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9008 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9009 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9010 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9011 | Return True if the process returning 'status' was dumped to a core file."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9012 | |
| 9013 | static PyObject * |
| 9014 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 9015 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9016 | WAIT_TYPE status; |
| 9017 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9018 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9019 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 9020 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9021 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9022 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9023 | } |
| 9024 | #endif /* WCOREDUMP */ |
| 9025 | |
| 9026 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9027 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9028 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9029 | Return True if the process returning 'status' was continued from a\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9030 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9031 | |
| 9032 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 9033 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9034 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9035 | WAIT_TYPE status; |
| 9036 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9037 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9038 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 9039 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9040 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9041 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9042 | } |
| 9043 | #endif /* WIFCONTINUED */ |
| 9044 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9045 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9046 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9047 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9048 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9049 | |
| 9050 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9051 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9052 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9053 | WAIT_TYPE status; |
| 9054 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9055 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9056 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 9057 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9058 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9059 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9060 | } |
| 9061 | #endif /* WIFSTOPPED */ |
| 9062 | |
| 9063 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9064 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9065 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9066 | Return True if the process returning 'status' was terminated by a signal."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9067 | |
| 9068 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9069 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9070 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9071 | WAIT_TYPE status; |
| 9072 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9073 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9074 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 9075 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9076 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9077 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9078 | } |
| 9079 | #endif /* WIFSIGNALED */ |
| 9080 | |
| 9081 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9082 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9083 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 9084 | Return true if the process returning 'status' exited using the exit()\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9085 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9086 | |
| 9087 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9088 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9089 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9090 | WAIT_TYPE status; |
| 9091 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9092 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9093 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 9094 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9095 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9096 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9097 | } |
| 9098 | #endif /* WIFEXITED */ |
| 9099 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 9100 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9101 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9102 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9103 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9104 | |
| 9105 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9106 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9107 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9108 | WAIT_TYPE status; |
| 9109 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9110 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9111 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 9112 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9113 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9114 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9115 | } |
| 9116 | #endif /* WEXITSTATUS */ |
| 9117 | |
| 9118 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9119 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9120 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 9121 | Return the signal that terminated the process that provided the 'status'\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9122 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9123 | |
| 9124 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9125 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9126 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9127 | WAIT_TYPE status; |
| 9128 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9129 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9130 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 9131 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9132 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9133 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9134 | } |
| 9135 | #endif /* WTERMSIG */ |
| 9136 | |
| 9137 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9138 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9139 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9140 | Return the signal that stopped the process that provided\n\ |
| 9141 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9142 | |
| 9143 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9144 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9145 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9146 | WAIT_TYPE status; |
| 9147 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9148 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9149 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 9150 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9151 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9152 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9153 | } |
| 9154 | #endif /* WSTOPSIG */ |
| 9155 | |
| 9156 | #endif /* HAVE_SYS_WAIT_H */ |
| 9157 | |
| 9158 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9159 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 9160 | #ifdef _SCO_DS |
| 9161 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 9162 | needed definitions in sys/statvfs.h */ |
| 9163 | #define _SVID3 |
| 9164 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9165 | #include <sys/statvfs.h> |
| 9166 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9167 | static PyObject* |
| 9168 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9169 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 9170 | if (v == NULL) |
| 9171 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9172 | |
| 9173 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9174 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9175 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9176 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 9177 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 9178 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 9179 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 9180 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 9181 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 9182 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9183 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9184 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9185 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9186 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9187 | PyStructSequence_SET_ITEM(v, 2, |
| 9188 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 9189 | PyStructSequence_SET_ITEM(v, 3, |
| 9190 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 9191 | PyStructSequence_SET_ITEM(v, 4, |
| 9192 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 9193 | PyStructSequence_SET_ITEM(v, 5, |
| 9194 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 9195 | PyStructSequence_SET_ITEM(v, 6, |
| 9196 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 9197 | PyStructSequence_SET_ITEM(v, 7, |
| 9198 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 9199 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9200 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9201 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 9202 | if (PyErr_Occurred()) { |
| 9203 | Py_DECREF(v); |
| 9204 | return NULL; |
| 9205 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9206 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9207 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9208 | } |
| 9209 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9210 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9211 | "fstatvfs(fd) -> statvfs result\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9212 | Perform an fstatvfs system call on the given fd.\n\ |
| 9213 | Equivalent to statvfs(fd)."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9214 | |
| 9215 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9216 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9217 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9218 | int fd, res; |
| 9219 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9220 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9221 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 9222 | return NULL; |
| 9223 | Py_BEGIN_ALLOW_THREADS |
| 9224 | res = fstatvfs(fd, &st); |
| 9225 | Py_END_ALLOW_THREADS |
| 9226 | if (res != 0) |
| 9227 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9228 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9229 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9230 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9231 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9232 | |
| 9233 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9234 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9235 | #include <sys/statvfs.h> |
| 9236 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9237 | PyDoc_STRVAR(posix_statvfs__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9238 | "statvfs(path)\n\n\ |
| 9239 | Perform a statvfs system call on the given path.\n\ |
| 9240 | \n\ |
| 9241 | path may always be specified as a string.\n\ |
| 9242 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 9243 | If this functionality is unavailable, using it raises an exception."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9244 | |
| 9245 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9246 | posix_statvfs(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9247 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9248 | static char *keywords[] = {"path", NULL}; |
| 9249 | path_t path; |
| 9250 | int result; |
| 9251 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9252 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9253 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9254 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9255 | path.function_name = "statvfs"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9256 | #ifdef HAVE_FSTATVFS |
| 9257 | path.allow_fd = 1; |
| 9258 | #endif |
| 9259 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:statvfs", keywords, |
| 9260 | path_converter, &path |
| 9261 | )) |
| 9262 | return NULL; |
| 9263 | |
| 9264 | Py_BEGIN_ALLOW_THREADS |
| 9265 | #ifdef HAVE_FSTATVFS |
| 9266 | if (path.fd != -1) { |
| 9267 | #ifdef __APPLE__ |
| 9268 | /* handle weak-linking on Mac OS X 10.3 */ |
| 9269 | if (fstatvfs == NULL) { |
| 9270 | fd_specified("statvfs", path.fd); |
| 9271 | goto exit; |
| 9272 | } |
| 9273 | #endif |
| 9274 | result = fstatvfs(path.fd, &st); |
| 9275 | } |
| 9276 | else |
| 9277 | #endif |
| 9278 | result = statvfs(path.narrow, &st); |
| 9279 | Py_END_ALLOW_THREADS |
| 9280 | |
| 9281 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9282 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9283 | goto exit; |
| 9284 | } |
| 9285 | |
| 9286 | return_value = _pystatvfs_fromstructstatvfs(st); |
| 9287 | |
| 9288 | exit: |
| 9289 | path_cleanup(&path); |
| 9290 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9291 | } |
| 9292 | #endif /* HAVE_STATVFS */ |
| 9293 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9294 | #ifdef MS_WINDOWS |
| 9295 | PyDoc_STRVAR(win32__getdiskusage__doc__, |
| 9296 | "_getdiskusage(path) -> (total, free)\n\n\ |
| 9297 | Return disk usage statistics about the given path as (total, free) tuple."); |
| 9298 | |
| 9299 | static PyObject * |
| 9300 | win32__getdiskusage(PyObject *self, PyObject *args) |
| 9301 | { |
| 9302 | BOOL retval; |
| 9303 | ULARGE_INTEGER _, total, free; |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 9304 | const wchar_t *path; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9305 | |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 9306 | if (! PyArg_ParseTuple(args, "u", &path)) |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9307 | return NULL; |
| 9308 | |
| 9309 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 9310 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9311 | Py_END_ALLOW_THREADS |
| 9312 | if (retval == 0) |
| 9313 | return PyErr_SetFromWindowsErr(0); |
| 9314 | |
| 9315 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 9316 | } |
| 9317 | #endif |
| 9318 | |
| 9319 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9320 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 9321 | * It maps strings representing configuration variable names to |
| 9322 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 9323 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9324 | * rarely-used constants. There are three separate tables that use |
| 9325 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9326 | * |
| 9327 | * This code is always included, even if none of the interfaces that |
| 9328 | * need it are included. The #if hackery needed to avoid it would be |
| 9329 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9330 | */ |
| 9331 | struct constdef { |
| 9332 | char *name; |
| 9333 | long value; |
| 9334 | }; |
| 9335 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9336 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9337 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 9338 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9339 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9340 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9341 | *valuep = PyLong_AS_LONG(arg); |
| 9342 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9343 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 9344 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9345 | /* look up the value in the table using a binary search */ |
| 9346 | size_t lo = 0; |
| 9347 | size_t mid; |
| 9348 | size_t hi = tablesize; |
| 9349 | int cmp; |
| 9350 | const char *confname; |
| 9351 | if (!PyUnicode_Check(arg)) { |
| 9352 | PyErr_SetString(PyExc_TypeError, |
| 9353 | "configuration names must be strings or integers"); |
| 9354 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9355 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9356 | confname = _PyUnicode_AsString(arg); |
| 9357 | if (confname == NULL) |
| 9358 | return 0; |
| 9359 | while (lo < hi) { |
| 9360 | mid = (lo + hi) / 2; |
| 9361 | cmp = strcmp(confname, table[mid].name); |
| 9362 | if (cmp < 0) |
| 9363 | hi = mid; |
| 9364 | else if (cmp > 0) |
| 9365 | lo = mid + 1; |
| 9366 | else { |
| 9367 | *valuep = table[mid].value; |
| 9368 | return 1; |
| 9369 | } |
| 9370 | } |
| 9371 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 9372 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9373 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9374 | } |
| 9375 | |
| 9376 | |
| 9377 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 9378 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9379 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9380 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9381 | #endif |
| 9382 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9383 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9384 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9385 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9386 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9387 | #endif |
| 9388 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9389 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9390 | #endif |
| 9391 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9392 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9393 | #endif |
| 9394 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9395 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9396 | #endif |
| 9397 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9398 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9399 | #endif |
| 9400 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9401 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9402 | #endif |
| 9403 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9404 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9405 | #endif |
| 9406 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9407 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9408 | #endif |
| 9409 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9410 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9411 | #endif |
| 9412 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9413 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9414 | #endif |
| 9415 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9416 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9417 | #endif |
| 9418 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9419 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9420 | #endif |
| 9421 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9422 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9423 | #endif |
| 9424 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9425 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9426 | #endif |
| 9427 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9428 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9429 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 9430 | #ifdef _PC_ACL_ENABLED |
| 9431 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 9432 | #endif |
| 9433 | #ifdef _PC_MIN_HOLE_SIZE |
| 9434 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 9435 | #endif |
| 9436 | #ifdef _PC_ALLOC_SIZE_MIN |
| 9437 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 9438 | #endif |
| 9439 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 9440 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 9441 | #endif |
| 9442 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 9443 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 9444 | #endif |
| 9445 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 9446 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 9447 | #endif |
| 9448 | #ifdef _PC_REC_XFER_ALIGN |
| 9449 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 9450 | #endif |
| 9451 | #ifdef _PC_SYMLINK_MAX |
| 9452 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 9453 | #endif |
| 9454 | #ifdef _PC_XATTR_ENABLED |
| 9455 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 9456 | #endif |
| 9457 | #ifdef _PC_XATTR_EXISTS |
| 9458 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 9459 | #endif |
| 9460 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 9461 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 9462 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9463 | }; |
| 9464 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9465 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9466 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9467 | { |
| 9468 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 9469 | sizeof(posix_constants_pathconf) |
| 9470 | / sizeof(struct constdef)); |
| 9471 | } |
| 9472 | #endif |
| 9473 | |
| 9474 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9475 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9476 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9477 | Return the configuration limit name for the file descriptor fd.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9478 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9479 | |
| 9480 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9481 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9482 | { |
| 9483 | PyObject *result = NULL; |
| 9484 | int name, fd; |
| 9485 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9486 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 9487 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9488 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9489 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9490 | errno = 0; |
| 9491 | limit = fpathconf(fd, name); |
| 9492 | if (limit == -1 && errno != 0) |
| 9493 | posix_error(); |
| 9494 | else |
| 9495 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9496 | } |
| 9497 | return result; |
| 9498 | } |
| 9499 | #endif |
| 9500 | |
| 9501 | |
| 9502 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9503 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9504 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9505 | Return the configuration limit name for the file or directory path.\n\ |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9506 | If there is no limit, return -1.\n\ |
| 9507 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 9508 | If this functionality is unavailable, using it raises an exception."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9509 | |
| 9510 | static PyObject * |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9511 | posix_pathconf(PyObject *self, PyObject *args, PyObject *kwargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9512 | { |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9513 | path_t path; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9514 | PyObject *result = NULL; |
| 9515 | int name; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9516 | static char *keywords[] = {"path", "name", NULL}; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9517 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9518 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9519 | path.function_name = "pathconf"; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9520 | #ifdef HAVE_FPATHCONF |
| 9521 | path.allow_fd = 1; |
| 9522 | #endif |
| 9523 | if (PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:pathconf", keywords, |
| 9524 | path_converter, &path, |
| 9525 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9526 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9527 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9528 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9529 | #ifdef HAVE_FPATHCONF |
| 9530 | if (path.fd != -1) |
| 9531 | limit = fpathconf(path.fd, name); |
| 9532 | else |
| 9533 | #endif |
| 9534 | limit = pathconf(path.narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9535 | if (limit == -1 && errno != 0) { |
| 9536 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 9537 | /* could be a path or name problem */ |
| 9538 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9539 | else |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9540 | result = path_error(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9541 | } |
| 9542 | else |
| 9543 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9544 | } |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9545 | path_cleanup(&path); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9546 | return result; |
| 9547 | } |
| 9548 | #endif |
| 9549 | |
| 9550 | #ifdef HAVE_CONFSTR |
| 9551 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9552 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9553 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9554 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9555 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9556 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9557 | #endif |
| 9558 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9559 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9560 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9561 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9562 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9563 | #endif |
| 9564 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9565 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9566 | #endif |
| 9567 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9568 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9569 | #endif |
| 9570 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9571 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9572 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9573 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9574 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9575 | #endif |
| 9576 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9577 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9578 | #endif |
| 9579 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9580 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9581 | #endif |
| 9582 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9583 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9584 | #endif |
| 9585 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9586 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9587 | #endif |
| 9588 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9589 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9590 | #endif |
| 9591 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9592 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9593 | #endif |
| 9594 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9595 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9596 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9597 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9598 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9599 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9600 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9601 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9602 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9603 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9604 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9605 | #endif |
| 9606 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9607 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9608 | #endif |
| 9609 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9610 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9611 | #endif |
| 9612 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9613 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9614 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9615 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9616 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9617 | #endif |
| 9618 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9619 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9620 | #endif |
| 9621 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9622 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9623 | #endif |
| 9624 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9625 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9626 | #endif |
| 9627 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9628 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9629 | #endif |
| 9630 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9631 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9632 | #endif |
| 9633 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9634 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9635 | #endif |
| 9636 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9637 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9638 | #endif |
| 9639 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9640 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9641 | #endif |
| 9642 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9643 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9644 | #endif |
| 9645 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9646 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9647 | #endif |
| 9648 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9649 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9650 | #endif |
| 9651 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9652 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9653 | #endif |
| 9654 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9655 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9656 | #endif |
| 9657 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9658 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9659 | #endif |
| 9660 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9661 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9662 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9663 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9664 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9665 | #endif |
| 9666 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9667 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9668 | #endif |
| 9669 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9670 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9671 | #endif |
| 9672 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9673 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9674 | #endif |
| 9675 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9676 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9677 | #endif |
| 9678 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9679 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9680 | #endif |
| 9681 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9682 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9683 | #endif |
| 9684 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9685 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9686 | #endif |
| 9687 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9688 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9689 | #endif |
| 9690 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9691 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9692 | #endif |
| 9693 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9694 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9695 | #endif |
| 9696 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9697 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9698 | #endif |
| 9699 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9700 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9701 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9702 | }; |
| 9703 | |
| 9704 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9705 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9706 | { |
| 9707 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 9708 | sizeof(posix_constants_confstr) |
| 9709 | / sizeof(struct constdef)); |
| 9710 | } |
| 9711 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9712 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9713 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9714 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9715 | |
| 9716 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9717 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9718 | { |
| 9719 | PyObject *result = NULL; |
| 9720 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9721 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9722 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9723 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9724 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 9725 | return NULL; |
| 9726 | |
| 9727 | errno = 0; |
| 9728 | len = confstr(name, buffer, sizeof(buffer)); |
| 9729 | if (len == 0) { |
| 9730 | if (errno) { |
| 9731 | posix_error(); |
| 9732 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9733 | } |
| 9734 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9735 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9736 | } |
| 9737 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9738 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9739 | if (len >= sizeof(buffer)) { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9740 | char *buf = PyMem_Malloc(len); |
| 9741 | if (buf == NULL) |
| 9742 | return PyErr_NoMemory(); |
| 9743 | confstr(name, buf, len); |
| 9744 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 9745 | PyMem_Free(buf); |
| 9746 | } |
| 9747 | else |
| 9748 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9749 | return result; |
| 9750 | } |
| 9751 | #endif |
| 9752 | |
| 9753 | |
| 9754 | #ifdef HAVE_SYSCONF |
| 9755 | static struct constdef posix_constants_sysconf[] = { |
| 9756 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9757 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9758 | #endif |
| 9759 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9760 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9761 | #endif |
| 9762 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9763 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9764 | #endif |
| 9765 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9766 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9767 | #endif |
| 9768 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9769 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9770 | #endif |
| 9771 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9772 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9773 | #endif |
| 9774 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9775 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9776 | #endif |
| 9777 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9778 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9779 | #endif |
| 9780 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9781 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9782 | #endif |
| 9783 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9784 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9785 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9786 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9787 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9788 | #endif |
| 9789 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9790 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9791 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9792 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9793 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9794 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9795 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9796 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9797 | #endif |
| 9798 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9799 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9800 | #endif |
| 9801 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9802 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9803 | #endif |
| 9804 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9805 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9806 | #endif |
| 9807 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9808 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9809 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9810 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9811 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9812 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9813 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9814 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9815 | #endif |
| 9816 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9817 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9818 | #endif |
| 9819 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9820 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9821 | #endif |
| 9822 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9823 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9824 | #endif |
| 9825 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9826 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9827 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9828 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9829 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9830 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9831 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9832 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9833 | #endif |
| 9834 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9835 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9836 | #endif |
| 9837 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9838 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9839 | #endif |
| 9840 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9841 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9842 | #endif |
| 9843 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9844 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9845 | #endif |
| 9846 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9847 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9848 | #endif |
| 9849 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9850 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9851 | #endif |
| 9852 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9853 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9854 | #endif |
| 9855 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9856 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9857 | #endif |
| 9858 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9859 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9860 | #endif |
| 9861 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9862 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9863 | #endif |
| 9864 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9865 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9866 | #endif |
| 9867 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9868 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9869 | #endif |
| 9870 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9871 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9872 | #endif |
| 9873 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9874 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9875 | #endif |
| 9876 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9877 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9878 | #endif |
| 9879 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9880 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9881 | #endif |
| 9882 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9883 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9884 | #endif |
| 9885 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9886 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9887 | #endif |
| 9888 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9889 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9890 | #endif |
| 9891 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9892 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9893 | #endif |
| 9894 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9895 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9896 | #endif |
| 9897 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9898 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9899 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9900 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9901 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9902 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9903 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9904 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9905 | #endif |
| 9906 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9907 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9908 | #endif |
| 9909 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9910 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9911 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9912 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9913 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9914 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9915 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9916 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9917 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9918 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9919 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9920 | #endif |
| 9921 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9922 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9923 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9924 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9925 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9926 | #endif |
| 9927 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9928 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9929 | #endif |
| 9930 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9931 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9932 | #endif |
| 9933 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9934 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9935 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9936 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9937 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9938 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9939 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9940 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9941 | #endif |
| 9942 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9943 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9944 | #endif |
| 9945 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9946 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9947 | #endif |
| 9948 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9949 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9950 | #endif |
| 9951 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9952 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9953 | #endif |
| 9954 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9955 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9956 | #endif |
| 9957 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9958 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9959 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9960 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9961 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9962 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9963 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9964 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9965 | #endif |
| 9966 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9967 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9968 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9969 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9970 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9971 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9972 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9973 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9974 | #endif |
| 9975 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9976 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9977 | #endif |
| 9978 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9979 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9980 | #endif |
| 9981 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9982 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9983 | #endif |
| 9984 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9985 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9986 | #endif |
| 9987 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9988 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9989 | #endif |
| 9990 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9991 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9992 | #endif |
| 9993 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9994 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9995 | #endif |
| 9996 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9997 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9998 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9999 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10000 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10001 | #endif |
| 10002 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10003 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10004 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10005 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10006 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10007 | #endif |
| 10008 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10009 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10010 | #endif |
| 10011 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10012 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10013 | #endif |
| 10014 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10015 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10016 | #endif |
| 10017 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10018 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10019 | #endif |
| 10020 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10021 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10022 | #endif |
| 10023 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10024 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10025 | #endif |
| 10026 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10027 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10028 | #endif |
| 10029 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10030 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10031 | #endif |
| 10032 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10033 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10034 | #endif |
| 10035 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10036 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10037 | #endif |
| 10038 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10039 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10040 | #endif |
| 10041 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10042 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10043 | #endif |
| 10044 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10045 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10046 | #endif |
| 10047 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10048 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10049 | #endif |
| 10050 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10051 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10052 | #endif |
| 10053 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10054 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10055 | #endif |
| 10056 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10057 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10058 | #endif |
| 10059 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10060 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10061 | #endif |
| 10062 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10063 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10064 | #endif |
| 10065 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10066 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10067 | #endif |
| 10068 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10069 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10070 | #endif |
| 10071 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10072 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10073 | #endif |
| 10074 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10075 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10076 | #endif |
| 10077 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10078 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10079 | #endif |
| 10080 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10081 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10082 | #endif |
| 10083 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10084 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10085 | #endif |
| 10086 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10087 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10088 | #endif |
| 10089 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10090 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10091 | #endif |
| 10092 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10093 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10094 | #endif |
| 10095 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10096 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10097 | #endif |
| 10098 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10099 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10100 | #endif |
| 10101 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10102 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10103 | #endif |
| 10104 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10105 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10106 | #endif |
| 10107 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10108 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10109 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10110 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10111 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10112 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10113 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10114 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10115 | #endif |
| 10116 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10117 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10118 | #endif |
| 10119 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10120 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10121 | #endif |
| 10122 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10123 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10124 | #endif |
| 10125 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10126 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10127 | #endif |
| 10128 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10129 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10130 | #endif |
| 10131 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10132 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10133 | #endif |
| 10134 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10135 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10136 | #endif |
| 10137 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10138 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10139 | #endif |
| 10140 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10141 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10142 | #endif |
| 10143 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10144 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10145 | #endif |
| 10146 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10147 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10148 | #endif |
| 10149 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10150 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10151 | #endif |
| 10152 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10153 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10154 | #endif |
| 10155 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10156 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10157 | #endif |
| 10158 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10159 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10160 | #endif |
| 10161 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10162 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10163 | #endif |
| 10164 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10165 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10166 | #endif |
| 10167 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10168 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10169 | #endif |
| 10170 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10171 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10172 | #endif |
| 10173 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10174 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10175 | #endif |
| 10176 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10177 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10178 | #endif |
| 10179 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10180 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10181 | #endif |
| 10182 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10183 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10184 | #endif |
| 10185 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10186 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10187 | #endif |
| 10188 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10189 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10190 | #endif |
| 10191 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10192 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10193 | #endif |
| 10194 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10195 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10196 | #endif |
| 10197 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10198 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10199 | #endif |
| 10200 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10201 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10202 | #endif |
| 10203 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10204 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10205 | #endif |
| 10206 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10207 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10208 | #endif |
| 10209 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10210 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10211 | #endif |
| 10212 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10213 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10214 | #endif |
| 10215 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10216 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10217 | #endif |
| 10218 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10219 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10220 | #endif |
| 10221 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10222 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10223 | #endif |
| 10224 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10225 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10226 | #endif |
| 10227 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10228 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10229 | #endif |
| 10230 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10231 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10232 | #endif |
| 10233 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10234 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10235 | #endif |
| 10236 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10237 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10238 | #endif |
| 10239 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10240 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10241 | #endif |
| 10242 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10243 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10244 | #endif |
| 10245 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10246 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10247 | #endif |
| 10248 | }; |
| 10249 | |
| 10250 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10251 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10252 | { |
| 10253 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 10254 | sizeof(posix_constants_sysconf) |
| 10255 | / sizeof(struct constdef)); |
| 10256 | } |
| 10257 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10258 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 10259 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10260 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10261 | |
| 10262 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10263 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10264 | { |
| 10265 | PyObject *result = NULL; |
| 10266 | int name; |
| 10267 | |
| 10268 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
Victor Stinner | 6fdd7b8 | 2013-05-16 22:26:29 +0200 | [diff] [blame] | 10269 | long value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10270 | |
| 10271 | errno = 0; |
| 10272 | value = sysconf(name); |
| 10273 | if (value == -1 && errno != 0) |
| 10274 | posix_error(); |
| 10275 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10276 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10277 | } |
| 10278 | return result; |
| 10279 | } |
| 10280 | #endif |
| 10281 | |
| 10282 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10283 | /* This code is used to ensure that the tables of configuration value names |
| 10284 | * are in sorted order as required by conv_confname(), and also to build the |
| 10285 | * the exported dictionaries that are used to publish information about the |
| 10286 | * names available on the host platform. |
| 10287 | * |
| 10288 | * Sorting the table at runtime ensures that the table is properly ordered |
| 10289 | * when used, even for platforms we're not able to test on. It also makes |
| 10290 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10291 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10292 | |
| 10293 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10294 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10295 | { |
| 10296 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10297 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10298 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10299 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10300 | |
| 10301 | return strcmp(c1->name, c2->name); |
| 10302 | } |
| 10303 | |
| 10304 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10305 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10306 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10307 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10308 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10309 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10310 | |
| 10311 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 10312 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10313 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10314 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10315 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10316 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10317 | PyObject *o = PyLong_FromLong(table[i].value); |
| 10318 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 10319 | Py_XDECREF(o); |
| 10320 | Py_DECREF(d); |
| 10321 | return -1; |
| 10322 | } |
| 10323 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10324 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10325 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10326 | } |
| 10327 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10328 | /* Return -1 on failure, 0 on success. */ |
| 10329 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10330 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10331 | { |
| 10332 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10333 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10334 | sizeof(posix_constants_pathconf) |
| 10335 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10336 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10337 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10338 | #endif |
| 10339 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10340 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10341 | sizeof(posix_constants_confstr) |
| 10342 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10343 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10344 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10345 | #endif |
| 10346 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10347 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10348 | sizeof(posix_constants_sysconf) |
| 10349 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10350 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10351 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10352 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10353 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10354 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10355 | |
| 10356 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10357 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 10358 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10359 | Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10360 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10361 | |
| 10362 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 10363 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10364 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10365 | abort(); |
| 10366 | /*NOTREACHED*/ |
| 10367 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 10368 | return NULL; |
| 10369 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10370 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10371 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10372 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 10373 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 10374 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10375 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 10376 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 10377 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 10378 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 10379 | application (if any) its extension is associated.\n\ |
| 10380 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 10381 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10382 | \n\ |
| 10383 | startfile returns as soon as the associated application is launched.\n\ |
| 10384 | There is no option to wait for the application to close, and no way\n\ |
| 10385 | to retrieve the application's exit status.\n\ |
| 10386 | \n\ |
| 10387 | The filepath is relative to the current directory. If you want to use\n\ |
| 10388 | 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] | 10389 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10390 | |
| 10391 | static PyObject * |
| 10392 | win32_startfile(PyObject *self, PyObject *args) |
| 10393 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10394 | PyObject *ofilepath; |
| 10395 | char *filepath; |
| 10396 | char *operation = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10397 | wchar_t *wpath, *woperation; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10398 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 10399 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10400 | PyObject *unipath, *uoperation = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10401 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 10402 | &unipath, &operation)) { |
| 10403 | PyErr_Clear(); |
| 10404 | goto normal; |
| 10405 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10406 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10407 | if (operation) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10408 | uoperation = PyUnicode_DecodeASCII(operation, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10409 | strlen(operation), NULL); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10410 | if (!uoperation) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10411 | PyErr_Clear(); |
| 10412 | operation = NULL; |
| 10413 | goto normal; |
| 10414 | } |
| 10415 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10416 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10417 | wpath = PyUnicode_AsUnicode(unipath); |
| 10418 | if (wpath == NULL) |
| 10419 | goto normal; |
| 10420 | if (uoperation) { |
| 10421 | woperation = PyUnicode_AsUnicode(uoperation); |
| 10422 | if (woperation == NULL) |
| 10423 | goto normal; |
| 10424 | } |
| 10425 | else |
| 10426 | woperation = NULL; |
| 10427 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10428 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10429 | rc = ShellExecuteW((HWND)0, woperation, wpath, |
| 10430 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10431 | Py_END_ALLOW_THREADS |
| 10432 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10433 | Py_XDECREF(uoperation); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10434 | if (rc <= (HINSTANCE)32) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10435 | win32_error_object("startfile", unipath); |
| 10436 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10437 | } |
| 10438 | Py_INCREF(Py_None); |
| 10439 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10440 | |
| 10441 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10442 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 10443 | PyUnicode_FSConverter, &ofilepath, |
| 10444 | &operation)) |
| 10445 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 10446 | if (win32_warn_bytes_api()) { |
| 10447 | Py_DECREF(ofilepath); |
| 10448 | return NULL; |
| 10449 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10450 | filepath = PyBytes_AsString(ofilepath); |
| 10451 | Py_BEGIN_ALLOW_THREADS |
| 10452 | rc = ShellExecute((HWND)0, operation, filepath, |
| 10453 | NULL, NULL, SW_SHOWNORMAL); |
| 10454 | Py_END_ALLOW_THREADS |
| 10455 | if (rc <= (HINSTANCE)32) { |
| 10456 | PyObject *errval = win32_error("startfile", filepath); |
| 10457 | Py_DECREF(ofilepath); |
| 10458 | return errval; |
| 10459 | } |
| 10460 | Py_DECREF(ofilepath); |
| 10461 | Py_INCREF(Py_None); |
| 10462 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10463 | } |
| 10464 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10465 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10466 | #ifdef HAVE_GETLOADAVG |
| 10467 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 10468 | "getloadavg() -> (float, float, float)\n\n\ |
| 10469 | Return the number of processes in the system run queue averaged over\n\ |
| 10470 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 10471 | was unobtainable"); |
| 10472 | |
| 10473 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 10474 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10475 | { |
| 10476 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10477 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10478 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 10479 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10480 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10481 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10482 | } |
| 10483 | #endif |
| 10484 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 10485 | PyDoc_STRVAR(device_encoding__doc__, |
| 10486 | "device_encoding(fd) -> str\n\n\ |
| 10487 | Return a string describing the encoding of the device\n\ |
| 10488 | if the output is a terminal; else return None."); |
| 10489 | |
| 10490 | static PyObject * |
| 10491 | device_encoding(PyObject *self, PyObject *args) |
| 10492 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10493 | int fd; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10494 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10495 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 10496 | return NULL; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10497 | |
| 10498 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 10499 | } |
| 10500 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10501 | #ifdef HAVE_SETRESUID |
| 10502 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 10503 | "setresuid(ruid, euid, suid)\n\n\ |
| 10504 | Set the current process's real, effective, and saved user ids."); |
| 10505 | |
| 10506 | static PyObject* |
| 10507 | posix_setresuid (PyObject *self, PyObject *args) |
| 10508 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10509 | /* We assume uid_t is no larger than a long. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10510 | uid_t ruid, euid, suid; |
| 10511 | if (!PyArg_ParseTuple(args, "O&O&O&:setresuid", |
| 10512 | _Py_Uid_Converter, &ruid, |
| 10513 | _Py_Uid_Converter, &euid, |
| 10514 | _Py_Uid_Converter, &suid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10515 | return NULL; |
| 10516 | if (setresuid(ruid, euid, suid) < 0) |
| 10517 | return posix_error(); |
| 10518 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10519 | } |
| 10520 | #endif |
| 10521 | |
| 10522 | #ifdef HAVE_SETRESGID |
| 10523 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 10524 | "setresgid(rgid, egid, sgid)\n\n\ |
| 10525 | Set the current process's real, effective, and saved group ids."); |
| 10526 | |
| 10527 | static PyObject* |
| 10528 | posix_setresgid (PyObject *self, PyObject *args) |
| 10529 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10530 | gid_t rgid, egid, sgid; |
| 10531 | if (!PyArg_ParseTuple(args, "O&O&O&:setresgid", |
| 10532 | _Py_Gid_Converter, &rgid, |
| 10533 | _Py_Gid_Converter, &egid, |
| 10534 | _Py_Gid_Converter, &sgid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10535 | return NULL; |
| 10536 | if (setresgid(rgid, egid, sgid) < 0) |
| 10537 | return posix_error(); |
| 10538 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10539 | } |
| 10540 | #endif |
| 10541 | |
| 10542 | #ifdef HAVE_GETRESUID |
| 10543 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 10544 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 10545 | Get tuple of the current process's real, effective, and saved user ids."); |
| 10546 | |
| 10547 | static PyObject* |
| 10548 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 10549 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10550 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10551 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 10552 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10553 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 10554 | _PyLong_FromUid(euid), |
| 10555 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10556 | } |
| 10557 | #endif |
| 10558 | |
| 10559 | #ifdef HAVE_GETRESGID |
| 10560 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 10561 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 10562 | Get tuple of the current process's real, effective, and saved group ids."); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10563 | |
| 10564 | static PyObject* |
| 10565 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 10566 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10567 | uid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10568 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 10569 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10570 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 10571 | _PyLong_FromGid(egid), |
| 10572 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10573 | } |
| 10574 | #endif |
| 10575 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10576 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10577 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10578 | PyDoc_STRVAR(posix_getxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10579 | "getxattr(path, attribute, *, follow_symlinks=True) -> value\n\n\ |
| 10580 | Return the value of extended attribute attribute on path.\n\ |
| 10581 | \n\ |
| 10582 | path may be either a string or an open file descriptor.\n\ |
| 10583 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10584 | link, getxattr will examine the symbolic link itself instead of the file\n\ |
| 10585 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10586 | |
| 10587 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10588 | posix_getxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10589 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10590 | path_t path; |
| 10591 | path_t attribute; |
| 10592 | int follow_symlinks = 1; |
| 10593 | PyObject *buffer = NULL; |
| 10594 | int i; |
| 10595 | static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10596 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10597 | memset(&path, 0, sizeof(path)); |
| 10598 | memset(&attribute, 0, sizeof(attribute)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10599 | path.function_name = "getxattr"; |
| 10600 | attribute.function_name = "getxattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10601 | path.allow_fd = 1; |
| 10602 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:getxattr", keywords, |
| 10603 | path_converter, &path, |
| 10604 | path_converter, &attribute, |
| 10605 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10606 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10607 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10608 | if (fd_and_follow_symlinks_invalid("getxattr", path.fd, follow_symlinks)) |
| 10609 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10610 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10611 | for (i = 0; ; i++) { |
| 10612 | void *ptr; |
| 10613 | ssize_t result; |
| 10614 | static Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
| 10615 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10616 | if (!buffer_size) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10617 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10618 | goto exit; |
| 10619 | } |
| 10620 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 10621 | if (!buffer) |
| 10622 | goto exit; |
| 10623 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10624 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10625 | Py_BEGIN_ALLOW_THREADS; |
| 10626 | if (path.fd >= 0) |
| 10627 | result = fgetxattr(path.fd, attribute.narrow, ptr, buffer_size); |
| 10628 | else if (follow_symlinks) |
| 10629 | result = getxattr(path.narrow, attribute.narrow, ptr, buffer_size); |
| 10630 | else |
| 10631 | result = lgetxattr(path.narrow, attribute.narrow, ptr, buffer_size); |
| 10632 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10633 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10634 | if (result < 0) { |
| 10635 | Py_DECREF(buffer); |
| 10636 | buffer = NULL; |
| 10637 | if (errno == ERANGE) |
| 10638 | continue; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10639 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10640 | goto exit; |
| 10641 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10642 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10643 | if (result != buffer_size) { |
| 10644 | /* Can only shrink. */ |
| 10645 | _PyBytes_Resize(&buffer, result); |
| 10646 | } |
| 10647 | break; |
| 10648 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10649 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10650 | exit: |
| 10651 | path_cleanup(&path); |
| 10652 | path_cleanup(&attribute); |
| 10653 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10654 | } |
| 10655 | |
| 10656 | PyDoc_STRVAR(posix_setxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10657 | "setxattr(path, attribute, value, flags=0, *, follow_symlinks=True)\n\n\ |
| 10658 | Set extended attribute attribute on path to value.\n\ |
| 10659 | path may be either a string or an open file descriptor.\n\ |
| 10660 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10661 | link, setxattr will modify the symbolic link itself instead of the file\n\ |
| 10662 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10663 | |
| 10664 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10665 | posix_setxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10666 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10667 | path_t path; |
| 10668 | path_t attribute; |
| 10669 | Py_buffer value; |
| 10670 | int flags = 0; |
| 10671 | int follow_symlinks = 1; |
| 10672 | int result; |
| 10673 | PyObject *return_value = NULL; |
| 10674 | static char *keywords[] = {"path", "attribute", "value", |
| 10675 | "flags", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10676 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10677 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10678 | path.function_name = "setxattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10679 | path.allow_fd = 1; |
| 10680 | memset(&attribute, 0, sizeof(attribute)); |
| 10681 | memset(&value, 0, sizeof(value)); |
| 10682 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&y*|i$p:setxattr", |
| 10683 | keywords, |
| 10684 | path_converter, &path, |
| 10685 | path_converter, &attribute, |
| 10686 | &value, &flags, |
| 10687 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10688 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10689 | |
| 10690 | if (fd_and_follow_symlinks_invalid("setxattr", path.fd, follow_symlinks)) |
| 10691 | goto exit; |
| 10692 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10693 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10694 | if (path.fd > -1) |
| 10695 | result = fsetxattr(path.fd, attribute.narrow, |
| 10696 | value.buf, value.len, flags); |
| 10697 | else if (follow_symlinks) |
| 10698 | result = setxattr(path.narrow, attribute.narrow, |
| 10699 | value.buf, value.len, flags); |
| 10700 | else |
| 10701 | result = lsetxattr(path.narrow, attribute.narrow, |
| 10702 | value.buf, value.len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10703 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10704 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10705 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10706 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10707 | goto exit; |
| 10708 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10709 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10710 | return_value = Py_None; |
| 10711 | Py_INCREF(return_value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10712 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10713 | exit: |
| 10714 | path_cleanup(&path); |
| 10715 | path_cleanup(&attribute); |
| 10716 | PyBuffer_Release(&value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10717 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10718 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10719 | } |
| 10720 | |
| 10721 | PyDoc_STRVAR(posix_removexattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10722 | "removexattr(path, attribute, *, follow_symlinks=True)\n\n\ |
| 10723 | Remove extended attribute attribute on path.\n\ |
| 10724 | path may be either a string or an open file descriptor.\n\ |
| 10725 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10726 | link, removexattr will modify the symbolic link itself instead of the file\n\ |
| 10727 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10728 | |
| 10729 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10730 | posix_removexattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10731 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10732 | path_t path; |
| 10733 | path_t attribute; |
| 10734 | int follow_symlinks = 1; |
| 10735 | int result; |
| 10736 | PyObject *return_value = NULL; |
| 10737 | static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10738 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10739 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10740 | path.function_name = "removexattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10741 | memset(&attribute, 0, sizeof(attribute)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10742 | attribute.function_name = "removexattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10743 | path.allow_fd = 1; |
| 10744 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:removexattr", |
| 10745 | keywords, |
| 10746 | path_converter, &path, |
| 10747 | path_converter, &attribute, |
| 10748 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10749 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10750 | |
| 10751 | if (fd_and_follow_symlinks_invalid("removexattr", path.fd, follow_symlinks)) |
| 10752 | goto exit; |
| 10753 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10754 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10755 | if (path.fd > -1) |
| 10756 | result = fremovexattr(path.fd, attribute.narrow); |
| 10757 | else if (follow_symlinks) |
| 10758 | result = removexattr(path.narrow, attribute.narrow); |
| 10759 | else |
| 10760 | result = lremovexattr(path.narrow, attribute.narrow); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10761 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10762 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10763 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10764 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10765 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10766 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10767 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10768 | return_value = Py_None; |
| 10769 | Py_INCREF(return_value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10770 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10771 | exit: |
| 10772 | path_cleanup(&path); |
| 10773 | path_cleanup(&attribute); |
| 10774 | |
| 10775 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10776 | } |
| 10777 | |
| 10778 | PyDoc_STRVAR(posix_listxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10779 | "listxattr(path='.', *, follow_symlinks=True)\n\n\ |
| 10780 | Return a list of extended attributes on path.\n\ |
| 10781 | \n\ |
| 10782 | path may be either None, a string, or an open file descriptor.\n\ |
| 10783 | if path is None, listxattr will examine the current directory.\n\ |
| 10784 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10785 | link, listxattr will examine the symbolic link itself instead of the file\n\ |
| 10786 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10787 | |
| 10788 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10789 | posix_listxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10790 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10791 | path_t path; |
| 10792 | int follow_symlinks = 1; |
| 10793 | Py_ssize_t i; |
| 10794 | PyObject *result = NULL; |
| 10795 | char *buffer = NULL; |
| 10796 | char *name; |
| 10797 | static char *keywords[] = {"path", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10798 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10799 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10800 | path.function_name = "listxattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10801 | path.allow_fd = 1; |
| 10802 | path.fd = -1; |
| 10803 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&$p:listxattr", keywords, |
| 10804 | path_converter, &path, |
| 10805 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10806 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10807 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10808 | if (fd_and_follow_symlinks_invalid("listxattr", path.fd, follow_symlinks)) |
| 10809 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10810 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10811 | name = path.narrow ? path.narrow : "."; |
| 10812 | for (i = 0; ; i++) { |
| 10813 | char *start, *trace, *end; |
| 10814 | ssize_t length; |
| 10815 | static Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
| 10816 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10817 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 10818 | /* ERANGE */ |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10819 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10820 | break; |
| 10821 | } |
| 10822 | buffer = PyMem_MALLOC(buffer_size); |
| 10823 | if (!buffer) { |
| 10824 | PyErr_NoMemory(); |
| 10825 | break; |
| 10826 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10827 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10828 | Py_BEGIN_ALLOW_THREADS; |
| 10829 | if (path.fd > -1) |
| 10830 | length = flistxattr(path.fd, buffer, buffer_size); |
| 10831 | else if (follow_symlinks) |
| 10832 | length = listxattr(name, buffer, buffer_size); |
| 10833 | else |
| 10834 | length = llistxattr(name, buffer, buffer_size); |
| 10835 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10836 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10837 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 10838 | if (errno == ERANGE) { |
| 10839 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 10840 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10841 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 10842 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10843 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10844 | break; |
| 10845 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10846 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10847 | result = PyList_New(0); |
| 10848 | if (!result) { |
| 10849 | goto exit; |
| 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 | end = buffer + length; |
| 10853 | for (trace = start = buffer; trace != end; trace++) { |
| 10854 | if (!*trace) { |
| 10855 | int error; |
| 10856 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 10857 | trace - start); |
| 10858 | if (!attribute) { |
| 10859 | Py_DECREF(result); |
| 10860 | result = NULL; |
| 10861 | goto exit; |
| 10862 | } |
| 10863 | error = PyList_Append(result, attribute); |
| 10864 | Py_DECREF(attribute); |
| 10865 | if (error) { |
| 10866 | Py_DECREF(result); |
| 10867 | result = NULL; |
| 10868 | goto exit; |
| 10869 | } |
| 10870 | start = trace + 1; |
| 10871 | } |
| 10872 | } |
| 10873 | break; |
| 10874 | } |
| 10875 | exit: |
| 10876 | path_cleanup(&path); |
| 10877 | if (buffer) |
| 10878 | PyMem_FREE(buffer); |
| 10879 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10880 | } |
| 10881 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10882 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10883 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10884 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10885 | PyDoc_STRVAR(posix_urandom__doc__, |
| 10886 | "urandom(n) -> str\n\n\ |
| 10887 | Return n random bytes suitable for cryptographic use."); |
| 10888 | |
| 10889 | static PyObject * |
| 10890 | posix_urandom(PyObject *self, PyObject *args) |
| 10891 | { |
| 10892 | Py_ssize_t size; |
| 10893 | PyObject *result; |
| 10894 | int ret; |
| 10895 | |
| 10896 | /* Read arguments */ |
| 10897 | if (!PyArg_ParseTuple(args, "n:urandom", &size)) |
| 10898 | return NULL; |
| 10899 | if (size < 0) |
| 10900 | return PyErr_Format(PyExc_ValueError, |
| 10901 | "negative argument not allowed"); |
| 10902 | result = PyBytes_FromStringAndSize(NULL, size); |
| 10903 | if (result == NULL) |
| 10904 | return NULL; |
| 10905 | |
| 10906 | ret = _PyOS_URandom(PyBytes_AS_STRING(result), |
| 10907 | PyBytes_GET_SIZE(result)); |
| 10908 | if (ret == -1) { |
| 10909 | Py_DECREF(result); |
| 10910 | return NULL; |
| 10911 | } |
| 10912 | return result; |
| 10913 | } |
| 10914 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10915 | /* Terminal size querying */ |
| 10916 | |
| 10917 | static PyTypeObject TerminalSizeType; |
| 10918 | |
| 10919 | PyDoc_STRVAR(TerminalSize_docstring, |
| 10920 | "A tuple of (columns, lines) for holding terminal window size"); |
| 10921 | |
| 10922 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 10923 | {"columns", "width of the terminal window in characters"}, |
| 10924 | {"lines", "height of the terminal window in characters"}, |
| 10925 | {NULL, NULL} |
| 10926 | }; |
| 10927 | |
| 10928 | static PyStructSequence_Desc TerminalSize_desc = { |
| 10929 | "os.terminal_size", |
| 10930 | TerminalSize_docstring, |
| 10931 | TerminalSize_fields, |
| 10932 | 2, |
| 10933 | }; |
| 10934 | |
| 10935 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 10936 | PyDoc_STRVAR(termsize__doc__, |
| 10937 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 10938 | "\n" \ |
| 10939 | "The optional argument fd (default standard output) specifies\n" \ |
| 10940 | "which file descriptor should be queried.\n" \ |
| 10941 | "\n" \ |
| 10942 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 10943 | "is thrown.\n" \ |
| 10944 | "\n" \ |
| 10945 | "This function will only be defined if an implementation is\n" \ |
| 10946 | "available for this system.\n" \ |
| 10947 | "\n" \ |
| 10948 | "shutil.get_terminal_size is the high-level function which should \n" \ |
| 10949 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 10950 | |
| 10951 | static PyObject* |
| 10952 | get_terminal_size(PyObject *self, PyObject *args) |
| 10953 | { |
| 10954 | int columns, lines; |
| 10955 | PyObject *termsize; |
| 10956 | |
| 10957 | int fd = fileno(stdout); |
| 10958 | /* Under some conditions stdout may not be connected and |
| 10959 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 10960 | * GUI apps don't have valid standard streams by default. |
| 10961 | * |
| 10962 | * If this happens, and the optional fd argument is not present, |
| 10963 | * the ioctl below will fail returning EBADF. This is what we want. |
| 10964 | */ |
| 10965 | |
| 10966 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 10967 | return NULL; |
| 10968 | |
| 10969 | #ifdef TERMSIZE_USE_IOCTL |
| 10970 | { |
| 10971 | struct winsize w; |
| 10972 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 10973 | return PyErr_SetFromErrno(PyExc_OSError); |
| 10974 | columns = w.ws_col; |
| 10975 | lines = w.ws_row; |
| 10976 | } |
| 10977 | #endif /* TERMSIZE_USE_IOCTL */ |
| 10978 | |
| 10979 | #ifdef TERMSIZE_USE_CONIO |
| 10980 | { |
| 10981 | DWORD nhandle; |
| 10982 | HANDLE handle; |
| 10983 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 10984 | switch (fd) { |
| 10985 | case 0: nhandle = STD_INPUT_HANDLE; |
| 10986 | break; |
| 10987 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 10988 | break; |
| 10989 | case 2: nhandle = STD_ERROR_HANDLE; |
| 10990 | break; |
| 10991 | default: |
| 10992 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 10993 | } |
| 10994 | handle = GetStdHandle(nhandle); |
| 10995 | if (handle == NULL) |
| 10996 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 10997 | if (handle == INVALID_HANDLE_VALUE) |
| 10998 | return PyErr_SetFromWindowsErr(0); |
| 10999 | |
| 11000 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 11001 | return PyErr_SetFromWindowsErr(0); |
| 11002 | |
| 11003 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 11004 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 11005 | } |
| 11006 | #endif /* TERMSIZE_USE_CONIO */ |
| 11007 | |
| 11008 | termsize = PyStructSequence_New(&TerminalSizeType); |
| 11009 | if (termsize == NULL) |
| 11010 | return NULL; |
| 11011 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 11012 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 11013 | if (PyErr_Occurred()) { |
| 11014 | Py_DECREF(termsize); |
| 11015 | return NULL; |
| 11016 | } |
| 11017 | return termsize; |
| 11018 | } |
| 11019 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 11020 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11021 | PyDoc_STRVAR(posix_cpu_count__doc__, |
| 11022 | "cpu_count() -> integer\n\n\ |
| 11023 | Return the number of CPUs in the system, or None if this value cannot be\n\ |
| 11024 | established."); |
| 11025 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11026 | static PyObject * |
| 11027 | posix_cpu_count(PyObject *self) |
| 11028 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11029 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11030 | #ifdef MS_WINDOWS |
| 11031 | SYSTEM_INFO sysinfo; |
| 11032 | GetSystemInfo(&sysinfo); |
| 11033 | ncpu = sysinfo.dwNumberOfProcessors; |
| 11034 | #elif defined(__hpux) |
| 11035 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 11036 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 11037 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11038 | #elif defined(__DragonFly__) || \ |
| 11039 | defined(__OpenBSD__) || \ |
| 11040 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11041 | defined(__NetBSD__) || \ |
| 11042 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 11043 | int mib[2]; |
| 11044 | size_t len = sizeof(ncpu); |
| 11045 | mib[0] = CTL_HW; |
| 11046 | mib[1] = HW_NCPU; |
| 11047 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 11048 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11049 | #endif |
| 11050 | if (ncpu >= 1) |
| 11051 | return PyLong_FromLong(ncpu); |
| 11052 | else |
| 11053 | Py_RETURN_NONE; |
| 11054 | } |
| 11055 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11056 | PyDoc_STRVAR(get_inheritable__doc__, |
| 11057 | "get_inheritable(fd) -> bool\n" \ |
| 11058 | "\n" \ |
| 11059 | "Get the close-on-exe flag of the specified file descriptor."); |
| 11060 | |
| 11061 | static PyObject* |
| 11062 | posix_get_inheritable(PyObject *self, PyObject *args) |
| 11063 | { |
| 11064 | int fd; |
| 11065 | int inheritable; |
| 11066 | |
| 11067 | if (!PyArg_ParseTuple(args, "i:get_inheritable", &fd)) |
| 11068 | return NULL; |
| 11069 | |
| 11070 | if (!_PyVerify_fd(fd)) |
| 11071 | return posix_error(); |
| 11072 | |
| 11073 | inheritable = _Py_get_inheritable(fd); |
| 11074 | if (inheritable < 0) |
| 11075 | return NULL; |
| 11076 | return PyBool_FromLong(inheritable); |
| 11077 | } |
| 11078 | |
| 11079 | PyDoc_STRVAR(set_inheritable__doc__, |
| 11080 | "set_inheritable(fd, inheritable)\n" \ |
| 11081 | "\n" \ |
| 11082 | "Set the inheritable flag of the specified file descriptor."); |
| 11083 | |
| 11084 | static PyObject* |
| 11085 | posix_set_inheritable(PyObject *self, PyObject *args) |
| 11086 | { |
| 11087 | int fd, inheritable; |
| 11088 | |
| 11089 | if (!PyArg_ParseTuple(args, "ii:set_inheritable", &fd, &inheritable)) |
| 11090 | return NULL; |
| 11091 | |
| 11092 | if (!_PyVerify_fd(fd)) |
| 11093 | return posix_error(); |
| 11094 | |
| 11095 | if (_Py_set_inheritable(fd, inheritable, NULL) < 0) |
| 11096 | return NULL; |
| 11097 | Py_RETURN_NONE; |
| 11098 | } |
| 11099 | |
| 11100 | |
| 11101 | #ifdef MS_WINDOWS |
| 11102 | PyDoc_STRVAR(get_handle_inheritable__doc__, |
| 11103 | "get_handle_inheritable(fd) -> bool\n" \ |
| 11104 | "\n" \ |
| 11105 | "Get the close-on-exe flag of the specified file descriptor."); |
| 11106 | |
| 11107 | static PyObject* |
| 11108 | posix_get_handle_inheritable(PyObject *self, PyObject *args) |
| 11109 | { |
| 11110 | Py_intptr_t handle; |
| 11111 | DWORD flags; |
| 11112 | |
| 11113 | if (!PyArg_ParseTuple(args, _Py_PARSE_INTPTR ":get_handle_inheritable", &handle)) |
| 11114 | return NULL; |
| 11115 | |
| 11116 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 11117 | PyErr_SetFromWindowsErr(0); |
| 11118 | return NULL; |
| 11119 | } |
| 11120 | |
| 11121 | return PyBool_FromLong(flags & HANDLE_FLAG_INHERIT); |
| 11122 | } |
| 11123 | |
| 11124 | PyDoc_STRVAR(set_handle_inheritable__doc__, |
| 11125 | "set_handle_inheritable(fd, inheritable)\n" \ |
| 11126 | "\n" \ |
| 11127 | "Set the inheritable flag of the specified handle."); |
| 11128 | |
| 11129 | static PyObject* |
| 11130 | posix_set_handle_inheritable(PyObject *self, PyObject *args) |
| 11131 | { |
| 11132 | int inheritable = 1; |
| 11133 | Py_intptr_t handle; |
| 11134 | DWORD flags; |
| 11135 | |
| 11136 | if (!PyArg_ParseTuple(args, _Py_PARSE_INTPTR "i:set_handle_inheritable", |
| 11137 | &handle, &inheritable)) |
| 11138 | return NULL; |
| 11139 | |
| 11140 | if (inheritable) |
| 11141 | flags = HANDLE_FLAG_INHERIT; |
| 11142 | else |
| 11143 | flags = 0; |
| 11144 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 11145 | PyErr_SetFromWindowsErr(0); |
| 11146 | return NULL; |
| 11147 | } |
| 11148 | Py_RETURN_NONE; |
| 11149 | } |
| 11150 | #endif /* MS_WINDOWS */ |
| 11151 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11152 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 11153 | /*[clinic input] |
| 11154 | dump buffer |
| 11155 | [clinic start generated code]*/ |
| 11156 | |
| 11157 | #ifndef OS_TTYNAME_METHODDEF |
| 11158 | #define OS_TTYNAME_METHODDEF |
| 11159 | #endif /* !defined(OS_TTYNAME_METHODDEF) */ |
| 11160 | /*[clinic end generated code: output=5d071bbc8f49ea12 input=524ce2e021e4eba6]*/ |
| 11161 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 11162 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11163 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 11164 | |
| 11165 | OS_STAT_METHODDEF |
| 11166 | OS_ACCESS_METHODDEF |
| 11167 | OS_TTYNAME_METHODDEF |
| 11168 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11169 | {"chdir", (PyCFunction)posix_chdir, |
| 11170 | METH_VARARGS | METH_KEYWORDS, |
| 11171 | posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 11172 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11173 | {"chflags", (PyCFunction)posix_chflags, |
| 11174 | METH_VARARGS | METH_KEYWORDS, |
| 11175 | posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 11176 | #endif /* HAVE_CHFLAGS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11177 | {"chmod", (PyCFunction)posix_chmod, |
| 11178 | METH_VARARGS | METH_KEYWORDS, |
| 11179 | posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 11180 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11181 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 11182 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11183 | #ifdef HAVE_CHOWN |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11184 | {"chown", (PyCFunction)posix_chown, |
| 11185 | METH_VARARGS | METH_KEYWORDS, |
| 11186 | posix_chown__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 11187 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 11188 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11189 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 11190 | #endif /* HAVE_LCHMOD */ |
| 11191 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11192 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 11193 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 11194 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11195 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 11196 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 11197 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11198 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 11199 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 11200 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11201 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 11202 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11203 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11204 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11205 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11206 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 11207 | METH_NOARGS, posix_getcwd__doc__}, |
| 11208 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 11209 | METH_NOARGS, posix_getcwdb__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11210 | #if defined(HAVE_LINK) || defined(MS_WINDOWS) |
| 11211 | {"link", (PyCFunction)posix_link, |
| 11212 | METH_VARARGS | METH_KEYWORDS, |
| 11213 | posix_link__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11214 | #endif /* HAVE_LINK */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11215 | {"listdir", (PyCFunction)posix_listdir, |
| 11216 | METH_VARARGS | METH_KEYWORDS, |
| 11217 | posix_listdir__doc__}, |
| 11218 | {"lstat", (PyCFunction)posix_lstat, |
| 11219 | METH_VARARGS | METH_KEYWORDS, |
| 11220 | posix_lstat__doc__}, |
| 11221 | {"mkdir", (PyCFunction)posix_mkdir, |
| 11222 | METH_VARARGS | METH_KEYWORDS, |
| 11223 | posix_mkdir__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 11224 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11225 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 11226 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11227 | #ifdef HAVE_GETPRIORITY |
| 11228 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 11229 | #endif /* HAVE_GETPRIORITY */ |
| 11230 | #ifdef HAVE_SETPRIORITY |
| 11231 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 11232 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 11233 | #ifdef HAVE_READLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11234 | {"readlink", (PyCFunction)posix_readlink, |
| 11235 | METH_VARARGS | METH_KEYWORDS, |
| 11236 | readlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 11237 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 11238 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11239 | {"readlink", (PyCFunction)win_readlink, |
| 11240 | METH_VARARGS | METH_KEYWORDS, |
| 11241 | readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 11242 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11243 | {"rename", (PyCFunction)posix_rename, |
| 11244 | METH_VARARGS | METH_KEYWORDS, |
| 11245 | posix_rename__doc__}, |
| 11246 | {"replace", (PyCFunction)posix_replace, |
| 11247 | METH_VARARGS | METH_KEYWORDS, |
| 11248 | posix_replace__doc__}, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 11249 | {"rmdir", (PyCFunction)posix_rmdir, |
| 11250 | METH_VARARGS | METH_KEYWORDS, |
| 11251 | posix_rmdir__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11252 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11253 | #if defined(HAVE_SYMLINK) |
| 11254 | {"symlink", (PyCFunction)posix_symlink, |
| 11255 | METH_VARARGS | METH_KEYWORDS, |
| 11256 | posix_symlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 11257 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 11258 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11259 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11260 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11261 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11262 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11263 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11264 | #endif /* HAVE_UNAME */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11265 | {"unlink", (PyCFunction)posix_unlink, |
| 11266 | METH_VARARGS | METH_KEYWORDS, |
| 11267 | posix_unlink__doc__}, |
| 11268 | {"remove", (PyCFunction)posix_unlink, |
| 11269 | METH_VARARGS | METH_KEYWORDS, |
| 11270 | posix_remove__doc__}, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 11271 | {"utime", (PyCFunction)posix_utime, |
| 11272 | METH_VARARGS | METH_KEYWORDS, posix_utime__doc__}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 11273 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11274 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 11275 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11276 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 11277 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11278 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11279 | {"execve", (PyCFunction)posix_execve, |
| 11280 | METH_VARARGS | METH_KEYWORDS, |
| 11281 | posix_execve__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 11282 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 11283 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11284 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 11285 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 11286 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 11287 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11288 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 11289 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11290 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11291 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11292 | #endif /* HAVE_FORK */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11293 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 11294 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11295 | {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__}, |
| 11296 | {"sched_get_priority_min", posix_sched_get_priority_min, METH_VARARGS, posix_sched_get_priority_min__doc__}, |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 11297 | #endif |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11298 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11299 | {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11300 | #endif |
| 11301 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11302 | {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11303 | #endif |
| 11304 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11305 | {"sched_rr_get_interval", posix_sched_rr_get_interval, METH_VARARGS, posix_sched_rr_get_interval__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11306 | #endif |
| 11307 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11308 | {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11309 | #endif |
| 11310 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11311 | {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11312 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11313 | {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__}, |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11314 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11315 | {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__}, |
| 11316 | {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__}, |
| 11317 | #endif |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 11318 | #endif /* HAVE_SCHED_H */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 11319 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11320 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 11321 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 11322 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11323 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 11324 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 11325 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11326 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 11327 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11328 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11329 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 11330 | #endif /* HAVE_GETEUID */ |
| 11331 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11332 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11333 | #endif /* HAVE_GETGID */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 11334 | #ifdef HAVE_GETGROUPLIST |
| 11335 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 11336 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11337 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11338 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11339 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11340 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11341 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11342 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11343 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11344 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11345 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11346 | #endif /* HAVE_GETPPID */ |
| 11347 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11348 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11349 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 11350 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11351 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 11352 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11353 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11354 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11355 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 11356 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11357 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 11358 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 11359 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11360 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 11361 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 11362 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11363 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 11364 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 11365 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11366 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11367 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11368 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11369 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11370 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11371 | #endif /* HAVE_SETEUID */ |
| 11372 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11373 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11374 | #endif /* HAVE_SETEGID */ |
| 11375 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11376 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11377 | #endif /* HAVE_SETREUID */ |
| 11378 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11379 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11380 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11381 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11382 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11383 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 11384 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11385 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 11386 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 11387 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11388 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 11389 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 11390 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11391 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 11392 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11393 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11394 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11395 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11396 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11397 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11398 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11399 | #ifdef HAVE_WAIT3 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 11400 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11401 | #endif /* HAVE_WAIT3 */ |
| 11402 | #ifdef HAVE_WAIT4 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 11403 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11404 | #endif /* HAVE_WAIT4 */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11405 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11406 | {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__}, |
| 11407 | #endif |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 11408 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11409 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11410 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 11411 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11412 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 11413 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11414 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11415 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11416 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11417 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11418 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11419 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11420 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11421 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11422 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11423 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11424 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11425 | #endif /* HAVE_TCSETPGRP */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11426 | {"open", (PyCFunction)posix_open,\ |
| 11427 | METH_VARARGS | METH_KEYWORDS, |
| 11428 | posix_open__doc__}, |
Benjamin Peterson | 932bba3 | 2014-02-11 10:16:16 -0500 | [diff] [blame] | 11429 | {"close", posix_close_, METH_VARARGS, posix_close__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11430 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 11431 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 11432 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11433 | {"dup2", (PyCFunction)posix_dup2, |
| 11434 | METH_VARARGS | METH_KEYWORDS, posix_dup2__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11435 | #ifdef HAVE_LOCKF |
| 11436 | {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__}, |
| 11437 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11438 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 11439 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11440 | #ifdef HAVE_READV |
| 11441 | {"readv", posix_readv, METH_VARARGS, posix_readv__doc__}, |
| 11442 | #endif |
| 11443 | #ifdef HAVE_PREAD |
| 11444 | {"pread", posix_pread, METH_VARARGS, posix_pread__doc__}, |
| 11445 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11446 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11447 | #ifdef HAVE_WRITEV |
| 11448 | {"writev", posix_writev, METH_VARARGS, posix_writev__doc__}, |
| 11449 | #endif |
| 11450 | #ifdef HAVE_PWRITE |
| 11451 | {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__}, |
| 11452 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11453 | #ifdef HAVE_SENDFILE |
| 11454 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 11455 | posix_sendfile__doc__}, |
| 11456 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 11457 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11458 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11459 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11460 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11461 | #endif |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 11462 | #ifdef HAVE_PIPE2 |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 11463 | {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__}, |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 11464 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11465 | #ifdef HAVE_MKFIFO |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11466 | {"mkfifo", (PyCFunction)posix_mkfifo, |
| 11467 | METH_VARARGS | METH_KEYWORDS, |
| 11468 | posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11469 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 11470 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11471 | {"mknod", (PyCFunction)posix_mknod, |
| 11472 | METH_VARARGS | METH_KEYWORDS, |
| 11473 | posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 11474 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 11475 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11476 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 11477 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 11478 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 11479 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11480 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11481 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11482 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11483 | #ifdef HAVE_TRUNCATE |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11484 | {"truncate", (PyCFunction)posix_truncate, |
| 11485 | METH_VARARGS | METH_KEYWORDS, |
| 11486 | posix_truncate__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11487 | #endif |
| 11488 | #ifdef HAVE_POSIX_FALLOCATE |
| 11489 | {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__}, |
| 11490 | #endif |
| 11491 | #ifdef HAVE_POSIX_FADVISE |
| 11492 | {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__}, |
| 11493 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 11494 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11495 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 11496 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 11497 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11498 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 11499 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11500 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11501 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11502 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11503 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11504 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11505 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11506 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11507 | #ifdef HAVE_SYNC |
| 11508 | {"sync", posix_sync, METH_NOARGS, posix_sync__doc__}, |
| 11509 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11510 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11511 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11512 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11513 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11514 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11515 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11516 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 11517 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11518 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 11519 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11520 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11521 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11522 | #endif /* WIFSTOPPED */ |
| 11523 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11524 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11525 | #endif /* WIFSIGNALED */ |
| 11526 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11527 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11528 | #endif /* WIFEXITED */ |
| 11529 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11530 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11531 | #endif /* WEXITSTATUS */ |
| 11532 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11533 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11534 | #endif /* WTERMSIG */ |
| 11535 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11536 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11537 | #endif /* WSTOPSIG */ |
| 11538 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11539 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11540 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11541 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11542 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11543 | {"statvfs", (PyCFunction)posix_statvfs, |
| 11544 | METH_VARARGS | METH_KEYWORDS, |
| 11545 | posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11546 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11547 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11548 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11549 | #endif |
| 11550 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11551 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11552 | #endif |
| 11553 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11554 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11555 | #endif |
| 11556 | #ifdef HAVE_PATHCONF |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11557 | {"pathconf", (PyCFunction)posix_pathconf, |
| 11558 | METH_VARARGS | METH_KEYWORDS, |
| 11559 | posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11560 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11561 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11562 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11563 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 11564 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 11565 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 11566 | {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__}, |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 11567 | {"_getvolumepathname", posix__getvolumepathname, METH_VARARGS, posix__getvolumepathname__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 11568 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11569 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11570 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11571 | #endif |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 11572 | {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11573 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11574 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11575 | #endif |
| 11576 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11577 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11578 | #endif |
| 11579 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11580 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11581 | #endif |
| 11582 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11583 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11584 | #endif |
| 11585 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11586 | #ifdef USE_XATTRS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11587 | {"setxattr", (PyCFunction)posix_setxattr, |
| 11588 | METH_VARARGS | METH_KEYWORDS, |
| 11589 | posix_setxattr__doc__}, |
| 11590 | {"getxattr", (PyCFunction)posix_getxattr, |
| 11591 | METH_VARARGS | METH_KEYWORDS, |
| 11592 | posix_getxattr__doc__}, |
| 11593 | {"removexattr", (PyCFunction)posix_removexattr, |
| 11594 | METH_VARARGS | METH_KEYWORDS, |
| 11595 | posix_removexattr__doc__}, |
| 11596 | {"listxattr", (PyCFunction)posix_listxattr, |
| 11597 | METH_VARARGS | METH_KEYWORDS, |
| 11598 | posix_listxattr__doc__}, |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11599 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11600 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 11601 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 11602 | #endif |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11603 | {"cpu_count", (PyCFunction)posix_cpu_count, |
| 11604 | METH_NOARGS, posix_cpu_count__doc__}, |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11605 | {"get_inheritable", posix_get_inheritable, METH_VARARGS, get_inheritable__doc__}, |
| 11606 | {"set_inheritable", posix_set_inheritable, METH_VARARGS, set_inheritable__doc__}, |
| 11607 | #ifdef MS_WINDOWS |
| 11608 | {"get_handle_inheritable", posix_get_handle_inheritable, |
| 11609 | METH_VARARGS, get_handle_inheritable__doc__}, |
| 11610 | {"set_handle_inheritable", posix_set_handle_inheritable, |
| 11611 | METH_VARARGS, set_handle_inheritable__doc__}, |
| 11612 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11613 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11614 | }; |
| 11615 | |
| 11616 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11617 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11618 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11619 | enable_symlink() |
| 11620 | { |
| 11621 | HANDLE tok; |
| 11622 | TOKEN_PRIVILEGES tok_priv; |
| 11623 | LUID luid; |
| 11624 | int meth_idx = 0; |
| 11625 | |
| 11626 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11627 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11628 | |
| 11629 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11630 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11631 | |
| 11632 | tok_priv.PrivilegeCount = 1; |
| 11633 | tok_priv.Privileges[0].Luid = luid; |
| 11634 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 11635 | |
| 11636 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 11637 | sizeof(TOKEN_PRIVILEGES), |
| 11638 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11639 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11640 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11641 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 11642 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11643 | } |
| 11644 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 11645 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11646 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11647 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11648 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11649 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11650 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11651 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11652 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11653 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11654 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11655 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11656 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11657 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11658 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11659 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11660 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11661 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11662 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11663 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11664 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11665 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11666 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11667 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11668 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11669 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11670 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11671 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11672 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11673 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11674 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11675 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11676 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11677 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11678 | #endif |
| 11679 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11680 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11681 | #endif |
| 11682 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11683 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11684 | #endif |
| 11685 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11686 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11687 | #endif |
| 11688 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11689 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11690 | #endif |
| 11691 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11692 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11693 | #endif |
| 11694 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11695 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11696 | #endif |
| 11697 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11698 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11699 | #endif |
| 11700 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11701 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11702 | #endif |
| 11703 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11704 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11705 | #endif |
| 11706 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11707 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11708 | #endif |
| 11709 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11710 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11711 | #endif |
| 11712 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11713 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11714 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11715 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11716 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11717 | #endif |
| 11718 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11719 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11720 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11721 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11722 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11723 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11724 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11725 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11726 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11727 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11728 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11729 | #endif |
| 11730 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11731 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11732 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11733 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11734 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11735 | #endif |
| 11736 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11737 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11738 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 11739 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11740 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 11741 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11742 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11743 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11744 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 11745 | #ifdef O_TMPFILE |
| 11746 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 11747 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11748 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11749 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11750 | #endif |
| 11751 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11752 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11753 | #endif |
| 11754 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11755 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11756 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 11757 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11758 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 11759 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11760 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11761 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11762 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11763 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11764 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 11765 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11766 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 11767 | #endif |
| 11768 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11769 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 11770 | #endif |
| 11771 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11772 | /* MS Windows */ |
| 11773 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11774 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11775 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11776 | #endif |
| 11777 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11778 | /* Optimize for short life (keep in memory). */ |
| 11779 | /* 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] | 11780 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11781 | #endif |
| 11782 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11783 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11784 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11785 | #endif |
| 11786 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11787 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11788 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11789 | #endif |
| 11790 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11791 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11792 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11793 | #endif |
| 11794 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11795 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11796 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11797 | /* Send a SIGIO signal whenever input or output |
| 11798 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11799 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11800 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11801 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11802 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11803 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11804 | #endif |
| 11805 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11806 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11807 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11808 | #endif |
| 11809 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11810 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11811 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11812 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11813 | #ifdef O_NOLINKS |
| 11814 | /* 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] | 11815 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11816 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11817 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11818 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11819 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11820 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11821 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11822 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11823 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11824 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11825 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11826 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11827 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11828 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11829 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11830 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11831 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11832 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11833 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11834 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11835 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11836 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11837 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11838 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11839 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11840 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11841 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11842 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11843 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11844 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11845 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11846 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11847 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11848 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11849 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11850 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11851 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11852 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11853 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11854 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11855 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11856 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11857 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11858 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11859 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11860 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11861 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11862 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11863 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11864 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11865 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11866 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11867 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11868 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11869 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11870 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11871 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11872 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11873 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11874 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11875 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11876 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11877 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11878 | #endif /* ST_RDONLY */ |
| 11879 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11880 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11881 | #endif /* ST_NOSUID */ |
| 11882 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 11883 | /* GNU extensions */ |
| 11884 | #ifdef ST_NODEV |
| 11885 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 11886 | #endif /* ST_NODEV */ |
| 11887 | #ifdef ST_NOEXEC |
| 11888 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 11889 | #endif /* ST_NOEXEC */ |
| 11890 | #ifdef ST_SYNCHRONOUS |
| 11891 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 11892 | #endif /* ST_SYNCHRONOUS */ |
| 11893 | #ifdef ST_MANDLOCK |
| 11894 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 11895 | #endif /* ST_MANDLOCK */ |
| 11896 | #ifdef ST_WRITE |
| 11897 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 11898 | #endif /* ST_WRITE */ |
| 11899 | #ifdef ST_APPEND |
| 11900 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 11901 | #endif /* ST_APPEND */ |
| 11902 | #ifdef ST_NOATIME |
| 11903 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 11904 | #endif /* ST_NOATIME */ |
| 11905 | #ifdef ST_NODIRATIME |
| 11906 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 11907 | #endif /* ST_NODIRATIME */ |
| 11908 | #ifdef ST_RELATIME |
| 11909 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 11910 | #endif /* ST_RELATIME */ |
| 11911 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11912 | /* FreeBSD sendfile() constants */ |
| 11913 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11914 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11915 | #endif |
| 11916 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11917 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11918 | #endif |
| 11919 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11920 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11921 | #endif |
| 11922 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11923 | /* constants for posix_fadvise */ |
| 11924 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11925 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11926 | #endif |
| 11927 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11928 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11929 | #endif |
| 11930 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11931 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11932 | #endif |
| 11933 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11934 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11935 | #endif |
| 11936 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11937 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11938 | #endif |
| 11939 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11940 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11941 | #endif |
| 11942 | |
| 11943 | /* constants for waitid */ |
| 11944 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11945 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 11946 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 11947 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11948 | #endif |
| 11949 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11950 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11951 | #endif |
| 11952 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11953 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11954 | #endif |
| 11955 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11956 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11957 | #endif |
| 11958 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11959 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11960 | #endif |
| 11961 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11962 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11963 | #endif |
| 11964 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11965 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11966 | #endif |
| 11967 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11968 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11969 | #endif |
| 11970 | |
| 11971 | /* constants for lockf */ |
| 11972 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11973 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11974 | #endif |
| 11975 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11976 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11977 | #endif |
| 11978 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11979 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11980 | #endif |
| 11981 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11982 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11983 | #endif |
| 11984 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11985 | #ifdef HAVE_SPAWNV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11986 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 11987 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
| 11988 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
| 11989 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
| 11990 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11991 | #endif |
| 11992 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11993 | #ifdef HAVE_SCHED_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11994 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
| 11995 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
| 11996 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11997 | #ifdef SCHED_SPORADIC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11998 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11999 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12000 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12001 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12002 | #endif |
| 12003 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12004 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12005 | #endif |
| 12006 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12007 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12008 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12009 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12010 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12011 | #endif |
| 12012 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12013 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12014 | #endif |
| 12015 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12016 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12017 | #endif |
| 12018 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12019 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12020 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12021 | #endif |
| 12022 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12023 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12024 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 12025 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 12026 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12027 | #endif |
| 12028 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12029 | #ifdef RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12030 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12031 | #endif |
| 12032 | #ifdef RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12033 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12034 | #endif |
| 12035 | #ifdef RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12036 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12037 | #endif |
| 12038 | #ifdef RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12039 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12040 | #endif |
| 12041 | #ifdef RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12042 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12043 | #endif |
| 12044 | #ifdef RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12045 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12046 | #endif |
| 12047 | #ifdef RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12048 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12049 | #endif |
| 12050 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12051 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12052 | } |
| 12053 | |
| 12054 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12055 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12056 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 12057 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 12058 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 12059 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12060 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 12061 | #define MODNAME "posix" |
| 12062 | #endif |
| 12063 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12064 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12065 | PyModuleDef_HEAD_INIT, |
| 12066 | MODNAME, |
| 12067 | posix__doc__, |
| 12068 | -1, |
| 12069 | posix_methods, |
| 12070 | NULL, |
| 12071 | NULL, |
| 12072 | NULL, |
| 12073 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12074 | }; |
| 12075 | |
| 12076 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12077 | static char *have_functions[] = { |
| 12078 | |
| 12079 | #ifdef HAVE_FACCESSAT |
| 12080 | "HAVE_FACCESSAT", |
| 12081 | #endif |
| 12082 | |
| 12083 | #ifdef HAVE_FCHDIR |
| 12084 | "HAVE_FCHDIR", |
| 12085 | #endif |
| 12086 | |
| 12087 | #ifdef HAVE_FCHMOD |
| 12088 | "HAVE_FCHMOD", |
| 12089 | #endif |
| 12090 | |
| 12091 | #ifdef HAVE_FCHMODAT |
| 12092 | "HAVE_FCHMODAT", |
| 12093 | #endif |
| 12094 | |
| 12095 | #ifdef HAVE_FCHOWN |
| 12096 | "HAVE_FCHOWN", |
| 12097 | #endif |
| 12098 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 12099 | #ifdef HAVE_FCHOWNAT |
| 12100 | "HAVE_FCHOWNAT", |
| 12101 | #endif |
| 12102 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12103 | #ifdef HAVE_FEXECVE |
| 12104 | "HAVE_FEXECVE", |
| 12105 | #endif |
| 12106 | |
| 12107 | #ifdef HAVE_FDOPENDIR |
| 12108 | "HAVE_FDOPENDIR", |
| 12109 | #endif |
| 12110 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12111 | #ifdef HAVE_FPATHCONF |
| 12112 | "HAVE_FPATHCONF", |
| 12113 | #endif |
| 12114 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12115 | #ifdef HAVE_FSTATAT |
| 12116 | "HAVE_FSTATAT", |
| 12117 | #endif |
| 12118 | |
| 12119 | #ifdef HAVE_FSTATVFS |
| 12120 | "HAVE_FSTATVFS", |
| 12121 | #endif |
| 12122 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12123 | #ifdef HAVE_FTRUNCATE |
| 12124 | "HAVE_FTRUNCATE", |
| 12125 | #endif |
| 12126 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12127 | #ifdef HAVE_FUTIMENS |
| 12128 | "HAVE_FUTIMENS", |
| 12129 | #endif |
| 12130 | |
| 12131 | #ifdef HAVE_FUTIMES |
| 12132 | "HAVE_FUTIMES", |
| 12133 | #endif |
| 12134 | |
| 12135 | #ifdef HAVE_FUTIMESAT |
| 12136 | "HAVE_FUTIMESAT", |
| 12137 | #endif |
| 12138 | |
| 12139 | #ifdef HAVE_LINKAT |
| 12140 | "HAVE_LINKAT", |
| 12141 | #endif |
| 12142 | |
| 12143 | #ifdef HAVE_LCHFLAGS |
| 12144 | "HAVE_LCHFLAGS", |
| 12145 | #endif |
| 12146 | |
| 12147 | #ifdef HAVE_LCHMOD |
| 12148 | "HAVE_LCHMOD", |
| 12149 | #endif |
| 12150 | |
| 12151 | #ifdef HAVE_LCHOWN |
| 12152 | "HAVE_LCHOWN", |
| 12153 | #endif |
| 12154 | |
| 12155 | #ifdef HAVE_LSTAT |
| 12156 | "HAVE_LSTAT", |
| 12157 | #endif |
| 12158 | |
| 12159 | #ifdef HAVE_LUTIMES |
| 12160 | "HAVE_LUTIMES", |
| 12161 | #endif |
| 12162 | |
| 12163 | #ifdef HAVE_MKDIRAT |
| 12164 | "HAVE_MKDIRAT", |
| 12165 | #endif |
| 12166 | |
| 12167 | #ifdef HAVE_MKFIFOAT |
| 12168 | "HAVE_MKFIFOAT", |
| 12169 | #endif |
| 12170 | |
| 12171 | #ifdef HAVE_MKNODAT |
| 12172 | "HAVE_MKNODAT", |
| 12173 | #endif |
| 12174 | |
| 12175 | #ifdef HAVE_OPENAT |
| 12176 | "HAVE_OPENAT", |
| 12177 | #endif |
| 12178 | |
| 12179 | #ifdef HAVE_READLINKAT |
| 12180 | "HAVE_READLINKAT", |
| 12181 | #endif |
| 12182 | |
| 12183 | #ifdef HAVE_RENAMEAT |
| 12184 | "HAVE_RENAMEAT", |
| 12185 | #endif |
| 12186 | |
| 12187 | #ifdef HAVE_SYMLINKAT |
| 12188 | "HAVE_SYMLINKAT", |
| 12189 | #endif |
| 12190 | |
| 12191 | #ifdef HAVE_UNLINKAT |
| 12192 | "HAVE_UNLINKAT", |
| 12193 | #endif |
| 12194 | |
| 12195 | #ifdef HAVE_UTIMENSAT |
| 12196 | "HAVE_UTIMENSAT", |
| 12197 | #endif |
| 12198 | |
| 12199 | #ifdef MS_WINDOWS |
| 12200 | "MS_WINDOWS", |
| 12201 | #endif |
| 12202 | |
| 12203 | NULL |
| 12204 | }; |
| 12205 | |
| 12206 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 12207 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 12208 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12209 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12210 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12211 | PyObject *list; |
| 12212 | char **trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12213 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12214 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12215 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12216 | #endif |
| 12217 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12218 | m = PyModule_Create(&posixmodule); |
| 12219 | if (m == NULL) |
| 12220 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12221 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12222 | /* Initialize environ dictionary */ |
| 12223 | v = convertenviron(); |
| 12224 | Py_XINCREF(v); |
| 12225 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 12226 | return NULL; |
| 12227 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12228 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12229 | if (all_ins(m)) |
| 12230 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12231 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12232 | if (setup_confname_tables(m)) |
| 12233 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12234 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12235 | Py_INCREF(PyExc_OSError); |
| 12236 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 12237 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12238 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12239 | if (posix_putenv_garbage == NULL) |
| 12240 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12241 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 12242 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12243 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12244 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 12245 | waitid_result_desc.name = MODNAME ".waitid_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12246 | if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0) |
| 12247 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12248 | #endif |
| 12249 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 12250 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12251 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 12252 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 12253 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12254 | if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0) |
| 12255 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12256 | structseq_new = StatResultType.tp_new; |
| 12257 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12258 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 12259 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12260 | if (PyStructSequence_InitType2(&StatVFSResultType, |
| 12261 | &statvfs_result_desc) < 0) |
| 12262 | return NULL; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12263 | #ifdef NEED_TICKS_PER_SECOND |
| 12264 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12265 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12266 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12267 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12268 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12269 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12270 | # endif |
| 12271 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12272 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 12273 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12274 | sched_param_desc.name = MODNAME ".sched_param"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12275 | if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0) |
| 12276 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12277 | SchedParamType.tp_new = sched_param_new; |
| 12278 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12279 | |
| 12280 | /* initialize TerminalSize_info */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12281 | if (PyStructSequence_InitType2(&TerminalSizeType, |
| 12282 | &TerminalSize_desc) < 0) |
| 12283 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12284 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12285 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 12286 | Py_INCREF((PyObject*) &WaitidResultType); |
| 12287 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 12288 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12289 | Py_INCREF((PyObject*) &StatResultType); |
| 12290 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 12291 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 12292 | PyModule_AddObject(m, "statvfs_result", |
| 12293 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 12294 | |
| 12295 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12296 | Py_INCREF(&SchedParamType); |
| 12297 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 12298 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12299 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 12300 | times_result_desc.name = MODNAME ".times_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12301 | if (PyStructSequence_InitType2(&TimesResultType, ×_result_desc) < 0) |
| 12302 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 12303 | PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType); |
| 12304 | |
| 12305 | uname_result_desc.name = MODNAME ".uname_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12306 | if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0) |
| 12307 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 12308 | PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType); |
| 12309 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12310 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12311 | /* |
| 12312 | * Step 2 of weak-linking support on Mac OS X. |
| 12313 | * |
| 12314 | * The code below removes functions that are not available on the |
| 12315 | * currently active platform. |
| 12316 | * |
| 12317 | * 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] | 12318 | * 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] | 12319 | * OSX 10.4. |
| 12320 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12321 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12322 | if (fstatvfs == NULL) { |
| 12323 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 12324 | return NULL; |
| 12325 | } |
| 12326 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12327 | #endif /* HAVE_FSTATVFS */ |
| 12328 | |
| 12329 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12330 | if (statvfs == NULL) { |
| 12331 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 12332 | return NULL; |
| 12333 | } |
| 12334 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12335 | #endif /* HAVE_STATVFS */ |
| 12336 | |
| 12337 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12338 | if (lchown == NULL) { |
| 12339 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 12340 | return NULL; |
| 12341 | } |
| 12342 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12343 | #endif /* HAVE_LCHOWN */ |
| 12344 | |
| 12345 | |
| 12346 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12347 | |
Antoine Pitrou | 9d20e0e | 2012-09-12 18:01:36 +0200 | [diff] [blame] | 12348 | Py_INCREF(&TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12349 | PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); |
| 12350 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 12351 | billion = PyLong_FromLong(1000000000); |
| 12352 | if (!billion) |
| 12353 | return NULL; |
| 12354 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12355 | /* suppress "function not used" warnings */ |
| 12356 | { |
| 12357 | int ignored; |
| 12358 | fd_specified("", -1); |
| 12359 | follow_symlinks_specified("", 1); |
| 12360 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 12361 | dir_fd_converter(Py_None, &ignored); |
| 12362 | dir_fd_unavailable(Py_None, &ignored); |
| 12363 | } |
| 12364 | |
| 12365 | /* |
| 12366 | * provide list of locally available functions |
| 12367 | * so os.py can populate support_* lists |
| 12368 | */ |
| 12369 | list = PyList_New(0); |
| 12370 | if (!list) |
| 12371 | return NULL; |
| 12372 | for (trace = have_functions; *trace; trace++) { |
| 12373 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 12374 | if (!unicode) |
| 12375 | return NULL; |
| 12376 | if (PyList_Append(list, unicode)) |
| 12377 | return NULL; |
| 12378 | Py_DECREF(unicode); |
| 12379 | } |
| 12380 | PyModule_AddObject(m, "_have_functions", list); |
| 12381 | |
| 12382 | initialized = 1; |
| 12383 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12384 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12385 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12386 | |
| 12387 | #ifdef __cplusplus |
| 12388 | } |
| 12389 | #endif |