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]*/ |
| 187 | /*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ |
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) { |
| 783 | Py_DECREF(path->cleanup); |
| 784 | path->cleanup = NULL; |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | static int |
| 789 | path_converter(PyObject *o, void *p) { |
| 790 | path_t *path = (path_t *)p; |
| 791 | PyObject *unicode, *bytes; |
| 792 | Py_ssize_t length; |
| 793 | char *narrow; |
| 794 | |
| 795 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 796 | PyErr_Format(exc, "%s%s" fmt, \ |
| 797 | path->function_name ? path->function_name : "", \ |
| 798 | path->function_name ? ": " : "", \ |
| 799 | path->argument_name ? path->argument_name : "path") |
| 800 | |
| 801 | /* Py_CLEANUP_SUPPORTED support */ |
| 802 | if (o == NULL) { |
| 803 | path_cleanup(path); |
| 804 | return 1; |
| 805 | } |
| 806 | |
| 807 | /* ensure it's always safe to call path_cleanup() */ |
| 808 | path->cleanup = NULL; |
| 809 | |
| 810 | if (o == Py_None) { |
| 811 | if (!path->nullable) { |
| 812 | FORMAT_EXCEPTION(PyExc_TypeError, |
| 813 | "can't specify None for %s argument"); |
| 814 | return 0; |
| 815 | } |
| 816 | path->wide = NULL; |
| 817 | path->narrow = NULL; |
| 818 | path->length = 0; |
| 819 | path->object = o; |
| 820 | path->fd = -1; |
| 821 | return 1; |
| 822 | } |
| 823 | |
| 824 | unicode = PyUnicode_FromObject(o); |
| 825 | if (unicode) { |
| 826 | #ifdef MS_WINDOWS |
| 827 | wchar_t *wide; |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 828 | |
| 829 | wide = PyUnicode_AsUnicodeAndSize(unicode, &length); |
| 830 | if (!wide) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 831 | Py_DECREF(unicode); |
| 832 | return 0; |
| 833 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 834 | if (length > 32767) { |
| 835 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 836 | Py_DECREF(unicode); |
| 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | path->wide = wide; |
| 841 | path->narrow = NULL; |
| 842 | path->length = length; |
| 843 | path->object = o; |
| 844 | path->fd = -1; |
| 845 | path->cleanup = unicode; |
| 846 | return Py_CLEANUP_SUPPORTED; |
| 847 | #else |
| 848 | int converted = PyUnicode_FSConverter(unicode, &bytes); |
| 849 | Py_DECREF(unicode); |
| 850 | if (!converted) |
| 851 | bytes = NULL; |
| 852 | #endif |
| 853 | } |
| 854 | else { |
| 855 | PyErr_Clear(); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 856 | if (PyObject_CheckBuffer(o)) |
| 857 | bytes = PyBytes_FromObject(o); |
| 858 | else |
| 859 | bytes = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 860 | if (!bytes) { |
| 861 | PyErr_Clear(); |
| 862 | if (path->allow_fd) { |
| 863 | int fd; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 864 | int result = _fd_converter(o, &fd, |
| 865 | "string, bytes or integer"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 866 | if (result) { |
| 867 | path->wide = NULL; |
| 868 | path->narrow = NULL; |
| 869 | path->length = 0; |
| 870 | path->object = o; |
| 871 | path->fd = fd; |
| 872 | return result; |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | if (!bytes) { |
| 879 | if (!PyErr_Occurred()) |
| 880 | FORMAT_EXCEPTION(PyExc_TypeError, "illegal type for %s parameter"); |
| 881 | return 0; |
| 882 | } |
| 883 | |
| 884 | #ifdef MS_WINDOWS |
| 885 | if (win32_warn_bytes_api()) { |
| 886 | Py_DECREF(bytes); |
| 887 | return 0; |
| 888 | } |
| 889 | #endif |
| 890 | |
| 891 | length = PyBytes_GET_SIZE(bytes); |
| 892 | #ifdef MS_WINDOWS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 893 | if (length > MAX_PATH-1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 894 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
| 895 | Py_DECREF(bytes); |
| 896 | return 0; |
| 897 | } |
| 898 | #endif |
| 899 | |
| 900 | narrow = PyBytes_AS_STRING(bytes); |
| 901 | if (length != strlen(narrow)) { |
| 902 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded NUL character in %s"); |
| 903 | Py_DECREF(bytes); |
| 904 | return 0; |
| 905 | } |
| 906 | |
| 907 | path->wide = NULL; |
| 908 | path->narrow = narrow; |
| 909 | path->length = length; |
| 910 | path->object = o; |
| 911 | path->fd = -1; |
| 912 | path->cleanup = bytes; |
| 913 | return Py_CLEANUP_SUPPORTED; |
| 914 | } |
| 915 | |
| 916 | static void |
| 917 | argument_unavailable_error(char *function_name, char *argument_name) { |
| 918 | PyErr_Format(PyExc_NotImplementedError, |
| 919 | "%s%s%s unavailable on this platform", |
| 920 | (function_name != NULL) ? function_name : "", |
| 921 | (function_name != NULL) ? ": ": "", |
| 922 | argument_name); |
| 923 | } |
| 924 | |
| 925 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 926 | dir_fd_unavailable(PyObject *o, void *p) |
| 927 | { |
| 928 | int dir_fd; |
| 929 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 930 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 931 | if (dir_fd != DEFAULT_DIR_FD) { |
| 932 | argument_unavailable_error(NULL, "dir_fd"); |
| 933 | return 0; |
| 934 | } |
| 935 | *(int *)p = dir_fd; |
| 936 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | static int |
| 940 | fd_specified(char *function_name, int fd) { |
| 941 | if (fd == -1) |
| 942 | return 0; |
| 943 | |
| 944 | argument_unavailable_error(function_name, "fd"); |
| 945 | return 1; |
| 946 | } |
| 947 | |
| 948 | static int |
| 949 | follow_symlinks_specified(char *function_name, int follow_symlinks) { |
| 950 | if (follow_symlinks) |
| 951 | return 0; |
| 952 | |
| 953 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 954 | return 1; |
| 955 | } |
| 956 | |
| 957 | static int |
| 958 | path_and_dir_fd_invalid(char *function_name, path_t *path, int dir_fd) { |
| 959 | if (!path->narrow && !path->wide && (dir_fd != DEFAULT_DIR_FD)) { |
| 960 | PyErr_Format(PyExc_ValueError, |
| 961 | "%s: can't specify dir_fd without matching path", |
| 962 | function_name); |
| 963 | return 1; |
| 964 | } |
| 965 | return 0; |
| 966 | } |
| 967 | |
| 968 | static int |
| 969 | dir_fd_and_fd_invalid(char *function_name, int dir_fd, int fd) { |
| 970 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 971 | PyErr_Format(PyExc_ValueError, |
| 972 | "%s: can't specify both dir_fd and fd", |
| 973 | function_name); |
| 974 | return 1; |
| 975 | } |
| 976 | return 0; |
| 977 | } |
| 978 | |
| 979 | static int |
| 980 | fd_and_follow_symlinks_invalid(char *function_name, int fd, |
| 981 | int follow_symlinks) { |
| 982 | if ((fd > 0) && (!follow_symlinks)) { |
| 983 | PyErr_Format(PyExc_ValueError, |
| 984 | "%s: cannot use fd and follow_symlinks together", |
| 985 | function_name); |
| 986 | return 1; |
| 987 | } |
| 988 | return 0; |
| 989 | } |
| 990 | |
| 991 | static int |
| 992 | dir_fd_and_follow_symlinks_invalid(char *function_name, int dir_fd, |
| 993 | int follow_symlinks) { |
| 994 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 995 | PyErr_Format(PyExc_ValueError, |
| 996 | "%s: cannot use dir_fd and follow_symlinks together", |
| 997 | function_name); |
| 998 | return 1; |
| 999 | } |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1003 | /* A helper used by a number of POSIX-only functions */ |
| 1004 | #ifndef MS_WINDOWS |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 1005 | static int |
| 1006 | _parse_off_t(PyObject* arg, void* addr) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1007 | { |
| 1008 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 1009 | *((off_t*)addr) = PyLong_AsLong(arg); |
| 1010 | #else |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 1011 | *((off_t*)addr) = PyLong_AsLongLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1012 | #endif |
| 1013 | if (PyErr_Occurred()) |
| 1014 | return 0; |
| 1015 | return 1; |
| 1016 | } |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1017 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1018 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1019 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 1020 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 1021 | * valid and raise an assertion if it isn't. |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1022 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 1023 | * an assertion can be useful, but it does contradict the POSIX standard |
| 1024 | * which for write(2) states: |
| 1025 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 1026 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 1027 | * writing." |
| 1028 | * Furthermore, python allows the user to enter any old integer |
| 1029 | * as a fd and should merely raise a python exception on error. |
| 1030 | * The Microsoft CRT doesn't provide an official way to check for the |
| 1031 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1032 | * 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] | 1033 | * internal structures involved. |
| 1034 | * The structures below must be updated for each version of visual studio |
| 1035 | * according to the file internal.h in the CRT source, until MS comes |
| 1036 | * up with a less hacky way to do this. |
| 1037 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 1038 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 1039 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 1040 | /* The actual size of the structure is determined at runtime. |
| 1041 | * Only the first items must be present. |
| 1042 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1043 | typedef struct { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1044 | intptr_t osfhnd; |
| 1045 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 1046 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1047 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 1048 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1049 | #define IOINFO_L2E 5 |
| 1050 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 1051 | #define IOINFO_ARRAYS 64 |
| 1052 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 1053 | #define FOPEN 0x01 |
| 1054 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 1055 | |
| 1056 | /* This function emulates what the windows CRT does to validate file handles */ |
| 1057 | int |
| 1058 | _PyVerify_fd(int fd) |
| 1059 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1060 | const int i1 = fd >> IOINFO_L2E; |
| 1061 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 1062 | |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 1063 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1064 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1065 | /* Determine the actual size of the ioinfo structure, |
| 1066 | * as used by the CRT loaded in memory |
| 1067 | */ |
| 1068 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 1069 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 1070 | } |
| 1071 | if (sizeof_ioinfo == 0) { |
| 1072 | /* This should not happen... */ |
| 1073 | goto fail; |
| 1074 | } |
| 1075 | |
| 1076 | /* See that it isn't a special CLEAR fileno */ |
| 1077 | if (fd != _NO_CONSOLE_FILENO) { |
| 1078 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 1079 | * we check pointer validity and other info |
| 1080 | */ |
| 1081 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 1082 | /* finally, check that the file is open */ |
| 1083 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 1084 | if (info->osfile & FOPEN) { |
| 1085 | return 1; |
| 1086 | } |
| 1087 | } |
| 1088 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 1089 | fail: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1090 | errno = EBADF; |
| 1091 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 1095 | static int |
| 1096 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 1097 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1098 | if (!_PyVerify_fd(fd1)) |
| 1099 | return 0; |
| 1100 | if (fd2 == _NO_CONSOLE_FILENO) |
| 1101 | return 0; |
| 1102 | if ((unsigned)fd2 < _NHANDLE_) |
| 1103 | return 1; |
| 1104 | else |
| 1105 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1106 | } |
| 1107 | #else |
| 1108 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 1109 | #define _PyVerify_fd_dup2(A, B) (1) |
| 1110 | #endif |
| 1111 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1112 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1113 | /* The following structure was copied from |
| 1114 | http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required |
| 1115 | include doesn't seem to be present in the Windows SDK (at least as included |
| 1116 | with Visual Studio Express). */ |
| 1117 | typedef struct _REPARSE_DATA_BUFFER { |
| 1118 | ULONG ReparseTag; |
| 1119 | USHORT ReparseDataLength; |
| 1120 | USHORT Reserved; |
| 1121 | union { |
| 1122 | struct { |
| 1123 | USHORT SubstituteNameOffset; |
| 1124 | USHORT SubstituteNameLength; |
| 1125 | USHORT PrintNameOffset; |
| 1126 | USHORT PrintNameLength; |
| 1127 | ULONG Flags; |
| 1128 | WCHAR PathBuffer[1]; |
| 1129 | } SymbolicLinkReparseBuffer; |
| 1130 | |
| 1131 | struct { |
| 1132 | USHORT SubstituteNameOffset; |
| 1133 | USHORT SubstituteNameLength; |
| 1134 | USHORT PrintNameOffset; |
| 1135 | USHORT PrintNameLength; |
| 1136 | WCHAR PathBuffer[1]; |
| 1137 | } MountPointReparseBuffer; |
| 1138 | |
| 1139 | struct { |
| 1140 | UCHAR DataBuffer[1]; |
| 1141 | } GenericReparseBuffer; |
| 1142 | }; |
| 1143 | } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 1144 | |
| 1145 | #define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ |
| 1146 | GenericReparseBuffer) |
| 1147 | #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) |
| 1148 | |
| 1149 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1150 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1151 | { |
| 1152 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1153 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 1154 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1155 | |
| 1156 | if (0 == DeviceIoControl( |
| 1157 | reparse_point_handle, |
| 1158 | FSCTL_GET_REPARSE_POINT, |
| 1159 | NULL, 0, /* in buffer */ |
| 1160 | target_buffer, sizeof(target_buffer), |
| 1161 | &n_bytes_returned, |
| 1162 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1163 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1164 | |
| 1165 | if (reparse_tag) |
| 1166 | *reparse_tag = rdb->ReparseTag; |
| 1167 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1168 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1169 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1170 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1171 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1172 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1173 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1174 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1175 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1176 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1177 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1178 | */ |
| 1179 | #include <crt_externs.h> |
| 1180 | static char **environ; |
| 1181 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1182 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1183 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1184 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1185 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1186 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1187 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1188 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1189 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1190 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1191 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1192 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1193 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1194 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1195 | d = PyDict_New(); |
| 1196 | if (d == NULL) |
| 1197 | return NULL; |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1198 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1199 | if (environ == NULL) |
| 1200 | environ = *_NSGetEnviron(); |
| 1201 | #endif |
| 1202 | #ifdef MS_WINDOWS |
| 1203 | /* _wenviron must be initialized in this way if the program is started |
| 1204 | through main() instead of wmain(). */ |
| 1205 | _wgetenv(L""); |
| 1206 | if (_wenviron == NULL) |
| 1207 | return d; |
| 1208 | /* This part ignores errors */ |
| 1209 | for (e = _wenviron; *e != NULL; e++) { |
| 1210 | PyObject *k; |
| 1211 | PyObject *v; |
| 1212 | wchar_t *p = wcschr(*e, L'='); |
| 1213 | if (p == NULL) |
| 1214 | continue; |
| 1215 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1216 | if (k == NULL) { |
| 1217 | PyErr_Clear(); |
| 1218 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1219 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1220 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1221 | if (v == NULL) { |
| 1222 | PyErr_Clear(); |
| 1223 | Py_DECREF(k); |
| 1224 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1225 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1226 | if (PyDict_GetItem(d, k) == NULL) { |
| 1227 | if (PyDict_SetItem(d, k, v) != 0) |
| 1228 | PyErr_Clear(); |
| 1229 | } |
| 1230 | Py_DECREF(k); |
| 1231 | Py_DECREF(v); |
| 1232 | } |
| 1233 | #else |
| 1234 | if (environ == NULL) |
| 1235 | return d; |
| 1236 | /* This part ignores errors */ |
| 1237 | for (e = environ; *e != NULL; e++) { |
| 1238 | PyObject *k; |
| 1239 | PyObject *v; |
| 1240 | char *p = strchr(*e, '='); |
| 1241 | if (p == NULL) |
| 1242 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1243 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1244 | if (k == NULL) { |
| 1245 | PyErr_Clear(); |
| 1246 | continue; |
| 1247 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1248 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1249 | if (v == NULL) { |
| 1250 | PyErr_Clear(); |
| 1251 | Py_DECREF(k); |
| 1252 | continue; |
| 1253 | } |
| 1254 | if (PyDict_GetItem(d, k) == NULL) { |
| 1255 | if (PyDict_SetItem(d, k, v) != 0) |
| 1256 | PyErr_Clear(); |
| 1257 | } |
| 1258 | Py_DECREF(k); |
| 1259 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1260 | } |
| 1261 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1262 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1265 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1266 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1267 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1268 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1269 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1270 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1271 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1272 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1273 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1274 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1275 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1276 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1277 | /* XXX We should pass the function name along in the future. |
| 1278 | (winreg.c also wants to pass the function name.) |
| 1279 | This would however require an additional param to the |
| 1280 | Windows error object, which is non-trivial. |
| 1281 | */ |
| 1282 | errno = GetLastError(); |
| 1283 | if (filename) |
| 1284 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1285 | else |
| 1286 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1287 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1288 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1289 | static PyObject * |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1290 | win32_error_object(char* function, PyObject* filename) |
| 1291 | { |
| 1292 | /* XXX - see win32_error for comments on 'function' */ |
| 1293 | errno = GetLastError(); |
| 1294 | if (filename) |
| 1295 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1296 | PyExc_OSError, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1297 | errno, |
| 1298 | filename); |
| 1299 | else |
| 1300 | return PyErr_SetFromWindowsErr(errno); |
| 1301 | } |
| 1302 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1303 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1304 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1305 | static PyObject * |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1306 | path_error(path_t *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1307 | { |
| 1308 | #ifdef MS_WINDOWS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1309 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 1310 | 0, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1311 | #else |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1312 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1313 | #endif |
| 1314 | } |
| 1315 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1316 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1317 | /* POSIX generic methods */ |
| 1318 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1319 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1320 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 1321 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1322 | int fd; |
| 1323 | int res; |
| 1324 | fd = PyObject_AsFileDescriptor(fdobj); |
| 1325 | if (fd < 0) |
| 1326 | return NULL; |
| 1327 | if (!_PyVerify_fd(fd)) |
| 1328 | return posix_error(); |
| 1329 | Py_BEGIN_ALLOW_THREADS |
| 1330 | res = (*func)(fd); |
| 1331 | Py_END_ALLOW_THREADS |
| 1332 | if (res < 0) |
| 1333 | return posix_error(); |
| 1334 | Py_INCREF(Py_None); |
| 1335 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1336 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1337 | |
| 1338 | static PyObject * |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1339 | posix_1str(const char *func_name, PyObject *args, char *format, |
| 1340 | int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1341 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1342 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1343 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1344 | memset(&path, 0, sizeof(path)); |
| 1345 | path.function_name = func_name; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1346 | if (!PyArg_ParseTuple(args, format, |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1347 | path_converter, &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1348 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1349 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1350 | res = (*func)(path.narrow); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1351 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1352 | if (res < 0) { |
| 1353 | path_error(&path); |
| 1354 | path_cleanup(&path); |
| 1355 | return NULL; |
| 1356 | } |
| 1357 | path_cleanup(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1358 | Py_INCREF(Py_None); |
| 1359 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1362 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1363 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1364 | /* This is a reimplementation of the C library's chdir function, |
| 1365 | but one that produces Win32 errors instead of DOS error codes. |
| 1366 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1367 | it also needs to set "magic" environment variables indicating |
| 1368 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1369 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1370 | win32_chdir(LPCSTR path) |
| 1371 | { |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1372 | char new_path[MAX_PATH]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1373 | int result; |
| 1374 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1375 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1376 | if(!SetCurrentDirectoryA(path)) |
| 1377 | return FALSE; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1378 | result = GetCurrentDirectoryA(Py_ARRAY_LENGTH(new_path), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1379 | if (!result) |
| 1380 | return FALSE; |
| 1381 | /* In the ANSI API, there should not be any paths longer |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1382 | than MAX_PATH-1 (not including the final null character). */ |
| 1383 | assert(result < Py_ARRAY_LENGTH(new_path)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1384 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 1385 | strncmp(new_path, "//", 2) == 0) |
| 1386 | /* UNC path, nothing to do. */ |
| 1387 | return TRUE; |
| 1388 | env[1] = new_path[0]; |
| 1389 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | /* The Unicode version differs from the ANSI version |
| 1393 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1394 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1395 | win32_wchdir(LPCWSTR path) |
| 1396 | { |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1397 | wchar_t _new_path[MAX_PATH], *new_path = _new_path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1398 | int result; |
| 1399 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1400 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1401 | if(!SetCurrentDirectoryW(path)) |
| 1402 | return FALSE; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1403 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(new_path), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1404 | if (!result) |
| 1405 | return FALSE; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1406 | if (result > Py_ARRAY_LENGTH(new_path)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1407 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1408 | if (!new_path) { |
| 1409 | SetLastError(ERROR_OUTOFMEMORY); |
| 1410 | return FALSE; |
| 1411 | } |
| 1412 | result = GetCurrentDirectoryW(result, new_path); |
| 1413 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1414 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1415 | return FALSE; |
| 1416 | } |
| 1417 | } |
| 1418 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1419 | wcsncmp(new_path, L"//", 2) == 0) |
| 1420 | /* UNC path, nothing to do. */ |
| 1421 | return TRUE; |
| 1422 | env[1] = new_path[0]; |
| 1423 | result = SetEnvironmentVariableW(env, new_path); |
| 1424 | if (new_path != _new_path) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1425 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1426 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1427 | } |
| 1428 | #endif |
| 1429 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1430 | #ifdef MS_WINDOWS |
| 1431 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1432 | - time stamps are restricted to second resolution |
| 1433 | - file modification times suffer from forth-and-back conversions between |
| 1434 | UTC and local time |
| 1435 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1436 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1437 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1438 | |
| 1439 | struct win32_stat{ |
Brian Curtin | 87e63a2 | 2012-12-31 11:59:48 -0600 | [diff] [blame] | 1440 | unsigned long st_dev; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1441 | __int64 st_ino; |
| 1442 | unsigned short st_mode; |
| 1443 | int st_nlink; |
| 1444 | int st_uid; |
| 1445 | int st_gid; |
Brian Curtin | 87e63a2 | 2012-12-31 11:59:48 -0600 | [diff] [blame] | 1446 | unsigned long st_rdev; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1447 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1448 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1449 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1450 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1451 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1452 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1453 | int st_ctime_nsec; |
| 1454 | }; |
| 1455 | |
| 1456 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 1457 | |
| 1458 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1459 | 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] | 1460 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1461 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 1462 | /* Cannot simply cast and dereference in_ptr, |
| 1463 | since it might not be aligned properly */ |
| 1464 | __int64 in; |
| 1465 | memcpy(&in, in_ptr, sizeof(in)); |
| 1466 | *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] | 1467 | *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] | 1468 | } |
| 1469 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1470 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1471 | 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] | 1472 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1473 | /* XXX endianness */ |
| 1474 | __int64 out; |
| 1475 | out = time_in + secs_between_epochs; |
| 1476 | out = out * 10000000 + nsec_in / 100; |
| 1477 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1480 | /* Below, we *know* that ugo+r is 0444 */ |
| 1481 | #if _S_IREAD != 0400 |
| 1482 | #error Unsupported C library |
| 1483 | #endif |
| 1484 | static int |
| 1485 | attributes_to_mode(DWORD attr) |
| 1486 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1487 | int m = 0; |
| 1488 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1489 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1490 | else |
| 1491 | m |= _S_IFREG; |
| 1492 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1493 | m |= 0444; |
| 1494 | else |
| 1495 | m |= 0666; |
| 1496 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1500 | 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] | 1501 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1502 | memset(result, 0, sizeof(*result)); |
| 1503 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1504 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
Brian Curtin | 490b32a | 2012-12-26 07:03:03 -0600 | [diff] [blame] | 1505 | result->st_dev = info->dwVolumeSerialNumber; |
| 1506 | result->st_rdev = result->st_dev; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1507 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1508 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1509 | 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] | 1510 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1511 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1512 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1513 | /* first clear the S_IFMT bits */ |
Christian Heimes | 99d6135 | 2013-06-23 23:56:05 +0200 | [diff] [blame] | 1514 | result->st_mode ^= (result->st_mode & S_IFMT); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1515 | /* now set the bits that make this a symlink */ |
Christian Heimes | 99d6135 | 2013-06-23 23:56:05 +0200 | [diff] [blame] | 1516 | result->st_mode |= S_IFLNK; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1517 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1518 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1519 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1522 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1523 | 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] | 1524 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1525 | HANDLE hFindFile; |
| 1526 | WIN32_FIND_DATAA FileData; |
| 1527 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1528 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1529 | return FALSE; |
| 1530 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1531 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1532 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1533 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1534 | info->ftCreationTime = FileData.ftCreationTime; |
| 1535 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1536 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1537 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1538 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1539 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1540 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1541 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1542 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1546 | 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] | 1547 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1548 | HANDLE hFindFile; |
| 1549 | WIN32_FIND_DATAW FileData; |
| 1550 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1551 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1552 | return FALSE; |
| 1553 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1554 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1555 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1556 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1557 | info->ftCreationTime = FileData.ftCreationTime; |
| 1558 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1559 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1560 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1561 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1562 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1563 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1564 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1565 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1568 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
Antoine Pitrou | 06eecea | 2012-10-21 16:33:33 +0200 | [diff] [blame] | 1569 | static int has_GetFinalPathNameByHandle = -1; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1570 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 1571 | DWORD); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1572 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1573 | check_GetFinalPathNameByHandle() |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1574 | { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1575 | HINSTANCE hKernel32; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1576 | DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 1577 | DWORD); |
| 1578 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1579 | /* only recheck */ |
Antoine Pitrou | 06eecea | 2012-10-21 16:33:33 +0200 | [diff] [blame] | 1580 | if (-1 == has_GetFinalPathNameByHandle) |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1581 | { |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1582 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1583 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 1584 | "GetFinalPathNameByHandleA"); |
| 1585 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 1586 | "GetFinalPathNameByHandleW"); |
| 1587 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 1588 | Py_GetFinalPathNameByHandleW; |
| 1589 | } |
| 1590 | return has_GetFinalPathNameByHandle; |
| 1591 | } |
| 1592 | |
| 1593 | static BOOL |
| 1594 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1595 | { |
| 1596 | int buf_size, result_length; |
| 1597 | wchar_t *buf; |
| 1598 | |
| 1599 | /* We have a good handle to the target, use it to determine |
| 1600 | the target path name (then we'll call lstat on it). */ |
| 1601 | buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1602 | VOLUME_NAME_DOS); |
| 1603 | if(!buf_size) |
| 1604 | return FALSE; |
| 1605 | |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1606 | buf = (wchar_t *)PyMem_Malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1607 | if (!buf) { |
| 1608 | SetLastError(ERROR_OUTOFMEMORY); |
| 1609 | return FALSE; |
| 1610 | } |
| 1611 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1612 | result_length = Py_GetFinalPathNameByHandleW(hdl, |
| 1613 | buf, buf_size, VOLUME_NAME_DOS); |
| 1614 | |
| 1615 | if(!result_length) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1616 | PyMem_Free(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1617 | return FALSE; |
| 1618 | } |
| 1619 | |
| 1620 | if(!CloseHandle(hdl)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1621 | PyMem_Free(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1622 | return FALSE; |
| 1623 | } |
| 1624 | |
| 1625 | buf[result_length] = 0; |
| 1626 | |
| 1627 | *target_path = buf; |
| 1628 | return TRUE; |
| 1629 | } |
| 1630 | |
| 1631 | static int |
| 1632 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1633 | BOOL traverse); |
| 1634 | static int |
| 1635 | win32_xstat_impl(const char *path, struct win32_stat *result, |
| 1636 | BOOL traverse) |
| 1637 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1638 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1639 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1640 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1641 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1642 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1643 | const char *dot; |
| 1644 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1645 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1646 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1647 | traverse reparse point. */ |
| 1648 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1651 | hFile = CreateFileA( |
| 1652 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1653 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1654 | 0, /* share mode */ |
| 1655 | NULL, /* security attributes */ |
| 1656 | OPEN_EXISTING, |
| 1657 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1658 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1659 | Because of this, calls like GetFinalPathNameByHandle will return |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 1660 | the symlink path again and not the actual final path. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1661 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1662 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1663 | NULL); |
| 1664 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1665 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1666 | /* Either the target doesn't exist, or we don't have access to |
| 1667 | get a handle to it. If the former, we need to return an error. |
| 1668 | If the latter, we can use attributes_from_dir. */ |
| 1669 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1670 | return -1; |
| 1671 | /* Could not get attributes on open file. Fall back to |
| 1672 | reading the directory. */ |
| 1673 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1674 | /* Very strange. This should not fail now */ |
| 1675 | return -1; |
| 1676 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1677 | if (traverse) { |
| 1678 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1679 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1680 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1681 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1682 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1683 | } else { |
| 1684 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1685 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1686 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1687 | } |
| 1688 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1689 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1690 | return -1; |
| 1691 | |
| 1692 | /* Close the outer open file handle now that we're about to |
| 1693 | reopen it with different flags. */ |
| 1694 | if (!CloseHandle(hFile)) |
| 1695 | return -1; |
| 1696 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1697 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1698 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1699 | the file without the reparse handling flag set. */ |
| 1700 | hFile2 = CreateFileA( |
| 1701 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1702 | NULL, OPEN_EXISTING, |
| 1703 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1704 | NULL); |
| 1705 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1706 | return -1; |
| 1707 | |
| 1708 | if (!get_target_path(hFile2, &target_path)) |
| 1709 | return -1; |
| 1710 | |
| 1711 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1712 | PyMem_Free(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1713 | return code; |
| 1714 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1715 | } else |
| 1716 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1717 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1718 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1719 | |
| 1720 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1721 | dot = strrchr(path, '.'); |
| 1722 | if (dot) { |
| 1723 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1724 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1725 | result->st_mode |= 0111; |
| 1726 | } |
| 1727 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1731 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1732 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1733 | { |
| 1734 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1735 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1736 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1737 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1738 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1739 | const wchar_t *dot; |
| 1740 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1741 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1742 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1743 | traverse reparse point. */ |
| 1744 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1747 | hFile = CreateFileW( |
| 1748 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1749 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1750 | 0, /* share mode */ |
| 1751 | NULL, /* security attributes */ |
| 1752 | OPEN_EXISTING, |
| 1753 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1754 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1755 | Because of this, calls like GetFinalPathNameByHandle will return |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 1756 | the symlink path again and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1757 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1758 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1759 | NULL); |
| 1760 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1761 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1762 | /* Either the target doesn't exist, or we don't have access to |
| 1763 | get a handle to it. If the former, we need to return an error. |
| 1764 | If the latter, we can use attributes_from_dir. */ |
| 1765 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1766 | return -1; |
| 1767 | /* Could not get attributes on open file. Fall back to |
| 1768 | reading the directory. */ |
| 1769 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1770 | /* Very strange. This should not fail now */ |
| 1771 | return -1; |
| 1772 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1773 | if (traverse) { |
| 1774 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1775 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1776 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1777 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1778 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1779 | } else { |
| 1780 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1781 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1782 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1783 | } |
| 1784 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1785 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1786 | return -1; |
| 1787 | |
| 1788 | /* Close the outer open file handle now that we're about to |
| 1789 | reopen it with different flags. */ |
| 1790 | if (!CloseHandle(hFile)) |
| 1791 | return -1; |
| 1792 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1793 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1794 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1795 | the file without the reparse handling flag set. */ |
| 1796 | hFile2 = CreateFileW( |
| 1797 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1798 | NULL, OPEN_EXISTING, |
| 1799 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1800 | NULL); |
| 1801 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1802 | return -1; |
| 1803 | |
| 1804 | if (!get_target_path(hFile2, &target_path)) |
| 1805 | return -1; |
| 1806 | |
| 1807 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1808 | PyMem_Free(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1809 | return code; |
| 1810 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1811 | } else |
| 1812 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1813 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1814 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1815 | |
| 1816 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1817 | dot = wcsrchr(path, '.'); |
| 1818 | if (dot) { |
| 1819 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1820 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1821 | result->st_mode |= 0111; |
| 1822 | } |
| 1823 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
| 1826 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1827 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1828 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1829 | /* Protocol violation: we explicitly clear errno, instead of |
| 1830 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1831 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1832 | errno = 0; |
| 1833 | return code; |
| 1834 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1835 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1836 | static int |
| 1837 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1838 | { |
| 1839 | /* Protocol violation: we explicitly clear errno, instead of |
| 1840 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1841 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1842 | errno = 0; |
| 1843 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1844 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1845 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1846 | |
| 1847 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1848 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1849 | default does not traverse symlinks and instead returns attributes for |
| 1850 | the symlink. |
| 1851 | |
| 1852 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1853 | win32_stat will first explicitly resolve the symlink target and then will |
| 1854 | call win32_lstat on that result. |
| 1855 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1856 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1857 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1858 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1859 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1860 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1861 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1864 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1865 | 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] | 1866 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1867 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | static int |
| 1871 | win32_stat(const char* path, struct win32_stat *result) |
| 1872 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1873 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1876 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1877 | win32_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1878 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1879 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | static int |
| 1883 | win32_fstat(int file_number, struct win32_stat *result) |
| 1884 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1885 | BY_HANDLE_FILE_INFORMATION info; |
| 1886 | HANDLE h; |
| 1887 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1888 | |
Richard Oudkerk | 2240ac1 | 2012-07-06 12:05:32 +0100 | [diff] [blame] | 1889 | if (!_PyVerify_fd(file_number)) |
| 1890 | h = INVALID_HANDLE_VALUE; |
| 1891 | else |
| 1892 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1893 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1894 | /* Protocol violation: we explicitly clear errno, instead of |
| 1895 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1896 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1897 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1898 | if (h == INVALID_HANDLE_VALUE) { |
| 1899 | /* This is really a C library error (invalid file handle). |
| 1900 | We set the Win32 error to the closes one matching. */ |
| 1901 | SetLastError(ERROR_INVALID_HANDLE); |
| 1902 | return -1; |
| 1903 | } |
| 1904 | memset(result, 0, sizeof(*result)); |
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 | type = GetFileType(h); |
| 1907 | if (type == FILE_TYPE_UNKNOWN) { |
| 1908 | DWORD error = GetLastError(); |
| 1909 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1910 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1911 | } |
| 1912 | /* else: valid but unknown file */ |
| 1913 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1914 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1915 | if (type != FILE_TYPE_DISK) { |
| 1916 | if (type == FILE_TYPE_CHAR) |
| 1917 | result->st_mode = _S_IFCHR; |
| 1918 | else if (type == FILE_TYPE_PIPE) |
| 1919 | result->st_mode = _S_IFIFO; |
| 1920 | return 0; |
| 1921 | } |
| 1922 | |
| 1923 | if (!GetFileInformationByHandle(h, &info)) { |
| 1924 | return -1; |
| 1925 | } |
| 1926 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1927 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1928 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1929 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1930 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | #endif /* MS_WINDOWS */ |
| 1934 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1935 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1936 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1937 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1938 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1939 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1940 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1941 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1942 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1943 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1944 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1945 | |
| 1946 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1947 | {"st_mode", "protection bits"}, |
| 1948 | {"st_ino", "inode"}, |
| 1949 | {"st_dev", "device"}, |
| 1950 | {"st_nlink", "number of hard links"}, |
| 1951 | {"st_uid", "user ID of owner"}, |
| 1952 | {"st_gid", "group ID of owner"}, |
| 1953 | {"st_size", "total size, in bytes"}, |
| 1954 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1955 | {NULL, "integer time of last access"}, |
| 1956 | {NULL, "integer time of last modification"}, |
| 1957 | {NULL, "integer time of last change"}, |
| 1958 | {"st_atime", "time of last access"}, |
| 1959 | {"st_mtime", "time of last modification"}, |
| 1960 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1961 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1962 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1963 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1964 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1965 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1966 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1967 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1968 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1969 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1970 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1971 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1972 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1973 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1974 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1975 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1976 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1977 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1978 | #endif |
| 1979 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1980 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1981 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1982 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1983 | }; |
| 1984 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1985 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1986 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1987 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1988 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1989 | #endif |
| 1990 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1991 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1992 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1993 | #else |
| 1994 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1995 | #endif |
| 1996 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1997 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1998 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1999 | #else |
| 2000 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 2001 | #endif |
| 2002 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2003 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 2004 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 2005 | #else |
| 2006 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 2007 | #endif |
| 2008 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2009 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 2010 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2011 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 2012 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2013 | #endif |
| 2014 | |
| 2015 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 2016 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 2017 | #else |
| 2018 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 2019 | #endif |
| 2020 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2021 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2022 | "stat_result", /* name */ |
| 2023 | stat_result__doc__, /* doc */ |
| 2024 | stat_result_fields, |
| 2025 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2026 | }; |
| 2027 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2028 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2029 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 2030 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2031 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 2032 | 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] | 2033 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2034 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2035 | |
| 2036 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2037 | {"f_bsize", }, |
| 2038 | {"f_frsize", }, |
| 2039 | {"f_blocks", }, |
| 2040 | {"f_bfree", }, |
| 2041 | {"f_bavail", }, |
| 2042 | {"f_files", }, |
| 2043 | {"f_ffree", }, |
| 2044 | {"f_favail", }, |
| 2045 | {"f_flag", }, |
| 2046 | {"f_namemax",}, |
| 2047 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2048 | }; |
| 2049 | |
| 2050 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2051 | "statvfs_result", /* name */ |
| 2052 | statvfs_result__doc__, /* doc */ |
| 2053 | statvfs_result_fields, |
| 2054 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2055 | }; |
| 2056 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2057 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 2058 | PyDoc_STRVAR(waitid_result__doc__, |
| 2059 | "waitid_result: Result from waitid.\n\n\ |
| 2060 | This object may be accessed either as a tuple of\n\ |
| 2061 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 2062 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 2063 | \n\ |
| 2064 | See os.waitid for more information."); |
| 2065 | |
| 2066 | static PyStructSequence_Field waitid_result_fields[] = { |
| 2067 | {"si_pid", }, |
| 2068 | {"si_uid", }, |
| 2069 | {"si_signo", }, |
| 2070 | {"si_status", }, |
| 2071 | {"si_code", }, |
| 2072 | {0} |
| 2073 | }; |
| 2074 | |
| 2075 | static PyStructSequence_Desc waitid_result_desc = { |
| 2076 | "waitid_result", /* name */ |
| 2077 | waitid_result__doc__, /* doc */ |
| 2078 | waitid_result_fields, |
| 2079 | 5 |
| 2080 | }; |
| 2081 | static PyTypeObject WaitidResultType; |
| 2082 | #endif |
| 2083 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2084 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2085 | static PyTypeObject StatResultType; |
| 2086 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2087 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 2088 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2089 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2090 | static newfunc structseq_new; |
| 2091 | |
| 2092 | static PyObject * |
| 2093 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2094 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2095 | PyStructSequence *result; |
| 2096 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2097 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2098 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 2099 | if (!result) |
| 2100 | return NULL; |
| 2101 | /* If we have been initialized from a tuple, |
| 2102 | st_?time might be set to None. Initialize it |
| 2103 | from the int slots. */ |
| 2104 | for (i = 7; i <= 9; i++) { |
| 2105 | if (result->ob_item[i+3] == Py_None) { |
| 2106 | Py_DECREF(Py_None); |
| 2107 | Py_INCREF(result->ob_item[i]); |
| 2108 | result->ob_item[i+3] = result->ob_item[i]; |
| 2109 | } |
| 2110 | } |
| 2111 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | |
| 2115 | |
| 2116 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 2117 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2118 | |
| 2119 | PyDoc_STRVAR(stat_float_times__doc__, |
| 2120 | "stat_float_times([newval]) -> oldval\n\n\ |
| 2121 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 2122 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 2123 | future calls return ints. \n\ |
| 2124 | If newval is omitted, return the current setting.\n"); |
| 2125 | |
| 2126 | static PyObject* |
| 2127 | stat_float_times(PyObject* self, PyObject *args) |
| 2128 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2129 | int newval = -1; |
| 2130 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 2131 | return NULL; |
Victor Stinner | 034d0aa | 2012-06-05 01:22:15 +0200 | [diff] [blame] | 2132 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2133 | "stat_float_times() is deprecated", |
| 2134 | 1)) |
| 2135 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2136 | if (newval == -1) |
| 2137 | /* Return old value */ |
| 2138 | return PyBool_FromLong(_stat_float_times); |
| 2139 | _stat_float_times = newval; |
| 2140 | Py_INCREF(Py_None); |
| 2141 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2142 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2143 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2144 | static PyObject *billion = NULL; |
| 2145 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2146 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2147 | 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] | 2148 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2149 | PyObject *s = _PyLong_FromTime_t(sec); |
| 2150 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 2151 | PyObject *s_in_ns = NULL; |
| 2152 | PyObject *ns_total = NULL; |
| 2153 | PyObject *float_s = NULL; |
| 2154 | |
| 2155 | if (!(s && ns_fractional)) |
| 2156 | goto exit; |
| 2157 | |
| 2158 | s_in_ns = PyNumber_Multiply(s, billion); |
| 2159 | if (!s_in_ns) |
| 2160 | goto exit; |
| 2161 | |
| 2162 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 2163 | if (!ns_total) |
| 2164 | goto exit; |
| 2165 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2166 | if (_stat_float_times) { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2167 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2168 | if (!float_s) |
| 2169 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2170 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2171 | else { |
| 2172 | float_s = s; |
| 2173 | Py_INCREF(float_s); |
| 2174 | } |
| 2175 | |
| 2176 | PyStructSequence_SET_ITEM(v, index, s); |
| 2177 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2178 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2179 | s = NULL; |
| 2180 | float_s = NULL; |
| 2181 | ns_total = NULL; |
| 2182 | exit: |
| 2183 | Py_XDECREF(s); |
| 2184 | Py_XDECREF(ns_fractional); |
| 2185 | Py_XDECREF(s_in_ns); |
| 2186 | Py_XDECREF(ns_total); |
| 2187 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2190 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2191 | (used by posix_stat() and posix_fstat()) */ |
| 2192 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2193 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2194 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2195 | unsigned long ansec, mnsec, cnsec; |
| 2196 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 2197 | if (v == NULL) |
| 2198 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2199 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2200 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2201 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2202 | PyStructSequence_SET_ITEM(v, 1, |
| 2203 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2204 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2205 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2206 | #endif |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2207 | #ifdef MS_WINDOWS |
| 2208 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
| 2209 | #elif defined(HAVE_LONG_LONG) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2210 | PyStructSequence_SET_ITEM(v, 2, |
| 2211 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2212 | #else |
Brian Curtin | 9cc4321 | 2013-01-01 12:31:06 -0600 | [diff] [blame] | 2213 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2214 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2215 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2216 | #if defined(MS_WINDOWS) |
| 2217 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2218 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2219 | #else |
| 2220 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2221 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2222 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2223 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2224 | PyStructSequence_SET_ITEM(v, 6, |
| 2225 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2226 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2227 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2228 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2229 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2230 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2231 | ansec = st->st_atim.tv_nsec; |
| 2232 | mnsec = st->st_mtim.tv_nsec; |
| 2233 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2234 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2235 | ansec = st->st_atimespec.tv_nsec; |
| 2236 | mnsec = st->st_mtimespec.tv_nsec; |
| 2237 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2238 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2239 | ansec = st->st_atime_nsec; |
| 2240 | mnsec = st->st_mtime_nsec; |
| 2241 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2242 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2243 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2244 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2245 | fill_time(v, 7, st->st_atime, ansec); |
| 2246 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2247 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2248 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2249 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2250 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2251 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2252 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2253 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2254 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2255 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2256 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2257 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2258 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2259 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2260 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2261 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2262 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2263 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2264 | #endif |
| 2265 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2266 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2267 | PyObject *val; |
| 2268 | unsigned long bsec,bnsec; |
| 2269 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2270 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2271 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2272 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2273 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2274 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2275 | if (_stat_float_times) { |
| 2276 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 2277 | } else { |
| 2278 | val = PyLong_FromLong((long)bsec); |
| 2279 | } |
| 2280 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2281 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2282 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2283 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2284 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2285 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2286 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2287 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2288 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2289 | if (PyErr_Occurred()) { |
| 2290 | Py_DECREF(v); |
| 2291 | return NULL; |
| 2292 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2293 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2294 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2297 | /* POSIX methods */ |
| 2298 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2299 | |
| 2300 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2301 | posix_do_stat(char *function_name, path_t *path, |
| 2302 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2303 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2304 | STRUCT_STAT st; |
| 2305 | int result; |
| 2306 | |
| 2307 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2308 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2309 | return NULL; |
| 2310 | #endif |
| 2311 | |
| 2312 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2313 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2314 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2315 | return NULL; |
| 2316 | |
| 2317 | Py_BEGIN_ALLOW_THREADS |
| 2318 | if (path->fd != -1) |
| 2319 | result = FSTAT(path->fd, &st); |
| 2320 | else |
| 2321 | #ifdef MS_WINDOWS |
| 2322 | if (path->wide) { |
| 2323 | if (follow_symlinks) |
| 2324 | result = win32_stat_w(path->wide, &st); |
| 2325 | else |
| 2326 | result = win32_lstat_w(path->wide, &st); |
| 2327 | } |
| 2328 | else |
| 2329 | #endif |
| 2330 | #if defined(HAVE_LSTAT) || defined(MS_WINDOWS) |
| 2331 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2332 | result = LSTAT(path->narrow, &st); |
| 2333 | else |
| 2334 | #endif |
| 2335 | #ifdef HAVE_FSTATAT |
| 2336 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2337 | result = fstatat(dir_fd, path->narrow, &st, |
| 2338 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2339 | else |
| 2340 | #endif |
| 2341 | result = STAT(path->narrow, &st); |
| 2342 | Py_END_ALLOW_THREADS |
| 2343 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2344 | if (result != 0) { |
| 2345 | return path_error(path); |
| 2346 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2347 | |
| 2348 | return _pystat_fromstructstat(&st); |
| 2349 | } |
| 2350 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2351 | #ifdef HAVE_FSTATAT |
| 2352 | #define OS_STAT_DIR_FD_CONVERTER dir_fd_converter |
| 2353 | #else |
| 2354 | #define OS_STAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2355 | #endif |
| 2356 | |
| 2357 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2358 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2359 | |
| 2360 | class path_t_converter(CConverter): |
| 2361 | |
| 2362 | type = "path_t" |
| 2363 | impl_by_reference = True |
| 2364 | parse_by_reference = True |
| 2365 | |
| 2366 | converter = 'path_converter' |
| 2367 | |
| 2368 | def converter_init(self, *, allow_fd=False, nullable=False): |
| 2369 | def strify(value): |
| 2370 | return str(int(bool(value))) |
| 2371 | |
| 2372 | # right now path_t doesn't support default values. |
| 2373 | # to support a default value, you'll need to override initialize(). |
| 2374 | |
| 2375 | assert self.default is unspecified |
| 2376 | |
| 2377 | self.nullable = nullable |
| 2378 | self.allow_fd = allow_fd |
| 2379 | |
| 2380 | self.c_default = 'PATH_T_INITIALIZE("{}", {}, {})'.format( |
| 2381 | self.function.name, |
| 2382 | strify(nullable), |
| 2383 | strify(allow_fd), |
| 2384 | ) |
| 2385 | |
| 2386 | def cleanup(self): |
| 2387 | return "path_cleanup(&" + self.name + ");\n" |
| 2388 | |
| 2389 | |
| 2390 | class dir_fd_converter(CConverter): |
| 2391 | type = 'int' |
| 2392 | converter = 'OS_STAT_DIR_FD_CONVERTER' |
| 2393 | |
| 2394 | def converter_init(self): |
| 2395 | if self.default in (unspecified, None): |
| 2396 | self.c_default = 'DEFAULT_DIR_FD' |
| 2397 | |
| 2398 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2399 | [python start generated code]*/ |
| 2400 | /*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2401 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2402 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2403 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2404 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2405 | |
| 2406 | path : path_t(allow_fd=True) |
| 2407 | Path to be examined; can be string, bytes, or open-file-descriptor int. |
| 2408 | |
| 2409 | * |
| 2410 | |
| 2411 | dir_fd : dir_fd = None |
| 2412 | If not None, it should be a file descriptor open to a directory, |
| 2413 | and path should be a relative string; path will then be relative to |
| 2414 | that directory. |
| 2415 | |
| 2416 | follow_symlinks: bool = True |
| 2417 | If False, and the last element of the path is a symbolic link, |
| 2418 | stat will examine the symbolic link itself instead of the file |
| 2419 | the link points to. |
| 2420 | |
| 2421 | Perform a stat system call on the given path. |
| 2422 | |
| 2423 | dir_fd and follow_symlinks may not be implemented |
| 2424 | on your platform. If they are unavailable, using them will raise a |
| 2425 | NotImplementedError. |
| 2426 | |
| 2427 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2428 | an open file descriptor. |
| 2429 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2430 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2431 | |
| 2432 | PyDoc_STRVAR(os_stat__doc__, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 2433 | "stat(path, *, dir_fd=None, follow_symlinks=True)\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2434 | "Perform a stat system call on the given path.\n" |
| 2435 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2436 | " path\n" |
| 2437 | " Path to be examined; can be string, bytes, or open-file-descriptor int.\n" |
| 2438 | " dir_fd\n" |
| 2439 | " If not None, it should be a file descriptor open to a directory,\n" |
| 2440 | " and path should be a relative string; path will then be relative to\n" |
| 2441 | " that directory.\n" |
| 2442 | " follow_symlinks\n" |
| 2443 | " If False, and the last element of the path is a symbolic link,\n" |
| 2444 | " stat will examine the symbolic link itself instead of the file\n" |
| 2445 | " the link points to.\n" |
| 2446 | "\n" |
| 2447 | "dir_fd and follow_symlinks may not be implemented\n" |
| 2448 | " on your platform. If they are unavailable, using them will raise a\n" |
| 2449 | " NotImplementedError.\n" |
| 2450 | "\n" |
| 2451 | "It\'s an error to use dir_fd or follow_symlinks when specifying path as\n" |
| 2452 | " an open file descriptor."); |
| 2453 | |
| 2454 | #define OS_STAT_METHODDEF \ |
| 2455 | {"stat", (PyCFunction)os_stat, METH_VARARGS|METH_KEYWORDS, os_stat__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2456 | |
| 2457 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2458 | 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] | 2459 | |
| 2460 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2461 | os_stat(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2462 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2463 | PyObject *return_value = NULL; |
| 2464 | static char *_keywords[] = {"path", "dir_fd", "follow_symlinks", NULL}; |
| 2465 | path_t path = PATH_T_INITIALIZE("stat", 0, 1); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2466 | int dir_fd = DEFAULT_DIR_FD; |
| 2467 | int follow_symlinks = 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2468 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2469 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2470 | "O&|$O&p:stat", _keywords, |
| 2471 | path_converter, &path, OS_STAT_DIR_FD_CONVERTER, &dir_fd, &follow_symlinks)) |
| 2472 | goto exit; |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 2473 | return_value = os_stat_impl(module, &path, dir_fd, follow_symlinks); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2474 | |
| 2475 | exit: |
| 2476 | /* Cleanup for path */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2477 | path_cleanup(&path); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2478 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2479 | return return_value; |
| 2480 | } |
| 2481 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2482 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2483 | os_stat_impl(PyModuleDef *module, path_t *path, int dir_fd, int follow_symlinks) |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2484 | /*[clinic end generated code: checksum=85a71ad602e89f8e280118da976f70cd2f9abdf1]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2485 | { |
| 2486 | return posix_do_stat("stat", path, dir_fd, follow_symlinks); |
| 2487 | } |
| 2488 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2489 | PyDoc_STRVAR(posix_lstat__doc__, |
| 2490 | "lstat(path, *, dir_fd=None) -> stat result\n\n\ |
| 2491 | Like stat(), but do not follow symbolic links.\n\ |
| 2492 | Equivalent to stat(path, follow_symlinks=False)."); |
| 2493 | |
| 2494 | static PyObject * |
| 2495 | posix_lstat(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2496 | { |
| 2497 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 2498 | path_t path; |
| 2499 | int dir_fd = DEFAULT_DIR_FD; |
| 2500 | int follow_symlinks = 0; |
| 2501 | PyObject *return_value; |
| 2502 | |
| 2503 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2504 | path.function_name = "lstat"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2505 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:lstat", keywords, |
| 2506 | path_converter, &path, |
| 2507 | #ifdef HAVE_FSTATAT |
| 2508 | dir_fd_converter, &dir_fd |
| 2509 | #else |
| 2510 | dir_fd_unavailable, &dir_fd |
| 2511 | #endif |
| 2512 | )) |
| 2513 | return NULL; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2514 | return_value = posix_do_stat("lstat", &path, dir_fd, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2515 | path_cleanup(&path); |
| 2516 | return return_value; |
| 2517 | } |
| 2518 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2519 | |
| 2520 | #ifdef HAVE_FACCESSAT |
| 2521 | #define OS_ACCESS_DIR_FD_CONVERTER dir_fd_converter |
| 2522 | #else |
| 2523 | #define OS_ACCESS_DIR_FD_CONVERTER dir_fd_unavailable |
| 2524 | #endif |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2525 | /*[clinic input] |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2526 | os.access |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2527 | |
| 2528 | path: path_t(allow_fd=True) |
| 2529 | Path to be tested; can be string, bytes, or open-file-descriptor int. |
| 2530 | |
| 2531 | mode: int |
| 2532 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2533 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2534 | |
| 2535 | * |
| 2536 | |
| 2537 | dir_fd : dir_fd = None |
| 2538 | If not None, it should be a file descriptor open to a directory, |
| 2539 | and path should be relative; path will then be relative to that |
| 2540 | directory. |
| 2541 | |
| 2542 | effective_ids: bool = False |
| 2543 | If True, access will use the effective uid/gid instead of |
| 2544 | the real uid/gid. |
| 2545 | |
| 2546 | follow_symlinks: bool = True |
| 2547 | If False, and the last element of the path is a symbolic link, |
| 2548 | access will examine the symbolic link itself instead of the file |
| 2549 | the link points to. |
| 2550 | |
| 2551 | Use the real uid/gid to test for access to a path. |
| 2552 | |
| 2553 | {parameters} |
| 2554 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2555 | on your platform. If they are unavailable, using them will raise a |
| 2556 | NotImplementedError. |
| 2557 | |
| 2558 | Note that most operations will use the effective uid/gid, therefore this |
| 2559 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2560 | has the specified access to the path. |
| 2561 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2562 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2563 | |
| 2564 | PyDoc_STRVAR(os_access__doc__, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 2565 | "access(path, mode, *, dir_fd=None, effective_ids=False, follow_symlinks=True)\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2566 | "Use the real uid/gid to test for access to a path.\n" |
| 2567 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2568 | " path\n" |
| 2569 | " Path to be tested; can be string, bytes, or open-file-descriptor int.\n" |
| 2570 | " mode\n" |
| 2571 | " Operating-system mode bitfield. Can be F_OK to test existence,\n" |
| 2572 | " or the inclusive-OR of R_OK, W_OK, and X_OK.\n" |
| 2573 | " dir_fd\n" |
| 2574 | " If not None, it should be a file descriptor open to a directory,\n" |
| 2575 | " and path should be relative; path will then be relative to that\n" |
| 2576 | " directory.\n" |
| 2577 | " effective_ids\n" |
| 2578 | " If True, access will use the effective uid/gid instead of\n" |
| 2579 | " the real uid/gid.\n" |
| 2580 | " follow_symlinks\n" |
| 2581 | " If False, and the last element of the path is a symbolic link,\n" |
| 2582 | " access will examine the symbolic link itself instead of the file\n" |
| 2583 | " the link points to.\n" |
| 2584 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2585 | "dir_fd, effective_ids, and follow_symlinks may not be implemented\n" |
| 2586 | " on your platform. If they are unavailable, using them will raise a\n" |
| 2587 | " NotImplementedError.\n" |
| 2588 | "\n" |
| 2589 | "Note that most operations will use the effective uid/gid, therefore this\n" |
| 2590 | " routine can be used in a suid/sgid environment to test if the invoking user\n" |
| 2591 | " has the specified access to the path."); |
| 2592 | |
| 2593 | #define OS_ACCESS_METHODDEF \ |
| 2594 | {"access", (PyCFunction)os_access, METH_VARARGS|METH_KEYWORDS, os_access__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2595 | |
| 2596 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2597 | 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] | 2598 | |
| 2599 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2600 | os_access(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2601 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2602 | PyObject *return_value = NULL; |
| 2603 | static char *_keywords[] = {"path", "mode", "dir_fd", "effective_ids", "follow_symlinks", NULL}; |
| 2604 | path_t path = PATH_T_INITIALIZE("access", 0, 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2605 | int mode; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2606 | int dir_fd = DEFAULT_DIR_FD; |
| 2607 | int effective_ids = 0; |
| 2608 | int follow_symlinks = 1; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2609 | |
| 2610 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2611 | "O&i|$O&pp:access", _keywords, |
| 2612 | path_converter, &path, &mode, OS_STAT_DIR_FD_CONVERTER, &dir_fd, &effective_ids, &follow_symlinks)) |
| 2613 | goto exit; |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 2614 | 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] | 2615 | |
| 2616 | exit: |
| 2617 | /* Cleanup for path */ |
| 2618 | path_cleanup(&path); |
| 2619 | |
| 2620 | return return_value; |
| 2621 | } |
| 2622 | |
| 2623 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2624 | os_access_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, int effective_ids, int follow_symlinks) |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2625 | /*[clinic end generated code: checksum=636e835c36562a2fc11acab75314634127fdf769]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2626 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2627 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2628 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2629 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2630 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2631 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2632 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2633 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2634 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2635 | #ifndef HAVE_FACCESSAT |
| 2636 | if (follow_symlinks_specified("access", follow_symlinks)) |
| 2637 | goto exit; |
| 2638 | |
| 2639 | if (effective_ids) { |
| 2640 | argument_unavailable_error("access", "effective_ids"); |
| 2641 | goto exit; |
| 2642 | } |
| 2643 | #endif |
| 2644 | |
| 2645 | #ifdef MS_WINDOWS |
| 2646 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | ebe83f9 | 2013-10-19 22:36:17 +0200 | [diff] [blame] | 2647 | if (path->wide != NULL) |
| 2648 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2649 | else |
Christian Heimes | ebe83f9 | 2013-10-19 22:36:17 +0200 | [diff] [blame] | 2650 | attr = GetFileAttributesA(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2651 | Py_END_ALLOW_THREADS |
| 2652 | |
| 2653 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2654 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2655 | * * we didn't get a -1, and |
| 2656 | * * write access wasn't requested, |
| 2657 | * * or the file isn't read-only, |
| 2658 | * * or it's a directory. |
| 2659 | * (Directories cannot be read-only on Windows.) |
| 2660 | */ |
| 2661 | return_value = PyBool_FromLong( |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 2662 | (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2663 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2664 | !(attr & FILE_ATTRIBUTE_READONLY) || |
| 2665 | (attr & FILE_ATTRIBUTE_DIRECTORY))); |
| 2666 | #else |
| 2667 | |
| 2668 | Py_BEGIN_ALLOW_THREADS |
| 2669 | #ifdef HAVE_FACCESSAT |
| 2670 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2671 | effective_ids || |
| 2672 | !follow_symlinks) { |
| 2673 | int flags = 0; |
| 2674 | if (!follow_symlinks) |
| 2675 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2676 | if (effective_ids) |
| 2677 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2678 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2679 | } |
| 2680 | else |
| 2681 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2682 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2683 | Py_END_ALLOW_THREADS |
| 2684 | return_value = PyBool_FromLong(!result); |
| 2685 | #endif |
| 2686 | |
| 2687 | #ifndef HAVE_FACCESSAT |
| 2688 | exit: |
| 2689 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2690 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2691 | } |
| 2692 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2693 | #ifndef F_OK |
| 2694 | #define F_OK 0 |
| 2695 | #endif |
| 2696 | #ifndef R_OK |
| 2697 | #define R_OK 4 |
| 2698 | #endif |
| 2699 | #ifndef W_OK |
| 2700 | #define W_OK 2 |
| 2701 | #endif |
| 2702 | #ifndef X_OK |
| 2703 | #define X_OK 1 |
| 2704 | #endif |
| 2705 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2706 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2707 | #ifdef HAVE_TTYNAME |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2708 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2709 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2710 | os.ttyname -> DecodeFSDefault |
| 2711 | |
| 2712 | fd: int |
| 2713 | Integer file descriptor handle. |
| 2714 | |
| 2715 | / |
| 2716 | |
| 2717 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2718 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2719 | |
| 2720 | PyDoc_STRVAR(os_ttyname__doc__, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 2721 | "ttyname(fd)\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2722 | "Return the name of the terminal device connected to \'fd\'.\n" |
| 2723 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2724 | " fd\n" |
| 2725 | " Integer file descriptor handle."); |
| 2726 | |
| 2727 | #define OS_TTYNAME_METHODDEF \ |
| 2728 | {"ttyname", (PyCFunction)os_ttyname, METH_VARARGS, os_ttyname__doc__}, |
| 2729 | |
| 2730 | static char * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2731 | os_ttyname_impl(PyModuleDef *module, int fd); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2732 | |
| 2733 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2734 | os_ttyname(PyModuleDef *module, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2735 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2736 | PyObject *return_value = NULL; |
| 2737 | int fd; |
| 2738 | char *_return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2739 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2740 | if (!PyArg_ParseTuple(args, |
| 2741 | "i:ttyname", |
| 2742 | &fd)) |
| 2743 | goto exit; |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 2744 | _return_value = os_ttyname_impl(module, fd); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2745 | if (_return_value == NULL) |
| 2746 | goto exit; |
| 2747 | return_value = PyUnicode_DecodeFSDefault(_return_value); |
| 2748 | |
| 2749 | exit: |
| 2750 | return return_value; |
| 2751 | } |
| 2752 | |
| 2753 | static char * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2754 | os_ttyname_impl(PyModuleDef *module, int fd) |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2755 | /*[clinic end generated code: checksum=0f368134dc0a7f21f25185e2e6bacf7675fb473a]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2756 | { |
| 2757 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2758 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2759 | ret = ttyname(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2760 | if (ret == NULL) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2761 | posix_error(); |
| 2762 | return ret; |
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 | #else |
| 2765 | #define OS_TTYNAME_METHODDEF |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2766 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2767 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2768 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2769 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2770 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2771 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2772 | |
| 2773 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2774 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2775 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2776 | char *ret; |
| 2777 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2778 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2779 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2780 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2781 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2782 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2783 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2784 | if (ret == NULL) |
| 2785 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2786 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2787 | } |
| 2788 | #endif |
| 2789 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2790 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2791 | "chdir(path)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2792 | Change the current working directory to the specified path.\n\ |
| 2793 | \n\ |
| 2794 | path may always be specified as a string.\n\ |
| 2795 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2796 | If this functionality is unavailable, using it raises an exception."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2797 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2798 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2799 | posix_chdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2800 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2801 | path_t path; |
| 2802 | int result; |
| 2803 | PyObject *return_value = NULL; |
| 2804 | static char *keywords[] = {"path", NULL}; |
| 2805 | |
| 2806 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2807 | path.function_name = "chdir"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2808 | #ifdef HAVE_FCHDIR |
| 2809 | path.allow_fd = 1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2810 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2811 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:chdir", keywords, |
| 2812 | path_converter, &path |
| 2813 | )) |
| 2814 | return NULL; |
| 2815 | |
| 2816 | Py_BEGIN_ALLOW_THREADS |
| 2817 | #ifdef MS_WINDOWS |
| 2818 | if (path.wide) |
| 2819 | result = win32_wchdir(path.wide); |
| 2820 | else |
| 2821 | result = win32_chdir(path.narrow); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 2822 | result = !result; /* on unix, success = 0, on windows, success = !0 */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2823 | #else |
| 2824 | #ifdef HAVE_FCHDIR |
| 2825 | if (path.fd != -1) |
| 2826 | result = fchdir(path.fd); |
| 2827 | else |
| 2828 | #endif |
| 2829 | result = chdir(path.narrow); |
| 2830 | #endif |
| 2831 | Py_END_ALLOW_THREADS |
| 2832 | |
| 2833 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2834 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2835 | goto exit; |
| 2836 | } |
| 2837 | |
| 2838 | return_value = Py_None; |
| 2839 | Py_INCREF(Py_None); |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2840 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2841 | exit: |
| 2842 | path_cleanup(&path); |
| 2843 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2846 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2847 | PyDoc_STRVAR(posix_fchdir__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2848 | "fchdir(fd)\n\n\ |
| 2849 | Change to the directory of the given file descriptor. fd must be\n\ |
| 2850 | opened on a directory, not a file. Equivalent to os.chdir(fd)."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2851 | |
| 2852 | static PyObject * |
| 2853 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 2854 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2855 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2856 | } |
| 2857 | #endif /* HAVE_FCHDIR */ |
| 2858 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2859 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2860 | PyDoc_STRVAR(posix_chmod__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2861 | "chmod(path, mode, *, dir_fd=None, follow_symlinks=True)\n\n\ |
| 2862 | Change the access permissions of a file.\n\ |
| 2863 | \n\ |
| 2864 | path may always be specified as a string.\n\ |
| 2865 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2866 | If this functionality is unavailable, using it raises an exception.\n\ |
| 2867 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2868 | and path should be relative; path will then be relative to that directory.\n\ |
| 2869 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2870 | link, chmod will modify the symbolic link itself instead of the file the\n\ |
| 2871 | link points to.\n\ |
| 2872 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2873 | an open file descriptor.\n\ |
| 2874 | dir_fd and follow_symlinks may not be implemented on your platform.\n\ |
| 2875 | If they are unavailable, using them will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2876 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2877 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2878 | posix_chmod(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2879 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2880 | path_t path; |
| 2881 | int mode; |
| 2882 | int dir_fd = DEFAULT_DIR_FD; |
| 2883 | int follow_symlinks = 1; |
| 2884 | int result; |
| 2885 | PyObject *return_value = NULL; |
| 2886 | static char *keywords[] = {"path", "mode", "dir_fd", |
| 2887 | "follow_symlinks", NULL}; |
| 2888 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2889 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2890 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2891 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2892 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2893 | #ifdef HAVE_FCHMODAT |
| 2894 | int fchmodat_nofollow_unsupported = 0; |
| 2895 | #endif |
| 2896 | |
| 2897 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2898 | path.function_name = "chmod"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2899 | #ifdef HAVE_FCHMOD |
| 2900 | path.allow_fd = 1; |
| 2901 | #endif |
| 2902 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&p:chmod", keywords, |
| 2903 | path_converter, &path, |
| 2904 | &mode, |
| 2905 | #ifdef HAVE_FCHMODAT |
| 2906 | dir_fd_converter, &dir_fd, |
| 2907 | #else |
| 2908 | dir_fd_unavailable, &dir_fd, |
| 2909 | #endif |
| 2910 | &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2911 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2912 | |
| 2913 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2914 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
| 2915 | goto exit; |
| 2916 | #endif |
| 2917 | |
| 2918 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2919 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2920 | if (path.wide) |
| 2921 | attr = GetFileAttributesW(path.wide); |
| 2922 | else |
| 2923 | attr = GetFileAttributesA(path.narrow); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 2924 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2925 | result = 0; |
| 2926 | else { |
| 2927 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2928 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2929 | else |
| 2930 | attr |= FILE_ATTRIBUTE_READONLY; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2931 | if (path.wide) |
| 2932 | result = SetFileAttributesW(path.wide, attr); |
| 2933 | else |
| 2934 | result = SetFileAttributesA(path.narrow, attr); |
| 2935 | } |
| 2936 | Py_END_ALLOW_THREADS |
| 2937 | |
| 2938 | if (!result) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 2939 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2940 | goto exit; |
| 2941 | } |
| 2942 | #else /* MS_WINDOWS */ |
| 2943 | Py_BEGIN_ALLOW_THREADS |
| 2944 | #ifdef HAVE_FCHMOD |
| 2945 | if (path.fd != -1) |
| 2946 | result = fchmod(path.fd, mode); |
| 2947 | else |
| 2948 | #endif |
| 2949 | #ifdef HAVE_LCHMOD |
| 2950 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2951 | result = lchmod(path.narrow, mode); |
| 2952 | else |
| 2953 | #endif |
| 2954 | #ifdef HAVE_FCHMODAT |
| 2955 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2956 | /* |
| 2957 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2958 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2959 | * and then says it isn't implemented yet. |
| 2960 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2961 | * |
| 2962 | * Once it is supported, os.chmod will automatically |
| 2963 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2964 | * Until then, we need to be careful what exception we raise. |
| 2965 | */ |
| 2966 | result = fchmodat(dir_fd, path.narrow, mode, |
| 2967 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2968 | /* |
| 2969 | * But wait! We can't throw the exception without allowing threads, |
| 2970 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2971 | */ |
| 2972 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2973 | result && |
| 2974 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2975 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2976 | } |
| 2977 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2978 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2979 | result = chmod(path.narrow, mode); |
| 2980 | Py_END_ALLOW_THREADS |
| 2981 | |
| 2982 | if (result) { |
| 2983 | #ifdef HAVE_FCHMODAT |
| 2984 | if (fchmodat_nofollow_unsupported) { |
| 2985 | if (dir_fd != DEFAULT_DIR_FD) |
| 2986 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 2987 | dir_fd, follow_symlinks); |
| 2988 | else |
| 2989 | follow_symlinks_specified("chmod", follow_symlinks); |
| 2990 | } |
| 2991 | else |
| 2992 | #endif |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2993 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2994 | goto exit; |
| 2995 | } |
| 2996 | #endif |
| 2997 | |
| 2998 | Py_INCREF(Py_None); |
| 2999 | return_value = Py_None; |
| 3000 | exit: |
| 3001 | path_cleanup(&path); |
| 3002 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3005 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3006 | #ifdef HAVE_FCHMOD |
| 3007 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 3008 | "fchmod(fd, mode)\n\n\ |
| 3009 | Change the access permissions of the file given by file\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3010 | descriptor fd. Equivalent to os.chmod(fd, mode)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3011 | |
| 3012 | static PyObject * |
| 3013 | posix_fchmod(PyObject *self, PyObject *args) |
| 3014 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3015 | int fd, mode, res; |
| 3016 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 3017 | return NULL; |
| 3018 | Py_BEGIN_ALLOW_THREADS |
| 3019 | res = fchmod(fd, mode); |
| 3020 | Py_END_ALLOW_THREADS |
| 3021 | if (res < 0) |
| 3022 | return posix_error(); |
| 3023 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3024 | } |
| 3025 | #endif /* HAVE_FCHMOD */ |
| 3026 | |
| 3027 | #ifdef HAVE_LCHMOD |
| 3028 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 3029 | "lchmod(path, mode)\n\n\ |
| 3030 | 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] | 3031 | affects the link itself rather than the target.\n\ |
| 3032 | Equivalent to chmod(path, mode, follow_symlinks=False)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3033 | |
| 3034 | static PyObject * |
| 3035 | posix_lchmod(PyObject *self, PyObject *args) |
| 3036 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3037 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3038 | int i; |
| 3039 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3040 | memset(&path, 0, sizeof(path)); |
| 3041 | path.function_name = "lchmod"; |
| 3042 | if (!PyArg_ParseTuple(args, "O&i:lchmod", |
| 3043 | path_converter, &path, &i)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3044 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3045 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3046 | res = lchmod(path.narrow, i); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3047 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3048 | if (res < 0) { |
| 3049 | path_error(&path); |
| 3050 | path_cleanup(&path); |
| 3051 | return NULL; |
| 3052 | } |
| 3053 | path_cleanup(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3054 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3055 | } |
| 3056 | #endif /* HAVE_LCHMOD */ |
| 3057 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3058 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3059 | #ifdef HAVE_CHFLAGS |
| 3060 | PyDoc_STRVAR(posix_chflags__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3061 | "chflags(path, flags, *, follow_symlinks=True)\n\n\ |
| 3062 | Set file flags.\n\ |
| 3063 | \n\ |
| 3064 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 3065 | link, chflags will change flags on the symbolic link itself instead of the\n\ |
| 3066 | file the link points to.\n\ |
| 3067 | follow_symlinks may not be implemented on your platform. If it is\n\ |
| 3068 | unavailable, using it will raise a NotImplementedError."); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3069 | |
| 3070 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3071 | posix_chflags(PyObject *self, PyObject *args, PyObject *kwargs) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3072 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3073 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3074 | unsigned long flags; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3075 | int follow_symlinks = 1; |
| 3076 | int result; |
Victor Stinner | 45e9039 | 2013-07-18 23:57:35 +0200 | [diff] [blame] | 3077 | PyObject *return_value = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3078 | static char *keywords[] = {"path", "flags", "follow_symlinks", NULL}; |
| 3079 | |
| 3080 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3081 | path.function_name = "chflags"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3082 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&k|$i:chflags", keywords, |
| 3083 | path_converter, &path, |
| 3084 | &flags, &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3085 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3086 | |
| 3087 | #ifndef HAVE_LCHFLAGS |
| 3088 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
| 3089 | goto exit; |
| 3090 | #endif |
| 3091 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3092 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3093 | #ifdef HAVE_LCHFLAGS |
| 3094 | if (!follow_symlinks) |
| 3095 | result = lchflags(path.narrow, flags); |
| 3096 | else |
| 3097 | #endif |
| 3098 | result = chflags(path.narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3099 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3100 | |
| 3101 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3102 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3103 | goto exit; |
| 3104 | } |
| 3105 | |
| 3106 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3107 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3108 | |
| 3109 | exit: |
| 3110 | path_cleanup(&path); |
| 3111 | return return_value; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3112 | } |
| 3113 | #endif /* HAVE_CHFLAGS */ |
| 3114 | |
| 3115 | #ifdef HAVE_LCHFLAGS |
| 3116 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 3117 | "lchflags(path, flags)\n\n\ |
| 3118 | Set file flags.\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3119 | This function will not follow symbolic links.\n\ |
| 3120 | Equivalent to chflags(path, flags, follow_symlinks=False)."); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3121 | |
| 3122 | static PyObject * |
| 3123 | posix_lchflags(PyObject *self, PyObject *args) |
| 3124 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3125 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3126 | unsigned long flags; |
| 3127 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3128 | memset(&path, 0, sizeof(path)); |
| 3129 | path.function_name = "lchflags"; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3130 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3131 | path_converter, &path, &flags)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3132 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3133 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3134 | res = lchflags(path.narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3135 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3136 | if (res < 0) { |
| 3137 | path_error(&path); |
| 3138 | path_cleanup(&path); |
| 3139 | return NULL; |
| 3140 | } |
| 3141 | path_cleanup(&path); |
| 3142 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3143 | } |
| 3144 | #endif /* HAVE_LCHFLAGS */ |
| 3145 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3146 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3147 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3148 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3149 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3150 | |
| 3151 | static PyObject * |
| 3152 | posix_chroot(PyObject *self, PyObject *args) |
| 3153 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3154 | return posix_1str("chroot", args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3155 | } |
| 3156 | #endif |
| 3157 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3158 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3159 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3160 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3161 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3162 | |
| 3163 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 3164 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3165 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 3166 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3167 | } |
| 3168 | #endif /* HAVE_FSYNC */ |
| 3169 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3170 | #ifdef HAVE_SYNC |
| 3171 | PyDoc_STRVAR(posix_sync__doc__, |
| 3172 | "sync()\n\n\ |
| 3173 | Force write of everything to disk."); |
| 3174 | |
| 3175 | static PyObject * |
| 3176 | posix_sync(PyObject *self, PyObject *noargs) |
| 3177 | { |
| 3178 | Py_BEGIN_ALLOW_THREADS |
| 3179 | sync(); |
| 3180 | Py_END_ALLOW_THREADS |
| 3181 | Py_RETURN_NONE; |
| 3182 | } |
| 3183 | #endif |
| 3184 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3185 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3186 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3187 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3188 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3189 | #endif |
| 3190 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3191 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3192 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3193 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3194 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3195 | |
| 3196 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 3197 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3198 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 3199 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3200 | } |
| 3201 | #endif /* HAVE_FDATASYNC */ |
| 3202 | |
| 3203 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3204 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3205 | PyDoc_STRVAR(posix_chown__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3206 | "chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True)\n\n\ |
| 3207 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 3208 | \n\ |
| 3209 | path may always be specified as a string.\n\ |
| 3210 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 3211 | If this functionality is unavailable, using it raises an exception.\n\ |
| 3212 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 3213 | and path should be relative; path will then be relative to that directory.\n\ |
| 3214 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 3215 | link, chown will modify the symbolic link itself instead of the file the\n\ |
| 3216 | link points to.\n\ |
| 3217 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 3218 | an open file descriptor.\n\ |
| 3219 | dir_fd and follow_symlinks may not be implemented on your platform.\n\ |
| 3220 | If they are unavailable, using them will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3221 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3222 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3223 | posix_chown(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3224 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3225 | path_t path; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3226 | uid_t uid; |
| 3227 | gid_t gid; |
| 3228 | int dir_fd = DEFAULT_DIR_FD; |
| 3229 | int follow_symlinks = 1; |
| 3230 | int result; |
| 3231 | PyObject *return_value = NULL; |
| 3232 | static char *keywords[] = {"path", "uid", "gid", "dir_fd", |
| 3233 | "follow_symlinks", NULL}; |
| 3234 | |
| 3235 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3236 | path.function_name = "chown"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3237 | #ifdef HAVE_FCHOWN |
| 3238 | path.allow_fd = 1; |
| 3239 | #endif |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3240 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&O&|$O&p:chown", keywords, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3241 | path_converter, &path, |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3242 | _Py_Uid_Converter, &uid, |
| 3243 | _Py_Gid_Converter, &gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3244 | #ifdef HAVE_FCHOWNAT |
| 3245 | dir_fd_converter, &dir_fd, |
| 3246 | #else |
| 3247 | dir_fd_unavailable, &dir_fd, |
| 3248 | #endif |
| 3249 | &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3250 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3251 | |
| 3252 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3253 | if (follow_symlinks_specified("chown", follow_symlinks)) |
| 3254 | goto exit; |
| 3255 | #endif |
| 3256 | if (dir_fd_and_fd_invalid("chown", dir_fd, path.fd) || |
| 3257 | fd_and_follow_symlinks_invalid("chown", path.fd, follow_symlinks)) |
| 3258 | goto exit; |
| 3259 | |
| 3260 | #ifdef __APPLE__ |
| 3261 | /* |
| 3262 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3263 | * (But we still have an lchown symbol because of weak-linking.) |
| 3264 | * It doesn't have fchownat either. So there's no possibility |
| 3265 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3266 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3267 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3268 | follow_symlinks_specified("chown", follow_symlinks); |
| 3269 | goto exit; |
| 3270 | } |
| 3271 | #endif |
| 3272 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3273 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3274 | #ifdef HAVE_FCHOWN |
| 3275 | if (path.fd != -1) |
| 3276 | result = fchown(path.fd, uid, gid); |
| 3277 | else |
| 3278 | #endif |
| 3279 | #ifdef HAVE_LCHOWN |
| 3280 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 3281 | result = lchown(path.narrow, uid, gid); |
| 3282 | else |
| 3283 | #endif |
| 3284 | #ifdef HAVE_FCHOWNAT |
| 3285 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
| 3286 | result = fchownat(dir_fd, path.narrow, uid, gid, |
| 3287 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3288 | else |
| 3289 | #endif |
| 3290 | result = chown(path.narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3291 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3292 | |
| 3293 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3294 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3295 | goto exit; |
| 3296 | } |
| 3297 | |
| 3298 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3299 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3300 | |
| 3301 | exit: |
| 3302 | path_cleanup(&path); |
| 3303 | return return_value; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3304 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3305 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3306 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3307 | #ifdef HAVE_FCHOWN |
| 3308 | PyDoc_STRVAR(posix_fchown__doc__, |
| 3309 | "fchown(fd, uid, gid)\n\n\ |
| 3310 | 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] | 3311 | 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] | 3312 | |
| 3313 | static PyObject * |
| 3314 | posix_fchown(PyObject *self, PyObject *args) |
| 3315 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3316 | int fd; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3317 | uid_t uid; |
| 3318 | gid_t gid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3319 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3320 | if (!PyArg_ParseTuple(args, "iO&O&:fchown", &fd, |
| 3321 | _Py_Uid_Converter, &uid, |
| 3322 | _Py_Gid_Converter, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3323 | return NULL; |
| 3324 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3325 | res = fchown(fd, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3326 | Py_END_ALLOW_THREADS |
| 3327 | if (res < 0) |
| 3328 | return posix_error(); |
| 3329 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3330 | } |
| 3331 | #endif /* HAVE_FCHOWN */ |
| 3332 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3333 | #ifdef HAVE_LCHOWN |
| 3334 | PyDoc_STRVAR(posix_lchown__doc__, |
| 3335 | "lchown(path, uid, gid)\n\n\ |
| 3336 | 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] | 3337 | This function will not follow symbolic links.\n\ |
| 3338 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False)."); |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3339 | |
| 3340 | static PyObject * |
| 3341 | posix_lchown(PyObject *self, PyObject *args) |
| 3342 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3343 | path_t path; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3344 | uid_t uid; |
| 3345 | gid_t gid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3346 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3347 | memset(&path, 0, sizeof(path)); |
| 3348 | path.function_name = "lchown"; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3349 | if (!PyArg_ParseTuple(args, "O&O&O&:lchown", |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3350 | path_converter, &path, |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3351 | _Py_Uid_Converter, &uid, |
| 3352 | _Py_Gid_Converter, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3353 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3354 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | c2d0200 | 2013-02-10 22:03:08 +0200 | [diff] [blame] | 3355 | res = lchown(path.narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3356 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3357 | if (res < 0) { |
| 3358 | path_error(&path); |
| 3359 | path_cleanup(&path); |
| 3360 | return NULL; |
| 3361 | } |
| 3362 | path_cleanup(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3363 | Py_INCREF(Py_None); |
| 3364 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3365 | } |
| 3366 | #endif /* HAVE_LCHOWN */ |
| 3367 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3368 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3369 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3370 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3371 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3372 | char buf[1026]; |
| 3373 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3374 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3375 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3376 | if (!use_bytes) { |
| 3377 | wchar_t wbuf[1026]; |
| 3378 | wchar_t *wbuf2 = wbuf; |
| 3379 | PyObject *resobj; |
| 3380 | DWORD len; |
| 3381 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3382 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3383 | /* If the buffer is large enough, len does not include the |
| 3384 | terminating \0. If the buffer is too small, len includes |
| 3385 | the space needed for the terminator. */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3386 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3387 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3388 | if (wbuf2) |
| 3389 | len = GetCurrentDirectoryW(len, wbuf2); |
| 3390 | } |
| 3391 | Py_END_ALLOW_THREADS |
| 3392 | if (!wbuf2) { |
| 3393 | PyErr_NoMemory(); |
| 3394 | return NULL; |
| 3395 | } |
| 3396 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3397 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3398 | PyMem_RawFree(wbuf2); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3399 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3400 | } |
| 3401 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3402 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3403 | PyMem_RawFree(wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3404 | return resobj; |
| 3405 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3406 | |
| 3407 | if (win32_warn_bytes_api()) |
| 3408 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3409 | #endif |
| 3410 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3411 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3412 | res = getcwd(buf, sizeof buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3413 | Py_END_ALLOW_THREADS |
| 3414 | if (res == NULL) |
| 3415 | return posix_error(); |
| 3416 | if (use_bytes) |
| 3417 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 3418 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3419 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3420 | |
| 3421 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 3422 | "getcwd() -> path\n\n\ |
| 3423 | Return a unicode string representing the current working directory."); |
| 3424 | |
| 3425 | static PyObject * |
| 3426 | posix_getcwd_unicode(PyObject *self) |
| 3427 | { |
| 3428 | return posix_getcwd(0); |
| 3429 | } |
| 3430 | |
| 3431 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 3432 | "getcwdb() -> path\n\n\ |
| 3433 | Return a bytes string representing the current working directory."); |
| 3434 | |
| 3435 | static PyObject * |
| 3436 | posix_getcwd_bytes(PyObject *self) |
| 3437 | { |
| 3438 | return posix_getcwd(1); |
| 3439 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3440 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3441 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3442 | #define HAVE_LINK 1 |
| 3443 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3444 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3445 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3446 | PyDoc_STRVAR(posix_link__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3447 | "link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True)\n\n\ |
| 3448 | Create a hard link to a file.\n\ |
| 3449 | \n\ |
| 3450 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 3451 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 3452 | should be relative; the path will then be relative to that directory.\n\ |
| 3453 | If follow_symlinks is False, and the last element of src is a symbolic\n\ |
| 3454 | link, link will create a link to the symbolic link itself instead of the\n\ |
| 3455 | file the link points to.\n\ |
| 3456 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your\n\ |
| 3457 | platform. If they are unavailable, using them will raise a\n\ |
| 3458 | NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3459 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3460 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3461 | posix_link(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3462 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3463 | path_t src, dst; |
| 3464 | int src_dir_fd = DEFAULT_DIR_FD; |
| 3465 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 3466 | int follow_symlinks = 1; |
| 3467 | PyObject *return_value = NULL; |
| 3468 | static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", |
| 3469 | "follow_symlinks", NULL}; |
| 3470 | #ifdef MS_WINDOWS |
| 3471 | BOOL result; |
| 3472 | #else |
| 3473 | int result; |
| 3474 | #endif |
| 3475 | |
| 3476 | memset(&src, 0, sizeof(src)); |
| 3477 | memset(&dst, 0, sizeof(dst)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3478 | src.function_name = "link"; |
| 3479 | dst.function_name = "link"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3480 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|O&O&p:link", keywords, |
| 3481 | path_converter, &src, |
| 3482 | path_converter, &dst, |
| 3483 | dir_fd_converter, &src_dir_fd, |
| 3484 | dir_fd_converter, &dst_dir_fd, |
| 3485 | &follow_symlinks)) |
| 3486 | return NULL; |
| 3487 | |
| 3488 | #ifndef HAVE_LINKAT |
| 3489 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3490 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
| 3491 | goto exit; |
| 3492 | } |
| 3493 | #endif |
| 3494 | |
| 3495 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 3496 | PyErr_SetString(PyExc_NotImplementedError, |
| 3497 | "link: src and dst must be the same type"); |
| 3498 | goto exit; |
| 3499 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3500 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3501 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3502 | Py_BEGIN_ALLOW_THREADS |
| 3503 | if (src.wide) |
| 3504 | result = CreateHardLinkW(dst.wide, src.wide, NULL); |
| 3505 | else |
| 3506 | result = CreateHardLinkA(dst.narrow, src.narrow, NULL); |
| 3507 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3508 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3509 | if (!result) { |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 3510 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3511 | goto exit; |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3512 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3513 | #else |
| 3514 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3515 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3516 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3517 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3518 | (!follow_symlinks)) |
| 3519 | result = linkat(src_dir_fd, src.narrow, |
| 3520 | dst_dir_fd, dst.narrow, |
| 3521 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3522 | else |
| 3523 | #endif |
| 3524 | result = link(src.narrow, dst.narrow); |
| 3525 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3526 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3527 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3528 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3529 | goto exit; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3530 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3531 | #endif |
| 3532 | |
| 3533 | return_value = Py_None; |
| 3534 | Py_INCREF(Py_None); |
| 3535 | |
| 3536 | exit: |
| 3537 | path_cleanup(&src); |
| 3538 | path_cleanup(&dst); |
| 3539 | return return_value; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3540 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3541 | #endif |
| 3542 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3543 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3544 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3545 | PyDoc_STRVAR(posix_listdir__doc__, |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3546 | "listdir(path='.') -> list_of_filenames\n\n\ |
| 3547 | 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] | 3548 | 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] | 3549 | entries '.' and '..' even if they are present in the directory.\n\ |
| 3550 | \n\ |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3551 | path can be specified as either str or bytes. If path is bytes,\n\ |
| 3552 | the filenames returned will also be bytes; in all other circumstances\n\ |
| 3553 | the filenames returned will be str.\n\ |
| 3554 | On some platforms, path may also be specified as an open file descriptor;\n\ |
| 3555 | the file descriptor must refer to a directory.\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3556 | If this functionality is unavailable, using it raises NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3557 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3558 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3559 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3560 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3561 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3562 | static char *keywords[] = {"path", NULL}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3563 | PyObject *v; |
| 3564 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3565 | BOOL result; |
| 3566 | WIN32_FIND_DATA FileData; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3567 | char namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3568 | char *bufptr = namebuf; |
| 3569 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3570 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3571 | PyObject *po = NULL; |
| 3572 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3573 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3574 | if (!path->narrow) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3575 | WIN32_FIND_DATAW wFileData; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3576 | wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3577 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3578 | if (!path->wide) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3579 | po_wchars = L"."; |
| 3580 | len = 1; |
| 3581 | } else { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3582 | po_wchars = path->wide; |
| 3583 | len = wcslen(path->wide); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3584 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3585 | /* The +5 is so we can append "\\*.*\0" */ |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3586 | wnamebuf = PyMem_Malloc((len + 5) * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3587 | if (!wnamebuf) { |
| 3588 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3589 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3590 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3591 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3592 | if (len > 0) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3593 | wchar_t wch = wnamebuf[len-1]; |
Tim Golden | 781bbeb | 2013-10-25 20:24:06 +0100 | [diff] [blame] | 3594 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3595 | wnamebuf[len++] = SEP; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3596 | wcscpy(wnamebuf + len, L"*.*"); |
| 3597 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3598 | if ((list = PyList_New(0)) == NULL) { |
| 3599 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3600 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3601 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3602 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3603 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3604 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3605 | int error = GetLastError(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3606 | if (error == ERROR_FILE_NOT_FOUND) |
| 3607 | goto exit; |
| 3608 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3609 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3610 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3611 | } |
| 3612 | do { |
| 3613 | /* Skip over . and .. */ |
| 3614 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3615 | wcscmp(wFileData.cFileName, L"..") != 0) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3616 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3617 | wcslen(wFileData.cFileName)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3618 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3619 | Py_DECREF(list); |
| 3620 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3621 | break; |
| 3622 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3623 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3624 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3625 | Py_DECREF(list); |
| 3626 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3627 | break; |
| 3628 | } |
| 3629 | Py_DECREF(v); |
| 3630 | } |
| 3631 | Py_BEGIN_ALLOW_THREADS |
| 3632 | result = FindNextFileW(hFindFile, &wFileData); |
| 3633 | Py_END_ALLOW_THREADS |
| 3634 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3635 | it got to the end of the directory. */ |
| 3636 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3637 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3638 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3639 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3640 | } |
| 3641 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3642 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3643 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3644 | } |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3645 | strcpy(namebuf, path->narrow); |
| 3646 | len = path->length; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3647 | if (len > 0) { |
| 3648 | char ch = namebuf[len-1]; |
Tim Golden | 781bbeb | 2013-10-25 20:24:06 +0100 | [diff] [blame] | 3649 | if (ch != '\\' && ch != '/' && ch != ':') |
| 3650 | namebuf[len++] = '\\'; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3651 | strcpy(namebuf + len, "*.*"); |
| 3652 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3653 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3654 | if ((list = PyList_New(0)) == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3655 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3656 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3657 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3658 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3659 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3660 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3661 | int error = GetLastError(); |
| 3662 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3663 | goto exit; |
| 3664 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3665 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3666 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3667 | } |
| 3668 | do { |
| 3669 | /* Skip over . and .. */ |
| 3670 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 3671 | strcmp(FileData.cFileName, "..") != 0) { |
| 3672 | v = PyBytes_FromString(FileData.cFileName); |
| 3673 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3674 | Py_DECREF(list); |
| 3675 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3676 | break; |
| 3677 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3678 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3679 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3680 | Py_DECREF(list); |
| 3681 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3682 | break; |
| 3683 | } |
| 3684 | Py_DECREF(v); |
| 3685 | } |
| 3686 | Py_BEGIN_ALLOW_THREADS |
| 3687 | result = FindNextFile(hFindFile, &FileData); |
| 3688 | Py_END_ALLOW_THREADS |
| 3689 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3690 | it got to the end of the directory. */ |
| 3691 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3692 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3693 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3694 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3695 | } |
| 3696 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3697 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3698 | exit: |
| 3699 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3700 | if (FindClose(hFindFile) == FALSE) { |
| 3701 | if (list != NULL) { |
| 3702 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3703 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3704 | } |
| 3705 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3706 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3707 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3708 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3709 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3710 | } /* end of _listdir_windows_no_opendir */ |
| 3711 | |
| 3712 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3713 | |
| 3714 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3715 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3716 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3717 | PyObject *v; |
| 3718 | DIR *dirp = NULL; |
| 3719 | struct dirent *ep; |
| 3720 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3721 | #ifdef HAVE_FDOPENDIR |
| 3722 | int fd = -1; |
| 3723 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3724 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3725 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3726 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3727 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3728 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3729 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3730 | if (fd == -1) |
| 3731 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3732 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3733 | return_str = 1; |
| 3734 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3735 | Py_BEGIN_ALLOW_THREADS |
| 3736 | dirp = fdopendir(fd); |
| 3737 | Py_END_ALLOW_THREADS |
| 3738 | } |
| 3739 | else |
| 3740 | #endif |
| 3741 | { |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3742 | char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3743 | if (path->narrow) { |
| 3744 | name = path->narrow; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3745 | /* only return bytes if they specified a bytes object */ |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3746 | return_str = !(PyBytes_Check(path->object)); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3747 | } |
| 3748 | else { |
| 3749 | name = "."; |
| 3750 | return_str = 1; |
| 3751 | } |
| 3752 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3753 | Py_BEGIN_ALLOW_THREADS |
| 3754 | dirp = opendir(name); |
| 3755 | Py_END_ALLOW_THREADS |
| 3756 | } |
| 3757 | |
| 3758 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3759 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3760 | #ifdef HAVE_FDOPENDIR |
| 3761 | if (fd != -1) { |
| 3762 | Py_BEGIN_ALLOW_THREADS |
| 3763 | close(fd); |
| 3764 | Py_END_ALLOW_THREADS |
| 3765 | } |
| 3766 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3767 | goto exit; |
| 3768 | } |
| 3769 | if ((list = PyList_New(0)) == NULL) { |
| 3770 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3771 | } |
| 3772 | for (;;) { |
| 3773 | errno = 0; |
| 3774 | Py_BEGIN_ALLOW_THREADS |
| 3775 | ep = readdir(dirp); |
| 3776 | Py_END_ALLOW_THREADS |
| 3777 | if (ep == NULL) { |
| 3778 | if (errno == 0) { |
| 3779 | break; |
| 3780 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3781 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3782 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3783 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3784 | } |
| 3785 | } |
| 3786 | if (ep->d_name[0] == '.' && |
| 3787 | (NAMLEN(ep) == 1 || |
| 3788 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3789 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3790 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3791 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3792 | else |
| 3793 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3794 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3795 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3796 | break; |
| 3797 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3798 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3799 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3800 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3801 | break; |
| 3802 | } |
| 3803 | Py_DECREF(v); |
| 3804 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3805 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3806 | exit: |
| 3807 | if (dirp != NULL) { |
| 3808 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3809 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3810 | if (fd > -1) |
| 3811 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3812 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3813 | closedir(dirp); |
| 3814 | Py_END_ALLOW_THREADS |
| 3815 | } |
| 3816 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3817 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3818 | } /* end of _posix_listdir */ |
| 3819 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3820 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3821 | static PyObject * |
| 3822 | posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs) |
| 3823 | { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3824 | path_t path; |
| 3825 | PyObject *list = NULL; |
| 3826 | static char *keywords[] = {"path", NULL}; |
| 3827 | PyObject *return_value; |
| 3828 | |
| 3829 | memset(&path, 0, sizeof(path)); |
| 3830 | path.function_name = "listdir"; |
| 3831 | path.nullable = 1; |
| 3832 | #ifdef HAVE_FDOPENDIR |
| 3833 | path.allow_fd = 1; |
| 3834 | path.fd = -1; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3835 | #endif |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3836 | |
| 3837 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:listdir", keywords, |
| 3838 | path_converter, &path)) { |
| 3839 | return NULL; |
| 3840 | } |
| 3841 | |
| 3842 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3843 | return_value = _listdir_windows_no_opendir(&path, list); |
| 3844 | #else |
| 3845 | return_value = _posix_listdir(&path, list); |
| 3846 | #endif |
| 3847 | path_cleanup(&path); |
| 3848 | return return_value; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3849 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3850 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3851 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3852 | /* A helper function for abspath on win32 */ |
| 3853 | static PyObject * |
| 3854 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 3855 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3856 | const char *path; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3857 | char outbuf[MAX_PATH]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3858 | char *temp; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3859 | PyObject *po; |
| 3860 | |
| 3861 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) |
| 3862 | { |
| 3863 | wchar_t *wpath; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3864 | wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3865 | wchar_t *wtemp; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3866 | DWORD result; |
| 3867 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3868 | |
| 3869 | wpath = PyUnicode_AsUnicode(po); |
| 3870 | if (wpath == NULL) |
| 3871 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3872 | result = GetFullPathNameW(wpath, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3873 | Py_ARRAY_LENGTH(woutbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3874 | woutbuf, &wtemp); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3875 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3876 | woutbufp = PyMem_Malloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3877 | if (!woutbufp) |
| 3878 | return PyErr_NoMemory(); |
| 3879 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 3880 | } |
| 3881 | if (result) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3882 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3883 | else |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3884 | v = win32_error_object("GetFullPathNameW", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3885 | if (woutbufp != woutbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3886 | PyMem_Free(woutbufp); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3887 | return v; |
| 3888 | } |
| 3889 | /* Drop the argument parsing error as narrow strings |
| 3890 | are also valid. */ |
| 3891 | PyErr_Clear(); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3892 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3893 | if (!PyArg_ParseTuple (args, "y:_getfullpathname", |
| 3894 | &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3895 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3896 | if (win32_warn_bytes_api()) |
| 3897 | return NULL; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3898 | if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3899 | outbuf, &temp)) { |
| 3900 | win32_error("GetFullPathName", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3901 | return NULL; |
| 3902 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3903 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 3904 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 3905 | Py_FileSystemDefaultEncoding, NULL); |
| 3906 | } |
| 3907 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3908 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3909 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3910 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3911 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3912 | /* A helper function for samepath on windows */ |
| 3913 | static PyObject * |
| 3914 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 3915 | { |
| 3916 | HANDLE hFile; |
| 3917 | int buf_size; |
| 3918 | wchar_t *target_path; |
| 3919 | int result_length; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3920 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3921 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3922 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3923 | if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po)) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3924 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3925 | path = PyUnicode_AsUnicode(po); |
| 3926 | if (path == NULL) |
| 3927 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3928 | |
| 3929 | if(!check_GetFinalPathNameByHandle()) { |
| 3930 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 3931 | NotImplementedError. */ |
| 3932 | return PyErr_Format(PyExc_NotImplementedError, |
| 3933 | "GetFinalPathNameByHandle not available on this platform"); |
| 3934 | } |
| 3935 | |
| 3936 | hFile = CreateFileW( |
| 3937 | path, |
| 3938 | 0, /* desired access */ |
| 3939 | 0, /* share mode */ |
| 3940 | NULL, /* security attributes */ |
| 3941 | OPEN_EXISTING, |
| 3942 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3943 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3944 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3945 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3946 | if(hFile == INVALID_HANDLE_VALUE) |
| 3947 | return win32_error_object("CreateFileW", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3948 | |
| 3949 | /* We have a good handle to the target, use it to determine the |
| 3950 | target path name. */ |
| 3951 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 3952 | |
| 3953 | if(!buf_size) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3954 | return win32_error_object("GetFinalPathNameByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3955 | |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3956 | target_path = (wchar_t *)PyMem_Malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3957 | if(!target_path) |
| 3958 | return PyErr_NoMemory(); |
| 3959 | |
| 3960 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 3961 | buf_size, VOLUME_NAME_DOS); |
| 3962 | if(!result_length) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3963 | return win32_error_object("GetFinalPathNamyByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3964 | |
| 3965 | if(!CloseHandle(hFile)) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3966 | return win32_error_object("CloseHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3967 | |
| 3968 | target_path[result_length] = 0; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3969 | result = PyUnicode_FromWideChar(target_path, result_length); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3970 | PyMem_Free(target_path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3971 | return result; |
| 3972 | |
| 3973 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3974 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3975 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3976 | "Return true if the pathname refers to an existing directory."); |
| 3977 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3978 | static PyObject * |
| 3979 | posix__isdir(PyObject *self, PyObject *args) |
| 3980 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3981 | const char *path; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3982 | PyObject *po; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3983 | DWORD attributes; |
| 3984 | |
| 3985 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3986 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 3987 | if (wpath == NULL) |
| 3988 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3989 | |
| 3990 | attributes = GetFileAttributesW(wpath); |
| 3991 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3992 | Py_RETURN_FALSE; |
| 3993 | goto check; |
| 3994 | } |
| 3995 | /* Drop the argument parsing error as narrow strings |
| 3996 | are also valid. */ |
| 3997 | PyErr_Clear(); |
| 3998 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3999 | if (!PyArg_ParseTuple(args, "y:_isdir", &path)) |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 4000 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 4001 | if (win32_warn_bytes_api()) |
| 4002 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 4003 | attributes = GetFileAttributesA(path); |
| 4004 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 4005 | Py_RETURN_FALSE; |
| 4006 | |
| 4007 | check: |
| 4008 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 4009 | Py_RETURN_TRUE; |
| 4010 | else |
| 4011 | Py_RETURN_FALSE; |
| 4012 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4013 | |
| 4014 | PyDoc_STRVAR(posix__getvolumepathname__doc__, |
| 4015 | "Return volume mount point of the specified path."); |
| 4016 | |
| 4017 | /* A helper function for ismount on windows */ |
| 4018 | static PyObject * |
| 4019 | posix__getvolumepathname(PyObject *self, PyObject *args) |
| 4020 | { |
| 4021 | PyObject *po, *result; |
| 4022 | wchar_t *path, *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4023 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4024 | BOOL ret; |
| 4025 | |
| 4026 | if (!PyArg_ParseTuple(args, "U|:_getvolumepathname", &po)) |
| 4027 | return NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4028 | path = PyUnicode_AsUnicodeAndSize(po, &buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4029 | if (path == NULL) |
| 4030 | return NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4031 | buflen += 1; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4032 | |
| 4033 | /* Volume path should be shorter than entire path */ |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4034 | buflen = Py_MAX(buflen, MAX_PATH); |
| 4035 | |
| 4036 | if (buflen > DWORD_MAX) { |
| 4037 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 4038 | return NULL; |
| 4039 | } |
| 4040 | |
| 4041 | mountpath = (wchar_t *)PyMem_Malloc(buflen * sizeof(wchar_t)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4042 | if (mountpath == NULL) |
| 4043 | return PyErr_NoMemory(); |
| 4044 | |
| 4045 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | 85ba92a | 2013-11-18 10:30:42 +0100 | [diff] [blame] | 4046 | ret = GetVolumePathNameW(path, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4047 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4048 | Py_END_ALLOW_THREADS |
| 4049 | |
| 4050 | if (!ret) { |
| 4051 | result = win32_error_object("_getvolumepathname", po); |
| 4052 | goto exit; |
| 4053 | } |
| 4054 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
| 4055 | |
| 4056 | exit: |
| 4057 | PyMem_Free(mountpath); |
| 4058 | return result; |
| 4059 | } |
| 4060 | /* end of posix__getvolumepathname */ |
| 4061 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4062 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4063 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4064 | PyDoc_STRVAR(posix_mkdir__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4065 | "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 4066 | Create a directory.\n\ |
| 4067 | \n\ |
| 4068 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4069 | and path should be relative; path will then be relative to that directory.\n\ |
| 4070 | dir_fd may not be implemented on your platform.\n\ |
| 4071 | If it is unavailable, using it will raise a NotImplementedError.\n\ |
| 4072 | \n\ |
| 4073 | The mode argument is ignored on Windows."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4074 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4075 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4076 | posix_mkdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4077 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4078 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4079 | int mode = 0777; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4080 | int dir_fd = DEFAULT_DIR_FD; |
| 4081 | static char *keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 4082 | PyObject *return_value = NULL; |
| 4083 | int result; |
| 4084 | |
| 4085 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4086 | path.function_name = "mkdir"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4087 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkdir", keywords, |
| 4088 | path_converter, &path, &mode, |
| 4089 | #ifdef HAVE_MKDIRAT |
| 4090 | dir_fd_converter, &dir_fd |
| 4091 | #else |
| 4092 | dir_fd_unavailable, &dir_fd |
| 4093 | #endif |
| 4094 | )) |
| 4095 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4096 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4097 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4098 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4099 | if (path.wide) |
| 4100 | result = CreateDirectoryW(path.wide, NULL); |
| 4101 | else |
| 4102 | result = CreateDirectoryA(path.narrow, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4103 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4104 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4105 | if (!result) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4106 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4107 | goto exit; |
| 4108 | } |
| 4109 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4110 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4111 | #if HAVE_MKDIRAT |
| 4112 | if (dir_fd != DEFAULT_DIR_FD) |
| 4113 | result = mkdirat(dir_fd, path.narrow, mode); |
| 4114 | else |
| 4115 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4116 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4117 | result = mkdir(path.narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4118 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4119 | result = mkdir(path.narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4120 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4121 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4122 | if (result < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4123 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4124 | goto exit; |
| 4125 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4126 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4127 | return_value = Py_None; |
| 4128 | Py_INCREF(Py_None); |
| 4129 | exit: |
| 4130 | path_cleanup(&path); |
| 4131 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4132 | } |
| 4133 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4134 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4135 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 4136 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4137 | #include <sys/resource.h> |
| 4138 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4139 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4140 | |
| 4141 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4142 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4143 | "nice(inc) -> new_priority\n\n\ |
| 4144 | 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] | 4145 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4146 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4147 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4148 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4149 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4150 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4151 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 4152 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4153 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4154 | /* There are two flavours of 'nice': one that returns the new |
| 4155 | priority (as required by almost all standards out there) and the |
| 4156 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 4157 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4158 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4159 | If we are of the nice family that returns the new priority, we |
| 4160 | need to clear errno before the call, and check if errno is filled |
| 4161 | before calling posix_error() on a returnvalue of -1, because the |
| 4162 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4163 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4164 | errno = 0; |
| 4165 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4166 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4167 | if (value == 0) |
| 4168 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4169 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4170 | if (value == -1 && errno != 0) |
| 4171 | /* either nice() or getpriority() returned an error */ |
| 4172 | return posix_error(); |
| 4173 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4174 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4175 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4176 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4177 | |
| 4178 | #ifdef HAVE_GETPRIORITY |
| 4179 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 4180 | "getpriority(which, who) -> current_priority\n\n\ |
| 4181 | Get program scheduling priority."); |
| 4182 | |
| 4183 | static PyObject * |
| 4184 | posix_getpriority(PyObject *self, PyObject *args) |
| 4185 | { |
| 4186 | int which, who, retval; |
| 4187 | |
| 4188 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 4189 | return NULL; |
| 4190 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4191 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4192 | if (errno != 0) |
| 4193 | return posix_error(); |
| 4194 | return PyLong_FromLong((long)retval); |
| 4195 | } |
| 4196 | #endif /* HAVE_GETPRIORITY */ |
| 4197 | |
| 4198 | |
| 4199 | #ifdef HAVE_SETPRIORITY |
| 4200 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 4201 | "setpriority(which, who, prio) -> None\n\n\ |
| 4202 | Set program scheduling priority."); |
| 4203 | |
| 4204 | static PyObject * |
| 4205 | posix_setpriority(PyObject *self, PyObject *args) |
| 4206 | { |
| 4207 | int which, who, prio, retval; |
| 4208 | |
| 4209 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 4210 | return NULL; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4211 | retval = setpriority(which, who, prio); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4212 | if (retval == -1) |
| 4213 | return posix_error(); |
| 4214 | Py_RETURN_NONE; |
| 4215 | } |
| 4216 | #endif /* HAVE_SETPRIORITY */ |
| 4217 | |
| 4218 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4219 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4220 | internal_rename(PyObject *args, PyObject *kwargs, int is_replace) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4221 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4222 | char *function_name = is_replace ? "replace" : "rename"; |
| 4223 | path_t src; |
| 4224 | path_t dst; |
| 4225 | int src_dir_fd = DEFAULT_DIR_FD; |
| 4226 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 4227 | int dir_fd_specified; |
| 4228 | PyObject *return_value = NULL; |
| 4229 | char format[24]; |
| 4230 | static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL}; |
| 4231 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4232 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4233 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4234 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4235 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4236 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4237 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4238 | |
| 4239 | memset(&src, 0, sizeof(src)); |
| 4240 | memset(&dst, 0, sizeof(dst)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4241 | src.function_name = function_name; |
| 4242 | dst.function_name = function_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4243 | strcpy(format, "O&O&|$O&O&:"); |
| 4244 | strcat(format, function_name); |
| 4245 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, keywords, |
| 4246 | path_converter, &src, |
| 4247 | path_converter, &dst, |
| 4248 | dir_fd_converter, &src_dir_fd, |
| 4249 | dir_fd_converter, &dst_dir_fd)) |
| 4250 | return NULL; |
| 4251 | |
| 4252 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4253 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4254 | #ifndef HAVE_RENAMEAT |
| 4255 | if (dir_fd_specified) { |
| 4256 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
| 4257 | goto exit; |
| 4258 | } |
| 4259 | #endif |
| 4260 | |
| 4261 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 4262 | PyErr_Format(PyExc_ValueError, |
| 4263 | "%s: src and dst must be the same type", function_name); |
| 4264 | goto exit; |
| 4265 | } |
| 4266 | |
| 4267 | #ifdef MS_WINDOWS |
| 4268 | Py_BEGIN_ALLOW_THREADS |
| 4269 | if (src.wide) |
| 4270 | result = MoveFileExW(src.wide, dst.wide, flags); |
| 4271 | else |
| 4272 | result = MoveFileExA(src.narrow, dst.narrow, flags); |
| 4273 | Py_END_ALLOW_THREADS |
| 4274 | |
| 4275 | if (!result) { |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 4276 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4277 | goto exit; |
| 4278 | } |
| 4279 | |
| 4280 | #else |
| 4281 | Py_BEGIN_ALLOW_THREADS |
| 4282 | #ifdef HAVE_RENAMEAT |
| 4283 | if (dir_fd_specified) |
| 4284 | result = renameat(src_dir_fd, src.narrow, dst_dir_fd, dst.narrow); |
| 4285 | else |
| 4286 | #endif |
| 4287 | result = rename(src.narrow, dst.narrow); |
| 4288 | Py_END_ALLOW_THREADS |
| 4289 | |
| 4290 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4291 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4292 | goto exit; |
| 4293 | } |
| 4294 | #endif |
| 4295 | |
| 4296 | Py_INCREF(Py_None); |
| 4297 | return_value = Py_None; |
| 4298 | exit: |
| 4299 | path_cleanup(&src); |
| 4300 | path_cleanup(&dst); |
| 4301 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4302 | } |
| 4303 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4304 | PyDoc_STRVAR(posix_rename__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4305 | "rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\ |
| 4306 | Rename a file or directory.\n\ |
| 4307 | \n\ |
| 4308 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 4309 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 4310 | should be relative; the path will then be relative to that directory.\n\ |
| 4311 | src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\ |
| 4312 | If they are unavailable, using them will raise a NotImplementedError."); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4313 | |
| 4314 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4315 | posix_rename(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4316 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4317 | return internal_rename(args, kwargs, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4318 | } |
| 4319 | |
| 4320 | PyDoc_STRVAR(posix_replace__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4321 | "replace(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\ |
| 4322 | Rename a file or directory, overwriting the destination.\n\ |
| 4323 | \n\ |
| 4324 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 4325 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 4326 | should be relative; the path will then be relative to that directory.\n\ |
| 4327 | src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\ |
| 4328 | If they are unavailable, using them will raise a NotImplementedError."); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4329 | |
| 4330 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4331 | posix_replace(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4332 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4333 | return internal_rename(args, kwargs, 1); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4334 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4335 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4336 | PyDoc_STRVAR(posix_rmdir__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4337 | "rmdir(path, *, dir_fd=None)\n\n\ |
| 4338 | Remove a directory.\n\ |
| 4339 | \n\ |
| 4340 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4341 | and path should be relative; path will then be relative to that directory.\n\ |
| 4342 | dir_fd may not be implemented on your platform.\n\ |
| 4343 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4344 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4345 | static PyObject * |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4346 | posix_rmdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4347 | { |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4348 | path_t path; |
| 4349 | int dir_fd = DEFAULT_DIR_FD; |
| 4350 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 4351 | int result; |
| 4352 | PyObject *return_value = NULL; |
| 4353 | |
| 4354 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4355 | path.function_name = "rmdir"; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4356 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:rmdir", keywords, |
| 4357 | path_converter, &path, |
| 4358 | #ifdef HAVE_UNLINKAT |
| 4359 | dir_fd_converter, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4360 | #else |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4361 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4362 | #endif |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4363 | )) |
| 4364 | return NULL; |
| 4365 | |
| 4366 | Py_BEGIN_ALLOW_THREADS |
| 4367 | #ifdef MS_WINDOWS |
| 4368 | if (path.wide) |
| 4369 | result = RemoveDirectoryW(path.wide); |
| 4370 | else |
| 4371 | result = RemoveDirectoryA(path.narrow); |
| 4372 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4373 | #else |
| 4374 | #ifdef HAVE_UNLINKAT |
| 4375 | if (dir_fd != DEFAULT_DIR_FD) |
| 4376 | result = unlinkat(dir_fd, path.narrow, AT_REMOVEDIR); |
| 4377 | else |
| 4378 | #endif |
| 4379 | result = rmdir(path.narrow); |
| 4380 | #endif |
| 4381 | Py_END_ALLOW_THREADS |
| 4382 | |
| 4383 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4384 | return_value = path_error(&path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4385 | goto exit; |
| 4386 | } |
| 4387 | |
| 4388 | return_value = Py_None; |
| 4389 | Py_INCREF(Py_None); |
| 4390 | |
| 4391 | exit: |
| 4392 | path_cleanup(&path); |
| 4393 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4394 | } |
| 4395 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4396 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4397 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4398 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4399 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4400 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4401 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4402 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4403 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4404 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4405 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 4406 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4407 | wchar_t *command; |
| 4408 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 4409 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4410 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4411 | Py_BEGIN_ALLOW_THREADS |
| 4412 | sts = _wsystem(command); |
| 4413 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4414 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4415 | PyObject *command_obj; |
| 4416 | char *command; |
| 4417 | if (!PyArg_ParseTuple(args, "O&:system", |
| 4418 | PyUnicode_FSConverter, &command_obj)) |
| 4419 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4420 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4421 | command = PyBytes_AsString(command_obj); |
| 4422 | Py_BEGIN_ALLOW_THREADS |
| 4423 | sts = system(command); |
| 4424 | Py_END_ALLOW_THREADS |
| 4425 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4426 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4427 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4428 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4429 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4430 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4431 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4432 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4433 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4434 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4435 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4436 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4437 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4438 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4439 | int i; |
| 4440 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 4441 | return NULL; |
| 4442 | i = (int)umask(i); |
| 4443 | if (i < 0) |
| 4444 | return posix_error(); |
| 4445 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4446 | } |
| 4447 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4448 | #ifdef MS_WINDOWS |
| 4449 | |
| 4450 | /* override the default DeleteFileW behavior so that directory |
| 4451 | symlinks can be removed with this function, the same as with |
| 4452 | Unix symlinks */ |
| 4453 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4454 | { |
| 4455 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4456 | WIN32_FIND_DATAW find_data; |
| 4457 | HANDLE find_data_handle; |
| 4458 | int is_directory = 0; |
| 4459 | int is_link = 0; |
| 4460 | |
| 4461 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4462 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4463 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4464 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4465 | it is a symlink */ |
| 4466 | if(is_directory && |
| 4467 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4468 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4469 | |
| 4470 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
| 4471 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; |
| 4472 | FindClose(find_data_handle); |
| 4473 | } |
| 4474 | } |
| 4475 | } |
| 4476 | |
| 4477 | if (is_directory && is_link) |
| 4478 | return RemoveDirectoryW(lpFileName); |
| 4479 | |
| 4480 | return DeleteFileW(lpFileName); |
| 4481 | } |
| 4482 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4483 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4484 | PyDoc_STRVAR(posix_unlink__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4485 | "unlink(path, *, dir_fd=None)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4486 | Remove a file (same as remove()).\n\ |
| 4487 | \n\ |
| 4488 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4489 | and path should be relative; path will then be relative to that directory.\n\ |
| 4490 | dir_fd may not be implemented on your platform.\n\ |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4491 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4492 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4493 | PyDoc_STRVAR(posix_remove__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4494 | "remove(path, *, dir_fd=None)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4495 | Remove a file (same as unlink()).\n\ |
| 4496 | \n\ |
| 4497 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4498 | and path should be relative; path will then be relative to that directory.\n\ |
| 4499 | dir_fd may not be implemented on your platform.\n\ |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4500 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4501 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4502 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4503 | posix_unlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4504 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4505 | path_t path; |
| 4506 | int dir_fd = DEFAULT_DIR_FD; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4507 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4508 | int result; |
| 4509 | PyObject *return_value = NULL; |
| 4510 | |
| 4511 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4512 | path.function_name = "unlink"; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4513 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:unlink", keywords, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4514 | path_converter, &path, |
| 4515 | #ifdef HAVE_UNLINKAT |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4516 | dir_fd_converter, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4517 | #else |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4518 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4519 | #endif |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4520 | )) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4521 | return NULL; |
| 4522 | |
| 4523 | Py_BEGIN_ALLOW_THREADS |
| 4524 | #ifdef MS_WINDOWS |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4525 | if (path.wide) |
| 4526 | result = Py_DeleteFileW(path.wide); |
| 4527 | else |
| 4528 | result = DeleteFileA(path.narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4529 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4530 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4531 | #ifdef HAVE_UNLINKAT |
| 4532 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4533 | result = unlinkat(dir_fd, path.narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4534 | else |
| 4535 | #endif /* HAVE_UNLINKAT */ |
| 4536 | result = unlink(path.narrow); |
| 4537 | #endif |
| 4538 | Py_END_ALLOW_THREADS |
| 4539 | |
| 4540 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4541 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4542 | goto exit; |
| 4543 | } |
| 4544 | |
| 4545 | return_value = Py_None; |
| 4546 | Py_INCREF(Py_None); |
| 4547 | |
| 4548 | exit: |
| 4549 | path_cleanup(&path); |
| 4550 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4551 | } |
| 4552 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4553 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4554 | PyDoc_STRVAR(posix_uname__doc__, |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4555 | "uname() -> uname_result\n\n\ |
| 4556 | Return an object identifying the current operating system.\n\ |
| 4557 | The object behaves like a named tuple with the following fields:\n\ |
| 4558 | (sysname, nodename, release, version, machine)"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4559 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4560 | static PyStructSequence_Field uname_result_fields[] = { |
| 4561 | {"sysname", "operating system name"}, |
| 4562 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4563 | {"release", "operating system release"}, |
| 4564 | {"version", "operating system version"}, |
| 4565 | {"machine", "hardware identifier"}, |
| 4566 | {NULL} |
| 4567 | }; |
| 4568 | |
| 4569 | PyDoc_STRVAR(uname_result__doc__, |
| 4570 | "uname_result: Result from os.uname().\n\n\ |
| 4571 | This object may be accessed either as a tuple of\n\ |
| 4572 | (sysname, nodename, release, version, machine),\n\ |
| 4573 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4574 | \n\ |
| 4575 | See os.uname for more information."); |
| 4576 | |
| 4577 | static PyStructSequence_Desc uname_result_desc = { |
| 4578 | "uname_result", /* name */ |
| 4579 | uname_result__doc__, /* doc */ |
| 4580 | uname_result_fields, |
| 4581 | 5 |
| 4582 | }; |
| 4583 | |
| 4584 | static PyTypeObject UnameResultType; |
| 4585 | |
| 4586 | |
| 4587 | #ifdef HAVE_UNAME |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4588 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4589 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4590 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4591 | struct utsname u; |
| 4592 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4593 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4594 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4595 | Py_BEGIN_ALLOW_THREADS |
| 4596 | res = uname(&u); |
| 4597 | Py_END_ALLOW_THREADS |
| 4598 | if (res < 0) |
| 4599 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4600 | |
| 4601 | value = PyStructSequence_New(&UnameResultType); |
| 4602 | if (value == NULL) |
| 4603 | return NULL; |
| 4604 | |
| 4605 | #define SET(i, field) \ |
| 4606 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4607 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4608 | if (!o) { \ |
| 4609 | Py_DECREF(value); \ |
| 4610 | return NULL; \ |
| 4611 | } \ |
| 4612 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4613 | } \ |
| 4614 | |
| 4615 | SET(0, u.sysname); |
| 4616 | SET(1, u.nodename); |
| 4617 | SET(2, u.release); |
| 4618 | SET(3, u.version); |
| 4619 | SET(4, u.machine); |
| 4620 | |
| 4621 | #undef SET |
| 4622 | |
| 4623 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4624 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4625 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4626 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4627 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4628 | PyDoc_STRVAR(posix_utime__doc__, |
| 4629 | "utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True)\n\ |
| 4630 | Set the access and modified time of path.\n\ |
| 4631 | \n\ |
| 4632 | path may always be specified as a string.\n\ |
| 4633 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 4634 | If this functionality is unavailable, using it raises an exception.\n\ |
| 4635 | \n\ |
| 4636 | If times is not None, it must be a tuple (atime, mtime);\n\ |
| 4637 | atime and mtime should be expressed as float seconds since the epoch.\n\ |
| 4638 | If ns is not None, it must be a tuple (atime_ns, mtime_ns);\n\ |
| 4639 | atime_ns and mtime_ns should be expressed as integer nanoseconds\n\ |
| 4640 | since the epoch.\n\ |
| 4641 | If both times and ns are None, utime uses the current time.\n\ |
| 4642 | Specifying tuples for both times and ns is an error.\n\ |
| 4643 | \n\ |
| 4644 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4645 | and path should be relative; path will then be relative to that directory.\n\ |
| 4646 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 4647 | link, utime will modify the symbolic link itself instead of the file the\n\ |
| 4648 | link points to.\n\ |
| 4649 | It is an error to use dir_fd or follow_symlinks when specifying path\n\ |
| 4650 | as an open file descriptor.\n\ |
| 4651 | dir_fd and follow_symlinks may not be available on your platform.\n\ |
| 4652 | If they are unavailable, using them will raise a NotImplementedError."); |
| 4653 | |
| 4654 | typedef struct { |
| 4655 | int now; |
| 4656 | time_t atime_s; |
| 4657 | long atime_ns; |
| 4658 | time_t mtime_s; |
| 4659 | long mtime_ns; |
| 4660 | } utime_t; |
| 4661 | |
| 4662 | /* |
| 4663 | * these macros assume that "utime" is a pointer to a utime_t |
| 4664 | * they also intentionally leak the declaration of a pointer named "time" |
| 4665 | */ |
| 4666 | #define UTIME_TO_TIMESPEC \ |
| 4667 | struct timespec ts[2]; \ |
| 4668 | struct timespec *time; \ |
| 4669 | if (utime->now) \ |
| 4670 | time = NULL; \ |
| 4671 | else { \ |
| 4672 | ts[0].tv_sec = utime->atime_s; \ |
| 4673 | ts[0].tv_nsec = utime->atime_ns; \ |
| 4674 | ts[1].tv_sec = utime->mtime_s; \ |
| 4675 | ts[1].tv_nsec = utime->mtime_ns; \ |
| 4676 | time = ts; \ |
| 4677 | } \ |
| 4678 | |
| 4679 | #define UTIME_TO_TIMEVAL \ |
| 4680 | struct timeval tv[2]; \ |
| 4681 | struct timeval *time; \ |
| 4682 | if (utime->now) \ |
| 4683 | time = NULL; \ |
| 4684 | else { \ |
| 4685 | tv[0].tv_sec = utime->atime_s; \ |
| 4686 | tv[0].tv_usec = utime->atime_ns / 1000; \ |
| 4687 | tv[1].tv_sec = utime->mtime_s; \ |
| 4688 | tv[1].tv_usec = utime->mtime_ns / 1000; \ |
| 4689 | time = tv; \ |
| 4690 | } \ |
| 4691 | |
| 4692 | #define UTIME_TO_UTIMBUF \ |
| 4693 | struct utimbuf u[2]; \ |
| 4694 | struct utimbuf *time; \ |
| 4695 | if (utime->now) \ |
| 4696 | time = NULL; \ |
| 4697 | else { \ |
| 4698 | u.actime = utime->atime_s; \ |
| 4699 | u.modtime = utime->mtime_s; \ |
| 4700 | time = u; \ |
| 4701 | } |
| 4702 | |
| 4703 | #define UTIME_TO_TIME_T \ |
| 4704 | time_t timet[2]; \ |
| 4705 | struct timet time; \ |
| 4706 | if (utime->now) \ |
| 4707 | time = NULL; \ |
| 4708 | else { \ |
| 4709 | timet[0] = utime->atime_s; \ |
| 4710 | timet[1] = utime->mtime_s; \ |
| 4711 | time = &timet; \ |
| 4712 | } \ |
| 4713 | |
| 4714 | |
| 4715 | #define UTIME_HAVE_DIR_FD (defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)) |
| 4716 | |
| 4717 | #if UTIME_HAVE_DIR_FD |
| 4718 | |
| 4719 | static int |
| 4720 | utime_dir_fd(utime_t *utime, int dir_fd, char *path, int follow_symlinks) |
| 4721 | { |
| 4722 | #ifdef HAVE_UTIMENSAT |
| 4723 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4724 | UTIME_TO_TIMESPEC; |
| 4725 | return utimensat(dir_fd, path, time, flags); |
| 4726 | #elif defined(HAVE_FUTIMESAT) |
| 4727 | UTIME_TO_TIMEVAL; |
| 4728 | /* |
| 4729 | * follow_symlinks will never be false here; |
| 4730 | * we only allow !follow_symlinks and dir_fd together |
| 4731 | * if we have utimensat() |
| 4732 | */ |
| 4733 | assert(follow_symlinks); |
| 4734 | return futimesat(dir_fd, path, time); |
| 4735 | #endif |
| 4736 | } |
| 4737 | |
| 4738 | #endif |
| 4739 | |
| 4740 | #define UTIME_HAVE_FD (defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)) |
| 4741 | |
| 4742 | #if UTIME_HAVE_FD |
| 4743 | |
| 4744 | static int |
| 4745 | utime_fd(utime_t *utime, int fd) |
| 4746 | { |
| 4747 | #ifdef HAVE_FUTIMENS |
| 4748 | UTIME_TO_TIMESPEC; |
| 4749 | return futimens(fd, time); |
| 4750 | #else |
| 4751 | UTIME_TO_TIMEVAL; |
| 4752 | return futimes(fd, time); |
| 4753 | #endif |
| 4754 | } |
| 4755 | |
| 4756 | #endif |
| 4757 | |
| 4758 | |
| 4759 | #define UTIME_HAVE_NOFOLLOW_SYMLINKS \ |
| 4760 | (defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)) |
| 4761 | |
| 4762 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4763 | |
| 4764 | static int |
| 4765 | utime_nofollow_symlinks(utime_t *utime, char *path) |
| 4766 | { |
| 4767 | #ifdef HAVE_UTIMENSAT |
| 4768 | UTIME_TO_TIMESPEC; |
| 4769 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4770 | #else |
| 4771 | UTIME_TO_TIMEVAL; |
| 4772 | return lutimes(path, time); |
| 4773 | #endif |
| 4774 | } |
| 4775 | |
| 4776 | #endif |
| 4777 | |
| 4778 | #ifndef MS_WINDOWS |
| 4779 | |
| 4780 | static int |
| 4781 | utime_default(utime_t *utime, char *path) |
| 4782 | { |
| 4783 | #ifdef HAVE_UTIMENSAT |
| 4784 | UTIME_TO_TIMESPEC; |
| 4785 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4786 | #elif defined(HAVE_UTIMES) |
| 4787 | UTIME_TO_TIMEVAL; |
| 4788 | return utimes(path, time); |
| 4789 | #elif defined(HAVE_UTIME_H) |
| 4790 | UTIME_TO_UTIMBUF; |
| 4791 | return utime(path, time); |
| 4792 | #else |
| 4793 | UTIME_TO_TIME_T; |
| 4794 | return utime(path, time); |
| 4795 | #endif |
| 4796 | } |
| 4797 | |
| 4798 | #endif |
| 4799 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4800 | static int |
| 4801 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4802 | { |
| 4803 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4804 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4805 | divmod = PyNumber_Divmod(py_long, billion); |
| 4806 | if (!divmod) |
| 4807 | goto exit; |
| 4808 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4809 | if ((*s == -1) && PyErr_Occurred()) |
| 4810 | goto exit; |
| 4811 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4812 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4813 | goto exit; |
| 4814 | |
| 4815 | result = 1; |
| 4816 | exit: |
| 4817 | Py_XDECREF(divmod); |
| 4818 | return result; |
| 4819 | } |
| 4820 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4821 | static PyObject * |
| 4822 | posix_utime(PyObject *self, PyObject *args, PyObject *kwargs) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4823 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4824 | path_t path; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4825 | PyObject *times = NULL; |
| 4826 | PyObject *ns = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4827 | int dir_fd = DEFAULT_DIR_FD; |
| 4828 | int follow_symlinks = 1; |
| 4829 | char *keywords[] = {"path", "times", "ns", "dir_fd", |
| 4830 | "follow_symlinks", NULL}; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4831 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4832 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4833 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4834 | #ifdef MS_WINDOWS |
| 4835 | HANDLE hFile; |
| 4836 | FILETIME atime, mtime; |
| 4837 | #else |
| 4838 | int result; |
| 4839 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4840 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4841 | PyObject *return_value = NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4842 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4843 | memset(&path, 0, sizeof(path)); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4844 | path.function_name = "utime"; |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4845 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4846 | #if UTIME_HAVE_FD |
| 4847 | path.allow_fd = 1; |
| 4848 | #endif |
| 4849 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 4850 | "O&|O$OO&p:utime", keywords, |
| 4851 | path_converter, &path, |
| 4852 | ×, &ns, |
| 4853 | #if UTIME_HAVE_DIR_FD |
| 4854 | dir_fd_converter, &dir_fd, |
| 4855 | #else |
| 4856 | dir_fd_unavailable, &dir_fd, |
| 4857 | #endif |
| 4858 | &follow_symlinks |
| 4859 | )) |
| 4860 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4861 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4862 | if (times && (times != Py_None) && ns) { |
| 4863 | PyErr_SetString(PyExc_ValueError, |
| 4864 | "utime: you may specify either 'times'" |
| 4865 | " or 'ns' but not both"); |
| 4866 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4867 | } |
| 4868 | |
| 4869 | if (times && (times != Py_None)) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4870 | time_t a_sec, m_sec; |
| 4871 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4872 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4873 | PyErr_SetString(PyExc_TypeError, |
| 4874 | "utime: 'times' must be either" |
| 4875 | " a tuple of two ints or None"); |
| 4876 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4877 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4878 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4879 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4880 | &a_sec, &a_nsec) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4881 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4882 | &m_sec, &m_nsec) == -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4883 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4884 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4885 | utime.atime_s = a_sec; |
| 4886 | utime.atime_ns = a_nsec; |
| 4887 | utime.mtime_s = m_sec; |
| 4888 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4889 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4890 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4891 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4892 | PyErr_SetString(PyExc_TypeError, |
| 4893 | "utime: 'ns' must be a tuple of two ints"); |
| 4894 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4895 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4896 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4897 | 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] | 4898 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4899 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4900 | &utime.mtime_s, &utime.mtime_ns)) { |
| 4901 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4902 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4903 | } |
| 4904 | else { |
| 4905 | /* times and ns are both None/unspecified. use "now". */ |
| 4906 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4907 | } |
| 4908 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4909 | #if !UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4910 | if (follow_symlinks_specified("utime", follow_symlinks)) |
| 4911 | goto exit; |
| 4912 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4913 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4914 | if (path_and_dir_fd_invalid("utime", &path, dir_fd) || |
| 4915 | dir_fd_and_fd_invalid("utime", dir_fd, path.fd) || |
| 4916 | fd_and_follow_symlinks_invalid("utime", path.fd, follow_symlinks)) |
| 4917 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4918 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4919 | #if !defined(HAVE_UTIMENSAT) |
| 4920 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4921 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4922 | "utime: cannot use dir_fd and follow_symlinks " |
| 4923 | "together on this platform"); |
| 4924 | goto exit; |
| 4925 | } |
| 4926 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4927 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4928 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4929 | Py_BEGIN_ALLOW_THREADS |
| 4930 | if (path.wide) |
| 4931 | hFile = CreateFileW(path.wide, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4932 | NULL, OPEN_EXISTING, |
| 4933 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4934 | else |
| 4935 | hFile = CreateFileA(path.narrow, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4936 | NULL, OPEN_EXISTING, |
| 4937 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4938 | Py_END_ALLOW_THREADS |
| 4939 | if (hFile == INVALID_HANDLE_VALUE) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4940 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4941 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4942 | } |
| 4943 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4944 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 4945 | GetSystemTimeAsFileTime(&mtime); |
| 4946 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4947 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4948 | else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4949 | time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4950 | time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4951 | } |
| 4952 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4953 | /* Avoid putting the file name into the error here, |
| 4954 | as that may confuse the user into believing that |
| 4955 | something is wrong with the file, when it also |
| 4956 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4957 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4958 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4959 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4960 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4961 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4962 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4963 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4964 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 4965 | result = utime_nofollow_symlinks(&utime, path.narrow); |
| 4966 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4967 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4968 | |
| 4969 | #if UTIME_HAVE_DIR_FD |
| 4970 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
| 4971 | result = utime_dir_fd(&utime, dir_fd, path.narrow, follow_symlinks); |
| 4972 | else |
| 4973 | #endif |
| 4974 | |
| 4975 | #if UTIME_HAVE_FD |
| 4976 | if (path.fd != -1) |
| 4977 | result = utime_fd(&utime, path.fd); |
| 4978 | else |
| 4979 | #endif |
| 4980 | |
| 4981 | result = utime_default(&utime, path.narrow); |
| 4982 | |
| 4983 | Py_END_ALLOW_THREADS |
| 4984 | |
| 4985 | if (result < 0) { |
| 4986 | /* see previous comment about not putting filename in error here */ |
| 4987 | return_value = posix_error(); |
| 4988 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4989 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4990 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4991 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4992 | |
| 4993 | Py_INCREF(Py_None); |
| 4994 | return_value = Py_None; |
| 4995 | |
| 4996 | exit: |
| 4997 | path_cleanup(&path); |
| 4998 | #ifdef MS_WINDOWS |
| 4999 | if (hFile != INVALID_HANDLE_VALUE) |
| 5000 | CloseHandle(hFile); |
| 5001 | #endif |
| 5002 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5003 | } |
| 5004 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5005 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5006 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5007 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5008 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5009 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5010 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5011 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5012 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5013 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5014 | int sts; |
| 5015 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 5016 | return NULL; |
| 5017 | _exit(sts); |
| 5018 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5019 | } |
| 5020 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5021 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 5022 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 5023 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5024 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5025 | Py_ssize_t i; |
| 5026 | for (i = 0; i < count; i++) |
| 5027 | PyMem_Free(array[i]); |
| 5028 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5029 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5030 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 5031 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5032 | int fsconvert_strdup(PyObject *o, char**out) |
| 5033 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5034 | PyObject *bytes; |
| 5035 | Py_ssize_t size; |
| 5036 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 5037 | return 0; |
| 5038 | size = PyBytes_GET_SIZE(bytes); |
| 5039 | *out = PyMem_Malloc(size+1); |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 5040 | if (!*out) { |
| 5041 | PyErr_NoMemory(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5042 | return 0; |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 5043 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5044 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 5045 | Py_DECREF(bytes); |
| 5046 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5047 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5048 | #endif |
| 5049 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5050 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5051 | static char** |
| 5052 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 5053 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5054 | char **envlist; |
| 5055 | Py_ssize_t i, pos, envc; |
| 5056 | PyObject *keys=NULL, *vals=NULL; |
| 5057 | PyObject *key, *val, *key2, *val2; |
| 5058 | char *p, *k, *v; |
| 5059 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5060 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5061 | i = PyMapping_Size(env); |
| 5062 | if (i < 0) |
| 5063 | return NULL; |
| 5064 | envlist = PyMem_NEW(char *, i + 1); |
| 5065 | if (envlist == NULL) { |
| 5066 | PyErr_NoMemory(); |
| 5067 | return NULL; |
| 5068 | } |
| 5069 | envc = 0; |
| 5070 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5071 | if (!keys) |
| 5072 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5073 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5074 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5075 | goto error; |
| 5076 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 5077 | PyErr_Format(PyExc_TypeError, |
| 5078 | "env.keys() or env.values() is not a list"); |
| 5079 | goto error; |
| 5080 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5081 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5082 | for (pos = 0; pos < i; pos++) { |
| 5083 | key = PyList_GetItem(keys, pos); |
| 5084 | val = PyList_GetItem(vals, pos); |
| 5085 | if (!key || !val) |
| 5086 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5087 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5088 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 5089 | goto error; |
| 5090 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 5091 | Py_DECREF(key2); |
| 5092 | goto error; |
| 5093 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5094 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5095 | k = PyBytes_AsString(key2); |
| 5096 | v = PyBytes_AsString(val2); |
| 5097 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5098 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5099 | p = PyMem_NEW(char, len); |
| 5100 | if (p == NULL) { |
| 5101 | PyErr_NoMemory(); |
| 5102 | Py_DECREF(key2); |
| 5103 | Py_DECREF(val2); |
| 5104 | goto error; |
| 5105 | } |
| 5106 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 5107 | envlist[envc++] = p; |
| 5108 | Py_DECREF(key2); |
| 5109 | Py_DECREF(val2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5110 | } |
| 5111 | Py_DECREF(vals); |
| 5112 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5113 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5114 | envlist[envc] = 0; |
| 5115 | *envc_ptr = envc; |
| 5116 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5117 | |
| 5118 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5119 | Py_XDECREF(keys); |
| 5120 | Py_XDECREF(vals); |
| 5121 | while (--envc >= 0) |
| 5122 | PyMem_DEL(envlist[envc]); |
| 5123 | PyMem_DEL(envlist); |
| 5124 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5125 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5126 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5127 | static char** |
| 5128 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 5129 | { |
| 5130 | int i; |
| 5131 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 5132 | if (argvlist == NULL) { |
| 5133 | PyErr_NoMemory(); |
| 5134 | return NULL; |
| 5135 | } |
| 5136 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5137 | PyObject* item = PySequence_ITEM(argv, i); |
| 5138 | if (item == NULL) |
| 5139 | goto fail; |
| 5140 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 5141 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5142 | goto fail; |
| 5143 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5144 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5145 | } |
| 5146 | argvlist[*argc] = NULL; |
| 5147 | return argvlist; |
| 5148 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5149 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5150 | free_string_array(argvlist, *argc); |
| 5151 | return NULL; |
| 5152 | } |
| 5153 | #endif |
| 5154 | |
| 5155 | #ifdef HAVE_EXECV |
| 5156 | PyDoc_STRVAR(posix_execv__doc__, |
| 5157 | "execv(path, args)\n\n\ |
| 5158 | Execute an executable path with arguments, replacing current process.\n\ |
| 5159 | \n\ |
| 5160 | path: path of executable file\n\ |
| 5161 | args: tuple or list of strings"); |
| 5162 | |
| 5163 | static PyObject * |
| 5164 | posix_execv(PyObject *self, PyObject *args) |
| 5165 | { |
| 5166 | PyObject *opath; |
| 5167 | char *path; |
| 5168 | PyObject *argv; |
| 5169 | char **argvlist; |
| 5170 | Py_ssize_t argc; |
| 5171 | |
| 5172 | /* execv has two arguments: (path, argv), where |
| 5173 | argv is a list or tuple of strings. */ |
| 5174 | |
| 5175 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 5176 | PyUnicode_FSConverter, |
| 5177 | &opath, &argv)) |
| 5178 | return NULL; |
| 5179 | path = PyBytes_AsString(opath); |
| 5180 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 5181 | PyErr_SetString(PyExc_TypeError, |
| 5182 | "execv() arg 2 must be a tuple or list"); |
| 5183 | Py_DECREF(opath); |
| 5184 | return NULL; |
| 5185 | } |
| 5186 | argc = PySequence_Size(argv); |
| 5187 | if (argc < 1) { |
| 5188 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 5189 | Py_DECREF(opath); |
| 5190 | return NULL; |
| 5191 | } |
| 5192 | |
| 5193 | argvlist = parse_arglist(argv, &argc); |
| 5194 | if (argvlist == NULL) { |
| 5195 | Py_DECREF(opath); |
| 5196 | return NULL; |
| 5197 | } |
| 5198 | |
| 5199 | execv(path, argvlist); |
| 5200 | |
| 5201 | /* If we get here it's definitely an error */ |
| 5202 | |
| 5203 | free_string_array(argvlist, argc); |
| 5204 | Py_DECREF(opath); |
| 5205 | return posix_error(); |
| 5206 | } |
| 5207 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5208 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5209 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5210 | Execute a path with arguments and environment, replacing current process.\n\ |
| 5211 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5212 | path: path of executable file\n\ |
| 5213 | args: tuple or list of arguments\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5214 | env: dictionary of strings mapping to strings\n\ |
| 5215 | \n\ |
| 5216 | On some platforms, you may specify an open file descriptor for path;\n\ |
| 5217 | execve will execute the program the file descriptor is open to.\n\ |
| 5218 | If this functionality is unavailable, using it raises NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5219 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5220 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5221 | posix_execve(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5222 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5223 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5224 | PyObject *argv, *env; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5225 | char **argvlist = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5226 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5227 | Py_ssize_t argc, envc; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5228 | static char *keywords[] = {"path", "argv", "environment", NULL}; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5229 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5230 | /* execve has three arguments: (path, argv, env), where |
| 5231 | argv is a list or tuple of strings and env is a dictionary |
| 5232 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5233 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5234 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 5235 | path.function_name = "execve"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5236 | #ifdef HAVE_FEXECVE |
| 5237 | path.allow_fd = 1; |
| 5238 | #endif |
| 5239 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&OO:execve", keywords, |
| 5240 | path_converter, &path, |
| 5241 | &argv, &env |
| 5242 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5243 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5244 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5245 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5246 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5247 | "execve: argv must be a tuple or list"); |
| 5248 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5249 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5250 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5251 | if (!PyMapping_Check(env)) { |
| 5252 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5253 | "execve: environment must be a mapping object"); |
| 5254 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5255 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5256 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5257 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5258 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5259 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5260 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5261 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5262 | envlist = parse_envlist(env, &envc); |
| 5263 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5264 | goto fail; |
| 5265 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5266 | #ifdef HAVE_FEXECVE |
| 5267 | if (path.fd > -1) |
| 5268 | fexecve(path.fd, argvlist, envlist); |
| 5269 | else |
| 5270 | #endif |
| 5271 | execve(path.narrow, argvlist, envlist); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5272 | |
| 5273 | /* If we get here it's definitely an error */ |
| 5274 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 5275 | path_error(&path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5276 | |
| 5277 | while (--envc >= 0) |
| 5278 | PyMem_DEL(envlist[envc]); |
| 5279 | PyMem_DEL(envlist); |
| 5280 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5281 | if (argvlist) |
| 5282 | free_string_array(argvlist, argc); |
| 5283 | path_cleanup(&path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5284 | return NULL; |
| 5285 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5286 | #endif /* HAVE_EXECV */ |
| 5287 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5288 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5289 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5290 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5291 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5292 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5293 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5294 | mode: mode of process creation\n\ |
| 5295 | path: path of executable file\n\ |
| 5296 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5297 | |
| 5298 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5299 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5300 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5301 | PyObject *opath; |
| 5302 | char *path; |
| 5303 | PyObject *argv; |
| 5304 | char **argvlist; |
| 5305 | int mode, i; |
| 5306 | Py_ssize_t argc; |
| 5307 | Py_intptr_t spawnval; |
| 5308 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5309 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5310 | /* spawnv has three arguments: (mode, path, argv), where |
| 5311 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5312 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5313 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 5314 | PyUnicode_FSConverter, |
| 5315 | &opath, &argv)) |
| 5316 | return NULL; |
| 5317 | path = PyBytes_AsString(opath); |
| 5318 | if (PyList_Check(argv)) { |
| 5319 | argc = PyList_Size(argv); |
| 5320 | getitem = PyList_GetItem; |
| 5321 | } |
| 5322 | else if (PyTuple_Check(argv)) { |
| 5323 | argc = PyTuple_Size(argv); |
| 5324 | getitem = PyTuple_GetItem; |
| 5325 | } |
| 5326 | else { |
| 5327 | PyErr_SetString(PyExc_TypeError, |
| 5328 | "spawnv() arg 2 must be a tuple or list"); |
| 5329 | Py_DECREF(opath); |
| 5330 | return NULL; |
| 5331 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5332 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5333 | argvlist = PyMem_NEW(char *, argc+1); |
| 5334 | if (argvlist == NULL) { |
| 5335 | Py_DECREF(opath); |
| 5336 | return PyErr_NoMemory(); |
| 5337 | } |
| 5338 | for (i = 0; i < argc; i++) { |
| 5339 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5340 | &argvlist[i])) { |
| 5341 | free_string_array(argvlist, i); |
| 5342 | PyErr_SetString( |
| 5343 | PyExc_TypeError, |
| 5344 | "spawnv() arg 2 must contain only strings"); |
| 5345 | Py_DECREF(opath); |
| 5346 | return NULL; |
| 5347 | } |
| 5348 | } |
| 5349 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5350 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5351 | if (mode == _OLD_P_OVERLAY) |
| 5352 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5353 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5354 | Py_BEGIN_ALLOW_THREADS |
| 5355 | spawnval = _spawnv(mode, path, argvlist); |
| 5356 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5357 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5358 | free_string_array(argvlist, argc); |
| 5359 | Py_DECREF(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5360 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5361 | if (spawnval == -1) |
| 5362 | return posix_error(); |
| 5363 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5364 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5365 | } |
| 5366 | |
| 5367 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5368 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5369 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5370 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5371 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5372 | mode: mode of process creation\n\ |
| 5373 | path: path of executable file\n\ |
| 5374 | args: tuple or list of arguments\n\ |
| 5375 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5376 | |
| 5377 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5378 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5379 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5380 | PyObject *opath; |
| 5381 | char *path; |
| 5382 | PyObject *argv, *env; |
| 5383 | char **argvlist; |
| 5384 | char **envlist; |
| 5385 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5386 | int mode; |
| 5387 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5388 | Py_intptr_t spawnval; |
| 5389 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 5390 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5391 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5392 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5393 | argv is a list or tuple of strings and env is a dictionary |
| 5394 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5395 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5396 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 5397 | PyUnicode_FSConverter, |
| 5398 | &opath, &argv, &env)) |
| 5399 | return NULL; |
| 5400 | path = PyBytes_AsString(opath); |
| 5401 | if (PyList_Check(argv)) { |
| 5402 | argc = PyList_Size(argv); |
| 5403 | getitem = PyList_GetItem; |
| 5404 | } |
| 5405 | else if (PyTuple_Check(argv)) { |
| 5406 | argc = PyTuple_Size(argv); |
| 5407 | getitem = PyTuple_GetItem; |
| 5408 | } |
| 5409 | else { |
| 5410 | PyErr_SetString(PyExc_TypeError, |
| 5411 | "spawnve() arg 2 must be a tuple or list"); |
| 5412 | goto fail_0; |
| 5413 | } |
| 5414 | if (!PyMapping_Check(env)) { |
| 5415 | PyErr_SetString(PyExc_TypeError, |
| 5416 | "spawnve() arg 3 must be a mapping object"); |
| 5417 | goto fail_0; |
| 5418 | } |
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 | argvlist = PyMem_NEW(char *, argc+1); |
| 5421 | if (argvlist == NULL) { |
| 5422 | PyErr_NoMemory(); |
| 5423 | goto fail_0; |
| 5424 | } |
| 5425 | for (i = 0; i < argc; i++) { |
| 5426 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5427 | &argvlist[i])) |
| 5428 | { |
| 5429 | lastarg = i; |
| 5430 | goto fail_1; |
| 5431 | } |
| 5432 | } |
| 5433 | lastarg = argc; |
| 5434 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5435 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5436 | envlist = parse_envlist(env, &envc); |
| 5437 | if (envlist == NULL) |
| 5438 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5439 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5440 | if (mode == _OLD_P_OVERLAY) |
| 5441 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5442 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5443 | Py_BEGIN_ALLOW_THREADS |
| 5444 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 5445 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5446 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5447 | if (spawnval == -1) |
| 5448 | (void) posix_error(); |
| 5449 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5450 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5451 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5452 | while (--envc >= 0) |
| 5453 | PyMem_DEL(envlist[envc]); |
| 5454 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5455 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5456 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5457 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5458 | Py_DECREF(opath); |
| 5459 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5460 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5461 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5462 | #endif /* HAVE_SPAWNV */ |
| 5463 | |
| 5464 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5465 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5466 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5467 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5468 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 5469 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5470 | 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] | 5471 | |
| 5472 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5473 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5474 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5475 | pid_t pid; |
| 5476 | int result = 0; |
| 5477 | _PyImport_AcquireLock(); |
| 5478 | pid = fork1(); |
| 5479 | if (pid == 0) { |
| 5480 | /* child: this clobbers and resets the import lock. */ |
| 5481 | PyOS_AfterFork(); |
| 5482 | } else { |
| 5483 | /* parent: release the import lock. */ |
| 5484 | result = _PyImport_ReleaseLock(); |
| 5485 | } |
| 5486 | if (pid == -1) |
| 5487 | return posix_error(); |
| 5488 | if (result < 0) { |
| 5489 | /* Don't clobber the OSError if the fork failed. */ |
| 5490 | PyErr_SetString(PyExc_RuntimeError, |
| 5491 | "not holding the import lock"); |
| 5492 | return NULL; |
| 5493 | } |
| 5494 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5495 | } |
| 5496 | #endif |
| 5497 | |
| 5498 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5499 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5500 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5501 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5502 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5503 | 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] | 5504 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5505 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5506 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5507 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5508 | pid_t pid; |
| 5509 | int result = 0; |
| 5510 | _PyImport_AcquireLock(); |
| 5511 | pid = fork(); |
| 5512 | if (pid == 0) { |
| 5513 | /* child: this clobbers and resets the import lock. */ |
| 5514 | PyOS_AfterFork(); |
| 5515 | } else { |
| 5516 | /* parent: release the import lock. */ |
| 5517 | result = _PyImport_ReleaseLock(); |
| 5518 | } |
| 5519 | if (pid == -1) |
| 5520 | return posix_error(); |
| 5521 | if (result < 0) { |
| 5522 | /* Don't clobber the OSError if the fork failed. */ |
| 5523 | PyErr_SetString(PyExc_RuntimeError, |
| 5524 | "not holding the import lock"); |
| 5525 | return NULL; |
| 5526 | } |
| 5527 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5528 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5529 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5530 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5531 | #ifdef HAVE_SCHED_H |
| 5532 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5533 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
| 5534 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5535 | PyDoc_STRVAR(posix_sched_get_priority_max__doc__, |
| 5536 | "sched_get_priority_max(policy)\n\n\ |
| 5537 | Get the maximum scheduling priority for *policy*."); |
| 5538 | |
| 5539 | static PyObject * |
| 5540 | posix_sched_get_priority_max(PyObject *self, PyObject *args) |
| 5541 | { |
| 5542 | int policy, max; |
| 5543 | |
| 5544 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy)) |
| 5545 | return NULL; |
| 5546 | max = sched_get_priority_max(policy); |
| 5547 | if (max < 0) |
| 5548 | return posix_error(); |
| 5549 | return PyLong_FromLong(max); |
| 5550 | } |
| 5551 | |
| 5552 | PyDoc_STRVAR(posix_sched_get_priority_min__doc__, |
| 5553 | "sched_get_priority_min(policy)\n\n\ |
| 5554 | Get the minimum scheduling priority for *policy*."); |
| 5555 | |
| 5556 | static PyObject * |
| 5557 | posix_sched_get_priority_min(PyObject *self, PyObject *args) |
| 5558 | { |
| 5559 | int policy, min; |
| 5560 | |
| 5561 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy)) |
| 5562 | return NULL; |
| 5563 | min = sched_get_priority_min(policy); |
| 5564 | if (min < 0) |
| 5565 | return posix_error(); |
| 5566 | return PyLong_FromLong(min); |
| 5567 | } |
| 5568 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5569 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 5570 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5571 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5572 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5573 | PyDoc_STRVAR(posix_sched_getscheduler__doc__, |
| 5574 | "sched_getscheduler(pid)\n\n\ |
| 5575 | Get the scheduling policy for the process with a PID of *pid*.\n\ |
| 5576 | Passing a PID of 0 returns the scheduling policy for the calling process."); |
| 5577 | |
| 5578 | static PyObject * |
| 5579 | posix_sched_getscheduler(PyObject *self, PyObject *args) |
| 5580 | { |
| 5581 | pid_t pid; |
| 5582 | int policy; |
| 5583 | |
| 5584 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid)) |
| 5585 | return NULL; |
| 5586 | policy = sched_getscheduler(pid); |
| 5587 | if (policy < 0) |
| 5588 | return posix_error(); |
| 5589 | return PyLong_FromLong(policy); |
| 5590 | } |
| 5591 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5592 | #endif |
| 5593 | |
| 5594 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
| 5595 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5596 | static PyObject * |
| 5597 | sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 5598 | { |
| 5599 | PyObject *res, *priority; |
Benjamin Peterson | 4e36d5a | 2011-08-02 19:56:11 -0500 | [diff] [blame] | 5600 | static char *kwlist[] = {"sched_priority", NULL}; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5601 | |
| 5602 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority)) |
| 5603 | return NULL; |
| 5604 | res = PyStructSequence_New(type); |
| 5605 | if (!res) |
| 5606 | return NULL; |
| 5607 | Py_INCREF(priority); |
| 5608 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5609 | return res; |
| 5610 | } |
| 5611 | |
| 5612 | PyDoc_STRVAR(sched_param__doc__, |
| 5613 | "sched_param(sched_priority): A scheduling parameter.\n\n\ |
| 5614 | Current has only one field: sched_priority"); |
| 5615 | |
| 5616 | static PyStructSequence_Field sched_param_fields[] = { |
| 5617 | {"sched_priority", "the scheduling priority"}, |
| 5618 | {0} |
| 5619 | }; |
| 5620 | |
| 5621 | static PyStructSequence_Desc sched_param_desc = { |
| 5622 | "sched_param", /* name */ |
| 5623 | sched_param__doc__, /* doc */ |
| 5624 | sched_param_fields, |
| 5625 | 1 |
| 5626 | }; |
| 5627 | |
| 5628 | static int |
| 5629 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 5630 | { |
| 5631 | long priority; |
| 5632 | |
| 5633 | if (Py_TYPE(param) != &SchedParamType) { |
| 5634 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 5635 | return 0; |
| 5636 | } |
| 5637 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 5638 | if (priority == -1 && PyErr_Occurred()) |
| 5639 | return 0; |
| 5640 | if (priority > INT_MAX || priority < INT_MIN) { |
| 5641 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 5642 | return 0; |
| 5643 | } |
| 5644 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 5645 | return 1; |
| 5646 | } |
| 5647 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5648 | #endif |
| 5649 | |
| 5650 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5651 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5652 | PyDoc_STRVAR(posix_sched_setscheduler__doc__, |
| 5653 | "sched_setscheduler(pid, policy, param)\n\n\ |
| 5654 | Set the scheduling policy, *policy*, for *pid*.\n\ |
| 5655 | If *pid* is 0, the calling process is changed.\n\ |
| 5656 | *param* is an instance of sched_param."); |
| 5657 | |
| 5658 | static PyObject * |
| 5659 | posix_sched_setscheduler(PyObject *self, PyObject *args) |
| 5660 | { |
| 5661 | pid_t pid; |
| 5662 | int policy; |
| 5663 | struct sched_param param; |
| 5664 | |
| 5665 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler", |
| 5666 | &pid, &policy, &convert_sched_param, ¶m)) |
| 5667 | return NULL; |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5668 | |
| 5669 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 5670 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 5671 | ** scheduling policy under Solaris/Illumos, and others. |
| 5672 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5673 | */ |
| 5674 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5675 | return posix_error(); |
| 5676 | Py_RETURN_NONE; |
| 5677 | } |
| 5678 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5679 | #endif |
| 5680 | |
| 5681 | #ifdef HAVE_SCHED_SETPARAM |
| 5682 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5683 | PyDoc_STRVAR(posix_sched_getparam__doc__, |
| 5684 | "sched_getparam(pid) -> sched_param\n\n\ |
| 5685 | Returns scheduling parameters for the process with *pid* as an instance of the\n\ |
| 5686 | sched_param class. A PID of 0 means the calling process."); |
| 5687 | |
| 5688 | static PyObject * |
| 5689 | posix_sched_getparam(PyObject *self, PyObject *args) |
| 5690 | { |
| 5691 | pid_t pid; |
| 5692 | struct sched_param param; |
| 5693 | PyObject *res, *priority; |
| 5694 | |
| 5695 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid)) |
| 5696 | return NULL; |
| 5697 | if (sched_getparam(pid, ¶m)) |
| 5698 | return posix_error(); |
| 5699 | res = PyStructSequence_New(&SchedParamType); |
| 5700 | if (!res) |
| 5701 | return NULL; |
| 5702 | priority = PyLong_FromLong(param.sched_priority); |
| 5703 | if (!priority) { |
| 5704 | Py_DECREF(res); |
| 5705 | return NULL; |
| 5706 | } |
| 5707 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5708 | return res; |
| 5709 | } |
| 5710 | |
| 5711 | PyDoc_STRVAR(posix_sched_setparam__doc__, |
| 5712 | "sched_setparam(pid, param)\n\n\ |
| 5713 | Set scheduling parameters for a process with PID *pid*.\n\ |
| 5714 | A PID of 0 means the calling process."); |
| 5715 | |
| 5716 | static PyObject * |
| 5717 | posix_sched_setparam(PyObject *self, PyObject *args) |
| 5718 | { |
| 5719 | pid_t pid; |
| 5720 | struct sched_param param; |
| 5721 | |
| 5722 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam", |
| 5723 | &pid, &convert_sched_param, ¶m)) |
| 5724 | return NULL; |
| 5725 | if (sched_setparam(pid, ¶m)) |
| 5726 | return posix_error(); |
| 5727 | Py_RETURN_NONE; |
| 5728 | } |
| 5729 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5730 | #endif |
| 5731 | |
| 5732 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
| 5733 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5734 | PyDoc_STRVAR(posix_sched_rr_get_interval__doc__, |
| 5735 | "sched_rr_get_interval(pid) -> float\n\n\ |
| 5736 | Return the round-robin quantum for the process with PID *pid* in seconds."); |
| 5737 | |
| 5738 | static PyObject * |
| 5739 | posix_sched_rr_get_interval(PyObject *self, PyObject *args) |
| 5740 | { |
| 5741 | pid_t pid; |
| 5742 | struct timespec interval; |
| 5743 | |
| 5744 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid)) |
| 5745 | return NULL; |
| 5746 | if (sched_rr_get_interval(pid, &interval)) |
| 5747 | return posix_error(); |
| 5748 | return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec); |
| 5749 | } |
| 5750 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5751 | #endif |
| 5752 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5753 | PyDoc_STRVAR(posix_sched_yield__doc__, |
| 5754 | "sched_yield()\n\n\ |
| 5755 | Voluntarily relinquish the CPU."); |
| 5756 | |
| 5757 | static PyObject * |
| 5758 | posix_sched_yield(PyObject *self, PyObject *noargs) |
| 5759 | { |
| 5760 | if (sched_yield()) |
| 5761 | return posix_error(); |
| 5762 | Py_RETURN_NONE; |
| 5763 | } |
| 5764 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5765 | #ifdef HAVE_SCHED_SETAFFINITY |
| 5766 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5767 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 5768 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5769 | |
| 5770 | PyDoc_STRVAR(posix_sched_setaffinity__doc__, |
| 5771 | "sched_setaffinity(pid, cpu_set)\n\n\ |
| 5772 | Set the affinity of the process with PID *pid* to *cpu_set*."); |
| 5773 | |
| 5774 | static PyObject * |
| 5775 | posix_sched_setaffinity(PyObject *self, PyObject *args) |
| 5776 | { |
| 5777 | pid_t pid; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5778 | int ncpus; |
| 5779 | size_t setsize; |
| 5780 | cpu_set_t *mask = NULL; |
| 5781 | PyObject *iterable, *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5782 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5783 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O:sched_setaffinity", |
| 5784 | &pid, &iterable)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5785 | return NULL; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5786 | |
| 5787 | iterator = PyObject_GetIter(iterable); |
| 5788 | if (iterator == NULL) |
| 5789 | return NULL; |
| 5790 | |
| 5791 | ncpus = NCPUS_START; |
| 5792 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5793 | mask = CPU_ALLOC(ncpus); |
| 5794 | if (mask == NULL) { |
| 5795 | PyErr_NoMemory(); |
| 5796 | goto error; |
| 5797 | } |
| 5798 | CPU_ZERO_S(setsize, mask); |
| 5799 | |
| 5800 | while ((item = PyIter_Next(iterator))) { |
| 5801 | long cpu; |
| 5802 | if (!PyLong_Check(item)) { |
| 5803 | PyErr_Format(PyExc_TypeError, |
| 5804 | "expected an iterator of ints, " |
| 5805 | "but iterator yielded %R", |
| 5806 | Py_TYPE(item)); |
| 5807 | Py_DECREF(item); |
| 5808 | goto error; |
| 5809 | } |
| 5810 | cpu = PyLong_AsLong(item); |
| 5811 | Py_DECREF(item); |
| 5812 | if (cpu < 0) { |
| 5813 | if (!PyErr_Occurred()) |
| 5814 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 5815 | goto error; |
| 5816 | } |
| 5817 | if (cpu > INT_MAX - 1) { |
| 5818 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 5819 | goto error; |
| 5820 | } |
| 5821 | if (cpu >= ncpus) { |
| 5822 | /* Grow CPU mask to fit the CPU number */ |
| 5823 | int newncpus = ncpus; |
| 5824 | cpu_set_t *newmask; |
| 5825 | size_t newsetsize; |
| 5826 | while (newncpus <= cpu) { |
| 5827 | if (newncpus > INT_MAX / 2) |
| 5828 | newncpus = cpu + 1; |
| 5829 | else |
| 5830 | newncpus = newncpus * 2; |
| 5831 | } |
| 5832 | newmask = CPU_ALLOC(newncpus); |
| 5833 | if (newmask == NULL) { |
| 5834 | PyErr_NoMemory(); |
| 5835 | goto error; |
| 5836 | } |
| 5837 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 5838 | CPU_ZERO_S(newsetsize, newmask); |
| 5839 | memcpy(newmask, mask, setsize); |
| 5840 | CPU_FREE(mask); |
| 5841 | setsize = newsetsize; |
| 5842 | mask = newmask; |
| 5843 | ncpus = newncpus; |
| 5844 | } |
| 5845 | CPU_SET_S(cpu, setsize, mask); |
| 5846 | } |
| 5847 | Py_CLEAR(iterator); |
| 5848 | |
| 5849 | if (sched_setaffinity(pid, setsize, mask)) { |
| 5850 | posix_error(); |
| 5851 | goto error; |
| 5852 | } |
| 5853 | CPU_FREE(mask); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5854 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5855 | |
| 5856 | error: |
| 5857 | if (mask) |
| 5858 | CPU_FREE(mask); |
| 5859 | Py_XDECREF(iterator); |
| 5860 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5861 | } |
| 5862 | |
| 5863 | PyDoc_STRVAR(posix_sched_getaffinity__doc__, |
| 5864 | "sched_getaffinity(pid, ncpus) -> cpu_set\n\n\ |
| 5865 | Return the affinity of the process with PID *pid*.\n\ |
| 5866 | The returned cpu_set will be of size *ncpus*."); |
| 5867 | |
| 5868 | static PyObject * |
| 5869 | posix_sched_getaffinity(PyObject *self, PyObject *args) |
| 5870 | { |
| 5871 | pid_t pid; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5872 | int cpu, ncpus, count; |
| 5873 | size_t setsize; |
| 5874 | cpu_set_t *mask = NULL; |
| 5875 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5876 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5877 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getaffinity", |
| 5878 | &pid)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5879 | return NULL; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5880 | |
| 5881 | ncpus = NCPUS_START; |
| 5882 | while (1) { |
| 5883 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5884 | mask = CPU_ALLOC(ncpus); |
| 5885 | if (mask == NULL) |
| 5886 | return PyErr_NoMemory(); |
| 5887 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 5888 | break; |
| 5889 | CPU_FREE(mask); |
| 5890 | if (errno != EINVAL) |
| 5891 | return posix_error(); |
| 5892 | if (ncpus > INT_MAX / 2) { |
| 5893 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 5894 | "a large enough CPU set"); |
| 5895 | return NULL; |
| 5896 | } |
| 5897 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5898 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5899 | |
| 5900 | res = PySet_New(NULL); |
| 5901 | if (res == NULL) |
| 5902 | goto error; |
| 5903 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 5904 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 5905 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 5906 | --count; |
| 5907 | if (cpu_num == NULL) |
| 5908 | goto error; |
| 5909 | if (PySet_Add(res, cpu_num)) { |
| 5910 | Py_DECREF(cpu_num); |
| 5911 | goto error; |
| 5912 | } |
| 5913 | Py_DECREF(cpu_num); |
| 5914 | } |
| 5915 | } |
| 5916 | CPU_FREE(mask); |
| 5917 | return res; |
| 5918 | |
| 5919 | error: |
| 5920 | if (mask) |
| 5921 | CPU_FREE(mask); |
| 5922 | Py_XDECREF(res); |
| 5923 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5924 | } |
| 5925 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5926 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5927 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5928 | #endif /* HAVE_SCHED_H */ |
| 5929 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5930 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5931 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5932 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5933 | #define DEV_PTY_FILE "/dev/ptc" |
| 5934 | #define HAVE_DEV_PTMX |
| 5935 | #else |
| 5936 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5937 | #endif |
| 5938 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5939 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5940 | #ifdef HAVE_PTY_H |
| 5941 | #include <pty.h> |
| 5942 | #else |
| 5943 | #ifdef HAVE_LIBUTIL_H |
| 5944 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5945 | #else |
| 5946 | #ifdef HAVE_UTIL_H |
| 5947 | #include <util.h> |
| 5948 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5949 | #endif /* HAVE_LIBUTIL_H */ |
| 5950 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5951 | #ifdef HAVE_STROPTS_H |
| 5952 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5953 | #endif |
| 5954 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5955 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5956 | #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] | 5957 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5958 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5959 | 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] | 5960 | |
| 5961 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5962 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5963 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5964 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5965 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5966 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5967 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5968 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5969 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5970 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5971 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5972 | #endif |
| 5973 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5974 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5975 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5976 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5977 | goto posix_error; |
| 5978 | |
| 5979 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5980 | goto error; |
| 5981 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 5982 | goto error; |
| 5983 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5984 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5985 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5986 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5987 | goto posix_error; |
| 5988 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5989 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5990 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5991 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5992 | if (slave_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5993 | goto posix_error; |
| 5994 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5995 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 5996 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5997 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5998 | goto posix_error; |
| 5999 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6000 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6001 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6002 | /* change permission of slave */ |
| 6003 | if (grantpt(master_fd) < 0) { |
| 6004 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6005 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6006 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6007 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6008 | /* unlock slave */ |
| 6009 | if (unlockpt(master_fd) < 0) { |
| 6010 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6011 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6012 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6013 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6014 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6015 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6016 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 6017 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6018 | goto posix_error; |
| 6019 | |
| 6020 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6021 | if (slave_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6022 | goto posix_error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6023 | |
| 6024 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6025 | goto posix_error; |
| 6026 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6027 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6028 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 6029 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6030 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6031 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6032 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6033 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 6034 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6035 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6036 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6037 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6038 | posix_error: |
| 6039 | posix_error(); |
Victor Stinner | b9981ba | 2013-08-28 01:51:06 +0200 | [diff] [blame] | 6040 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6041 | error: |
Victor Stinner | b9981ba | 2013-08-28 01:51:06 +0200 | [diff] [blame] | 6042 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6043 | if (master_fd != -1) |
| 6044 | close(master_fd); |
| 6045 | if (slave_fd != -1) |
| 6046 | close(slave_fd); |
| 6047 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6048 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6049 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6050 | |
| 6051 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6052 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6053 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6054 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 6055 | 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] | 6056 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6057 | |
| 6058 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6059 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6060 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6061 | int master_fd = -1, result = 0; |
| 6062 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6063 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6064 | _PyImport_AcquireLock(); |
| 6065 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 6066 | if (pid == 0) { |
| 6067 | /* child: this clobbers and resets the import lock. */ |
| 6068 | PyOS_AfterFork(); |
| 6069 | } else { |
| 6070 | /* parent: release the import lock. */ |
| 6071 | result = _PyImport_ReleaseLock(); |
| 6072 | } |
| 6073 | if (pid == -1) |
| 6074 | return posix_error(); |
| 6075 | if (result < 0) { |
| 6076 | /* Don't clobber the OSError if the fork failed. */ |
| 6077 | PyErr_SetString(PyExc_RuntimeError, |
| 6078 | "not holding the import lock"); |
| 6079 | return NULL; |
| 6080 | } |
| 6081 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6082 | } |
| 6083 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6084 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6085 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6086 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6087 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6088 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6089 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6090 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6091 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6092 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6093 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6094 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6095 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6096 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6097 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6098 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6099 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6100 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6101 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6102 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6103 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6104 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6105 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6106 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6107 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6108 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6109 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6110 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6111 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6112 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6113 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6114 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6115 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6116 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6117 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6118 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6119 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6120 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6121 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6122 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6123 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6124 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6125 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6126 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6127 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6128 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6129 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6130 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6131 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6132 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6133 | } |
| 6134 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6135 | #ifdef HAVE_GETGROUPLIST |
| 6136 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 6137 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 6138 | Returns a list of groups to which a user belongs.\n\n\ |
| 6139 | user: username to lookup\n\ |
| 6140 | group: base group id of the user"); |
| 6141 | |
| 6142 | static PyObject * |
| 6143 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 6144 | { |
| 6145 | #ifdef NGROUPS_MAX |
| 6146 | #define MAX_GROUPS NGROUPS_MAX |
| 6147 | #else |
| 6148 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6149 | #define MAX_GROUPS 64 |
| 6150 | #endif |
| 6151 | |
| 6152 | const char *user; |
| 6153 | int i, ngroups; |
| 6154 | PyObject *list; |
| 6155 | #ifdef __APPLE__ |
| 6156 | int *groups, basegid; |
| 6157 | #else |
| 6158 | gid_t *groups, basegid; |
| 6159 | #endif |
| 6160 | ngroups = MAX_GROUPS; |
| 6161 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6162 | #ifdef __APPLE__ |
| 6163 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6164 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6165 | #else |
| 6166 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 6167 | _Py_Gid_Converter, &basegid)) |
| 6168 | return NULL; |
| 6169 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6170 | |
| 6171 | #ifdef __APPLE__ |
| 6172 | groups = PyMem_Malloc(ngroups * sizeof(int)); |
| 6173 | #else |
| 6174 | groups = PyMem_Malloc(ngroups * sizeof(gid_t)); |
| 6175 | #endif |
| 6176 | if (groups == NULL) |
| 6177 | return PyErr_NoMemory(); |
| 6178 | |
| 6179 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 6180 | PyMem_Del(groups); |
| 6181 | return posix_error(); |
| 6182 | } |
| 6183 | |
| 6184 | list = PyList_New(ngroups); |
| 6185 | if (list == NULL) { |
| 6186 | PyMem_Del(groups); |
| 6187 | return NULL; |
| 6188 | } |
| 6189 | |
| 6190 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6191 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6192 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6193 | #else |
| 6194 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 6195 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6196 | if (o == NULL) { |
| 6197 | Py_DECREF(list); |
| 6198 | PyMem_Del(groups); |
| 6199 | return NULL; |
| 6200 | } |
| 6201 | PyList_SET_ITEM(list, i, o); |
| 6202 | } |
| 6203 | |
| 6204 | PyMem_Del(groups); |
| 6205 | |
| 6206 | return list; |
| 6207 | } |
| 6208 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6209 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6210 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6211 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6212 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6213 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6214 | |
| 6215 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6216 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6217 | { |
| 6218 | PyObject *result = NULL; |
| 6219 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6220 | #ifdef NGROUPS_MAX |
| 6221 | #define MAX_GROUPS NGROUPS_MAX |
| 6222 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6223 | /* 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] | 6224 | #define MAX_GROUPS 64 |
| 6225 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6226 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6227 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6228 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6229 | * This is a helper variable to store the intermediate result when |
| 6230 | * that happens. |
| 6231 | * |
| 6232 | * To keep the code readable the OSX behaviour is unconditional, |
| 6233 | * according to the POSIX spec this should be safe on all unix-y |
| 6234 | * systems. |
| 6235 | */ |
| 6236 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6237 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6238 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6239 | #ifdef __APPLE__ |
| 6240 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 6241 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 6242 | * always first call getgroups with length 0 to get the actual number |
| 6243 | * of groups. |
| 6244 | */ |
| 6245 | n = getgroups(0, NULL); |
| 6246 | if (n < 0) { |
| 6247 | return posix_error(); |
| 6248 | } else if (n <= MAX_GROUPS) { |
| 6249 | /* groups will fit in existing array */ |
| 6250 | alt_grouplist = grouplist; |
| 6251 | } else { |
| 6252 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 6253 | if (alt_grouplist == NULL) { |
| 6254 | errno = EINVAL; |
| 6255 | return posix_error(); |
| 6256 | } |
| 6257 | } |
| 6258 | |
| 6259 | n = getgroups(n, alt_grouplist); |
| 6260 | if (n == -1) { |
| 6261 | if (alt_grouplist != grouplist) { |
| 6262 | PyMem_Free(alt_grouplist); |
| 6263 | } |
| 6264 | return posix_error(); |
| 6265 | } |
| 6266 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6267 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6268 | if (n < 0) { |
| 6269 | if (errno == EINVAL) { |
| 6270 | n = getgroups(0, NULL); |
| 6271 | if (n == -1) { |
| 6272 | return posix_error(); |
| 6273 | } |
| 6274 | if (n == 0) { |
| 6275 | /* Avoid malloc(0) */ |
| 6276 | alt_grouplist = grouplist; |
| 6277 | } else { |
| 6278 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 6279 | if (alt_grouplist == NULL) { |
| 6280 | errno = EINVAL; |
| 6281 | return posix_error(); |
| 6282 | } |
| 6283 | n = getgroups(n, alt_grouplist); |
| 6284 | if (n == -1) { |
| 6285 | PyMem_Free(alt_grouplist); |
| 6286 | return posix_error(); |
| 6287 | } |
| 6288 | } |
| 6289 | } else { |
| 6290 | return posix_error(); |
| 6291 | } |
| 6292 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6293 | #endif |
| 6294 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6295 | result = PyList_New(n); |
| 6296 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6297 | int i; |
| 6298 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6299 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6300 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6301 | Py_DECREF(result); |
| 6302 | result = NULL; |
| 6303 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6304 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6305 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6306 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6307 | } |
| 6308 | |
| 6309 | if (alt_grouplist != grouplist) { |
| 6310 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6311 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6312 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6313 | return result; |
| 6314 | } |
| 6315 | #endif |
| 6316 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6317 | #ifdef HAVE_INITGROUPS |
| 6318 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 6319 | "initgroups(username, gid) -> None\n\n\ |
| 6320 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 6321 | the groups of which the specified username is a member, plus the specified\n\ |
| 6322 | group id."); |
| 6323 | |
| 6324 | static PyObject * |
| 6325 | posix_initgroups(PyObject *self, PyObject *args) |
| 6326 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6327 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6328 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6329 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6330 | #ifdef __APPLE__ |
| 6331 | int gid; |
| 6332 | #else |
| 6333 | gid_t gid; |
| 6334 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6335 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6336 | #ifdef __APPLE__ |
| 6337 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 6338 | PyUnicode_FSConverter, &oname, |
| 6339 | &gid)) |
| 6340 | #else |
| 6341 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 6342 | PyUnicode_FSConverter, &oname, |
| 6343 | _Py_Gid_Converter, &gid)) |
| 6344 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6345 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6346 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6347 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6348 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6349 | Py_DECREF(oname); |
| 6350 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6351 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6352 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6353 | Py_INCREF(Py_None); |
| 6354 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6355 | } |
| 6356 | #endif |
| 6357 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6358 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 6359 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6360 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 6361 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6362 | |
| 6363 | static PyObject * |
| 6364 | posix_getpgid(PyObject *self, PyObject *args) |
| 6365 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6366 | pid_t pid, pgid; |
| 6367 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 6368 | return NULL; |
| 6369 | pgid = getpgid(pid); |
| 6370 | if (pgid < 0) |
| 6371 | return posix_error(); |
| 6372 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6373 | } |
| 6374 | #endif /* HAVE_GETPGID */ |
| 6375 | |
| 6376 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6377 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6378 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6379 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6380 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6381 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6382 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6383 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6384 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6385 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6386 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6387 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6388 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6389 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6390 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6391 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6392 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6393 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6394 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6395 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6396 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 6397 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6398 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6399 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6400 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6401 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6402 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6403 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6404 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6405 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6406 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6407 | return posix_error(); |
| 6408 | Py_INCREF(Py_None); |
| 6409 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6410 | } |
| 6411 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6412 | #endif /* HAVE_SETPGRP */ |
| 6413 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6414 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6415 | |
| 6416 | #ifdef MS_WINDOWS |
| 6417 | #include <tlhelp32.h> |
| 6418 | |
| 6419 | static PyObject* |
| 6420 | win32_getppid() |
| 6421 | { |
| 6422 | HANDLE snapshot; |
| 6423 | pid_t mypid; |
| 6424 | PyObject* result = NULL; |
| 6425 | BOOL have_record; |
| 6426 | PROCESSENTRY32 pe; |
| 6427 | |
| 6428 | mypid = getpid(); /* This function never fails */ |
| 6429 | |
| 6430 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 6431 | if (snapshot == INVALID_HANDLE_VALUE) |
| 6432 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 6433 | |
| 6434 | pe.dwSize = sizeof(pe); |
| 6435 | have_record = Process32First(snapshot, &pe); |
| 6436 | while (have_record) { |
| 6437 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 6438 | /* We could cache the ulong value in a static variable. */ |
| 6439 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 6440 | break; |
| 6441 | } |
| 6442 | |
| 6443 | have_record = Process32Next(snapshot, &pe); |
| 6444 | } |
| 6445 | |
| 6446 | /* If our loop exits and our pid was not found (result will be NULL) |
| 6447 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 6448 | * error anyway, so let's raise it. */ |
| 6449 | if (!result) |
| 6450 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6451 | |
| 6452 | CloseHandle(snapshot); |
| 6453 | |
| 6454 | return result; |
| 6455 | } |
| 6456 | #endif /*MS_WINDOWS*/ |
| 6457 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6458 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6459 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6460 | Return the parent's process id. If the parent process has already exited,\n\ |
| 6461 | Windows machines will still return its id; others systems will return the id\n\ |
| 6462 | of the 'init' process (1)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6463 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6464 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6465 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6466 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6467 | #ifdef MS_WINDOWS |
| 6468 | return win32_getppid(); |
| 6469 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6470 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6471 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6472 | } |
| 6473 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6474 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6475 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6476 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6477 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6478 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6479 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6480 | |
| 6481 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6482 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6483 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6484 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6485 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6486 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6487 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6488 | |
| 6489 | if (GetUserNameW(user_name, &num_chars)) { |
| 6490 | /* num_chars is the number of unicode chars plus null terminator */ |
| 6491 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6492 | } |
| 6493 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6494 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6495 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6496 | char *name; |
| 6497 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6498 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6499 | errno = 0; |
| 6500 | name = getlogin(); |
| 6501 | if (name == NULL) { |
| 6502 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6503 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6504 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6505 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6506 | } |
| 6507 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6508 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6509 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6510 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6511 | return result; |
| 6512 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6513 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6514 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6515 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6516 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6517 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6518 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6519 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6520 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6521 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6522 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6523 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6524 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6525 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6526 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6527 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6528 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6529 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6530 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6531 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6532 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6533 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6534 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6535 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6536 | pid_t pid; |
| 6537 | int sig; |
| 6538 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 6539 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6540 | if (kill(pid, sig) == -1) |
| 6541 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6542 | Py_INCREF(Py_None); |
| 6543 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6544 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6545 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6546 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6547 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6548 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6549 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6550 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6551 | |
| 6552 | static PyObject * |
| 6553 | posix_killpg(PyObject *self, PyObject *args) |
| 6554 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6555 | int sig; |
| 6556 | pid_t pgid; |
| 6557 | /* XXX some man pages make the `pgid` parameter an int, others |
| 6558 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 6559 | take the same type. Moreover, pid_t is always at least as wide as |
| 6560 | int (else compilation of this module fails), which is safe. */ |
| 6561 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 6562 | return NULL; |
| 6563 | if (killpg(pgid, sig) == -1) |
| 6564 | return posix_error(); |
| 6565 | Py_INCREF(Py_None); |
| 6566 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6567 | } |
| 6568 | #endif |
| 6569 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6570 | #ifdef MS_WINDOWS |
| 6571 | PyDoc_STRVAR(win32_kill__doc__, |
| 6572 | "kill(pid, sig)\n\n\ |
| 6573 | Kill a process with a signal."); |
| 6574 | |
| 6575 | static PyObject * |
| 6576 | win32_kill(PyObject *self, PyObject *args) |
| 6577 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 6578 | PyObject *result; |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6579 | pid_t pid; |
| 6580 | DWORD sig, err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6581 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6582 | |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6583 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "k:kill", &pid, &sig)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6584 | return NULL; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6585 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6586 | /* Console processes which share a common console can be sent CTRL+C or |
| 6587 | CTRL+BREAK events, provided they handle said events. */ |
| 6588 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6589 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6590 | err = GetLastError(); |
| 6591 | PyErr_SetFromWindowsErr(err); |
| 6592 | } |
| 6593 | else |
| 6594 | Py_RETURN_NONE; |
| 6595 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6596 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6597 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 6598 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6599 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6600 | if (handle == NULL) { |
| 6601 | err = GetLastError(); |
| 6602 | return PyErr_SetFromWindowsErr(err); |
| 6603 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6604 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6605 | if (TerminateProcess(handle, sig) == 0) { |
| 6606 | err = GetLastError(); |
| 6607 | result = PyErr_SetFromWindowsErr(err); |
| 6608 | } else { |
| 6609 | Py_INCREF(Py_None); |
| 6610 | result = Py_None; |
| 6611 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6612 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6613 | CloseHandle(handle); |
| 6614 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6615 | } |
| 6616 | #endif /* MS_WINDOWS */ |
| 6617 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6618 | #ifdef HAVE_PLOCK |
| 6619 | |
| 6620 | #ifdef HAVE_SYS_LOCK_H |
| 6621 | #include <sys/lock.h> |
| 6622 | #endif |
| 6623 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6624 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6625 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6626 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6627 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6628 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6629 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6630 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6631 | int op; |
| 6632 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 6633 | return NULL; |
| 6634 | if (plock(op) == -1) |
| 6635 | return posix_error(); |
| 6636 | Py_INCREF(Py_None); |
| 6637 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6638 | } |
| 6639 | #endif |
| 6640 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6641 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6642 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6643 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6644 | Set the current process's user id."); |
| 6645 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6646 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6647 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6648 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6649 | uid_t uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6650 | if (!PyArg_ParseTuple(args, "O&:setuid", _Py_Uid_Converter, &uid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6651 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6652 | if (setuid(uid) < 0) |
| 6653 | return posix_error(); |
| 6654 | Py_INCREF(Py_None); |
| 6655 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6656 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6657 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6658 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6659 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6660 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6661 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6662 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6663 | Set the current process's effective user id."); |
| 6664 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6665 | static PyObject * |
| 6666 | posix_seteuid (PyObject *self, PyObject *args) |
| 6667 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6668 | uid_t euid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6669 | if (!PyArg_ParseTuple(args, "O&:seteuid", _Py_Uid_Converter, &euid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6670 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6671 | if (seteuid(euid) < 0) { |
| 6672 | return posix_error(); |
| 6673 | } else { |
| 6674 | Py_INCREF(Py_None); |
| 6675 | return Py_None; |
| 6676 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6677 | } |
| 6678 | #endif /* HAVE_SETEUID */ |
| 6679 | |
| 6680 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6681 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6682 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6683 | Set the current process's effective group id."); |
| 6684 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6685 | static PyObject * |
| 6686 | posix_setegid (PyObject *self, PyObject *args) |
| 6687 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6688 | gid_t egid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6689 | if (!PyArg_ParseTuple(args, "O&:setegid", _Py_Gid_Converter, &egid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6690 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6691 | if (setegid(egid) < 0) { |
| 6692 | return posix_error(); |
| 6693 | } else { |
| 6694 | Py_INCREF(Py_None); |
| 6695 | return Py_None; |
| 6696 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6697 | } |
| 6698 | #endif /* HAVE_SETEGID */ |
| 6699 | |
| 6700 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6701 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6702 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6703 | Set the current process's real and effective user ids."); |
| 6704 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6705 | static PyObject * |
| 6706 | posix_setreuid (PyObject *self, PyObject *args) |
| 6707 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6708 | uid_t ruid, euid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6709 | if (!PyArg_ParseTuple(args, "O&O&:setreuid", |
| 6710 | _Py_Uid_Converter, &ruid, |
| 6711 | _Py_Uid_Converter, &euid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6712 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6713 | if (setreuid(ruid, euid) < 0) { |
| 6714 | return posix_error(); |
| 6715 | } else { |
| 6716 | Py_INCREF(Py_None); |
| 6717 | return Py_None; |
| 6718 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6719 | } |
| 6720 | #endif /* HAVE_SETREUID */ |
| 6721 | |
| 6722 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6723 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6724 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6725 | Set the current process's real and effective group ids."); |
| 6726 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6727 | static PyObject * |
| 6728 | posix_setregid (PyObject *self, PyObject *args) |
| 6729 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6730 | gid_t rgid, egid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6731 | if (!PyArg_ParseTuple(args, "O&O&:setregid", |
| 6732 | _Py_Gid_Converter, &rgid, |
| 6733 | _Py_Gid_Converter, &egid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6734 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6735 | if (setregid(rgid, egid) < 0) { |
| 6736 | return posix_error(); |
| 6737 | } else { |
| 6738 | Py_INCREF(Py_None); |
| 6739 | return Py_None; |
| 6740 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6741 | } |
| 6742 | #endif /* HAVE_SETREGID */ |
| 6743 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6744 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6745 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6746 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6747 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6748 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6749 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6750 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6751 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6752 | gid_t gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6753 | if (!PyArg_ParseTuple(args, "O&:setgid", _Py_Gid_Converter, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6754 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6755 | if (setgid(gid) < 0) |
| 6756 | return posix_error(); |
| 6757 | Py_INCREF(Py_None); |
| 6758 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6759 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6760 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6761 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6762 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6763 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6764 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6765 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6766 | |
| 6767 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6768 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6769 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6770 | int i, len; |
| 6771 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6772 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6773 | if (!PySequence_Check(groups)) { |
| 6774 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6775 | return NULL; |
| 6776 | } |
| 6777 | len = PySequence_Size(groups); |
| 6778 | if (len > MAX_GROUPS) { |
| 6779 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6780 | return NULL; |
| 6781 | } |
| 6782 | for(i = 0; i < len; i++) { |
| 6783 | PyObject *elem; |
| 6784 | elem = PySequence_GetItem(groups, i); |
| 6785 | if (!elem) |
| 6786 | return NULL; |
| 6787 | if (!PyLong_Check(elem)) { |
| 6788 | PyErr_SetString(PyExc_TypeError, |
| 6789 | "groups must be integers"); |
| 6790 | Py_DECREF(elem); |
| 6791 | return NULL; |
| 6792 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6793 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6794 | Py_DECREF(elem); |
| 6795 | return NULL; |
| 6796 | } |
| 6797 | } |
| 6798 | Py_DECREF(elem); |
| 6799 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6800 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6801 | if (setgroups(len, grouplist) < 0) |
| 6802 | return posix_error(); |
| 6803 | Py_INCREF(Py_None); |
| 6804 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6805 | } |
| 6806 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6807 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6808 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6809 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6810 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6811 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6812 | PyObject *result; |
| 6813 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6814 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6815 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6816 | if (pid == -1) |
| 6817 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6818 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6819 | if (struct_rusage == NULL) { |
| 6820 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6821 | if (m == NULL) |
| 6822 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6823 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6824 | Py_DECREF(m); |
| 6825 | if (struct_rusage == NULL) |
| 6826 | return NULL; |
| 6827 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6828 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6829 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6830 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6831 | if (!result) |
| 6832 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6833 | |
| 6834 | #ifndef doubletime |
| 6835 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6836 | #endif |
| 6837 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6838 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6839 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6840 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6841 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6842 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6843 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6844 | SET_INT(result, 2, ru->ru_maxrss); |
| 6845 | SET_INT(result, 3, ru->ru_ixrss); |
| 6846 | SET_INT(result, 4, ru->ru_idrss); |
| 6847 | SET_INT(result, 5, ru->ru_isrss); |
| 6848 | SET_INT(result, 6, ru->ru_minflt); |
| 6849 | SET_INT(result, 7, ru->ru_majflt); |
| 6850 | SET_INT(result, 8, ru->ru_nswap); |
| 6851 | SET_INT(result, 9, ru->ru_inblock); |
| 6852 | SET_INT(result, 10, ru->ru_oublock); |
| 6853 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6854 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6855 | SET_INT(result, 13, ru->ru_nsignals); |
| 6856 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6857 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6858 | #undef SET_INT |
| 6859 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6860 | if (PyErr_Occurred()) { |
| 6861 | Py_DECREF(result); |
| 6862 | return NULL; |
| 6863 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6864 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6865 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6866 | } |
| 6867 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6868 | |
| 6869 | #ifdef HAVE_WAIT3 |
| 6870 | PyDoc_STRVAR(posix_wait3__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6871 | "wait3(options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6872 | Wait for completion of a child process."); |
| 6873 | |
| 6874 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6875 | posix_wait3(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6876 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6877 | pid_t pid; |
| 6878 | int options; |
| 6879 | struct rusage ru; |
| 6880 | WAIT_TYPE status; |
| 6881 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6882 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6883 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6884 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6885 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6886 | Py_BEGIN_ALLOW_THREADS |
| 6887 | pid = wait3(&status, options, &ru); |
| 6888 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6889 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6890 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6891 | } |
| 6892 | #endif /* HAVE_WAIT3 */ |
| 6893 | |
| 6894 | #ifdef HAVE_WAIT4 |
| 6895 | PyDoc_STRVAR(posix_wait4__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6896 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6897 | Wait for completion of a given child process."); |
| 6898 | |
| 6899 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6900 | posix_wait4(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6901 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6902 | pid_t pid; |
| 6903 | int options; |
| 6904 | struct rusage ru; |
| 6905 | WAIT_TYPE status; |
| 6906 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6907 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6908 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6909 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6910 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6911 | Py_BEGIN_ALLOW_THREADS |
| 6912 | pid = wait4(pid, &status, options, &ru); |
| 6913 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6914 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6915 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6916 | } |
| 6917 | #endif /* HAVE_WAIT4 */ |
| 6918 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6919 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 6920 | PyDoc_STRVAR(posix_waitid__doc__, |
| 6921 | "waitid(idtype, id, options) -> waitid_result\n\n\ |
| 6922 | Wait for the completion of one or more child processes.\n\n\ |
| 6923 | idtype can be P_PID, P_PGID or P_ALL.\n\ |
| 6924 | id specifies the pid to wait on.\n\ |
| 6925 | options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\ |
| 6926 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\ |
| 6927 | Returns either waitid_result or None if WNOHANG is specified and there are\n\ |
| 6928 | no children in a waitable state."); |
| 6929 | |
| 6930 | static PyObject * |
| 6931 | posix_waitid(PyObject *self, PyObject *args) |
| 6932 | { |
| 6933 | PyObject *result; |
| 6934 | idtype_t idtype; |
| 6935 | id_t id; |
| 6936 | int options, res; |
| 6937 | siginfo_t si; |
| 6938 | si.si_pid = 0; |
| 6939 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options)) |
| 6940 | return NULL; |
| 6941 | Py_BEGIN_ALLOW_THREADS |
| 6942 | res = waitid(idtype, id, &si, options); |
| 6943 | Py_END_ALLOW_THREADS |
| 6944 | if (res == -1) |
| 6945 | return posix_error(); |
| 6946 | |
| 6947 | if (si.si_pid == 0) |
| 6948 | Py_RETURN_NONE; |
| 6949 | |
| 6950 | result = PyStructSequence_New(&WaitidResultType); |
| 6951 | if (!result) |
| 6952 | return NULL; |
| 6953 | |
| 6954 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6955 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6956 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6957 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6958 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6959 | if (PyErr_Occurred()) { |
| 6960 | Py_DECREF(result); |
| 6961 | return NULL; |
| 6962 | } |
| 6963 | |
| 6964 | return result; |
| 6965 | } |
| 6966 | #endif |
| 6967 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6968 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6969 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6970 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6971 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6972 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6973 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6974 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6975 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6976 | pid_t pid; |
| 6977 | int options; |
| 6978 | WAIT_TYPE status; |
| 6979 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6980 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6981 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6982 | return NULL; |
| 6983 | Py_BEGIN_ALLOW_THREADS |
| 6984 | pid = waitpid(pid, &status, options); |
| 6985 | Py_END_ALLOW_THREADS |
| 6986 | if (pid == -1) |
| 6987 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6988 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6989 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6990 | } |
| 6991 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6992 | #elif defined(HAVE_CWAIT) |
| 6993 | |
| 6994 | /* 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] | 6995 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6996 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6997 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6998 | |
| 6999 | static PyObject * |
| 7000 | posix_waitpid(PyObject *self, PyObject *args) |
| 7001 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7002 | Py_intptr_t pid; |
| 7003 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7004 | |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7005 | if (!PyArg_ParseTuple(args, _Py_PARSE_INTPTR "i:waitpid", &pid, &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7006 | return NULL; |
| 7007 | Py_BEGIN_ALLOW_THREADS |
| 7008 | pid = _cwait(&status, pid, 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 | /* 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] | 7014 | return Py_BuildValue(_Py_PARSE_INTPTR "i", pid, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7015 | } |
| 7016 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7017 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7018 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7019 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7020 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7021 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7022 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7023 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7024 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7025 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7026 | pid_t pid; |
| 7027 | WAIT_TYPE status; |
| 7028 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7029 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7030 | Py_BEGIN_ALLOW_THREADS |
| 7031 | pid = wait(&status); |
| 7032 | Py_END_ALLOW_THREADS |
| 7033 | if (pid == -1) |
| 7034 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7035 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7036 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7037 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7038 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7039 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7040 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7041 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
| 7042 | PyDoc_STRVAR(readlink__doc__, |
| 7043 | "readlink(path, *, dir_fd=None) -> path\n\n\ |
| 7044 | Return a string representing the path to which the symbolic link points.\n\ |
| 7045 | \n\ |
| 7046 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7047 | and path should be relative; path will then be relative to that directory.\n\ |
| 7048 | dir_fd may not be implemented on your platform.\n\ |
| 7049 | If it is unavailable, using it will raise a NotImplementedError."); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7050 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7051 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7052 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7053 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7054 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7055 | posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7056 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7057 | path_t path; |
| 7058 | int dir_fd = DEFAULT_DIR_FD; |
| 7059 | char buffer[MAXPATHLEN]; |
| 7060 | ssize_t length; |
| 7061 | PyObject *return_value = NULL; |
| 7062 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7063 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7064 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7065 | path.function_name = "readlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7066 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords, |
| 7067 | path_converter, &path, |
| 7068 | #ifdef HAVE_READLINKAT |
| 7069 | dir_fd_converter, &dir_fd |
| 7070 | #else |
| 7071 | dir_fd_unavailable, &dir_fd |
| 7072 | #endif |
| 7073 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7074 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7075 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7076 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7077 | #ifdef HAVE_READLINKAT |
| 7078 | if (dir_fd != DEFAULT_DIR_FD) |
| 7079 | length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer)); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 7080 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7081 | #endif |
| 7082 | length = readlink(path.narrow, buffer, sizeof(buffer)); |
| 7083 | Py_END_ALLOW_THREADS |
| 7084 | |
| 7085 | if (length < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7086 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7087 | goto exit; |
| 7088 | } |
| 7089 | |
| 7090 | if (PyUnicode_Check(path.object)) |
| 7091 | return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 7092 | else |
| 7093 | return_value = PyBytes_FromStringAndSize(buffer, length); |
| 7094 | exit: |
| 7095 | path_cleanup(&path); |
| 7096 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7097 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7098 | |
| 7099 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7100 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7101 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7102 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7103 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7104 | PyDoc_STRVAR(posix_symlink__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7105 | "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 7106 | Create a symbolic link pointing to src named dst.\n\n\ |
| 7107 | target_is_directory is required on Windows if the target is to be\n\ |
| 7108 | interpreted as a directory. (On Windows, symlink requires\n\ |
| 7109 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.)\n\ |
| 7110 | target_is_directory is ignored on non-Windows platforms.\n\ |
| 7111 | \n\ |
| 7112 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7113 | and path should be relative; path will then be relative to that directory.\n\ |
| 7114 | dir_fd may not be implemented on your platform.\n\ |
| 7115 | If it is unavailable, using it will raise a NotImplementedError."); |
| 7116 | |
| 7117 | #if defined(MS_WINDOWS) |
| 7118 | |
| 7119 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 7120 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD) = NULL; |
| 7121 | static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPSTR, LPSTR, DWORD) = NULL; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7122 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7123 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7124 | check_CreateSymbolicLink(void) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7125 | { |
| 7126 | HINSTANCE hKernel32; |
| 7127 | /* only recheck */ |
| 7128 | if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA) |
| 7129 | return 1; |
| 7130 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 7131 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 7132 | "CreateSymbolicLinkW"); |
| 7133 | *(FARPROC*)&Py_CreateSymbolicLinkA = GetProcAddress(hKernel32, |
| 7134 | "CreateSymbolicLinkA"); |
| 7135 | return (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA); |
| 7136 | } |
| 7137 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7138 | /* Remove the last portion of the path */ |
| 7139 | static void |
| 7140 | _dirnameW(WCHAR *path) |
| 7141 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7142 | WCHAR *ptr; |
| 7143 | |
| 7144 | /* walk the path from the end until a backslash is encountered */ |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7145 | for(ptr = path + wcslen(path); ptr != path; ptr--) { |
Victor Stinner | 072318b | 2013-06-05 02:07:46 +0200 | [diff] [blame] | 7146 | if (*ptr == L'\\' || *ptr == L'/') |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7147 | break; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7148 | } |
| 7149 | *ptr = 0; |
| 7150 | } |
| 7151 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7152 | /* Remove the last portion of the path */ |
| 7153 | static void |
| 7154 | _dirnameA(char *path) |
| 7155 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7156 | char *ptr; |
| 7157 | |
| 7158 | /* walk the path from the end until a backslash is encountered */ |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7159 | for(ptr = path + strlen(path); ptr != path; ptr--) { |
| 7160 | if (*ptr == '\\' || *ptr == '/') |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7161 | break; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7162 | } |
| 7163 | *ptr = 0; |
| 7164 | } |
| 7165 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7166 | /* Is this path absolute? */ |
| 7167 | static int |
| 7168 | _is_absW(const WCHAR *path) |
| 7169 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7170 | return path[0] == L'\\' || path[0] == L'/' || path[1] == L':'; |
| 7171 | |
| 7172 | } |
| 7173 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7174 | /* Is this path absolute? */ |
| 7175 | static int |
| 7176 | _is_absA(const char *path) |
| 7177 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7178 | return path[0] == '\\' || path[0] == '/' || path[1] == ':'; |
| 7179 | |
| 7180 | } |
| 7181 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7182 | /* join root and rest with a backslash */ |
| 7183 | static void |
| 7184 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 7185 | { |
Victor Stinner | e7e7eba | 2013-06-05 00:35:54 +0200 | [diff] [blame] | 7186 | size_t root_len; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7187 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7188 | if (_is_absW(rest)) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7189 | wcscpy(dest_path, rest); |
| 7190 | return; |
| 7191 | } |
| 7192 | |
| 7193 | root_len = wcslen(root); |
| 7194 | |
| 7195 | wcscpy(dest_path, root); |
| 7196 | if(root_len) { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7197 | dest_path[root_len] = L'\\'; |
| 7198 | root_len++; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7199 | } |
| 7200 | wcscpy(dest_path+root_len, rest); |
| 7201 | } |
| 7202 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7203 | /* join root and rest with a backslash */ |
| 7204 | static void |
| 7205 | _joinA(char *dest_path, const char *root, const char *rest) |
| 7206 | { |
Victor Stinner | e7e7eba | 2013-06-05 00:35:54 +0200 | [diff] [blame] | 7207 | size_t root_len; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7208 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7209 | if (_is_absA(rest)) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7210 | strcpy(dest_path, rest); |
| 7211 | return; |
| 7212 | } |
| 7213 | |
| 7214 | root_len = strlen(root); |
| 7215 | |
| 7216 | strcpy(dest_path, root); |
| 7217 | if(root_len) { |
| 7218 | dest_path[root_len] = '\\'; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7219 | root_len++; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7220 | } |
| 7221 | strcpy(dest_path+root_len, rest); |
| 7222 | } |
| 7223 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7224 | /* Return True if the path at src relative to dest is a directory */ |
| 7225 | static int |
| 7226 | _check_dirW(WCHAR *src, WCHAR *dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7227 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7228 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7229 | WCHAR dest_parent[MAX_PATH]; |
| 7230 | WCHAR src_resolved[MAX_PATH] = L""; |
| 7231 | |
| 7232 | /* dest_parent = os.path.dirname(dest) */ |
| 7233 | wcscpy(dest_parent, dest); |
| 7234 | _dirnameW(dest_parent); |
| 7235 | /* src_resolved = os.path.join(dest_parent, src) */ |
| 7236 | _joinW(src_resolved, dest_parent, src); |
| 7237 | return ( |
| 7238 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 7239 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7240 | ); |
| 7241 | } |
| 7242 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7243 | /* Return True if the path at src relative to dest is a directory */ |
| 7244 | static int |
| 7245 | _check_dirA(char *src, char *dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7246 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7247 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7248 | char dest_parent[MAX_PATH]; |
| 7249 | char src_resolved[MAX_PATH] = ""; |
| 7250 | |
| 7251 | /* dest_parent = os.path.dirname(dest) */ |
| 7252 | strcpy(dest_parent, dest); |
Victor Stinner | 5a43676 | 2013-06-05 00:37:12 +0200 | [diff] [blame] | 7253 | _dirnameA(dest_parent); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7254 | /* src_resolved = os.path.join(dest_parent, src) */ |
Victor Stinner | 5a43676 | 2013-06-05 00:37:12 +0200 | [diff] [blame] | 7255 | _joinA(src_resolved, dest_parent, src); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7256 | return ( |
| 7257 | GetFileAttributesExA(src_resolved, GetFileExInfoStandard, &src_info) |
| 7258 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7259 | ); |
| 7260 | } |
| 7261 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7262 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7263 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7264 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7265 | posix_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7266 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7267 | path_t src; |
| 7268 | path_t dst; |
| 7269 | int dir_fd = DEFAULT_DIR_FD; |
| 7270 | int target_is_directory = 0; |
| 7271 | static char *keywords[] = {"src", "dst", "target_is_directory", |
| 7272 | "dir_fd", NULL}; |
| 7273 | PyObject *return_value; |
| 7274 | #ifdef MS_WINDOWS |
| 7275 | DWORD result; |
| 7276 | #else |
| 7277 | int result; |
| 7278 | #endif |
| 7279 | |
| 7280 | memset(&src, 0, sizeof(src)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7281 | src.function_name = "symlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7282 | src.argument_name = "src"; |
| 7283 | memset(&dst, 0, sizeof(dst)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7284 | dst.function_name = "symlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7285 | dst.argument_name = "dst"; |
| 7286 | |
| 7287 | #ifdef MS_WINDOWS |
| 7288 | if (!check_CreateSymbolicLink()) { |
| 7289 | PyErr_SetString(PyExc_NotImplementedError, |
| 7290 | "CreateSymbolicLink functions not found"); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7291 | return NULL; |
| 7292 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7293 | if (!win32_can_symlink) { |
| 7294 | PyErr_SetString(PyExc_OSError, "symbolic link privilege not held"); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7295 | return NULL; |
| 7296 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7297 | #endif |
| 7298 | |
| 7299 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|i$O&:symlink", |
| 7300 | keywords, |
| 7301 | path_converter, &src, |
| 7302 | path_converter, &dst, |
| 7303 | &target_is_directory, |
| 7304 | #ifdef HAVE_SYMLINKAT |
| 7305 | dir_fd_converter, &dir_fd |
| 7306 | #else |
| 7307 | dir_fd_unavailable, &dir_fd |
| 7308 | #endif |
| 7309 | )) |
| 7310 | return NULL; |
| 7311 | |
| 7312 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 7313 | PyErr_SetString(PyExc_ValueError, |
| 7314 | "symlink: src and dst must be the same type"); |
| 7315 | return_value = NULL; |
| 7316 | goto exit; |
| 7317 | } |
| 7318 | |
| 7319 | #ifdef MS_WINDOWS |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7320 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7321 | Py_BEGIN_ALLOW_THREADS |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7322 | if (dst.wide) { |
| 7323 | /* if src is a directory, ensure target_is_directory==1 */ |
| 7324 | target_is_directory |= _check_dirW(src.wide, dst.wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7325 | result = Py_CreateSymbolicLinkW(dst.wide, src.wide, |
| 7326 | target_is_directory); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7327 | } |
| 7328 | else { |
| 7329 | /* if src is a directory, ensure target_is_directory==1 */ |
| 7330 | target_is_directory |= _check_dirA(src.narrow, dst.narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7331 | result = Py_CreateSymbolicLinkA(dst.narrow, src.narrow, |
| 7332 | target_is_directory); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7333 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7334 | Py_END_ALLOW_THREADS |
| 7335 | |
| 7336 | if (!result) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 7337 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7338 | goto exit; |
| 7339 | } |
| 7340 | |
| 7341 | #else |
| 7342 | |
| 7343 | Py_BEGIN_ALLOW_THREADS |
| 7344 | #if HAVE_SYMLINKAT |
| 7345 | if (dir_fd != DEFAULT_DIR_FD) |
| 7346 | result = symlinkat(src.narrow, dir_fd, dst.narrow); |
| 7347 | else |
| 7348 | #endif |
| 7349 | result = symlink(src.narrow, dst.narrow); |
| 7350 | Py_END_ALLOW_THREADS |
| 7351 | |
| 7352 | if (result) { |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 7353 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7354 | goto exit; |
| 7355 | } |
| 7356 | #endif |
| 7357 | |
| 7358 | return_value = Py_None; |
| 7359 | Py_INCREF(Py_None); |
| 7360 | goto exit; /* silence "unused label" warning */ |
| 7361 | exit: |
| 7362 | path_cleanup(&src); |
| 7363 | path_cleanup(&dst); |
| 7364 | return return_value; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7365 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7366 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7367 | #endif /* HAVE_SYMLINK */ |
| 7368 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7369 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7370 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 7371 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7372 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7373 | win_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7374 | { |
| 7375 | wchar_t *path; |
| 7376 | DWORD n_bytes_returned; |
| 7377 | DWORD io_result; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7378 | PyObject *po, *result; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7379 | int dir_fd; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7380 | HANDLE reparse_point_handle; |
| 7381 | |
| 7382 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 7383 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 7384 | wchar_t *print_name; |
| 7385 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7386 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 7387 | |
| 7388 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords, |
| 7389 | &po, |
| 7390 | dir_fd_unavailable, &dir_fd |
| 7391 | )) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7392 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7393 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7394 | path = PyUnicode_AsUnicode(po); |
| 7395 | if (path == NULL) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7396 | return NULL; |
| 7397 | |
| 7398 | /* First get a handle to the reparse point */ |
| 7399 | Py_BEGIN_ALLOW_THREADS |
| 7400 | reparse_point_handle = CreateFileW( |
| 7401 | path, |
| 7402 | 0, |
| 7403 | 0, |
| 7404 | 0, |
| 7405 | OPEN_EXISTING, |
| 7406 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 7407 | 0); |
| 7408 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7409 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7410 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7411 | return win32_error_object("readlink", po); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7412 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7413 | Py_BEGIN_ALLOW_THREADS |
| 7414 | /* New call DeviceIoControl to read the reparse point */ |
| 7415 | io_result = DeviceIoControl( |
| 7416 | reparse_point_handle, |
| 7417 | FSCTL_GET_REPARSE_POINT, |
| 7418 | 0, 0, /* in buffer */ |
| 7419 | target_buffer, sizeof(target_buffer), |
| 7420 | &n_bytes_returned, |
| 7421 | 0 /* we're not using OVERLAPPED_IO */ |
| 7422 | ); |
| 7423 | CloseHandle(reparse_point_handle); |
| 7424 | Py_END_ALLOW_THREADS |
| 7425 | |
| 7426 | if (io_result==0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7427 | return win32_error_object("readlink", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7428 | |
| 7429 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 7430 | { |
| 7431 | PyErr_SetString(PyExc_ValueError, |
| 7432 | "not a symbolic link"); |
| 7433 | return NULL; |
| 7434 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 7435 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7436 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 7437 | |
| 7438 | result = PyUnicode_FromWideChar(print_name, |
| 7439 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7440 | return result; |
| 7441 | } |
| 7442 | |
| 7443 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 7444 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7445 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7446 | static PyStructSequence_Field times_result_fields[] = { |
| 7447 | {"user", "user time"}, |
| 7448 | {"system", "system time"}, |
| 7449 | {"children_user", "user time of children"}, |
| 7450 | {"children_system", "system time of children"}, |
| 7451 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 7452 | {NULL} |
| 7453 | }; |
| 7454 | |
| 7455 | PyDoc_STRVAR(times_result__doc__, |
| 7456 | "times_result: Result from os.times().\n\n\ |
| 7457 | This object may be accessed either as a tuple of\n\ |
| 7458 | (user, system, children_user, children_system, elapsed),\n\ |
| 7459 | or via the attributes user, system, children_user, children_system,\n\ |
| 7460 | and elapsed.\n\ |
| 7461 | \n\ |
| 7462 | See os.times for more information."); |
| 7463 | |
| 7464 | static PyStructSequence_Desc times_result_desc = { |
| 7465 | "times_result", /* name */ |
| 7466 | times_result__doc__, /* doc */ |
| 7467 | times_result_fields, |
| 7468 | 5 |
| 7469 | }; |
| 7470 | |
| 7471 | static PyTypeObject TimesResultType; |
| 7472 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7473 | #ifdef MS_WINDOWS |
| 7474 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 7475 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7476 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7477 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7478 | |
| 7479 | static PyObject * |
| 7480 | build_times_result(double user, double system, |
| 7481 | double children_user, double children_system, |
| 7482 | double elapsed) |
| 7483 | { |
| 7484 | PyObject *value = PyStructSequence_New(&TimesResultType); |
| 7485 | if (value == NULL) |
| 7486 | return NULL; |
| 7487 | |
| 7488 | #define SET(i, field) \ |
| 7489 | { \ |
| 7490 | PyObject *o = PyFloat_FromDouble(field); \ |
| 7491 | if (!o) { \ |
| 7492 | Py_DECREF(value); \ |
| 7493 | return NULL; \ |
| 7494 | } \ |
| 7495 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 7496 | } \ |
| 7497 | |
| 7498 | SET(0, user); |
| 7499 | SET(1, system); |
| 7500 | SET(2, children_user); |
| 7501 | SET(3, children_system); |
| 7502 | SET(4, elapsed); |
| 7503 | |
| 7504 | #undef SET |
| 7505 | |
| 7506 | return value; |
| 7507 | } |
| 7508 | |
| 7509 | PyDoc_STRVAR(posix_times__doc__, |
| 7510 | "times() -> times_result\n\n\ |
| 7511 | Return an object containing floating point numbers indicating process\n\ |
| 7512 | times. The object behaves like a named tuple with these fields:\n\ |
| 7513 | (utime, stime, cutime, cstime, elapsed_time)"); |
| 7514 | |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 7515 | #if defined(MS_WINDOWS) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7516 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7517 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7518 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7519 | FILETIME create, exit, kernel, user; |
| 7520 | HANDLE hProc; |
| 7521 | hProc = GetCurrentProcess(); |
| 7522 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 7523 | /* The fields of a FILETIME structure are the hi and lo part |
| 7524 | of a 64-bit value expressed in 100 nanosecond units. |
| 7525 | 1e7 is one second in such units; 1e-7 the inverse. |
| 7526 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 7527 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7528 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7529 | (double)(user.dwHighDateTime*429.4967296 + |
| 7530 | user.dwLowDateTime*1e-7), |
| 7531 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 7532 | kernel.dwLowDateTime*1e-7), |
| 7533 | (double)0, |
| 7534 | (double)0, |
| 7535 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7536 | } |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 7537 | #else /* Not Windows */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7538 | #define NEED_TICKS_PER_SECOND |
| 7539 | static long ticks_per_second = -1; |
| 7540 | static PyObject * |
| 7541 | posix_times(PyObject *self, PyObject *noargs) |
| 7542 | { |
| 7543 | struct tms t; |
| 7544 | clock_t c; |
| 7545 | errno = 0; |
| 7546 | c = times(&t); |
| 7547 | if (c == (clock_t) -1) |
| 7548 | return posix_error(); |
| 7549 | return build_times_result( |
| 7550 | (double)t.tms_utime / ticks_per_second, |
| 7551 | (double)t.tms_stime / ticks_per_second, |
| 7552 | (double)t.tms_cutime / ticks_per_second, |
| 7553 | (double)t.tms_cstime / ticks_per_second, |
| 7554 | (double)c / ticks_per_second); |
| 7555 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7556 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7557 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7558 | #endif /* HAVE_TIMES */ |
| 7559 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7560 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7561 | #ifdef HAVE_GETSID |
| 7562 | PyDoc_STRVAR(posix_getsid__doc__, |
| 7563 | "getsid(pid) -> sid\n\n\ |
| 7564 | Call the system call getsid()."); |
| 7565 | |
| 7566 | static PyObject * |
| 7567 | posix_getsid(PyObject *self, PyObject *args) |
| 7568 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7569 | pid_t pid; |
| 7570 | int sid; |
| 7571 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 7572 | return NULL; |
| 7573 | sid = getsid(pid); |
| 7574 | if (sid < 0) |
| 7575 | return posix_error(); |
| 7576 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7577 | } |
| 7578 | #endif /* HAVE_GETSID */ |
| 7579 | |
| 7580 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7581 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7582 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7583 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7584 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7585 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7586 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7587 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7588 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7589 | if (setsid() < 0) |
| 7590 | return posix_error(); |
| 7591 | Py_INCREF(Py_None); |
| 7592 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7593 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7594 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7595 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7596 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7597 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7598 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7599 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7600 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7601 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7602 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7603 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7604 | pid_t pid; |
| 7605 | int pgrp; |
| 7606 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 7607 | return NULL; |
| 7608 | if (setpgid(pid, pgrp) < 0) |
| 7609 | return posix_error(); |
| 7610 | Py_INCREF(Py_None); |
| 7611 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7612 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7613 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7614 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7615 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7616 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7617 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7618 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7619 | 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] | 7620 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7621 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7622 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7623 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7624 | int fd; |
| 7625 | pid_t pgid; |
| 7626 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 7627 | return NULL; |
| 7628 | pgid = tcgetpgrp(fd); |
| 7629 | if (pgid < 0) |
| 7630 | return posix_error(); |
| 7631 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7632 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7633 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7634 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7635 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7636 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7637 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7638 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7639 | 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] | 7640 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7641 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7642 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7643 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7644 | int fd; |
| 7645 | pid_t pgid; |
| 7646 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 7647 | return NULL; |
| 7648 | if (tcsetpgrp(fd, pgid) < 0) |
| 7649 | return posix_error(); |
| 7650 | Py_INCREF(Py_None); |
| 7651 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7652 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7653 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7654 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7655 | /* Functions acting on file descriptors */ |
| 7656 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7657 | #ifdef O_CLOEXEC |
| 7658 | extern int _Py_open_cloexec_works; |
| 7659 | #endif |
| 7660 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7661 | PyDoc_STRVAR(posix_open__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7662 | "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 7663 | Open a file for low level IO. Returns a file handle (integer).\n\ |
| 7664 | \n\ |
| 7665 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7666 | and path should be relative; path will then be relative to that directory.\n\ |
| 7667 | dir_fd may not be implemented on your platform.\n\ |
| 7668 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7669 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7670 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7671 | posix_open(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7672 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7673 | path_t path; |
| 7674 | int flags; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7675 | int mode = 0777; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7676 | int dir_fd = DEFAULT_DIR_FD; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7677 | int fd; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7678 | PyObject *return_value = NULL; |
| 7679 | static char *keywords[] = {"path", "flags", "mode", "dir_fd", NULL}; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7680 | #ifdef O_CLOEXEC |
| 7681 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 7682 | #elif !defined(MS_WINDOWS) |
| 7683 | int *atomic_flag_works = NULL; |
| 7684 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7685 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7686 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7687 | path.function_name = "open"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7688 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|i$O&:open", keywords, |
| 7689 | path_converter, &path, |
| 7690 | &flags, &mode, |
| 7691 | #ifdef HAVE_OPENAT |
| 7692 | dir_fd_converter, &dir_fd |
| 7693 | #else |
| 7694 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7695 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7696 | )) |
| 7697 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7698 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7699 | #ifdef MS_WINDOWS |
| 7700 | flags |= O_NOINHERIT; |
| 7701 | #elif defined(O_CLOEXEC) |
| 7702 | flags |= O_CLOEXEC; |
| 7703 | #endif |
| 7704 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7705 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7706 | #ifdef MS_WINDOWS |
| 7707 | if (path.wide) |
| 7708 | fd = _wopen(path.wide, flags, mode); |
| 7709 | else |
| 7710 | #endif |
| 7711 | #ifdef HAVE_OPENAT |
| 7712 | if (dir_fd != DEFAULT_DIR_FD) |
| 7713 | fd = openat(dir_fd, path.narrow, flags, mode); |
| 7714 | else |
| 7715 | #endif |
| 7716 | fd = open(path.narrow, flags, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7717 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7718 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7719 | if (fd == -1) { |
Victor Stinner | 4e7d2d4 | 2012-11-05 01:20:58 +0100 | [diff] [blame] | 7720 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path.object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7721 | goto exit; |
| 7722 | } |
| 7723 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7724 | #ifndef MS_WINDOWS |
| 7725 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 7726 | close(fd); |
| 7727 | goto exit; |
| 7728 | } |
| 7729 | #endif |
| 7730 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7731 | return_value = PyLong_FromLong((long)fd); |
| 7732 | |
| 7733 | exit: |
| 7734 | path_cleanup(&path); |
| 7735 | return return_value; |
| 7736 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7737 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7738 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7739 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7740 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7741 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7742 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7743 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7744 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7745 | int fd, res; |
| 7746 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 7747 | return NULL; |
| 7748 | if (!_PyVerify_fd(fd)) |
| 7749 | return posix_error(); |
| 7750 | Py_BEGIN_ALLOW_THREADS |
| 7751 | res = close(fd); |
| 7752 | Py_END_ALLOW_THREADS |
| 7753 | if (res < 0) |
| 7754 | return posix_error(); |
| 7755 | Py_INCREF(Py_None); |
| 7756 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7757 | } |
| 7758 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7759 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7760 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7761 | "closerange(fd_low, fd_high)\n\n\ |
| 7762 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 7763 | |
| 7764 | static PyObject * |
| 7765 | posix_closerange(PyObject *self, PyObject *args) |
| 7766 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7767 | int fd_from, fd_to, i; |
| 7768 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 7769 | return NULL; |
| 7770 | Py_BEGIN_ALLOW_THREADS |
| 7771 | for (i = fd_from; i < fd_to; i++) |
| 7772 | if (_PyVerify_fd(i)) |
| 7773 | close(i); |
| 7774 | Py_END_ALLOW_THREADS |
| 7775 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7776 | } |
| 7777 | |
| 7778 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7779 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7780 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7781 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7782 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7783 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7784 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7785 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7786 | int fd; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7787 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7788 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 7789 | return NULL; |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 7790 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7791 | fd = _Py_dup(fd); |
| 7792 | if (fd == -1) |
| 7793 | return NULL; |
| 7794 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7795 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7796 | } |
| 7797 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7798 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7799 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 7800 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7801 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7802 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7803 | static PyObject * |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7804 | posix_dup2(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7805 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7806 | static char *keywords[] = {"fd", "fd2", "inheritable", NULL}; |
| 7807 | int fd, fd2; |
| 7808 | int inheritable = 1; |
| 7809 | int res; |
| 7810 | #if defined(HAVE_DUP3) && \ |
| 7811 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 7812 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
| 7813 | int dup3_works = -1; |
| 7814 | #endif |
| 7815 | |
| 7816 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|i:dup2", keywords, |
| 7817 | &fd, &fd2, &inheritable)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7818 | return NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7819 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7820 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 7821 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7822 | |
| 7823 | #ifdef MS_WINDOWS |
| 7824 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7825 | res = dup2(fd, fd2); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7826 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7827 | if (res < 0) |
| 7828 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7829 | |
| 7830 | /* Character files like console cannot be make non-inheritable */ |
| 7831 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7832 | close(fd2); |
| 7833 | return NULL; |
| 7834 | } |
| 7835 | |
| 7836 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 7837 | Py_BEGIN_ALLOW_THREADS |
| 7838 | if (!inheritable) |
| 7839 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 7840 | else |
| 7841 | res = dup2(fd, fd2); |
| 7842 | Py_END_ALLOW_THREADS |
| 7843 | if (res < 0) |
| 7844 | return posix_error(); |
| 7845 | |
| 7846 | #else |
| 7847 | |
| 7848 | #ifdef HAVE_DUP3 |
| 7849 | if (!inheritable && dup3_works != 0) { |
| 7850 | Py_BEGIN_ALLOW_THREADS |
| 7851 | res = dup3(fd, fd2, O_CLOEXEC); |
| 7852 | Py_END_ALLOW_THREADS |
| 7853 | if (res < 0) { |
| 7854 | if (dup3_works == -1) |
| 7855 | dup3_works = (errno != ENOSYS); |
| 7856 | if (dup3_works) |
| 7857 | return posix_error(); |
| 7858 | } |
| 7859 | } |
| 7860 | |
| 7861 | if (inheritable || dup3_works == 0) |
| 7862 | { |
| 7863 | #endif |
| 7864 | Py_BEGIN_ALLOW_THREADS |
| 7865 | res = dup2(fd, fd2); |
| 7866 | Py_END_ALLOW_THREADS |
| 7867 | if (res < 0) |
| 7868 | return posix_error(); |
| 7869 | |
| 7870 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7871 | close(fd2); |
| 7872 | return NULL; |
| 7873 | } |
| 7874 | #ifdef HAVE_DUP3 |
| 7875 | } |
| 7876 | #endif |
| 7877 | |
| 7878 | #endif |
| 7879 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7880 | Py_INCREF(Py_None); |
| 7881 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7882 | } |
| 7883 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7884 | #ifdef HAVE_LOCKF |
| 7885 | PyDoc_STRVAR(posix_lockf__doc__, |
| 7886 | "lockf(fd, cmd, len)\n\n\ |
| 7887 | Apply, test or remove a POSIX lock on an open file descriptor.\n\n\ |
| 7888 | fd is an open file descriptor.\n\ |
| 7889 | cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\ |
| 7890 | F_TEST.\n\ |
| 7891 | len specifies the section of the file to lock."); |
| 7892 | |
| 7893 | static PyObject * |
| 7894 | posix_lockf(PyObject *self, PyObject *args) |
| 7895 | { |
| 7896 | int fd, cmd, res; |
| 7897 | off_t len; |
| 7898 | if (!PyArg_ParseTuple(args, "iiO&:lockf", |
| 7899 | &fd, &cmd, _parse_off_t, &len)) |
| 7900 | return NULL; |
| 7901 | |
| 7902 | Py_BEGIN_ALLOW_THREADS |
| 7903 | res = lockf(fd, cmd, len); |
| 7904 | Py_END_ALLOW_THREADS |
| 7905 | |
| 7906 | if (res < 0) |
| 7907 | return posix_error(); |
| 7908 | |
| 7909 | Py_RETURN_NONE; |
| 7910 | } |
| 7911 | #endif |
| 7912 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7913 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7914 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7915 | "lseek(fd, pos, how) -> newpos\n\n\ |
Victor Stinner | e83f899 | 2011-12-17 23:15:09 +0100 | [diff] [blame] | 7916 | Set the current position of a file descriptor.\n\ |
| 7917 | Return the new cursor position in bytes, starting from the beginning."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7918 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7919 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7920 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7921 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7922 | int fd, how; |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 7923 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7924 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7925 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7926 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7927 | #endif |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7928 | PyObject *posobj; |
| 7929 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7930 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7931 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7932 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 7933 | switch (how) { |
| 7934 | case 0: how = SEEK_SET; break; |
| 7935 | case 1: how = SEEK_CUR; break; |
| 7936 | case 2: how = SEEK_END; break; |
| 7937 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7938 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7939 | |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7940 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7941 | pos = PyLong_AsLong(posobj); |
| 7942 | #else |
| 7943 | pos = PyLong_AsLongLong(posobj); |
| 7944 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7945 | if (PyErr_Occurred()) |
| 7946 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7947 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7948 | if (!_PyVerify_fd(fd)) |
| 7949 | return posix_error(); |
| 7950 | Py_BEGIN_ALLOW_THREADS |
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 | res = _lseeki64(fd, pos, how); |
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 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7955 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7956 | Py_END_ALLOW_THREADS |
| 7957 | if (res < 0) |
| 7958 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7959 | |
| 7960 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7961 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7962 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7963 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7964 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7965 | } |
| 7966 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7967 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7968 | PyDoc_STRVAR(posix_read__doc__, |
Benjamin Peterson | 3b08a29 | 2013-05-24 14:35:57 -0700 | [diff] [blame] | 7969 | "read(fd, buffersize) -> bytes\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7970 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7971 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7972 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7973 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7974 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7975 | int fd, size; |
| 7976 | Py_ssize_t n; |
| 7977 | PyObject *buffer; |
| 7978 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 7979 | return NULL; |
| 7980 | if (size < 0) { |
| 7981 | errno = EINVAL; |
| 7982 | return posix_error(); |
| 7983 | } |
| 7984 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7985 | if (buffer == NULL) |
| 7986 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7987 | if (!_PyVerify_fd(fd)) { |
| 7988 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7989 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7990 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7991 | Py_BEGIN_ALLOW_THREADS |
| 7992 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 7993 | Py_END_ALLOW_THREADS |
| 7994 | if (n < 0) { |
| 7995 | Py_DECREF(buffer); |
| 7996 | return posix_error(); |
| 7997 | } |
| 7998 | if (n != size) |
| 7999 | _PyBytes_Resize(&buffer, n); |
| 8000 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8001 | } |
| 8002 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8003 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 8004 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8005 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8006 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 8007 | { |
| 8008 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8009 | Py_ssize_t blen, total = 0; |
| 8010 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8011 | *iov = PyMem_New(struct iovec, cnt); |
| 8012 | if (*iov == NULL) { |
| 8013 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8014 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8015 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8016 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8017 | *buf = PyMem_New(Py_buffer, cnt); |
| 8018 | if (*buf == NULL) { |
| 8019 | PyMem_Del(*iov); |
| 8020 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8021 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8022 | } |
| 8023 | |
| 8024 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8025 | PyObject *item = PySequence_GetItem(seq, i); |
| 8026 | if (item == NULL) |
| 8027 | goto fail; |
| 8028 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 8029 | Py_DECREF(item); |
| 8030 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8031 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8032 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8033 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8034 | blen = (*buf)[i].len; |
| 8035 | (*iov)[i].iov_len = blen; |
| 8036 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8037 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8038 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8039 | |
| 8040 | fail: |
| 8041 | PyMem_Del(*iov); |
| 8042 | for (j = 0; j < i; j++) { |
| 8043 | PyBuffer_Release(&(*buf)[j]); |
| 8044 | } |
| 8045 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8046 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8047 | } |
| 8048 | |
| 8049 | static void |
| 8050 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 8051 | { |
| 8052 | int i; |
| 8053 | PyMem_Del(iov); |
| 8054 | for (i = 0; i < cnt; i++) { |
| 8055 | PyBuffer_Release(&buf[i]); |
| 8056 | } |
| 8057 | PyMem_Del(buf); |
| 8058 | } |
| 8059 | #endif |
| 8060 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8061 | #ifdef HAVE_READV |
| 8062 | PyDoc_STRVAR(posix_readv__doc__, |
| 8063 | "readv(fd, buffers) -> bytesread\n\n\ |
Benjamin Peterson | e83ed43 | 2014-01-18 22:54:59 -0500 | [diff] [blame] | 8064 | Read from a file descriptor fd into a number of mutable, bytes-like\n\ |
| 8065 | objects (\"buffers\"). readv will transfer data into each buffer\n\ |
| 8066 | until it is full and then move on to the next buffer in the sequence\n\ |
| 8067 | to hold the rest of the data.\n\n\ |
| 8068 | readv returns the total number of bytes read (which may be less than\n\ |
| 8069 | the total capacity of all the buffers."); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8070 | |
| 8071 | static PyObject * |
| 8072 | posix_readv(PyObject *self, PyObject *args) |
| 8073 | { |
| 8074 | int fd, cnt; |
| 8075 | Py_ssize_t n; |
| 8076 | PyObject *seq; |
| 8077 | struct iovec *iov; |
| 8078 | Py_buffer *buf; |
| 8079 | |
| 8080 | if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq)) |
| 8081 | return NULL; |
| 8082 | if (!PySequence_Check(seq)) { |
| 8083 | PyErr_SetString(PyExc_TypeError, |
| 8084 | "readv() arg 2 must be a sequence"); |
| 8085 | return NULL; |
| 8086 | } |
| 8087 | cnt = PySequence_Size(seq); |
| 8088 | |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8089 | if (iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE) < 0) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8090 | return NULL; |
| 8091 | |
| 8092 | Py_BEGIN_ALLOW_THREADS |
| 8093 | n = readv(fd, iov, cnt); |
| 8094 | Py_END_ALLOW_THREADS |
| 8095 | |
| 8096 | iov_cleanup(iov, buf, cnt); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8097 | if (n < 0) |
| 8098 | return posix_error(); |
| 8099 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8100 | return PyLong_FromSsize_t(n); |
| 8101 | } |
| 8102 | #endif |
| 8103 | |
| 8104 | #ifdef HAVE_PREAD |
| 8105 | PyDoc_STRVAR(posix_pread__doc__, |
| 8106 | "pread(fd, buffersize, offset) -> string\n\n\ |
| 8107 | Read from a file descriptor, fd, at a position of offset. It will read up\n\ |
| 8108 | to buffersize number of bytes. The file offset remains unchanged."); |
| 8109 | |
| 8110 | static PyObject * |
| 8111 | posix_pread(PyObject *self, PyObject *args) |
| 8112 | { |
| 8113 | int fd, size; |
| 8114 | off_t offset; |
| 8115 | Py_ssize_t n; |
| 8116 | PyObject *buffer; |
| 8117 | if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset)) |
| 8118 | return NULL; |
| 8119 | |
| 8120 | if (size < 0) { |
| 8121 | errno = EINVAL; |
| 8122 | return posix_error(); |
| 8123 | } |
| 8124 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 8125 | if (buffer == NULL) |
| 8126 | return NULL; |
| 8127 | if (!_PyVerify_fd(fd)) { |
| 8128 | Py_DECREF(buffer); |
| 8129 | return posix_error(); |
| 8130 | } |
| 8131 | Py_BEGIN_ALLOW_THREADS |
| 8132 | n = pread(fd, PyBytes_AS_STRING(buffer), size, offset); |
| 8133 | Py_END_ALLOW_THREADS |
| 8134 | if (n < 0) { |
| 8135 | Py_DECREF(buffer); |
| 8136 | return posix_error(); |
| 8137 | } |
| 8138 | if (n != size) |
| 8139 | _PyBytes_Resize(&buffer, n); |
| 8140 | return buffer; |
| 8141 | } |
| 8142 | #endif |
| 8143 | |
| 8144 | PyDoc_STRVAR(posix_write__doc__, |
Benjamin Peterson | 3b08a29 | 2013-05-24 14:35:57 -0700 | [diff] [blame] | 8145 | "write(fd, data) -> byteswritten\n\n\ |
| 8146 | Write bytes to a file descriptor."); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8147 | |
| 8148 | static PyObject * |
| 8149 | posix_write(PyObject *self, PyObject *args) |
| 8150 | { |
| 8151 | Py_buffer pbuf; |
| 8152 | int fd; |
| 8153 | Py_ssize_t size, len; |
| 8154 | |
| 8155 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 8156 | return NULL; |
| 8157 | if (!_PyVerify_fd(fd)) { |
| 8158 | PyBuffer_Release(&pbuf); |
| 8159 | return posix_error(); |
| 8160 | } |
| 8161 | len = pbuf.len; |
| 8162 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 8163 | #ifdef MS_WINDOWS |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8164 | if (len > INT_MAX) |
| 8165 | len = INT_MAX; |
| 8166 | size = write(fd, pbuf.buf, (int)len); |
| 8167 | #else |
| 8168 | size = write(fd, pbuf.buf, len); |
| 8169 | #endif |
| 8170 | Py_END_ALLOW_THREADS |
| 8171 | PyBuffer_Release(&pbuf); |
| 8172 | if (size < 0) |
| 8173 | return posix_error(); |
| 8174 | return PyLong_FromSsize_t(size); |
| 8175 | } |
| 8176 | |
| 8177 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8178 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 8179 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 8180 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 8181 | -> byteswritten\n\ |
| 8182 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 8183 | |
| 8184 | static PyObject * |
| 8185 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 8186 | { |
| 8187 | int in, out; |
| 8188 | Py_ssize_t ret; |
| 8189 | off_t offset; |
| 8190 | |
| 8191 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 8192 | #ifndef __APPLE__ |
| 8193 | Py_ssize_t len; |
| 8194 | #endif |
| 8195 | PyObject *headers = NULL, *trailers = NULL; |
| 8196 | Py_buffer *hbuf, *tbuf; |
| 8197 | off_t sbytes; |
| 8198 | struct sf_hdtr sf; |
| 8199 | int flags = 0; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 8200 | static char *keywords[] = {"out", "in", |
| 8201 | "offset", "count", |
| 8202 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8203 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 8204 | sf.headers = NULL; |
| 8205 | sf.trailers = NULL; |
| 8206 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8207 | #ifdef __APPLE__ |
| 8208 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 8209 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8210 | #else |
| 8211 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 8212 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8213 | #endif |
| 8214 | &headers, &trailers, &flags)) |
| 8215 | return NULL; |
| 8216 | if (headers != NULL) { |
| 8217 | if (!PySequence_Check(headers)) { |
| 8218 | PyErr_SetString(PyExc_TypeError, |
| 8219 | "sendfile() headers must be a sequence or None"); |
| 8220 | return NULL; |
| 8221 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8222 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8223 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8224 | if (sf.hdr_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8225 | (i = iov_setup(&(sf.headers), &hbuf, |
| 8226 | headers, sf.hdr_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8227 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8228 | #ifdef __APPLE__ |
| 8229 | sbytes += i; |
| 8230 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8231 | } |
| 8232 | } |
| 8233 | if (trailers != NULL) { |
| 8234 | if (!PySequence_Check(trailers)) { |
| 8235 | PyErr_SetString(PyExc_TypeError, |
| 8236 | "sendfile() trailers must be a sequence or None"); |
| 8237 | return NULL; |
| 8238 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8239 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8240 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8241 | if (sf.trl_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8242 | (i = iov_setup(&(sf.trailers), &tbuf, |
| 8243 | trailers, sf.trl_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8244 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8245 | #ifdef __APPLE__ |
| 8246 | sbytes += i; |
| 8247 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8248 | } |
| 8249 | } |
| 8250 | |
| 8251 | Py_BEGIN_ALLOW_THREADS |
| 8252 | #ifdef __APPLE__ |
| 8253 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 8254 | #else |
| 8255 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 8256 | #endif |
| 8257 | Py_END_ALLOW_THREADS |
| 8258 | |
| 8259 | if (sf.headers != NULL) |
| 8260 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 8261 | if (sf.trailers != NULL) |
| 8262 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 8263 | |
| 8264 | if (ret < 0) { |
| 8265 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 8266 | if (sbytes != 0) { |
| 8267 | // some data has been sent |
| 8268 | goto done; |
| 8269 | } |
| 8270 | else { |
| 8271 | // no data has been sent; upper application is supposed |
| 8272 | // to retry on EAGAIN or EBUSY |
| 8273 | return posix_error(); |
| 8274 | } |
| 8275 | } |
| 8276 | return posix_error(); |
| 8277 | } |
| 8278 | goto done; |
| 8279 | |
| 8280 | done: |
| 8281 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 8282 | return Py_BuildValue("l", sbytes); |
| 8283 | #else |
| 8284 | return Py_BuildValue("L", sbytes); |
| 8285 | #endif |
| 8286 | |
| 8287 | #else |
| 8288 | Py_ssize_t count; |
| 8289 | PyObject *offobj; |
| 8290 | static char *keywords[] = {"out", "in", |
| 8291 | "offset", "count", NULL}; |
| 8292 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 8293 | keywords, &out, &in, &offobj, &count)) |
| 8294 | return NULL; |
| 8295 | #ifdef linux |
| 8296 | if (offobj == Py_None) { |
| 8297 | Py_BEGIN_ALLOW_THREADS |
| 8298 | ret = sendfile(out, in, NULL, count); |
| 8299 | Py_END_ALLOW_THREADS |
| 8300 | if (ret < 0) |
| 8301 | return posix_error(); |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 8302 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8303 | } |
| 8304 | #endif |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 8305 | if (!_parse_off_t(offobj, &offset)) |
| 8306 | return NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8307 | Py_BEGIN_ALLOW_THREADS |
| 8308 | ret = sendfile(out, in, &offset, count); |
| 8309 | Py_END_ALLOW_THREADS |
| 8310 | if (ret < 0) |
| 8311 | return posix_error(); |
| 8312 | return Py_BuildValue("n", ret); |
| 8313 | #endif |
| 8314 | } |
| 8315 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8316 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8317 | PyDoc_STRVAR(posix_fstat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8318 | "fstat(fd) -> stat result\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8319 | Like stat(), but for an open file descriptor.\n\ |
| 8320 | Equivalent to stat(fd=fd)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8321 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8322 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8323 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8324 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8325 | int fd; |
| 8326 | STRUCT_STAT st; |
| 8327 | int res; |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8328 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8329 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8330 | Py_BEGIN_ALLOW_THREADS |
| 8331 | res = FSTAT(fd, &st); |
| 8332 | Py_END_ALLOW_THREADS |
| 8333 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8334 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8335 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8336 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8337 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8338 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8339 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8340 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8341 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8342 | } |
| 8343 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8344 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8345 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8346 | 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] | 8347 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8348 | |
| 8349 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 8350 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8351 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8352 | int fd; |
| 8353 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 8354 | return NULL; |
| 8355 | if (!_PyVerify_fd(fd)) |
| 8356 | return PyBool_FromLong(0); |
| 8357 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8358 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8359 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8360 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8361 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8362 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8363 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8364 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8365 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8366 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8367 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8368 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8369 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8370 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8371 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8372 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8373 | #else |
| 8374 | int res; |
| 8375 | #endif |
| 8376 | |
| 8377 | #ifdef MS_WINDOWS |
| 8378 | attr.nLength = sizeof(attr); |
| 8379 | attr.lpSecurityDescriptor = NULL; |
| 8380 | attr.bInheritHandle = FALSE; |
| 8381 | |
| 8382 | Py_BEGIN_ALLOW_THREADS |
| 8383 | ok = CreatePipe(&read, &write, &attr, 0); |
| 8384 | if (ok) { |
| 8385 | fds[0] = _open_osfhandle((Py_intptr_t)read, _O_RDONLY); |
| 8386 | fds[1] = _open_osfhandle((Py_intptr_t)write, _O_WRONLY); |
| 8387 | if (fds[0] == -1 || fds[1] == -1) { |
| 8388 | CloseHandle(read); |
| 8389 | CloseHandle(write); |
| 8390 | ok = 0; |
| 8391 | } |
| 8392 | } |
| 8393 | Py_END_ALLOW_THREADS |
| 8394 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8395 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8396 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8397 | #else |
| 8398 | |
| 8399 | #ifdef HAVE_PIPE2 |
| 8400 | Py_BEGIN_ALLOW_THREADS |
| 8401 | res = pipe2(fds, O_CLOEXEC); |
| 8402 | Py_END_ALLOW_THREADS |
| 8403 | |
| 8404 | if (res != 0 && errno == ENOSYS) |
| 8405 | { |
| 8406 | #endif |
| 8407 | Py_BEGIN_ALLOW_THREADS |
| 8408 | res = pipe(fds); |
| 8409 | Py_END_ALLOW_THREADS |
| 8410 | |
| 8411 | if (res == 0) { |
| 8412 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 8413 | close(fds[0]); |
| 8414 | close(fds[1]); |
| 8415 | return NULL; |
| 8416 | } |
| 8417 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 8418 | close(fds[0]); |
| 8419 | close(fds[1]); |
| 8420 | return NULL; |
| 8421 | } |
| 8422 | } |
| 8423 | #ifdef HAVE_PIPE2 |
| 8424 | } |
| 8425 | #endif |
| 8426 | |
| 8427 | if (res != 0) |
| 8428 | return PyErr_SetFromErrno(PyExc_OSError); |
| 8429 | #endif /* !MS_WINDOWS */ |
| 8430 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8431 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8432 | #endif /* HAVE_PIPE */ |
| 8433 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8434 | #ifdef HAVE_PIPE2 |
| 8435 | PyDoc_STRVAR(posix_pipe2__doc__, |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8436 | "pipe2(flags) -> (read_end, write_end)\n\n\ |
| 8437 | Create a pipe with flags set atomically.\n\ |
| 8438 | flags can be constructed by ORing together one or more of these values:\n\ |
| 8439 | O_NONBLOCK, O_CLOEXEC.\n\ |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8440 | "); |
| 8441 | |
| 8442 | static PyObject * |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8443 | posix_pipe2(PyObject *self, PyObject *arg) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8444 | { |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8445 | int flags; |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8446 | int fds[2]; |
| 8447 | int res; |
| 8448 | |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 8449 | flags = _PyLong_AsInt(arg); |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8450 | if (flags == -1 && PyErr_Occurred()) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8451 | return NULL; |
| 8452 | |
| 8453 | res = pipe2(fds, flags); |
| 8454 | if (res != 0) |
| 8455 | return posix_error(); |
| 8456 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 8457 | } |
| 8458 | #endif /* HAVE_PIPE2 */ |
| 8459 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8460 | #ifdef HAVE_WRITEV |
| 8461 | PyDoc_STRVAR(posix_writev__doc__, |
| 8462 | "writev(fd, buffers) -> byteswritten\n\n\ |
Benjamin Peterson | e83ed43 | 2014-01-18 22:54:59 -0500 | [diff] [blame] | 8463 | Write the contents of *buffers* to file descriptor *fd*. *buffers*\n\ |
| 8464 | must be a sequence of bytes-like objects.\n\n\ |
| 8465 | writev writes the contents of each object to the file descriptor\n\ |
| 8466 | and returns the total number of bytes written."); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8467 | |
| 8468 | static PyObject * |
| 8469 | posix_writev(PyObject *self, PyObject *args) |
| 8470 | { |
| 8471 | int fd, cnt; |
| 8472 | Py_ssize_t res; |
| 8473 | PyObject *seq; |
| 8474 | struct iovec *iov; |
| 8475 | Py_buffer *buf; |
| 8476 | if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq)) |
| 8477 | return NULL; |
| 8478 | if (!PySequence_Check(seq)) { |
| 8479 | PyErr_SetString(PyExc_TypeError, |
| 8480 | "writev() arg 2 must be a sequence"); |
| 8481 | return NULL; |
| 8482 | } |
| 8483 | cnt = PySequence_Size(seq); |
| 8484 | |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8485 | if (iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE) < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8486 | return NULL; |
| 8487 | } |
| 8488 | |
| 8489 | Py_BEGIN_ALLOW_THREADS |
| 8490 | res = writev(fd, iov, cnt); |
| 8491 | Py_END_ALLOW_THREADS |
| 8492 | |
| 8493 | iov_cleanup(iov, buf, cnt); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8494 | if (res < 0) |
| 8495 | return posix_error(); |
| 8496 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8497 | return PyLong_FromSsize_t(res); |
| 8498 | } |
| 8499 | #endif |
| 8500 | |
| 8501 | #ifdef HAVE_PWRITE |
| 8502 | PyDoc_STRVAR(posix_pwrite__doc__, |
| 8503 | "pwrite(fd, string, offset) -> byteswritten\n\n\ |
| 8504 | Write string to a file descriptor, fd, from offset, leaving the file\n\ |
| 8505 | offset unchanged."); |
| 8506 | |
| 8507 | static PyObject * |
| 8508 | posix_pwrite(PyObject *self, PyObject *args) |
| 8509 | { |
| 8510 | Py_buffer pbuf; |
| 8511 | int fd; |
| 8512 | off_t offset; |
| 8513 | Py_ssize_t size; |
| 8514 | |
| 8515 | if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset)) |
| 8516 | return NULL; |
| 8517 | |
| 8518 | if (!_PyVerify_fd(fd)) { |
| 8519 | PyBuffer_Release(&pbuf); |
| 8520 | return posix_error(); |
| 8521 | } |
| 8522 | Py_BEGIN_ALLOW_THREADS |
| 8523 | size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset); |
| 8524 | Py_END_ALLOW_THREADS |
| 8525 | PyBuffer_Release(&pbuf); |
| 8526 | if (size < 0) |
| 8527 | return posix_error(); |
| 8528 | return PyLong_FromSsize_t(size); |
| 8529 | } |
| 8530 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8531 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8532 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8533 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8534 | "mkfifo(path, mode=0o666, *, dir_fd=None)\n\n\ |
| 8535 | Create a FIFO (a POSIX named pipe).\n\ |
| 8536 | \n\ |
| 8537 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 8538 | and path should be relative; path will then be relative to that directory.\n\ |
| 8539 | dir_fd may not be implemented on your platform.\n\ |
| 8540 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8541 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8542 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8543 | posix_mkfifo(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8544 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8545 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8546 | int mode = 0666; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8547 | int dir_fd = DEFAULT_DIR_FD; |
| 8548 | int result; |
| 8549 | PyObject *return_value = NULL; |
| 8550 | static char *keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 8551 | |
| 8552 | memset(&path, 0, sizeof(path)); |
| 8553 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkfifo", keywords, |
| 8554 | path_converter, &path, |
| 8555 | &mode, |
| 8556 | #ifdef HAVE_MKFIFOAT |
| 8557 | dir_fd_converter, &dir_fd |
| 8558 | #else |
| 8559 | dir_fd_unavailable, &dir_fd |
| 8560 | #endif |
| 8561 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8562 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8563 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8564 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8565 | #ifdef HAVE_MKFIFOAT |
| 8566 | if (dir_fd != DEFAULT_DIR_FD) |
| 8567 | result = mkfifoat(dir_fd, path.narrow, mode); |
| 8568 | else |
| 8569 | #endif |
| 8570 | result = mkfifo(path.narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8571 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8572 | |
| 8573 | if (result < 0) { |
| 8574 | return_value = posix_error(); |
| 8575 | goto exit; |
| 8576 | } |
| 8577 | |
| 8578 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8579 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8580 | |
| 8581 | exit: |
| 8582 | path_cleanup(&path); |
| 8583 | return return_value; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8584 | } |
| 8585 | #endif |
| 8586 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 8587 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8588 | PyDoc_STRVAR(posix_mknod__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8589 | "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] | 8590 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 8591 | named filename. mode specifies both the permissions to use and the\n\ |
| 8592 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 8593 | 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] | 8594 | device defines the newly created device special file (probably using\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8595 | os.makedev()), otherwise it is ignored.\n\ |
| 8596 | \n\ |
| 8597 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 8598 | and path should be relative; path will then be relative to that directory.\n\ |
| 8599 | dir_fd may not be implemented on your platform.\n\ |
| 8600 | If it is unavailable, using it will raise a NotImplementedError."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8601 | |
| 8602 | |
| 8603 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8604 | posix_mknod(PyObject *self, PyObject *args, PyObject *kwargs) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8605 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8606 | path_t path; |
| 8607 | int mode = 0666; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8608 | int device = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8609 | int dir_fd = DEFAULT_DIR_FD; |
| 8610 | int result; |
| 8611 | PyObject *return_value = NULL; |
| 8612 | static char *keywords[] = {"path", "mode", "device", "dir_fd", NULL}; |
| 8613 | |
| 8614 | memset(&path, 0, sizeof(path)); |
| 8615 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|ii$O&:mknod", keywords, |
| 8616 | path_converter, &path, |
| 8617 | &mode, &device, |
| 8618 | #ifdef HAVE_MKNODAT |
| 8619 | dir_fd_converter, &dir_fd |
| 8620 | #else |
| 8621 | dir_fd_unavailable, &dir_fd |
| 8622 | #endif |
| 8623 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8624 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8625 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8626 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8627 | #ifdef HAVE_MKNODAT |
| 8628 | if (dir_fd != DEFAULT_DIR_FD) |
| 8629 | result = mknodat(dir_fd, path.narrow, mode, device); |
| 8630 | else |
| 8631 | #endif |
| 8632 | result = mknod(path.narrow, mode, device); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8633 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8634 | |
| 8635 | if (result < 0) { |
| 8636 | return_value = posix_error(); |
| 8637 | goto exit; |
| 8638 | } |
| 8639 | |
| 8640 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8641 | Py_INCREF(Py_None); |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 8642 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8643 | exit: |
| 8644 | path_cleanup(&path); |
| 8645 | return return_value; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8646 | } |
| 8647 | #endif |
| 8648 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8649 | #ifdef HAVE_DEVICE_MACROS |
| 8650 | PyDoc_STRVAR(posix_major__doc__, |
| 8651 | "major(device) -> major number\n\ |
| 8652 | Extracts a device major number from a raw device number."); |
| 8653 | |
| 8654 | static PyObject * |
| 8655 | posix_major(PyObject *self, PyObject *args) |
| 8656 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8657 | int device; |
| 8658 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 8659 | return NULL; |
| 8660 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8661 | } |
| 8662 | |
| 8663 | PyDoc_STRVAR(posix_minor__doc__, |
| 8664 | "minor(device) -> minor number\n\ |
| 8665 | Extracts a device minor number from a raw device number."); |
| 8666 | |
| 8667 | static PyObject * |
| 8668 | posix_minor(PyObject *self, PyObject *args) |
| 8669 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8670 | int device; |
| 8671 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 8672 | return NULL; |
| 8673 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8674 | } |
| 8675 | |
| 8676 | PyDoc_STRVAR(posix_makedev__doc__, |
| 8677 | "makedev(major, minor) -> device number\n\ |
| 8678 | Composes a raw device number from the major and minor device numbers."); |
| 8679 | |
| 8680 | static PyObject * |
| 8681 | posix_makedev(PyObject *self, PyObject *args) |
| 8682 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8683 | int major, minor; |
| 8684 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 8685 | return NULL; |
| 8686 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8687 | } |
| 8688 | #endif /* device macros */ |
| 8689 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8690 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8691 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8692 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8693 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8694 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8695 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8696 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8697 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8698 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8699 | int fd; |
| 8700 | off_t length; |
| 8701 | int res; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8702 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8703 | if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8704 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8705 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8706 | Py_BEGIN_ALLOW_THREADS |
| 8707 | res = ftruncate(fd, length); |
| 8708 | Py_END_ALLOW_THREADS |
| 8709 | if (res < 0) |
| 8710 | return posix_error(); |
| 8711 | Py_INCREF(Py_None); |
| 8712 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8713 | } |
| 8714 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8715 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8716 | #ifdef HAVE_TRUNCATE |
| 8717 | PyDoc_STRVAR(posix_truncate__doc__, |
| 8718 | "truncate(path, length)\n\n\ |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8719 | Truncate the file given by path to length bytes.\n\ |
| 8720 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 8721 | If this functionality is unavailable, using it raises an exception."); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8722 | |
| 8723 | static PyObject * |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8724 | posix_truncate(PyObject *self, PyObject *args, PyObject *kwargs) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8725 | { |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8726 | path_t path; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8727 | off_t length; |
| 8728 | int res; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8729 | PyObject *result = NULL; |
| 8730 | static char *keywords[] = {"path", "length", NULL}; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8731 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8732 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8733 | path.function_name = "truncate"; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8734 | #ifdef HAVE_FTRUNCATE |
| 8735 | path.allow_fd = 1; |
| 8736 | #endif |
| 8737 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:truncate", keywords, |
| 8738 | path_converter, &path, |
| 8739 | _parse_off_t, &length)) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8740 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8741 | |
| 8742 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8743 | #ifdef HAVE_FTRUNCATE |
| 8744 | if (path.fd != -1) |
| 8745 | res = ftruncate(path.fd, length); |
| 8746 | else |
| 8747 | #endif |
| 8748 | res = truncate(path.narrow, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8749 | Py_END_ALLOW_THREADS |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8750 | if (res < 0) |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8751 | result = path_error(&path); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8752 | else { |
| 8753 | Py_INCREF(Py_None); |
| 8754 | result = Py_None; |
| 8755 | } |
| 8756 | path_cleanup(&path); |
| 8757 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8758 | } |
| 8759 | #endif |
| 8760 | |
| 8761 | #ifdef HAVE_POSIX_FALLOCATE |
| 8762 | PyDoc_STRVAR(posix_posix_fallocate__doc__, |
| 8763 | "posix_fallocate(fd, offset, len)\n\n\ |
| 8764 | Ensures that enough disk space is allocated for the file specified by fd\n\ |
| 8765 | starting from offset and continuing for len bytes."); |
| 8766 | |
| 8767 | static PyObject * |
| 8768 | posix_posix_fallocate(PyObject *self, PyObject *args) |
| 8769 | { |
| 8770 | off_t len, offset; |
| 8771 | int res, fd; |
| 8772 | |
| 8773 | if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate", |
| 8774 | &fd, _parse_off_t, &offset, _parse_off_t, &len)) |
| 8775 | return NULL; |
| 8776 | |
| 8777 | Py_BEGIN_ALLOW_THREADS |
| 8778 | res = posix_fallocate(fd, offset, len); |
| 8779 | Py_END_ALLOW_THREADS |
| 8780 | if (res != 0) { |
| 8781 | errno = res; |
| 8782 | return posix_error(); |
| 8783 | } |
| 8784 | Py_RETURN_NONE; |
| 8785 | } |
| 8786 | #endif |
| 8787 | |
| 8788 | #ifdef HAVE_POSIX_FADVISE |
| 8789 | PyDoc_STRVAR(posix_posix_fadvise__doc__, |
| 8790 | "posix_fadvise(fd, offset, len, advice)\n\n\ |
| 8791 | Announces an intention to access data in a specific pattern thus allowing\n\ |
| 8792 | the kernel to make optimizations.\n\ |
| 8793 | The advice applies to the region of the file specified by fd starting at\n\ |
| 8794 | offset and continuing for len bytes.\n\ |
| 8795 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\ |
| 8796 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\ |
| 8797 | POSIX_FADV_DONTNEED."); |
| 8798 | |
| 8799 | static PyObject * |
| 8800 | posix_posix_fadvise(PyObject *self, PyObject *args) |
| 8801 | { |
| 8802 | off_t len, offset; |
| 8803 | int res, fd, advice; |
| 8804 | |
| 8805 | if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise", |
| 8806 | &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice)) |
| 8807 | return NULL; |
| 8808 | |
| 8809 | Py_BEGIN_ALLOW_THREADS |
| 8810 | res = posix_fadvise(fd, offset, len, advice); |
| 8811 | Py_END_ALLOW_THREADS |
| 8812 | if (res != 0) { |
| 8813 | errno = res; |
| 8814 | return posix_error(); |
| 8815 | } |
| 8816 | Py_RETURN_NONE; |
| 8817 | } |
| 8818 | #endif |
| 8819 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8820 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8821 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8822 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8823 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8824 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8825 | /* Save putenv() parameters as values here, so we can collect them when they |
| 8826 | * get re-set with another call for the same key. */ |
| 8827 | static PyObject *posix_putenv_garbage; |
| 8828 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8829 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8830 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8831 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8832 | PyObject *newstr = NULL; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8833 | #ifdef MS_WINDOWS |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8834 | PyObject *os1, *os2; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8835 | wchar_t *newenv; |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8836 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8837 | if (!PyArg_ParseTuple(args, |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8838 | "UU:putenv", |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8839 | &os1, &os2)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8840 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8841 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8842 | newstr = PyUnicode_FromFormat("%U=%U", os1, os2); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8843 | if (newstr == NULL) { |
| 8844 | PyErr_NoMemory(); |
| 8845 | goto error; |
| 8846 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8847 | if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) { |
| 8848 | PyErr_Format(PyExc_ValueError, |
| 8849 | "the environment variable is longer than %u characters", |
| 8850 | _MAX_ENV); |
| 8851 | goto error; |
| 8852 | } |
| 8853 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8854 | newenv = PyUnicode_AsUnicode(newstr); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 8855 | if (newenv == NULL) |
| 8856 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8857 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8858 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8859 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8860 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8861 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8862 | PyObject *os1, *os2; |
| 8863 | char *s1, *s2; |
| 8864 | char *newenv; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8865 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8866 | if (!PyArg_ParseTuple(args, |
| 8867 | "O&O&:putenv", |
| 8868 | PyUnicode_FSConverter, &os1, |
| 8869 | PyUnicode_FSConverter, &os2)) |
| 8870 | return NULL; |
| 8871 | s1 = PyBytes_AsString(os1); |
| 8872 | s2 = PyBytes_AsString(os2); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8873 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8874 | newstr = PyBytes_FromFormat("%s=%s", s1, s2); |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8875 | if (newstr == NULL) { |
| 8876 | PyErr_NoMemory(); |
| 8877 | goto error; |
| 8878 | } |
| 8879 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8880 | newenv = PyBytes_AS_STRING(newstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8881 | if (putenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8882 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8883 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8884 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8885 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8886 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8887 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 8888 | * this will cause previous value to be collected. This has to |
| 8889 | * happen after the real putenv() call because the old value |
| 8890 | * was still accessible until then. */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8891 | if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8892 | /* really not much we can do; just leak */ |
| 8893 | PyErr_Clear(); |
| 8894 | } |
| 8895 | else { |
| 8896 | Py_DECREF(newstr); |
| 8897 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8898 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8899 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8900 | Py_DECREF(os1); |
| 8901 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8902 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8903 | Py_RETURN_NONE; |
| 8904 | |
| 8905 | error: |
| 8906 | #ifndef MS_WINDOWS |
| 8907 | Py_DECREF(os1); |
| 8908 | Py_DECREF(os2); |
| 8909 | #endif |
| 8910 | Py_XDECREF(newstr); |
| 8911 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8912 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8913 | #endif /* putenv */ |
| 8914 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8915 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8916 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8917 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8918 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8919 | |
| 8920 | static PyObject * |
| 8921 | posix_unsetenv(PyObject *self, PyObject *args) |
| 8922 | { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8923 | PyObject *name; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8924 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8925 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8926 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8927 | |
| 8928 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8929 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8930 | PyUnicode_FSConverter, &name)) |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8931 | return NULL; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8932 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8933 | #ifdef HAVE_BROKEN_UNSETENV |
| 8934 | unsetenv(PyBytes_AS_STRING(name)); |
| 8935 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8936 | err = unsetenv(PyBytes_AS_STRING(name)); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8937 | if (err) { |
Benjamin Peterson | e8eb0e8 | 2011-11-22 23:14:47 -0600 | [diff] [blame] | 8938 | Py_DECREF(name); |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8939 | return posix_error(); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8940 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8941 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8942 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8943 | /* Remove the key from posix_putenv_garbage; |
| 8944 | * this will cause it to be collected. This has to |
| 8945 | * happen after the real unsetenv() call because the |
| 8946 | * old value was still accessible until then. |
| 8947 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8948 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8949 | /* really not much we can do; just leak */ |
| 8950 | PyErr_Clear(); |
| 8951 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8952 | Py_DECREF(name); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8953 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8954 | } |
| 8955 | #endif /* unsetenv */ |
| 8956 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8957 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8958 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8959 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8960 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 8961 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8962 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8963 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8964 | int code; |
| 8965 | char *message; |
| 8966 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 8967 | return NULL; |
| 8968 | message = strerror(code); |
| 8969 | if (message == NULL) { |
| 8970 | PyErr_SetString(PyExc_ValueError, |
| 8971 | "strerror() argument out of range"); |
| 8972 | return NULL; |
| 8973 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 8974 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8975 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8976 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8977 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8978 | #ifdef HAVE_SYS_WAIT_H |
| 8979 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8980 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8981 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8982 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8983 | 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] | 8984 | |
| 8985 | static PyObject * |
| 8986 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 8987 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8988 | WAIT_TYPE status; |
| 8989 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8990 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8991 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 8992 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8993 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8994 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8995 | } |
| 8996 | #endif /* WCOREDUMP */ |
| 8997 | |
| 8998 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8999 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9000 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9001 | 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] | 9002 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9003 | |
| 9004 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 9005 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9006 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9007 | WAIT_TYPE status; |
| 9008 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9009 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9010 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 9011 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9012 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9013 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9014 | } |
| 9015 | #endif /* WIFCONTINUED */ |
| 9016 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9017 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9018 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9019 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9020 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9021 | |
| 9022 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9023 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9024 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9025 | WAIT_TYPE status; |
| 9026 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9027 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9028 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 9029 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9030 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9031 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9032 | } |
| 9033 | #endif /* WIFSTOPPED */ |
| 9034 | |
| 9035 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9036 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9037 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9038 | 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] | 9039 | |
| 9040 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9041 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9042 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9043 | WAIT_TYPE status; |
| 9044 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9045 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9046 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 9047 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9048 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9049 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9050 | } |
| 9051 | #endif /* WIFSIGNALED */ |
| 9052 | |
| 9053 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9054 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9055 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 9056 | 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] | 9057 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9058 | |
| 9059 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9060 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9061 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9062 | WAIT_TYPE status; |
| 9063 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9064 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9065 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 9066 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9067 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9068 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9069 | } |
| 9070 | #endif /* WIFEXITED */ |
| 9071 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 9072 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9073 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9074 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9075 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9076 | |
| 9077 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9078 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9079 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9080 | WAIT_TYPE status; |
| 9081 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9082 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9083 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 9084 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9085 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9086 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9087 | } |
| 9088 | #endif /* WEXITSTATUS */ |
| 9089 | |
| 9090 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9091 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9092 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 9093 | 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] | 9094 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9095 | |
| 9096 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9097 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9098 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9099 | WAIT_TYPE status; |
| 9100 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9101 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9102 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 9103 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9104 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9105 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9106 | } |
| 9107 | #endif /* WTERMSIG */ |
| 9108 | |
| 9109 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9110 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9111 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9112 | Return the signal that stopped the process that provided\n\ |
| 9113 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9114 | |
| 9115 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9116 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9117 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9118 | WAIT_TYPE status; |
| 9119 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9120 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9121 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 9122 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9123 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9124 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9125 | } |
| 9126 | #endif /* WSTOPSIG */ |
| 9127 | |
| 9128 | #endif /* HAVE_SYS_WAIT_H */ |
| 9129 | |
| 9130 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9131 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 9132 | #ifdef _SCO_DS |
| 9133 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 9134 | needed definitions in sys/statvfs.h */ |
| 9135 | #define _SVID3 |
| 9136 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9137 | #include <sys/statvfs.h> |
| 9138 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9139 | static PyObject* |
| 9140 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9141 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 9142 | if (v == NULL) |
| 9143 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9144 | |
| 9145 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9146 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9147 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9148 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 9149 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 9150 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 9151 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 9152 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 9153 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 9154 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9155 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9156 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9157 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9158 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9159 | PyStructSequence_SET_ITEM(v, 2, |
| 9160 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 9161 | PyStructSequence_SET_ITEM(v, 3, |
| 9162 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 9163 | PyStructSequence_SET_ITEM(v, 4, |
| 9164 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 9165 | PyStructSequence_SET_ITEM(v, 5, |
| 9166 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 9167 | PyStructSequence_SET_ITEM(v, 6, |
| 9168 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 9169 | PyStructSequence_SET_ITEM(v, 7, |
| 9170 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 9171 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9172 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9173 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 9174 | if (PyErr_Occurred()) { |
| 9175 | Py_DECREF(v); |
| 9176 | return NULL; |
| 9177 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9178 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9179 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9180 | } |
| 9181 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9182 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9183 | "fstatvfs(fd) -> statvfs result\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9184 | Perform an fstatvfs system call on the given fd.\n\ |
| 9185 | Equivalent to statvfs(fd)."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9186 | |
| 9187 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9188 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9189 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9190 | int fd, res; |
| 9191 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9192 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9193 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 9194 | return NULL; |
| 9195 | Py_BEGIN_ALLOW_THREADS |
| 9196 | res = fstatvfs(fd, &st); |
| 9197 | Py_END_ALLOW_THREADS |
| 9198 | if (res != 0) |
| 9199 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9200 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9201 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9202 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9203 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9204 | |
| 9205 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9206 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9207 | #include <sys/statvfs.h> |
| 9208 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9209 | PyDoc_STRVAR(posix_statvfs__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9210 | "statvfs(path)\n\n\ |
| 9211 | Perform a statvfs system call on the given path.\n\ |
| 9212 | \n\ |
| 9213 | path may always be specified as a string.\n\ |
| 9214 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 9215 | If this functionality is unavailable, using it raises an exception."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9216 | |
| 9217 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9218 | posix_statvfs(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9219 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9220 | static char *keywords[] = {"path", NULL}; |
| 9221 | path_t path; |
| 9222 | int result; |
| 9223 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9224 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9225 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9226 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9227 | path.function_name = "statvfs"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9228 | #ifdef HAVE_FSTATVFS |
| 9229 | path.allow_fd = 1; |
| 9230 | #endif |
| 9231 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:statvfs", keywords, |
| 9232 | path_converter, &path |
| 9233 | )) |
| 9234 | return NULL; |
| 9235 | |
| 9236 | Py_BEGIN_ALLOW_THREADS |
| 9237 | #ifdef HAVE_FSTATVFS |
| 9238 | if (path.fd != -1) { |
| 9239 | #ifdef __APPLE__ |
| 9240 | /* handle weak-linking on Mac OS X 10.3 */ |
| 9241 | if (fstatvfs == NULL) { |
| 9242 | fd_specified("statvfs", path.fd); |
| 9243 | goto exit; |
| 9244 | } |
| 9245 | #endif |
| 9246 | result = fstatvfs(path.fd, &st); |
| 9247 | } |
| 9248 | else |
| 9249 | #endif |
| 9250 | result = statvfs(path.narrow, &st); |
| 9251 | Py_END_ALLOW_THREADS |
| 9252 | |
| 9253 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9254 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9255 | goto exit; |
| 9256 | } |
| 9257 | |
| 9258 | return_value = _pystatvfs_fromstructstatvfs(st); |
| 9259 | |
| 9260 | exit: |
| 9261 | path_cleanup(&path); |
| 9262 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9263 | } |
| 9264 | #endif /* HAVE_STATVFS */ |
| 9265 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9266 | #ifdef MS_WINDOWS |
| 9267 | PyDoc_STRVAR(win32__getdiskusage__doc__, |
| 9268 | "_getdiskusage(path) -> (total, free)\n\n\ |
| 9269 | Return disk usage statistics about the given path as (total, free) tuple."); |
| 9270 | |
| 9271 | static PyObject * |
| 9272 | win32__getdiskusage(PyObject *self, PyObject *args) |
| 9273 | { |
| 9274 | BOOL retval; |
| 9275 | ULARGE_INTEGER _, total, free; |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 9276 | const wchar_t *path; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9277 | |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 9278 | if (! PyArg_ParseTuple(args, "u", &path)) |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9279 | return NULL; |
| 9280 | |
| 9281 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 9282 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9283 | Py_END_ALLOW_THREADS |
| 9284 | if (retval == 0) |
| 9285 | return PyErr_SetFromWindowsErr(0); |
| 9286 | |
| 9287 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 9288 | } |
| 9289 | #endif |
| 9290 | |
| 9291 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9292 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 9293 | * It maps strings representing configuration variable names to |
| 9294 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 9295 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9296 | * rarely-used constants. There are three separate tables that use |
| 9297 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9298 | * |
| 9299 | * This code is always included, even if none of the interfaces that |
| 9300 | * need it are included. The #if hackery needed to avoid it would be |
| 9301 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9302 | */ |
| 9303 | struct constdef { |
| 9304 | char *name; |
| 9305 | long value; |
| 9306 | }; |
| 9307 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9308 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9309 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 9310 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9311 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9312 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9313 | *valuep = PyLong_AS_LONG(arg); |
| 9314 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9315 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 9316 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9317 | /* look up the value in the table using a binary search */ |
| 9318 | size_t lo = 0; |
| 9319 | size_t mid; |
| 9320 | size_t hi = tablesize; |
| 9321 | int cmp; |
| 9322 | const char *confname; |
| 9323 | if (!PyUnicode_Check(arg)) { |
| 9324 | PyErr_SetString(PyExc_TypeError, |
| 9325 | "configuration names must be strings or integers"); |
| 9326 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9327 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9328 | confname = _PyUnicode_AsString(arg); |
| 9329 | if (confname == NULL) |
| 9330 | return 0; |
| 9331 | while (lo < hi) { |
| 9332 | mid = (lo + hi) / 2; |
| 9333 | cmp = strcmp(confname, table[mid].name); |
| 9334 | if (cmp < 0) |
| 9335 | hi = mid; |
| 9336 | else if (cmp > 0) |
| 9337 | lo = mid + 1; |
| 9338 | else { |
| 9339 | *valuep = table[mid].value; |
| 9340 | return 1; |
| 9341 | } |
| 9342 | } |
| 9343 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 9344 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9345 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9346 | } |
| 9347 | |
| 9348 | |
| 9349 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 9350 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9351 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9352 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9353 | #endif |
| 9354 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9355 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9356 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9357 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9358 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9359 | #endif |
| 9360 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9361 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9362 | #endif |
| 9363 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9364 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9365 | #endif |
| 9366 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9367 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9368 | #endif |
| 9369 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9370 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9371 | #endif |
| 9372 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9373 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9374 | #endif |
| 9375 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9376 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9377 | #endif |
| 9378 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9379 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9380 | #endif |
| 9381 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9382 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9383 | #endif |
| 9384 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9385 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9386 | #endif |
| 9387 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9388 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9389 | #endif |
| 9390 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9391 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9392 | #endif |
| 9393 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9394 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9395 | #endif |
| 9396 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9397 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9398 | #endif |
| 9399 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9400 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9401 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 9402 | #ifdef _PC_ACL_ENABLED |
| 9403 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 9404 | #endif |
| 9405 | #ifdef _PC_MIN_HOLE_SIZE |
| 9406 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 9407 | #endif |
| 9408 | #ifdef _PC_ALLOC_SIZE_MIN |
| 9409 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 9410 | #endif |
| 9411 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 9412 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 9413 | #endif |
| 9414 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 9415 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 9416 | #endif |
| 9417 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 9418 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 9419 | #endif |
| 9420 | #ifdef _PC_REC_XFER_ALIGN |
| 9421 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 9422 | #endif |
| 9423 | #ifdef _PC_SYMLINK_MAX |
| 9424 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 9425 | #endif |
| 9426 | #ifdef _PC_XATTR_ENABLED |
| 9427 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 9428 | #endif |
| 9429 | #ifdef _PC_XATTR_EXISTS |
| 9430 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 9431 | #endif |
| 9432 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 9433 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 9434 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9435 | }; |
| 9436 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9437 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9438 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9439 | { |
| 9440 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 9441 | sizeof(posix_constants_pathconf) |
| 9442 | / sizeof(struct constdef)); |
| 9443 | } |
| 9444 | #endif |
| 9445 | |
| 9446 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9447 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9448 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9449 | 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] | 9450 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9451 | |
| 9452 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9453 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9454 | { |
| 9455 | PyObject *result = NULL; |
| 9456 | int name, fd; |
| 9457 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9458 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 9459 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9460 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9461 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9462 | errno = 0; |
| 9463 | limit = fpathconf(fd, name); |
| 9464 | if (limit == -1 && errno != 0) |
| 9465 | posix_error(); |
| 9466 | else |
| 9467 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9468 | } |
| 9469 | return result; |
| 9470 | } |
| 9471 | #endif |
| 9472 | |
| 9473 | |
| 9474 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9475 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9476 | "pathconf(path, 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 or directory path.\n\ |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9478 | If there is no limit, return -1.\n\ |
| 9479 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 9480 | If this functionality is unavailable, using it raises an exception."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9481 | |
| 9482 | static PyObject * |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9483 | posix_pathconf(PyObject *self, PyObject *args, PyObject *kwargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9484 | { |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9485 | path_t path; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9486 | PyObject *result = NULL; |
| 9487 | int name; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9488 | static char *keywords[] = {"path", "name", NULL}; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9489 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9490 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9491 | path.function_name = "pathconf"; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9492 | #ifdef HAVE_FPATHCONF |
| 9493 | path.allow_fd = 1; |
| 9494 | #endif |
| 9495 | if (PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:pathconf", keywords, |
| 9496 | path_converter, &path, |
| 9497 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9498 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9499 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9500 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9501 | #ifdef HAVE_FPATHCONF |
| 9502 | if (path.fd != -1) |
| 9503 | limit = fpathconf(path.fd, name); |
| 9504 | else |
| 9505 | #endif |
| 9506 | limit = pathconf(path.narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9507 | if (limit == -1 && errno != 0) { |
| 9508 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 9509 | /* could be a path or name problem */ |
| 9510 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9511 | else |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9512 | result = path_error(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9513 | } |
| 9514 | else |
| 9515 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9516 | } |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9517 | path_cleanup(&path); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9518 | return result; |
| 9519 | } |
| 9520 | #endif |
| 9521 | |
| 9522 | #ifdef HAVE_CONFSTR |
| 9523 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9524 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9525 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9526 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9527 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9528 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9529 | #endif |
| 9530 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9531 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9532 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9533 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9534 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9535 | #endif |
| 9536 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9537 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9538 | #endif |
| 9539 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9540 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9541 | #endif |
| 9542 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9543 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9544 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9545 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9546 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9547 | #endif |
| 9548 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9549 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9550 | #endif |
| 9551 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9552 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9553 | #endif |
| 9554 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9555 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9556 | #endif |
| 9557 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9558 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9559 | #endif |
| 9560 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9561 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9562 | #endif |
| 9563 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9564 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9565 | #endif |
| 9566 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9567 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9568 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9569 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9570 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9571 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9572 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9573 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9574 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9575 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9576 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9577 | #endif |
| 9578 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9579 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9580 | #endif |
| 9581 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9582 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9583 | #endif |
| 9584 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9585 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9586 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9587 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9588 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9589 | #endif |
| 9590 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9591 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9592 | #endif |
| 9593 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9594 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9595 | #endif |
| 9596 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9597 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9598 | #endif |
| 9599 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9600 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9601 | #endif |
| 9602 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9603 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9604 | #endif |
| 9605 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9606 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9607 | #endif |
| 9608 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9609 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9610 | #endif |
| 9611 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9612 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9613 | #endif |
| 9614 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9615 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9616 | #endif |
| 9617 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9618 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9619 | #endif |
| 9620 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9621 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9622 | #endif |
| 9623 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9624 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9625 | #endif |
| 9626 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9627 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9628 | #endif |
| 9629 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9630 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9631 | #endif |
| 9632 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9633 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9634 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9635 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9636 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9637 | #endif |
| 9638 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9639 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9640 | #endif |
| 9641 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9642 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9643 | #endif |
| 9644 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9645 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9646 | #endif |
| 9647 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9648 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9649 | #endif |
| 9650 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9651 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9652 | #endif |
| 9653 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9654 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9655 | #endif |
| 9656 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9657 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9658 | #endif |
| 9659 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9660 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9661 | #endif |
| 9662 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9663 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9664 | #endif |
| 9665 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9666 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9667 | #endif |
| 9668 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9669 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9670 | #endif |
| 9671 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9672 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9673 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9674 | }; |
| 9675 | |
| 9676 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9677 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9678 | { |
| 9679 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 9680 | sizeof(posix_constants_confstr) |
| 9681 | / sizeof(struct constdef)); |
| 9682 | } |
| 9683 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9684 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9685 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9686 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9687 | |
| 9688 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9689 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9690 | { |
| 9691 | PyObject *result = NULL; |
| 9692 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9693 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9694 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9695 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9696 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 9697 | return NULL; |
| 9698 | |
| 9699 | errno = 0; |
| 9700 | len = confstr(name, buffer, sizeof(buffer)); |
| 9701 | if (len == 0) { |
| 9702 | if (errno) { |
| 9703 | posix_error(); |
| 9704 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9705 | } |
| 9706 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9707 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9708 | } |
| 9709 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9710 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9711 | if (len >= sizeof(buffer)) { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9712 | char *buf = PyMem_Malloc(len); |
| 9713 | if (buf == NULL) |
| 9714 | return PyErr_NoMemory(); |
| 9715 | confstr(name, buf, len); |
| 9716 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 9717 | PyMem_Free(buf); |
| 9718 | } |
| 9719 | else |
| 9720 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9721 | return result; |
| 9722 | } |
| 9723 | #endif |
| 9724 | |
| 9725 | |
| 9726 | #ifdef HAVE_SYSCONF |
| 9727 | static struct constdef posix_constants_sysconf[] = { |
| 9728 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9729 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9730 | #endif |
| 9731 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9732 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9733 | #endif |
| 9734 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9735 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9736 | #endif |
| 9737 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9738 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9739 | #endif |
| 9740 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9741 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9742 | #endif |
| 9743 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9744 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9745 | #endif |
| 9746 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9747 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9748 | #endif |
| 9749 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9750 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9751 | #endif |
| 9752 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9753 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9754 | #endif |
| 9755 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9756 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9757 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9758 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9759 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9760 | #endif |
| 9761 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9762 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9763 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9764 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9765 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9766 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9767 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9768 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9769 | #endif |
| 9770 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9771 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9772 | #endif |
| 9773 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9774 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9775 | #endif |
| 9776 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9777 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9778 | #endif |
| 9779 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9780 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9781 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9782 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9783 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9784 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9785 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9786 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9787 | #endif |
| 9788 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9789 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9790 | #endif |
| 9791 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9792 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9793 | #endif |
| 9794 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9795 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9796 | #endif |
| 9797 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9798 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9799 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9800 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9801 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9802 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9803 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9804 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9805 | #endif |
| 9806 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9807 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9808 | #endif |
| 9809 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9810 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9811 | #endif |
| 9812 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9813 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9814 | #endif |
| 9815 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9816 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9817 | #endif |
| 9818 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9819 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9820 | #endif |
| 9821 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9822 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9823 | #endif |
| 9824 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9825 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9826 | #endif |
| 9827 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9828 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9829 | #endif |
| 9830 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9831 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9832 | #endif |
| 9833 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9834 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9835 | #endif |
| 9836 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9837 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9838 | #endif |
| 9839 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9840 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9841 | #endif |
| 9842 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9843 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9844 | #endif |
| 9845 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9846 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9847 | #endif |
| 9848 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9849 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9850 | #endif |
| 9851 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9852 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9853 | #endif |
| 9854 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9855 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9856 | #endif |
| 9857 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9858 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9859 | #endif |
| 9860 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9861 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9862 | #endif |
| 9863 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9864 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9865 | #endif |
| 9866 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9867 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9868 | #endif |
| 9869 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9870 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9871 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9872 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9873 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9874 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9875 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9876 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9877 | #endif |
| 9878 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9879 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9880 | #endif |
| 9881 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9882 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9883 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9884 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9885 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9886 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9887 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9888 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9889 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9890 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9891 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9892 | #endif |
| 9893 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9894 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9895 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9896 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9897 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9898 | #endif |
| 9899 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9900 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9901 | #endif |
| 9902 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9903 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9904 | #endif |
| 9905 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9906 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9907 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9908 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9909 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9910 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9911 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9912 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9913 | #endif |
| 9914 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9915 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9916 | #endif |
| 9917 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9918 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9919 | #endif |
| 9920 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9921 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9922 | #endif |
| 9923 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9924 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9925 | #endif |
| 9926 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9927 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9928 | #endif |
| 9929 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9930 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9931 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9932 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9933 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9934 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9935 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9936 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9937 | #endif |
| 9938 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9939 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9940 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9941 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9942 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9943 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9944 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9945 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9946 | #endif |
| 9947 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9948 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9949 | #endif |
| 9950 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9951 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9952 | #endif |
| 9953 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9954 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9955 | #endif |
| 9956 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9957 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9958 | #endif |
| 9959 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9960 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9961 | #endif |
| 9962 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9963 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9964 | #endif |
| 9965 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9966 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9967 | #endif |
| 9968 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9969 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9970 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9971 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9972 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9973 | #endif |
| 9974 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9975 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9976 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9977 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9978 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9979 | #endif |
| 9980 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9981 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9982 | #endif |
| 9983 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9984 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9985 | #endif |
| 9986 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9987 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9988 | #endif |
| 9989 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9990 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9991 | #endif |
| 9992 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9993 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9994 | #endif |
| 9995 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9996 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9997 | #endif |
| 9998 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9999 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10000 | #endif |
| 10001 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10002 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10003 | #endif |
| 10004 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10005 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10006 | #endif |
| 10007 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10008 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10009 | #endif |
| 10010 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10011 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10012 | #endif |
| 10013 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10014 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10015 | #endif |
| 10016 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10017 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10018 | #endif |
| 10019 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10020 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10021 | #endif |
| 10022 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10023 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10024 | #endif |
| 10025 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10026 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10027 | #endif |
| 10028 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10029 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10030 | #endif |
| 10031 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10032 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10033 | #endif |
| 10034 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10035 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10036 | #endif |
| 10037 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10038 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10039 | #endif |
| 10040 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10041 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10042 | #endif |
| 10043 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10044 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10045 | #endif |
| 10046 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10047 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10048 | #endif |
| 10049 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10050 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10051 | #endif |
| 10052 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10053 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10054 | #endif |
| 10055 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10056 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10057 | #endif |
| 10058 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10059 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10060 | #endif |
| 10061 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10062 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10063 | #endif |
| 10064 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10065 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10066 | #endif |
| 10067 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10068 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10069 | #endif |
| 10070 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10071 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10072 | #endif |
| 10073 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10074 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10075 | #endif |
| 10076 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10077 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10078 | #endif |
| 10079 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10080 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10081 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10082 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10083 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10084 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10085 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10086 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10087 | #endif |
| 10088 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10089 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10090 | #endif |
| 10091 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10092 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10093 | #endif |
| 10094 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10095 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10096 | #endif |
| 10097 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10098 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10099 | #endif |
| 10100 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10101 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10102 | #endif |
| 10103 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10104 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10105 | #endif |
| 10106 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10107 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10108 | #endif |
| 10109 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10110 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10111 | #endif |
| 10112 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10113 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10114 | #endif |
| 10115 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10116 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10117 | #endif |
| 10118 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10119 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10120 | #endif |
| 10121 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10122 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10123 | #endif |
| 10124 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10125 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10126 | #endif |
| 10127 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10128 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10129 | #endif |
| 10130 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10131 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10132 | #endif |
| 10133 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10134 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10135 | #endif |
| 10136 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10137 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10138 | #endif |
| 10139 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10140 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10141 | #endif |
| 10142 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10143 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10144 | #endif |
| 10145 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10146 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10147 | #endif |
| 10148 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10149 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10150 | #endif |
| 10151 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10152 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10153 | #endif |
| 10154 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10155 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10156 | #endif |
| 10157 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10158 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10159 | #endif |
| 10160 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10161 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10162 | #endif |
| 10163 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10164 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10165 | #endif |
| 10166 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10167 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10168 | #endif |
| 10169 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10170 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10171 | #endif |
| 10172 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10173 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10174 | #endif |
| 10175 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10176 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10177 | #endif |
| 10178 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10179 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10180 | #endif |
| 10181 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10182 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10183 | #endif |
| 10184 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10185 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10186 | #endif |
| 10187 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10188 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10189 | #endif |
| 10190 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10191 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10192 | #endif |
| 10193 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10194 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10195 | #endif |
| 10196 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10197 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10198 | #endif |
| 10199 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10200 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10201 | #endif |
| 10202 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10203 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10204 | #endif |
| 10205 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10206 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10207 | #endif |
| 10208 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10209 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10210 | #endif |
| 10211 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10212 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10213 | #endif |
| 10214 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10215 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10216 | #endif |
| 10217 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10218 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10219 | #endif |
| 10220 | }; |
| 10221 | |
| 10222 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10223 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10224 | { |
| 10225 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 10226 | sizeof(posix_constants_sysconf) |
| 10227 | / sizeof(struct constdef)); |
| 10228 | } |
| 10229 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10230 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 10231 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10232 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10233 | |
| 10234 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10235 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10236 | { |
| 10237 | PyObject *result = NULL; |
| 10238 | int name; |
| 10239 | |
| 10240 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
Victor Stinner | 6fdd7b8 | 2013-05-16 22:26:29 +0200 | [diff] [blame] | 10241 | long value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10242 | |
| 10243 | errno = 0; |
| 10244 | value = sysconf(name); |
| 10245 | if (value == -1 && errno != 0) |
| 10246 | posix_error(); |
| 10247 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10248 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10249 | } |
| 10250 | return result; |
| 10251 | } |
| 10252 | #endif |
| 10253 | |
| 10254 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10255 | /* This code is used to ensure that the tables of configuration value names |
| 10256 | * are in sorted order as required by conv_confname(), and also to build the |
| 10257 | * the exported dictionaries that are used to publish information about the |
| 10258 | * names available on the host platform. |
| 10259 | * |
| 10260 | * Sorting the table at runtime ensures that the table is properly ordered |
| 10261 | * when used, even for platforms we're not able to test on. It also makes |
| 10262 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10263 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10264 | |
| 10265 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10266 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10267 | { |
| 10268 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10269 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10270 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10271 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10272 | |
| 10273 | return strcmp(c1->name, c2->name); |
| 10274 | } |
| 10275 | |
| 10276 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10277 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10278 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10279 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10280 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10281 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10282 | |
| 10283 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 10284 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10285 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10286 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10287 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10288 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10289 | PyObject *o = PyLong_FromLong(table[i].value); |
| 10290 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 10291 | Py_XDECREF(o); |
| 10292 | Py_DECREF(d); |
| 10293 | return -1; |
| 10294 | } |
| 10295 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10296 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10297 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10298 | } |
| 10299 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10300 | /* Return -1 on failure, 0 on success. */ |
| 10301 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10302 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10303 | { |
| 10304 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10305 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10306 | sizeof(posix_constants_pathconf) |
| 10307 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10308 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10309 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10310 | #endif |
| 10311 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10312 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10313 | sizeof(posix_constants_confstr) |
| 10314 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10315 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10316 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10317 | #endif |
| 10318 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10319 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10320 | sizeof(posix_constants_sysconf) |
| 10321 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10322 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10323 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10324 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10325 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10326 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10327 | |
| 10328 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10329 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 10330 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10331 | 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] | 10332 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10333 | |
| 10334 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 10335 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10336 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10337 | abort(); |
| 10338 | /*NOTREACHED*/ |
| 10339 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 10340 | return NULL; |
| 10341 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10342 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10343 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10344 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 10345 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 10346 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10347 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 10348 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 10349 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 10350 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 10351 | application (if any) its extension is associated.\n\ |
| 10352 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 10353 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10354 | \n\ |
| 10355 | startfile returns as soon as the associated application is launched.\n\ |
| 10356 | There is no option to wait for the application to close, and no way\n\ |
| 10357 | to retrieve the application's exit status.\n\ |
| 10358 | \n\ |
| 10359 | The filepath is relative to the current directory. If you want to use\n\ |
| 10360 | 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] | 10361 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10362 | |
| 10363 | static PyObject * |
| 10364 | win32_startfile(PyObject *self, PyObject *args) |
| 10365 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10366 | PyObject *ofilepath; |
| 10367 | char *filepath; |
| 10368 | char *operation = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10369 | wchar_t *wpath, *woperation; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10370 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 10371 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10372 | PyObject *unipath, *uoperation = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10373 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 10374 | &unipath, &operation)) { |
| 10375 | PyErr_Clear(); |
| 10376 | goto normal; |
| 10377 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10378 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10379 | if (operation) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10380 | uoperation = PyUnicode_DecodeASCII(operation, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10381 | strlen(operation), NULL); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10382 | if (!uoperation) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10383 | PyErr_Clear(); |
| 10384 | operation = NULL; |
| 10385 | goto normal; |
| 10386 | } |
| 10387 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10388 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10389 | wpath = PyUnicode_AsUnicode(unipath); |
| 10390 | if (wpath == NULL) |
| 10391 | goto normal; |
| 10392 | if (uoperation) { |
| 10393 | woperation = PyUnicode_AsUnicode(uoperation); |
| 10394 | if (woperation == NULL) |
| 10395 | goto normal; |
| 10396 | } |
| 10397 | else |
| 10398 | woperation = NULL; |
| 10399 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10400 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10401 | rc = ShellExecuteW((HWND)0, woperation, wpath, |
| 10402 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10403 | Py_END_ALLOW_THREADS |
| 10404 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10405 | Py_XDECREF(uoperation); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10406 | if (rc <= (HINSTANCE)32) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10407 | win32_error_object("startfile", unipath); |
| 10408 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10409 | } |
| 10410 | Py_INCREF(Py_None); |
| 10411 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10412 | |
| 10413 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10414 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 10415 | PyUnicode_FSConverter, &ofilepath, |
| 10416 | &operation)) |
| 10417 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 10418 | if (win32_warn_bytes_api()) { |
| 10419 | Py_DECREF(ofilepath); |
| 10420 | return NULL; |
| 10421 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10422 | filepath = PyBytes_AsString(ofilepath); |
| 10423 | Py_BEGIN_ALLOW_THREADS |
| 10424 | rc = ShellExecute((HWND)0, operation, filepath, |
| 10425 | NULL, NULL, SW_SHOWNORMAL); |
| 10426 | Py_END_ALLOW_THREADS |
| 10427 | if (rc <= (HINSTANCE)32) { |
| 10428 | PyObject *errval = win32_error("startfile", filepath); |
| 10429 | Py_DECREF(ofilepath); |
| 10430 | return errval; |
| 10431 | } |
| 10432 | Py_DECREF(ofilepath); |
| 10433 | Py_INCREF(Py_None); |
| 10434 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10435 | } |
| 10436 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10437 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10438 | #ifdef HAVE_GETLOADAVG |
| 10439 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 10440 | "getloadavg() -> (float, float, float)\n\n\ |
| 10441 | Return the number of processes in the system run queue averaged over\n\ |
| 10442 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 10443 | was unobtainable"); |
| 10444 | |
| 10445 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 10446 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10447 | { |
| 10448 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10449 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10450 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 10451 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10452 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10453 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10454 | } |
| 10455 | #endif |
| 10456 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 10457 | PyDoc_STRVAR(device_encoding__doc__, |
| 10458 | "device_encoding(fd) -> str\n\n\ |
| 10459 | Return a string describing the encoding of the device\n\ |
| 10460 | if the output is a terminal; else return None."); |
| 10461 | |
| 10462 | static PyObject * |
| 10463 | device_encoding(PyObject *self, PyObject *args) |
| 10464 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10465 | int fd; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10466 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10467 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 10468 | return NULL; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10469 | |
| 10470 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 10471 | } |
| 10472 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10473 | #ifdef HAVE_SETRESUID |
| 10474 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 10475 | "setresuid(ruid, euid, suid)\n\n\ |
| 10476 | Set the current process's real, effective, and saved user ids."); |
| 10477 | |
| 10478 | static PyObject* |
| 10479 | posix_setresuid (PyObject *self, PyObject *args) |
| 10480 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10481 | /* We assume uid_t is no larger than a long. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10482 | uid_t ruid, euid, suid; |
| 10483 | if (!PyArg_ParseTuple(args, "O&O&O&:setresuid", |
| 10484 | _Py_Uid_Converter, &ruid, |
| 10485 | _Py_Uid_Converter, &euid, |
| 10486 | _Py_Uid_Converter, &suid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10487 | return NULL; |
| 10488 | if (setresuid(ruid, euid, suid) < 0) |
| 10489 | return posix_error(); |
| 10490 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10491 | } |
| 10492 | #endif |
| 10493 | |
| 10494 | #ifdef HAVE_SETRESGID |
| 10495 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 10496 | "setresgid(rgid, egid, sgid)\n\n\ |
| 10497 | Set the current process's real, effective, and saved group ids."); |
| 10498 | |
| 10499 | static PyObject* |
| 10500 | posix_setresgid (PyObject *self, PyObject *args) |
| 10501 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10502 | gid_t rgid, egid, sgid; |
| 10503 | if (!PyArg_ParseTuple(args, "O&O&O&:setresgid", |
| 10504 | _Py_Gid_Converter, &rgid, |
| 10505 | _Py_Gid_Converter, &egid, |
| 10506 | _Py_Gid_Converter, &sgid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10507 | return NULL; |
| 10508 | if (setresgid(rgid, egid, sgid) < 0) |
| 10509 | return posix_error(); |
| 10510 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10511 | } |
| 10512 | #endif |
| 10513 | |
| 10514 | #ifdef HAVE_GETRESUID |
| 10515 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 10516 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 10517 | Get tuple of the current process's real, effective, and saved user ids."); |
| 10518 | |
| 10519 | static PyObject* |
| 10520 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 10521 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10522 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10523 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 10524 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10525 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 10526 | _PyLong_FromUid(euid), |
| 10527 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10528 | } |
| 10529 | #endif |
| 10530 | |
| 10531 | #ifdef HAVE_GETRESGID |
| 10532 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 10533 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 10534 | 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] | 10535 | |
| 10536 | static PyObject* |
| 10537 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 10538 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10539 | uid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10540 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 10541 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10542 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 10543 | _PyLong_FromGid(egid), |
| 10544 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10545 | } |
| 10546 | #endif |
| 10547 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10548 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10549 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10550 | PyDoc_STRVAR(posix_getxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10551 | "getxattr(path, attribute, *, follow_symlinks=True) -> value\n\n\ |
| 10552 | Return the value of extended attribute attribute on path.\n\ |
| 10553 | \n\ |
| 10554 | path may be either a string or an open file descriptor.\n\ |
| 10555 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10556 | link, getxattr will examine the symbolic link itself instead of the file\n\ |
| 10557 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10558 | |
| 10559 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10560 | posix_getxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10561 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10562 | path_t path; |
| 10563 | path_t attribute; |
| 10564 | int follow_symlinks = 1; |
| 10565 | PyObject *buffer = NULL; |
| 10566 | int i; |
| 10567 | static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10568 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10569 | memset(&path, 0, sizeof(path)); |
| 10570 | memset(&attribute, 0, sizeof(attribute)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10571 | path.function_name = "getxattr"; |
| 10572 | attribute.function_name = "getxattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10573 | path.allow_fd = 1; |
| 10574 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:getxattr", keywords, |
| 10575 | path_converter, &path, |
| 10576 | path_converter, &attribute, |
| 10577 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10578 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10579 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10580 | if (fd_and_follow_symlinks_invalid("getxattr", path.fd, follow_symlinks)) |
| 10581 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10582 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10583 | for (i = 0; ; i++) { |
| 10584 | void *ptr; |
| 10585 | ssize_t result; |
| 10586 | static Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
| 10587 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10588 | if (!buffer_size) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10589 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10590 | goto exit; |
| 10591 | } |
| 10592 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 10593 | if (!buffer) |
| 10594 | goto exit; |
| 10595 | ptr = PyBytes_AS_STRING(buffer); |
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 | Py_BEGIN_ALLOW_THREADS; |
| 10598 | if (path.fd >= 0) |
| 10599 | result = fgetxattr(path.fd, attribute.narrow, ptr, buffer_size); |
| 10600 | else if (follow_symlinks) |
| 10601 | result = getxattr(path.narrow, attribute.narrow, ptr, buffer_size); |
| 10602 | else |
| 10603 | result = lgetxattr(path.narrow, attribute.narrow, ptr, buffer_size); |
| 10604 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10605 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10606 | if (result < 0) { |
| 10607 | Py_DECREF(buffer); |
| 10608 | buffer = NULL; |
| 10609 | if (errno == ERANGE) |
| 10610 | continue; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10611 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10612 | goto exit; |
| 10613 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10614 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10615 | if (result != buffer_size) { |
| 10616 | /* Can only shrink. */ |
| 10617 | _PyBytes_Resize(&buffer, result); |
| 10618 | } |
| 10619 | break; |
| 10620 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10621 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10622 | exit: |
| 10623 | path_cleanup(&path); |
| 10624 | path_cleanup(&attribute); |
| 10625 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10626 | } |
| 10627 | |
| 10628 | PyDoc_STRVAR(posix_setxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10629 | "setxattr(path, attribute, value, flags=0, *, follow_symlinks=True)\n\n\ |
| 10630 | Set extended attribute attribute on path to value.\n\ |
| 10631 | path may be either a string or an open file descriptor.\n\ |
| 10632 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10633 | link, setxattr will modify the symbolic link itself instead of the file\n\ |
| 10634 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10635 | |
| 10636 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10637 | posix_setxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10638 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10639 | path_t path; |
| 10640 | path_t attribute; |
| 10641 | Py_buffer value; |
| 10642 | int flags = 0; |
| 10643 | int follow_symlinks = 1; |
| 10644 | int result; |
| 10645 | PyObject *return_value = NULL; |
| 10646 | static char *keywords[] = {"path", "attribute", "value", |
| 10647 | "flags", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10648 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10649 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10650 | path.function_name = "setxattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10651 | path.allow_fd = 1; |
| 10652 | memset(&attribute, 0, sizeof(attribute)); |
| 10653 | memset(&value, 0, sizeof(value)); |
| 10654 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&y*|i$p:setxattr", |
| 10655 | keywords, |
| 10656 | path_converter, &path, |
| 10657 | path_converter, &attribute, |
| 10658 | &value, &flags, |
| 10659 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10660 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10661 | |
| 10662 | if (fd_and_follow_symlinks_invalid("setxattr", path.fd, follow_symlinks)) |
| 10663 | goto exit; |
| 10664 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10665 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10666 | if (path.fd > -1) |
| 10667 | result = fsetxattr(path.fd, attribute.narrow, |
| 10668 | value.buf, value.len, flags); |
| 10669 | else if (follow_symlinks) |
| 10670 | result = setxattr(path.narrow, attribute.narrow, |
| 10671 | value.buf, value.len, flags); |
| 10672 | else |
| 10673 | result = lsetxattr(path.narrow, attribute.narrow, |
| 10674 | value.buf, value.len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10675 | Py_END_ALLOW_THREADS; |
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 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10678 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10679 | goto exit; |
| 10680 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10681 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10682 | return_value = Py_None; |
| 10683 | Py_INCREF(return_value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10684 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10685 | exit: |
| 10686 | path_cleanup(&path); |
| 10687 | path_cleanup(&attribute); |
| 10688 | PyBuffer_Release(&value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10689 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10690 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10691 | } |
| 10692 | |
| 10693 | PyDoc_STRVAR(posix_removexattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10694 | "removexattr(path, attribute, *, follow_symlinks=True)\n\n\ |
| 10695 | Remove extended attribute attribute on path.\n\ |
| 10696 | path may be either a string or an open file descriptor.\n\ |
| 10697 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10698 | link, removexattr will modify the symbolic link itself instead of the file\n\ |
| 10699 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10700 | |
| 10701 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10702 | posix_removexattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10703 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10704 | path_t path; |
| 10705 | path_t attribute; |
| 10706 | int follow_symlinks = 1; |
| 10707 | int result; |
| 10708 | PyObject *return_value = NULL; |
| 10709 | static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10710 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10711 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10712 | path.function_name = "removexattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10713 | memset(&attribute, 0, sizeof(attribute)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10714 | attribute.function_name = "removexattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10715 | path.allow_fd = 1; |
| 10716 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:removexattr", |
| 10717 | keywords, |
| 10718 | path_converter, &path, |
| 10719 | path_converter, &attribute, |
| 10720 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10721 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10722 | |
| 10723 | if (fd_and_follow_symlinks_invalid("removexattr", path.fd, follow_symlinks)) |
| 10724 | goto exit; |
| 10725 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10726 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10727 | if (path.fd > -1) |
| 10728 | result = fremovexattr(path.fd, attribute.narrow); |
| 10729 | else if (follow_symlinks) |
| 10730 | result = removexattr(path.narrow, attribute.narrow); |
| 10731 | else |
| 10732 | result = lremovexattr(path.narrow, attribute.narrow); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10733 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10734 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10735 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10736 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10737 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10738 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10739 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10740 | return_value = Py_None; |
| 10741 | Py_INCREF(return_value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10742 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10743 | exit: |
| 10744 | path_cleanup(&path); |
| 10745 | path_cleanup(&attribute); |
| 10746 | |
| 10747 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10748 | } |
| 10749 | |
| 10750 | PyDoc_STRVAR(posix_listxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10751 | "listxattr(path='.', *, follow_symlinks=True)\n\n\ |
| 10752 | Return a list of extended attributes on path.\n\ |
| 10753 | \n\ |
| 10754 | path may be either None, a string, or an open file descriptor.\n\ |
| 10755 | if path is None, listxattr will examine the current directory.\n\ |
| 10756 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10757 | link, listxattr will examine the symbolic link itself instead of the file\n\ |
| 10758 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10759 | |
| 10760 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10761 | posix_listxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
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 | path_t path; |
| 10764 | int follow_symlinks = 1; |
| 10765 | Py_ssize_t i; |
| 10766 | PyObject *result = NULL; |
| 10767 | char *buffer = NULL; |
| 10768 | char *name; |
| 10769 | static char *keywords[] = {"path", "follow_symlinks", NULL}; |
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 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10772 | path.function_name = "listxattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10773 | path.allow_fd = 1; |
| 10774 | path.fd = -1; |
| 10775 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&$p:listxattr", keywords, |
| 10776 | path_converter, &path, |
| 10777 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10778 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10779 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10780 | if (fd_and_follow_symlinks_invalid("listxattr", path.fd, follow_symlinks)) |
| 10781 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10782 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10783 | name = path.narrow ? path.narrow : "."; |
| 10784 | for (i = 0; ; i++) { |
| 10785 | char *start, *trace, *end; |
| 10786 | ssize_t length; |
| 10787 | static Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
| 10788 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10789 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 10790 | /* ERANGE */ |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10791 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10792 | break; |
| 10793 | } |
| 10794 | buffer = PyMem_MALLOC(buffer_size); |
| 10795 | if (!buffer) { |
| 10796 | PyErr_NoMemory(); |
| 10797 | break; |
| 10798 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10799 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10800 | Py_BEGIN_ALLOW_THREADS; |
| 10801 | if (path.fd > -1) |
| 10802 | length = flistxattr(path.fd, buffer, buffer_size); |
| 10803 | else if (follow_symlinks) |
| 10804 | length = listxattr(name, buffer, buffer_size); |
| 10805 | else |
| 10806 | length = llistxattr(name, buffer, buffer_size); |
| 10807 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10808 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10809 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 10810 | if (errno == ERANGE) { |
| 10811 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 10812 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10813 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 10814 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10815 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10816 | break; |
| 10817 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10818 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10819 | result = PyList_New(0); |
| 10820 | if (!result) { |
| 10821 | goto exit; |
| 10822 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10823 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10824 | end = buffer + length; |
| 10825 | for (trace = start = buffer; trace != end; trace++) { |
| 10826 | if (!*trace) { |
| 10827 | int error; |
| 10828 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 10829 | trace - start); |
| 10830 | if (!attribute) { |
| 10831 | Py_DECREF(result); |
| 10832 | result = NULL; |
| 10833 | goto exit; |
| 10834 | } |
| 10835 | error = PyList_Append(result, attribute); |
| 10836 | Py_DECREF(attribute); |
| 10837 | if (error) { |
| 10838 | Py_DECREF(result); |
| 10839 | result = NULL; |
| 10840 | goto exit; |
| 10841 | } |
| 10842 | start = trace + 1; |
| 10843 | } |
| 10844 | } |
| 10845 | break; |
| 10846 | } |
| 10847 | exit: |
| 10848 | path_cleanup(&path); |
| 10849 | if (buffer) |
| 10850 | PyMem_FREE(buffer); |
| 10851 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10852 | } |
| 10853 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10854 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10855 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10856 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10857 | PyDoc_STRVAR(posix_urandom__doc__, |
| 10858 | "urandom(n) -> str\n\n\ |
| 10859 | Return n random bytes suitable for cryptographic use."); |
| 10860 | |
| 10861 | static PyObject * |
| 10862 | posix_urandom(PyObject *self, PyObject *args) |
| 10863 | { |
| 10864 | Py_ssize_t size; |
| 10865 | PyObject *result; |
| 10866 | int ret; |
| 10867 | |
| 10868 | /* Read arguments */ |
| 10869 | if (!PyArg_ParseTuple(args, "n:urandom", &size)) |
| 10870 | return NULL; |
| 10871 | if (size < 0) |
| 10872 | return PyErr_Format(PyExc_ValueError, |
| 10873 | "negative argument not allowed"); |
| 10874 | result = PyBytes_FromStringAndSize(NULL, size); |
| 10875 | if (result == NULL) |
| 10876 | return NULL; |
| 10877 | |
| 10878 | ret = _PyOS_URandom(PyBytes_AS_STRING(result), |
| 10879 | PyBytes_GET_SIZE(result)); |
| 10880 | if (ret == -1) { |
| 10881 | Py_DECREF(result); |
| 10882 | return NULL; |
| 10883 | } |
| 10884 | return result; |
| 10885 | } |
| 10886 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10887 | /* Terminal size querying */ |
| 10888 | |
| 10889 | static PyTypeObject TerminalSizeType; |
| 10890 | |
| 10891 | PyDoc_STRVAR(TerminalSize_docstring, |
| 10892 | "A tuple of (columns, lines) for holding terminal window size"); |
| 10893 | |
| 10894 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 10895 | {"columns", "width of the terminal window in characters"}, |
| 10896 | {"lines", "height of the terminal window in characters"}, |
| 10897 | {NULL, NULL} |
| 10898 | }; |
| 10899 | |
| 10900 | static PyStructSequence_Desc TerminalSize_desc = { |
| 10901 | "os.terminal_size", |
| 10902 | TerminalSize_docstring, |
| 10903 | TerminalSize_fields, |
| 10904 | 2, |
| 10905 | }; |
| 10906 | |
| 10907 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 10908 | PyDoc_STRVAR(termsize__doc__, |
| 10909 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 10910 | "\n" \ |
| 10911 | "The optional argument fd (default standard output) specifies\n" \ |
| 10912 | "which file descriptor should be queried.\n" \ |
| 10913 | "\n" \ |
| 10914 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 10915 | "is thrown.\n" \ |
| 10916 | "\n" \ |
| 10917 | "This function will only be defined if an implementation is\n" \ |
| 10918 | "available for this system.\n" \ |
| 10919 | "\n" \ |
| 10920 | "shutil.get_terminal_size is the high-level function which should \n" \ |
| 10921 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 10922 | |
| 10923 | static PyObject* |
| 10924 | get_terminal_size(PyObject *self, PyObject *args) |
| 10925 | { |
| 10926 | int columns, lines; |
| 10927 | PyObject *termsize; |
| 10928 | |
| 10929 | int fd = fileno(stdout); |
| 10930 | /* Under some conditions stdout may not be connected and |
| 10931 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 10932 | * GUI apps don't have valid standard streams by default. |
| 10933 | * |
| 10934 | * If this happens, and the optional fd argument is not present, |
| 10935 | * the ioctl below will fail returning EBADF. This is what we want. |
| 10936 | */ |
| 10937 | |
| 10938 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 10939 | return NULL; |
| 10940 | |
| 10941 | #ifdef TERMSIZE_USE_IOCTL |
| 10942 | { |
| 10943 | struct winsize w; |
| 10944 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 10945 | return PyErr_SetFromErrno(PyExc_OSError); |
| 10946 | columns = w.ws_col; |
| 10947 | lines = w.ws_row; |
| 10948 | } |
| 10949 | #endif /* TERMSIZE_USE_IOCTL */ |
| 10950 | |
| 10951 | #ifdef TERMSIZE_USE_CONIO |
| 10952 | { |
| 10953 | DWORD nhandle; |
| 10954 | HANDLE handle; |
| 10955 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 10956 | switch (fd) { |
| 10957 | case 0: nhandle = STD_INPUT_HANDLE; |
| 10958 | break; |
| 10959 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 10960 | break; |
| 10961 | case 2: nhandle = STD_ERROR_HANDLE; |
| 10962 | break; |
| 10963 | default: |
| 10964 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 10965 | } |
| 10966 | handle = GetStdHandle(nhandle); |
| 10967 | if (handle == NULL) |
| 10968 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 10969 | if (handle == INVALID_HANDLE_VALUE) |
| 10970 | return PyErr_SetFromWindowsErr(0); |
| 10971 | |
| 10972 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 10973 | return PyErr_SetFromWindowsErr(0); |
| 10974 | |
| 10975 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 10976 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 10977 | } |
| 10978 | #endif /* TERMSIZE_USE_CONIO */ |
| 10979 | |
| 10980 | termsize = PyStructSequence_New(&TerminalSizeType); |
| 10981 | if (termsize == NULL) |
| 10982 | return NULL; |
| 10983 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 10984 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 10985 | if (PyErr_Occurred()) { |
| 10986 | Py_DECREF(termsize); |
| 10987 | return NULL; |
| 10988 | } |
| 10989 | return termsize; |
| 10990 | } |
| 10991 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 10992 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10993 | PyDoc_STRVAR(posix_cpu_count__doc__, |
| 10994 | "cpu_count() -> integer\n\n\ |
| 10995 | Return the number of CPUs in the system, or None if this value cannot be\n\ |
| 10996 | established."); |
| 10997 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10998 | static PyObject * |
| 10999 | posix_cpu_count(PyObject *self) |
| 11000 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11001 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11002 | #ifdef MS_WINDOWS |
| 11003 | SYSTEM_INFO sysinfo; |
| 11004 | GetSystemInfo(&sysinfo); |
| 11005 | ncpu = sysinfo.dwNumberOfProcessors; |
| 11006 | #elif defined(__hpux) |
| 11007 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 11008 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 11009 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11010 | #elif defined(__DragonFly__) || \ |
| 11011 | defined(__OpenBSD__) || \ |
| 11012 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11013 | defined(__NetBSD__) || \ |
| 11014 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 11015 | int mib[2]; |
| 11016 | size_t len = sizeof(ncpu); |
| 11017 | mib[0] = CTL_HW; |
| 11018 | mib[1] = HW_NCPU; |
| 11019 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 11020 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11021 | #endif |
| 11022 | if (ncpu >= 1) |
| 11023 | return PyLong_FromLong(ncpu); |
| 11024 | else |
| 11025 | Py_RETURN_NONE; |
| 11026 | } |
| 11027 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11028 | PyDoc_STRVAR(get_inheritable__doc__, |
| 11029 | "get_inheritable(fd) -> bool\n" \ |
| 11030 | "\n" \ |
| 11031 | "Get the close-on-exe flag of the specified file descriptor."); |
| 11032 | |
| 11033 | static PyObject* |
| 11034 | posix_get_inheritable(PyObject *self, PyObject *args) |
| 11035 | { |
| 11036 | int fd; |
| 11037 | int inheritable; |
| 11038 | |
| 11039 | if (!PyArg_ParseTuple(args, "i:get_inheritable", &fd)) |
| 11040 | return NULL; |
| 11041 | |
| 11042 | if (!_PyVerify_fd(fd)) |
| 11043 | return posix_error(); |
| 11044 | |
| 11045 | inheritable = _Py_get_inheritable(fd); |
| 11046 | if (inheritable < 0) |
| 11047 | return NULL; |
| 11048 | return PyBool_FromLong(inheritable); |
| 11049 | } |
| 11050 | |
| 11051 | PyDoc_STRVAR(set_inheritable__doc__, |
| 11052 | "set_inheritable(fd, inheritable)\n" \ |
| 11053 | "\n" \ |
| 11054 | "Set the inheritable flag of the specified file descriptor."); |
| 11055 | |
| 11056 | static PyObject* |
| 11057 | posix_set_inheritable(PyObject *self, PyObject *args) |
| 11058 | { |
| 11059 | int fd, inheritable; |
| 11060 | |
| 11061 | if (!PyArg_ParseTuple(args, "ii:set_inheritable", &fd, &inheritable)) |
| 11062 | return NULL; |
| 11063 | |
| 11064 | if (!_PyVerify_fd(fd)) |
| 11065 | return posix_error(); |
| 11066 | |
| 11067 | if (_Py_set_inheritable(fd, inheritable, NULL) < 0) |
| 11068 | return NULL; |
| 11069 | Py_RETURN_NONE; |
| 11070 | } |
| 11071 | |
| 11072 | |
| 11073 | #ifdef MS_WINDOWS |
| 11074 | PyDoc_STRVAR(get_handle_inheritable__doc__, |
| 11075 | "get_handle_inheritable(fd) -> bool\n" \ |
| 11076 | "\n" \ |
| 11077 | "Get the close-on-exe flag of the specified file descriptor."); |
| 11078 | |
| 11079 | static PyObject* |
| 11080 | posix_get_handle_inheritable(PyObject *self, PyObject *args) |
| 11081 | { |
| 11082 | Py_intptr_t handle; |
| 11083 | DWORD flags; |
| 11084 | |
| 11085 | if (!PyArg_ParseTuple(args, _Py_PARSE_INTPTR ":get_handle_inheritable", &handle)) |
| 11086 | return NULL; |
| 11087 | |
| 11088 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 11089 | PyErr_SetFromWindowsErr(0); |
| 11090 | return NULL; |
| 11091 | } |
| 11092 | |
| 11093 | return PyBool_FromLong(flags & HANDLE_FLAG_INHERIT); |
| 11094 | } |
| 11095 | |
| 11096 | PyDoc_STRVAR(set_handle_inheritable__doc__, |
| 11097 | "set_handle_inheritable(fd, inheritable)\n" \ |
| 11098 | "\n" \ |
| 11099 | "Set the inheritable flag of the specified handle."); |
| 11100 | |
| 11101 | static PyObject* |
| 11102 | posix_set_handle_inheritable(PyObject *self, PyObject *args) |
| 11103 | { |
| 11104 | int inheritable = 1; |
| 11105 | Py_intptr_t handle; |
| 11106 | DWORD flags; |
| 11107 | |
| 11108 | if (!PyArg_ParseTuple(args, _Py_PARSE_INTPTR "i:set_handle_inheritable", |
| 11109 | &handle, &inheritable)) |
| 11110 | return NULL; |
| 11111 | |
| 11112 | if (inheritable) |
| 11113 | flags = HANDLE_FLAG_INHERIT; |
| 11114 | else |
| 11115 | flags = 0; |
| 11116 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 11117 | PyErr_SetFromWindowsErr(0); |
| 11118 | return NULL; |
| 11119 | } |
| 11120 | Py_RETURN_NONE; |
| 11121 | } |
| 11122 | #endif /* MS_WINDOWS */ |
| 11123 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11124 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 11125 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11126 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 11127 | |
| 11128 | OS_STAT_METHODDEF |
| 11129 | OS_ACCESS_METHODDEF |
| 11130 | OS_TTYNAME_METHODDEF |
| 11131 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11132 | {"chdir", (PyCFunction)posix_chdir, |
| 11133 | METH_VARARGS | METH_KEYWORDS, |
| 11134 | posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 11135 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11136 | {"chflags", (PyCFunction)posix_chflags, |
| 11137 | METH_VARARGS | METH_KEYWORDS, |
| 11138 | posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 11139 | #endif /* HAVE_CHFLAGS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11140 | {"chmod", (PyCFunction)posix_chmod, |
| 11141 | METH_VARARGS | METH_KEYWORDS, |
| 11142 | posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 11143 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11144 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 11145 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11146 | #ifdef HAVE_CHOWN |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11147 | {"chown", (PyCFunction)posix_chown, |
| 11148 | METH_VARARGS | METH_KEYWORDS, |
| 11149 | posix_chown__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 11150 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 11151 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11152 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 11153 | #endif /* HAVE_LCHMOD */ |
| 11154 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11155 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 11156 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 11157 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11158 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 11159 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 11160 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11161 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 11162 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 11163 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11164 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 11165 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11166 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11167 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11168 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11169 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 11170 | METH_NOARGS, posix_getcwd__doc__}, |
| 11171 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 11172 | METH_NOARGS, posix_getcwdb__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11173 | #if defined(HAVE_LINK) || defined(MS_WINDOWS) |
| 11174 | {"link", (PyCFunction)posix_link, |
| 11175 | METH_VARARGS | METH_KEYWORDS, |
| 11176 | posix_link__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11177 | #endif /* HAVE_LINK */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11178 | {"listdir", (PyCFunction)posix_listdir, |
| 11179 | METH_VARARGS | METH_KEYWORDS, |
| 11180 | posix_listdir__doc__}, |
| 11181 | {"lstat", (PyCFunction)posix_lstat, |
| 11182 | METH_VARARGS | METH_KEYWORDS, |
| 11183 | posix_lstat__doc__}, |
| 11184 | {"mkdir", (PyCFunction)posix_mkdir, |
| 11185 | METH_VARARGS | METH_KEYWORDS, |
| 11186 | posix_mkdir__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 11187 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11188 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 11189 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11190 | #ifdef HAVE_GETPRIORITY |
| 11191 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 11192 | #endif /* HAVE_GETPRIORITY */ |
| 11193 | #ifdef HAVE_SETPRIORITY |
| 11194 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 11195 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 11196 | #ifdef HAVE_READLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11197 | {"readlink", (PyCFunction)posix_readlink, |
| 11198 | METH_VARARGS | METH_KEYWORDS, |
| 11199 | readlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 11200 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 11201 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11202 | {"readlink", (PyCFunction)win_readlink, |
| 11203 | METH_VARARGS | METH_KEYWORDS, |
| 11204 | readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 11205 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11206 | {"rename", (PyCFunction)posix_rename, |
| 11207 | METH_VARARGS | METH_KEYWORDS, |
| 11208 | posix_rename__doc__}, |
| 11209 | {"replace", (PyCFunction)posix_replace, |
| 11210 | METH_VARARGS | METH_KEYWORDS, |
| 11211 | posix_replace__doc__}, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 11212 | {"rmdir", (PyCFunction)posix_rmdir, |
| 11213 | METH_VARARGS | METH_KEYWORDS, |
| 11214 | posix_rmdir__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11215 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11216 | #if defined(HAVE_SYMLINK) |
| 11217 | {"symlink", (PyCFunction)posix_symlink, |
| 11218 | METH_VARARGS | METH_KEYWORDS, |
| 11219 | posix_symlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 11220 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 11221 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11222 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11223 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11224 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11225 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11226 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11227 | #endif /* HAVE_UNAME */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11228 | {"unlink", (PyCFunction)posix_unlink, |
| 11229 | METH_VARARGS | METH_KEYWORDS, |
| 11230 | posix_unlink__doc__}, |
| 11231 | {"remove", (PyCFunction)posix_unlink, |
| 11232 | METH_VARARGS | METH_KEYWORDS, |
| 11233 | posix_remove__doc__}, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 11234 | {"utime", (PyCFunction)posix_utime, |
| 11235 | METH_VARARGS | METH_KEYWORDS, posix_utime__doc__}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 11236 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11237 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 11238 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11239 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 11240 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11241 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11242 | {"execve", (PyCFunction)posix_execve, |
| 11243 | METH_VARARGS | METH_KEYWORDS, |
| 11244 | posix_execve__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 11245 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 11246 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11247 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 11248 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 11249 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 11250 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11251 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 11252 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11253 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11254 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11255 | #endif /* HAVE_FORK */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11256 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 11257 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11258 | {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__}, |
| 11259 | {"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] | 11260 | #endif |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11261 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11262 | {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11263 | #endif |
| 11264 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11265 | {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11266 | #endif |
| 11267 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11268 | {"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] | 11269 | #endif |
| 11270 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11271 | {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11272 | #endif |
| 11273 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11274 | {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11275 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11276 | {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__}, |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11277 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11278 | {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__}, |
| 11279 | {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__}, |
| 11280 | #endif |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 11281 | #endif /* HAVE_SCHED_H */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 11282 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11283 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 11284 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 11285 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11286 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 11287 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 11288 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11289 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 11290 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11291 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11292 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 11293 | #endif /* HAVE_GETEUID */ |
| 11294 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11295 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11296 | #endif /* HAVE_GETGID */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 11297 | #ifdef HAVE_GETGROUPLIST |
| 11298 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 11299 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11300 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11301 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11302 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11303 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11304 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11305 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11306 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11307 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11308 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11309 | #endif /* HAVE_GETPPID */ |
| 11310 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11311 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11312 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 11313 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11314 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 11315 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11316 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11317 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11318 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 11319 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11320 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 11321 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 11322 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11323 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 11324 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 11325 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11326 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 11327 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 11328 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11329 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11330 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11331 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11332 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11333 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11334 | #endif /* HAVE_SETEUID */ |
| 11335 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11336 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11337 | #endif /* HAVE_SETEGID */ |
| 11338 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11339 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11340 | #endif /* HAVE_SETREUID */ |
| 11341 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11342 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11343 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11344 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11345 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11346 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 11347 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11348 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 11349 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 11350 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11351 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 11352 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 11353 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11354 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 11355 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11356 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11357 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11358 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11359 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11360 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11361 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11362 | #ifdef HAVE_WAIT3 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 11363 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11364 | #endif /* HAVE_WAIT3 */ |
| 11365 | #ifdef HAVE_WAIT4 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 11366 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11367 | #endif /* HAVE_WAIT4 */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11368 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11369 | {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__}, |
| 11370 | #endif |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 11371 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11372 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11373 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 11374 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11375 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 11376 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11377 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11378 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11379 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11380 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11381 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11382 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11383 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11384 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11385 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11386 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11387 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11388 | #endif /* HAVE_TCSETPGRP */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11389 | {"open", (PyCFunction)posix_open,\ |
| 11390 | METH_VARARGS | METH_KEYWORDS, |
| 11391 | posix_open__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11392 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 11393 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 11394 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 11395 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11396 | {"dup2", (PyCFunction)posix_dup2, |
| 11397 | METH_VARARGS | METH_KEYWORDS, posix_dup2__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11398 | #ifdef HAVE_LOCKF |
| 11399 | {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__}, |
| 11400 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11401 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 11402 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11403 | #ifdef HAVE_READV |
| 11404 | {"readv", posix_readv, METH_VARARGS, posix_readv__doc__}, |
| 11405 | #endif |
| 11406 | #ifdef HAVE_PREAD |
| 11407 | {"pread", posix_pread, METH_VARARGS, posix_pread__doc__}, |
| 11408 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11409 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11410 | #ifdef HAVE_WRITEV |
| 11411 | {"writev", posix_writev, METH_VARARGS, posix_writev__doc__}, |
| 11412 | #endif |
| 11413 | #ifdef HAVE_PWRITE |
| 11414 | {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__}, |
| 11415 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11416 | #ifdef HAVE_SENDFILE |
| 11417 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 11418 | posix_sendfile__doc__}, |
| 11419 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 11420 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11421 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11422 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11423 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11424 | #endif |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 11425 | #ifdef HAVE_PIPE2 |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 11426 | {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__}, |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 11427 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11428 | #ifdef HAVE_MKFIFO |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11429 | {"mkfifo", (PyCFunction)posix_mkfifo, |
| 11430 | METH_VARARGS | METH_KEYWORDS, |
| 11431 | posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11432 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 11433 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11434 | {"mknod", (PyCFunction)posix_mknod, |
| 11435 | METH_VARARGS | METH_KEYWORDS, |
| 11436 | posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 11437 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 11438 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11439 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 11440 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 11441 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 11442 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11443 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11444 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11445 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11446 | #ifdef HAVE_TRUNCATE |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11447 | {"truncate", (PyCFunction)posix_truncate, |
| 11448 | METH_VARARGS | METH_KEYWORDS, |
| 11449 | posix_truncate__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11450 | #endif |
| 11451 | #ifdef HAVE_POSIX_FALLOCATE |
| 11452 | {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__}, |
| 11453 | #endif |
| 11454 | #ifdef HAVE_POSIX_FADVISE |
| 11455 | {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__}, |
| 11456 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 11457 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11458 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 11459 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 11460 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11461 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 11462 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11463 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11464 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11465 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11466 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11467 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11468 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11469 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11470 | #ifdef HAVE_SYNC |
| 11471 | {"sync", posix_sync, METH_NOARGS, posix_sync__doc__}, |
| 11472 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11473 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11474 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11475 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11476 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11477 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11478 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11479 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 11480 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11481 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 11482 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11483 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11484 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11485 | #endif /* WIFSTOPPED */ |
| 11486 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11487 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11488 | #endif /* WIFSIGNALED */ |
| 11489 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11490 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11491 | #endif /* WIFEXITED */ |
| 11492 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11493 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11494 | #endif /* WEXITSTATUS */ |
| 11495 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11496 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11497 | #endif /* WTERMSIG */ |
| 11498 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11499 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11500 | #endif /* WSTOPSIG */ |
| 11501 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11502 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11503 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11504 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11505 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11506 | {"statvfs", (PyCFunction)posix_statvfs, |
| 11507 | METH_VARARGS | METH_KEYWORDS, |
| 11508 | posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11509 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11510 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11511 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11512 | #endif |
| 11513 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11514 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11515 | #endif |
| 11516 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11517 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11518 | #endif |
| 11519 | #ifdef HAVE_PATHCONF |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11520 | {"pathconf", (PyCFunction)posix_pathconf, |
| 11521 | METH_VARARGS | METH_KEYWORDS, |
| 11522 | posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11523 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11524 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11525 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11526 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 11527 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 11528 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 11529 | {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__}, |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 11530 | {"_getvolumepathname", posix__getvolumepathname, METH_VARARGS, posix__getvolumepathname__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 11531 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11532 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11533 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11534 | #endif |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 11535 | {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11536 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11537 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11538 | #endif |
| 11539 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11540 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11541 | #endif |
| 11542 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11543 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11544 | #endif |
| 11545 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11546 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11547 | #endif |
| 11548 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11549 | #ifdef USE_XATTRS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11550 | {"setxattr", (PyCFunction)posix_setxattr, |
| 11551 | METH_VARARGS | METH_KEYWORDS, |
| 11552 | posix_setxattr__doc__}, |
| 11553 | {"getxattr", (PyCFunction)posix_getxattr, |
| 11554 | METH_VARARGS | METH_KEYWORDS, |
| 11555 | posix_getxattr__doc__}, |
| 11556 | {"removexattr", (PyCFunction)posix_removexattr, |
| 11557 | METH_VARARGS | METH_KEYWORDS, |
| 11558 | posix_removexattr__doc__}, |
| 11559 | {"listxattr", (PyCFunction)posix_listxattr, |
| 11560 | METH_VARARGS | METH_KEYWORDS, |
| 11561 | posix_listxattr__doc__}, |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11562 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11563 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 11564 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 11565 | #endif |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11566 | {"cpu_count", (PyCFunction)posix_cpu_count, |
| 11567 | METH_NOARGS, posix_cpu_count__doc__}, |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11568 | {"get_inheritable", posix_get_inheritable, METH_VARARGS, get_inheritable__doc__}, |
| 11569 | {"set_inheritable", posix_set_inheritable, METH_VARARGS, set_inheritable__doc__}, |
| 11570 | #ifdef MS_WINDOWS |
| 11571 | {"get_handle_inheritable", posix_get_handle_inheritable, |
| 11572 | METH_VARARGS, get_handle_inheritable__doc__}, |
| 11573 | {"set_handle_inheritable", posix_set_handle_inheritable, |
| 11574 | METH_VARARGS, set_handle_inheritable__doc__}, |
| 11575 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11576 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11577 | }; |
| 11578 | |
| 11579 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11580 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11581 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11582 | enable_symlink() |
| 11583 | { |
| 11584 | HANDLE tok; |
| 11585 | TOKEN_PRIVILEGES tok_priv; |
| 11586 | LUID luid; |
| 11587 | int meth_idx = 0; |
| 11588 | |
| 11589 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11590 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11591 | |
| 11592 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11593 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11594 | |
| 11595 | tok_priv.PrivilegeCount = 1; |
| 11596 | tok_priv.Privileges[0].Luid = luid; |
| 11597 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 11598 | |
| 11599 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 11600 | sizeof(TOKEN_PRIVILEGES), |
| 11601 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11602 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11603 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11604 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 11605 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11606 | } |
| 11607 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 11608 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11609 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11610 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11611 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11612 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11613 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11614 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11615 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11616 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11617 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11618 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11619 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11620 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11621 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11622 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11623 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11624 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11625 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11626 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11627 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11628 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11629 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11630 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11631 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11632 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11633 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11634 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11635 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11636 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11637 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11638 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11639 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11640 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11641 | #endif |
| 11642 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11643 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11644 | #endif |
| 11645 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11646 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11647 | #endif |
| 11648 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11649 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11650 | #endif |
| 11651 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11652 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11653 | #endif |
| 11654 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11655 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11656 | #endif |
| 11657 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11658 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11659 | #endif |
| 11660 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11661 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11662 | #endif |
| 11663 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11664 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11665 | #endif |
| 11666 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11667 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11668 | #endif |
| 11669 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11670 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11671 | #endif |
| 11672 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11673 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11674 | #endif |
| 11675 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11676 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11677 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11678 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11679 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11680 | #endif |
| 11681 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11682 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11683 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11684 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11685 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11686 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11687 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11688 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11689 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11690 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11691 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11692 | #endif |
| 11693 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11694 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11695 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11696 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11697 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11698 | #endif |
| 11699 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11700 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11701 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 11702 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11703 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 11704 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11705 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11706 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11707 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 11708 | #ifdef O_TMPFILE |
| 11709 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 11710 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11711 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11712 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11713 | #endif |
| 11714 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11715 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11716 | #endif |
| 11717 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11718 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11719 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 11720 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11721 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 11722 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11723 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11724 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11725 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11726 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11727 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 11728 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11729 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 11730 | #endif |
| 11731 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11732 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 11733 | #endif |
| 11734 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11735 | /* MS Windows */ |
| 11736 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11737 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11738 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11739 | #endif |
| 11740 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11741 | /* Optimize for short life (keep in memory). */ |
| 11742 | /* 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] | 11743 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11744 | #endif |
| 11745 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11746 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11747 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11748 | #endif |
| 11749 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11750 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11751 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11752 | #endif |
| 11753 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11754 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11755 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11756 | #endif |
| 11757 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11758 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11759 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11760 | /* Send a SIGIO signal whenever input or output |
| 11761 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11762 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11763 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11764 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11765 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11766 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11767 | #endif |
| 11768 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11769 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11770 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11771 | #endif |
| 11772 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11773 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11774 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11775 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11776 | #ifdef O_NOLINKS |
| 11777 | /* 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] | 11778 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11779 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11780 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11781 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11782 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11783 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11784 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11785 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11786 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11787 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11788 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11789 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11790 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11791 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11792 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11793 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11794 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11795 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11796 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11797 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11798 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11799 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11800 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11801 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11802 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11803 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11804 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11805 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11806 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11807 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11808 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11809 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11810 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11811 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11812 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11813 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11814 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11815 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11816 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11817 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11818 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11819 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11820 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11821 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11822 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11823 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11824 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11825 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11826 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11827 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11828 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11829 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11830 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11831 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11832 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11833 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11834 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11835 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11836 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11837 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11838 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11839 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11840 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11841 | #endif /* ST_RDONLY */ |
| 11842 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11843 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11844 | #endif /* ST_NOSUID */ |
| 11845 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 11846 | /* GNU extensions */ |
| 11847 | #ifdef ST_NODEV |
| 11848 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 11849 | #endif /* ST_NODEV */ |
| 11850 | #ifdef ST_NOEXEC |
| 11851 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 11852 | #endif /* ST_NOEXEC */ |
| 11853 | #ifdef ST_SYNCHRONOUS |
| 11854 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 11855 | #endif /* ST_SYNCHRONOUS */ |
| 11856 | #ifdef ST_MANDLOCK |
| 11857 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 11858 | #endif /* ST_MANDLOCK */ |
| 11859 | #ifdef ST_WRITE |
| 11860 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 11861 | #endif /* ST_WRITE */ |
| 11862 | #ifdef ST_APPEND |
| 11863 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 11864 | #endif /* ST_APPEND */ |
| 11865 | #ifdef ST_NOATIME |
| 11866 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 11867 | #endif /* ST_NOATIME */ |
| 11868 | #ifdef ST_NODIRATIME |
| 11869 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 11870 | #endif /* ST_NODIRATIME */ |
| 11871 | #ifdef ST_RELATIME |
| 11872 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 11873 | #endif /* ST_RELATIME */ |
| 11874 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11875 | /* FreeBSD sendfile() constants */ |
| 11876 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11877 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11878 | #endif |
| 11879 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11880 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11881 | #endif |
| 11882 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11883 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11884 | #endif |
| 11885 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11886 | /* constants for posix_fadvise */ |
| 11887 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11888 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11889 | #endif |
| 11890 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11891 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11892 | #endif |
| 11893 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11894 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11895 | #endif |
| 11896 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11897 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11898 | #endif |
| 11899 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11900 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11901 | #endif |
| 11902 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11903 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11904 | #endif |
| 11905 | |
| 11906 | /* constants for waitid */ |
| 11907 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11908 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 11909 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 11910 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11911 | #endif |
| 11912 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11913 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11914 | #endif |
| 11915 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11916 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11917 | #endif |
| 11918 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11919 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11920 | #endif |
| 11921 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11922 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11923 | #endif |
| 11924 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11925 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11926 | #endif |
| 11927 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11928 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11929 | #endif |
| 11930 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11931 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11932 | #endif |
| 11933 | |
| 11934 | /* constants for lockf */ |
| 11935 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11936 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11937 | #endif |
| 11938 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11939 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11940 | #endif |
| 11941 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11942 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11943 | #endif |
| 11944 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11945 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11946 | #endif |
| 11947 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11948 | #ifdef HAVE_SPAWNV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11949 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 11950 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
| 11951 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
| 11952 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
| 11953 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11954 | #endif |
| 11955 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11956 | #ifdef HAVE_SCHED_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11957 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
| 11958 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
| 11959 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11960 | #ifdef SCHED_SPORADIC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11961 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11962 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11963 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11964 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11965 | #endif |
| 11966 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11967 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11968 | #endif |
| 11969 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11970 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11971 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11972 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11973 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11974 | #endif |
| 11975 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11976 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11977 | #endif |
| 11978 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11979 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11980 | #endif |
| 11981 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11982 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11983 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11984 | #endif |
| 11985 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11986 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11987 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 11988 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 11989 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11990 | #endif |
| 11991 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11992 | #ifdef RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11993 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11994 | #endif |
| 11995 | #ifdef RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11996 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11997 | #endif |
| 11998 | #ifdef RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11999 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12000 | #endif |
| 12001 | #ifdef RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12002 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12003 | #endif |
| 12004 | #ifdef RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12005 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12006 | #endif |
| 12007 | #ifdef RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12008 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12009 | #endif |
| 12010 | #ifdef RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12011 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12012 | #endif |
| 12013 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12014 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12015 | } |
| 12016 | |
| 12017 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12018 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12019 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 12020 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 12021 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 12022 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12023 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 12024 | #define MODNAME "posix" |
| 12025 | #endif |
| 12026 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12027 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12028 | PyModuleDef_HEAD_INIT, |
| 12029 | MODNAME, |
| 12030 | posix__doc__, |
| 12031 | -1, |
| 12032 | posix_methods, |
| 12033 | NULL, |
| 12034 | NULL, |
| 12035 | NULL, |
| 12036 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12037 | }; |
| 12038 | |
| 12039 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12040 | static char *have_functions[] = { |
| 12041 | |
| 12042 | #ifdef HAVE_FACCESSAT |
| 12043 | "HAVE_FACCESSAT", |
| 12044 | #endif |
| 12045 | |
| 12046 | #ifdef HAVE_FCHDIR |
| 12047 | "HAVE_FCHDIR", |
| 12048 | #endif |
| 12049 | |
| 12050 | #ifdef HAVE_FCHMOD |
| 12051 | "HAVE_FCHMOD", |
| 12052 | #endif |
| 12053 | |
| 12054 | #ifdef HAVE_FCHMODAT |
| 12055 | "HAVE_FCHMODAT", |
| 12056 | #endif |
| 12057 | |
| 12058 | #ifdef HAVE_FCHOWN |
| 12059 | "HAVE_FCHOWN", |
| 12060 | #endif |
| 12061 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 12062 | #ifdef HAVE_FCHOWNAT |
| 12063 | "HAVE_FCHOWNAT", |
| 12064 | #endif |
| 12065 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12066 | #ifdef HAVE_FEXECVE |
| 12067 | "HAVE_FEXECVE", |
| 12068 | #endif |
| 12069 | |
| 12070 | #ifdef HAVE_FDOPENDIR |
| 12071 | "HAVE_FDOPENDIR", |
| 12072 | #endif |
| 12073 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12074 | #ifdef HAVE_FPATHCONF |
| 12075 | "HAVE_FPATHCONF", |
| 12076 | #endif |
| 12077 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12078 | #ifdef HAVE_FSTATAT |
| 12079 | "HAVE_FSTATAT", |
| 12080 | #endif |
| 12081 | |
| 12082 | #ifdef HAVE_FSTATVFS |
| 12083 | "HAVE_FSTATVFS", |
| 12084 | #endif |
| 12085 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12086 | #ifdef HAVE_FTRUNCATE |
| 12087 | "HAVE_FTRUNCATE", |
| 12088 | #endif |
| 12089 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12090 | #ifdef HAVE_FUTIMENS |
| 12091 | "HAVE_FUTIMENS", |
| 12092 | #endif |
| 12093 | |
| 12094 | #ifdef HAVE_FUTIMES |
| 12095 | "HAVE_FUTIMES", |
| 12096 | #endif |
| 12097 | |
| 12098 | #ifdef HAVE_FUTIMESAT |
| 12099 | "HAVE_FUTIMESAT", |
| 12100 | #endif |
| 12101 | |
| 12102 | #ifdef HAVE_LINKAT |
| 12103 | "HAVE_LINKAT", |
| 12104 | #endif |
| 12105 | |
| 12106 | #ifdef HAVE_LCHFLAGS |
| 12107 | "HAVE_LCHFLAGS", |
| 12108 | #endif |
| 12109 | |
| 12110 | #ifdef HAVE_LCHMOD |
| 12111 | "HAVE_LCHMOD", |
| 12112 | #endif |
| 12113 | |
| 12114 | #ifdef HAVE_LCHOWN |
| 12115 | "HAVE_LCHOWN", |
| 12116 | #endif |
| 12117 | |
| 12118 | #ifdef HAVE_LSTAT |
| 12119 | "HAVE_LSTAT", |
| 12120 | #endif |
| 12121 | |
| 12122 | #ifdef HAVE_LUTIMES |
| 12123 | "HAVE_LUTIMES", |
| 12124 | #endif |
| 12125 | |
| 12126 | #ifdef HAVE_MKDIRAT |
| 12127 | "HAVE_MKDIRAT", |
| 12128 | #endif |
| 12129 | |
| 12130 | #ifdef HAVE_MKFIFOAT |
| 12131 | "HAVE_MKFIFOAT", |
| 12132 | #endif |
| 12133 | |
| 12134 | #ifdef HAVE_MKNODAT |
| 12135 | "HAVE_MKNODAT", |
| 12136 | #endif |
| 12137 | |
| 12138 | #ifdef HAVE_OPENAT |
| 12139 | "HAVE_OPENAT", |
| 12140 | #endif |
| 12141 | |
| 12142 | #ifdef HAVE_READLINKAT |
| 12143 | "HAVE_READLINKAT", |
| 12144 | #endif |
| 12145 | |
| 12146 | #ifdef HAVE_RENAMEAT |
| 12147 | "HAVE_RENAMEAT", |
| 12148 | #endif |
| 12149 | |
| 12150 | #ifdef HAVE_SYMLINKAT |
| 12151 | "HAVE_SYMLINKAT", |
| 12152 | #endif |
| 12153 | |
| 12154 | #ifdef HAVE_UNLINKAT |
| 12155 | "HAVE_UNLINKAT", |
| 12156 | #endif |
| 12157 | |
| 12158 | #ifdef HAVE_UTIMENSAT |
| 12159 | "HAVE_UTIMENSAT", |
| 12160 | #endif |
| 12161 | |
| 12162 | #ifdef MS_WINDOWS |
| 12163 | "MS_WINDOWS", |
| 12164 | #endif |
| 12165 | |
| 12166 | NULL |
| 12167 | }; |
| 12168 | |
| 12169 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 12170 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 12171 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12172 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12173 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12174 | PyObject *list; |
| 12175 | char **trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12176 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12177 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12178 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12179 | #endif |
| 12180 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12181 | m = PyModule_Create(&posixmodule); |
| 12182 | if (m == NULL) |
| 12183 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12184 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12185 | /* Initialize environ dictionary */ |
| 12186 | v = convertenviron(); |
| 12187 | Py_XINCREF(v); |
| 12188 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 12189 | return NULL; |
| 12190 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12191 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12192 | if (all_ins(m)) |
| 12193 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12194 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12195 | if (setup_confname_tables(m)) |
| 12196 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12197 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12198 | Py_INCREF(PyExc_OSError); |
| 12199 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 12200 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12201 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12202 | if (posix_putenv_garbage == NULL) |
| 12203 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12204 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 12205 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12206 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12207 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 12208 | waitid_result_desc.name = MODNAME ".waitid_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12209 | if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0) |
| 12210 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12211 | #endif |
| 12212 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 12213 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12214 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 12215 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 12216 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12217 | if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0) |
| 12218 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12219 | structseq_new = StatResultType.tp_new; |
| 12220 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12221 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 12222 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12223 | if (PyStructSequence_InitType2(&StatVFSResultType, |
| 12224 | &statvfs_result_desc) < 0) |
| 12225 | return NULL; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12226 | #ifdef NEED_TICKS_PER_SECOND |
| 12227 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12228 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12229 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12230 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12231 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12232 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12233 | # endif |
| 12234 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12235 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 12236 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12237 | sched_param_desc.name = MODNAME ".sched_param"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12238 | if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0) |
| 12239 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12240 | SchedParamType.tp_new = sched_param_new; |
| 12241 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12242 | |
| 12243 | /* initialize TerminalSize_info */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12244 | if (PyStructSequence_InitType2(&TerminalSizeType, |
| 12245 | &TerminalSize_desc) < 0) |
| 12246 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12247 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12248 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 12249 | Py_INCREF((PyObject*) &WaitidResultType); |
| 12250 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 12251 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12252 | Py_INCREF((PyObject*) &StatResultType); |
| 12253 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 12254 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 12255 | PyModule_AddObject(m, "statvfs_result", |
| 12256 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 12257 | |
| 12258 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12259 | Py_INCREF(&SchedParamType); |
| 12260 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 12261 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12262 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 12263 | times_result_desc.name = MODNAME ".times_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12264 | if (PyStructSequence_InitType2(&TimesResultType, ×_result_desc) < 0) |
| 12265 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 12266 | PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType); |
| 12267 | |
| 12268 | uname_result_desc.name = MODNAME ".uname_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12269 | if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0) |
| 12270 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 12271 | PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType); |
| 12272 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12273 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12274 | /* |
| 12275 | * Step 2 of weak-linking support on Mac OS X. |
| 12276 | * |
| 12277 | * The code below removes functions that are not available on the |
| 12278 | * currently active platform. |
| 12279 | * |
| 12280 | * 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] | 12281 | * 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] | 12282 | * OSX 10.4. |
| 12283 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12284 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12285 | if (fstatvfs == NULL) { |
| 12286 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 12287 | return NULL; |
| 12288 | } |
| 12289 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12290 | #endif /* HAVE_FSTATVFS */ |
| 12291 | |
| 12292 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12293 | if (statvfs == NULL) { |
| 12294 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 12295 | return NULL; |
| 12296 | } |
| 12297 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12298 | #endif /* HAVE_STATVFS */ |
| 12299 | |
| 12300 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12301 | if (lchown == NULL) { |
| 12302 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 12303 | return NULL; |
| 12304 | } |
| 12305 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12306 | #endif /* HAVE_LCHOWN */ |
| 12307 | |
| 12308 | |
| 12309 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12310 | |
Antoine Pitrou | 9d20e0e | 2012-09-12 18:01:36 +0200 | [diff] [blame] | 12311 | Py_INCREF(&TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12312 | PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); |
| 12313 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 12314 | billion = PyLong_FromLong(1000000000); |
| 12315 | if (!billion) |
| 12316 | return NULL; |
| 12317 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12318 | /* suppress "function not used" warnings */ |
| 12319 | { |
| 12320 | int ignored; |
| 12321 | fd_specified("", -1); |
| 12322 | follow_symlinks_specified("", 1); |
| 12323 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 12324 | dir_fd_converter(Py_None, &ignored); |
| 12325 | dir_fd_unavailable(Py_None, &ignored); |
| 12326 | } |
| 12327 | |
| 12328 | /* |
| 12329 | * provide list of locally available functions |
| 12330 | * so os.py can populate support_* lists |
| 12331 | */ |
| 12332 | list = PyList_New(0); |
| 12333 | if (!list) |
| 12334 | return NULL; |
| 12335 | for (trace = have_functions; *trace; trace++) { |
| 12336 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 12337 | if (!unicode) |
| 12338 | return NULL; |
| 12339 | if (PyList_Append(list, unicode)) |
| 12340 | return NULL; |
| 12341 | Py_DECREF(unicode); |
| 12342 | } |
| 12343 | PyModule_AddObject(m, "_have_functions", list); |
| 12344 | |
| 12345 | initialized = 1; |
| 12346 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12347 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12348 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12349 | |
| 12350 | #ifdef __cplusplus |
| 12351 | } |
| 12352 | #endif |