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" |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 30 | #else |
| 31 | #include "winreparse.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 32 | #endif |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 33 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 34 | #ifdef __cplusplus |
| 35 | extern "C" { |
| 36 | #endif |
| 37 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 38 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 39 | "This module provides access to operating system functionality that is\n\ |
| 40 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 41 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 42 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 43 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 44 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 45 | #ifdef HAVE_SYS_UIO_H |
| 46 | #include <sys/uio.h> |
| 47 | #endif |
| 48 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 49 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 50 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 51 | #endif /* HAVE_SYS_TYPES_H */ |
| 52 | |
| 53 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 54 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 55 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 57 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 58 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 59 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 60 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 61 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 62 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 63 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 65 | #ifdef HAVE_FCNTL_H |
| 66 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 67 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 68 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 69 | #ifdef HAVE_GRP_H |
| 70 | #include <grp.h> |
| 71 | #endif |
| 72 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 73 | #ifdef HAVE_SYSEXITS_H |
| 74 | #include <sysexits.h> |
| 75 | #endif /* HAVE_SYSEXITS_H */ |
| 76 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 77 | #ifdef HAVE_SYS_LOADAVG_H |
| 78 | #include <sys/loadavg.h> |
| 79 | #endif |
| 80 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 81 | #ifdef HAVE_LANGINFO_H |
| 82 | #include <langinfo.h> |
| 83 | #endif |
| 84 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 85 | #ifdef HAVE_SYS_SENDFILE_H |
| 86 | #include <sys/sendfile.h> |
| 87 | #endif |
| 88 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 89 | #ifdef HAVE_SCHED_H |
| 90 | #include <sched.h> |
| 91 | #endif |
| 92 | |
Benjamin Peterson | 2dbda07 | 2012-03-16 10:12:55 -0500 | [diff] [blame] | 93 | #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) |
Benjamin Peterson | 7b51b8d | 2012-03-14 22:28:25 -0500 | [diff] [blame] | 94 | #undef HAVE_SCHED_SETAFFINITY |
| 95 | #endif |
| 96 | |
doko@ubuntu.com | 4a173bc | 2014-04-17 19:47:16 +0200 | [diff] [blame] | 97 | #if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__) |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 98 | #define USE_XATTRS |
| 99 | #endif |
| 100 | |
| 101 | #ifdef USE_XATTRS |
Benjamin Peterson | b77fe17 | 2011-09-13 17:20:47 -0400 | [diff] [blame] | 102 | #include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 103 | #endif |
| 104 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 105 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 106 | #ifdef HAVE_SYS_SOCKET_H |
| 107 | #include <sys/socket.h> |
| 108 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 109 | #endif |
| 110 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 111 | #ifdef HAVE_DLFCN_H |
| 112 | #include <dlfcn.h> |
| 113 | #endif |
| 114 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 115 | #ifdef __hpux |
| 116 | #include <sys/mpctl.h> |
| 117 | #endif |
| 118 | |
| 119 | #if defined(__DragonFly__) || \ |
| 120 | defined(__OpenBSD__) || \ |
| 121 | defined(__FreeBSD__) || \ |
| 122 | defined(__NetBSD__) || \ |
| 123 | defined(__APPLE__) |
| 124 | #include <sys/sysctl.h> |
| 125 | #endif |
| 126 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 127 | #if defined(MS_WINDOWS) |
| 128 | # define TERMSIZE_USE_CONIO |
| 129 | #elif defined(HAVE_SYS_IOCTL_H) |
| 130 | # include <sys/ioctl.h> |
| 131 | # if defined(HAVE_TERMIOS_H) |
| 132 | # include <termios.h> |
| 133 | # endif |
| 134 | # if defined(TIOCGWINSZ) |
| 135 | # define TERMSIZE_USE_IOCTL |
| 136 | # endif |
| 137 | #endif /* MS_WINDOWS */ |
| 138 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 139 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 140 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 141 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 142 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 143 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 144 | #include <process.h> |
| 145 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 146 | #ifdef __BORLANDC__ /* Borland compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 147 | #define HAVE_EXECV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 148 | #define HAVE_OPENDIR 1 |
| 149 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 150 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 151 | #define HAVE_WAIT 1 |
| 152 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 153 | #ifdef _MSC_VER /* Microsoft compiler */ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 154 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 155 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 156 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 157 | #define HAVE_EXECV 1 |
| 158 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 159 | #define HAVE_SYSTEM 1 |
| 160 | #define HAVE_CWAIT 1 |
| 161 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 162 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 163 | #else |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 164 | /* Unix functions that the configure script doesn't check for */ |
| 165 | #define HAVE_EXECV 1 |
| 166 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 167 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 168 | #define HAVE_FORK1 1 |
| 169 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 170 | #define HAVE_GETEGID 1 |
| 171 | #define HAVE_GETEUID 1 |
| 172 | #define HAVE_GETGID 1 |
| 173 | #define HAVE_GETPPID 1 |
| 174 | #define HAVE_GETUID 1 |
| 175 | #define HAVE_KILL 1 |
| 176 | #define HAVE_OPENDIR 1 |
| 177 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 178 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 179 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 180 | #define HAVE_TTYNAME 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 181 | #endif /* _MSC_VER */ |
| 182 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 183 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 184 | |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 185 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 186 | /*[clinic input] |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 187 | module os |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 188 | [clinic start generated code]*/ |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 189 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=8cff096d1133288f]*/ |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 190 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 191 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 192 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 193 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 194 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 195 | (default) */ |
| 196 | extern char *ctermid_r(char *); |
| 197 | #endif |
| 198 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 199 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 200 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 201 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 202 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 203 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 204 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 205 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 206 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 207 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 208 | #endif |
| 209 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 210 | extern int chdir(char *); |
| 211 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 212 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 213 | extern int chdir(const char *); |
| 214 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 215 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 216 | #ifdef __BORLANDC__ |
| 217 | extern int chmod(const char *, int); |
| 218 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 219 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 220 | #endif |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 221 | /*#ifdef HAVE_FCHMOD |
| 222 | extern int fchmod(int, mode_t); |
| 223 | #endif*/ |
| 224 | /*#ifdef HAVE_LCHMOD |
| 225 | extern int lchmod(const char *, mode_t); |
| 226 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 227 | extern int chown(const char *, uid_t, gid_t); |
| 228 | extern char *getcwd(char *, int); |
| 229 | extern char *strerror(int); |
| 230 | extern int link(const char *, const char *); |
| 231 | extern int rename(const char *, const char *); |
| 232 | extern int stat(const char *, struct stat *); |
| 233 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 234 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 235 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 236 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 237 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 238 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 239 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 240 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 241 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 242 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 243 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 244 | #ifdef HAVE_UTIME_H |
| 245 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 246 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 247 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 248 | #ifdef HAVE_SYS_UTIME_H |
| 249 | #include <sys/utime.h> |
| 250 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 251 | #endif /* HAVE_SYS_UTIME_H */ |
| 252 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 253 | #ifdef HAVE_SYS_TIMES_H |
| 254 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 255 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 256 | |
| 257 | #ifdef HAVE_SYS_PARAM_H |
| 258 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 259 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 260 | |
| 261 | #ifdef HAVE_SYS_UTSNAME_H |
| 262 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 263 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 264 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 265 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 266 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 267 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 268 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 269 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 270 | #include <direct.h> |
| 271 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 272 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 273 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 274 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 275 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 276 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 277 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 278 | #endif |
| 279 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 280 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 281 | #endif |
| 282 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 283 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 284 | #endif |
| 285 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 286 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 287 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 288 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 289 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 290 | #endif |
| 291 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 292 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 293 | #endif |
| 294 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 295 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 296 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 297 | #ifndef VOLUME_NAME_DOS |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 298 | #define VOLUME_NAME_DOS 0x0 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 299 | #endif |
| 300 | #ifndef VOLUME_NAME_NT |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 301 | #define VOLUME_NAME_NT 0x2 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 302 | #endif |
| 303 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 304 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 305 | #endif |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 306 | #ifndef IO_REPARSE_TAG_MOUNT_POINT |
| 307 | #define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L) |
| 308 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 309 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 310 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 311 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 312 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 313 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 314 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 315 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 316 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 317 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 318 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 319 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 320 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 321 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 322 | #define MAXPATHLEN PATH_MAX |
| 323 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 324 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 325 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 326 | #endif /* MAXPATHLEN */ |
| 327 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 328 | #ifdef UNION_WAIT |
| 329 | /* Emulate some macros on systems that have a union instead of macros */ |
| 330 | |
| 331 | #ifndef WIFEXITED |
| 332 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 333 | #endif |
| 334 | |
| 335 | #ifndef WEXITSTATUS |
| 336 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 337 | #endif |
| 338 | |
| 339 | #ifndef WTERMSIG |
| 340 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 341 | #endif |
| 342 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 343 | #define WAIT_TYPE union wait |
| 344 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 345 | |
| 346 | #else /* !UNION_WAIT */ |
| 347 | #define WAIT_TYPE int |
| 348 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 349 | #endif /* UNION_WAIT */ |
| 350 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 351 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 352 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 353 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 354 | #define USE_CTERMID_R |
| 355 | #endif |
| 356 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 357 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 358 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 359 | #undef FSTAT |
| 360 | #undef STRUCT_STAT |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 361 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 362 | # define STAT win32_stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 363 | # define LSTAT win32_lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 364 | # define FSTAT win32_fstat |
| 365 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 366 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 367 | # define STAT stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 368 | # define LSTAT lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 369 | # define FSTAT fstat |
| 370 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 371 | #endif |
| 372 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 373 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 374 | #include <sys/mkdev.h> |
| 375 | #else |
| 376 | #if defined(MAJOR_IN_SYSMACROS) |
| 377 | #include <sys/sysmacros.h> |
| 378 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 379 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 380 | #include <sys/mkdev.h> |
| 381 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 382 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 383 | |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 384 | #define DWORD_MAX 4294967295U |
| 385 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 386 | |
| 387 | #ifdef MS_WINDOWS |
| 388 | static int |
| 389 | win32_warn_bytes_api() |
| 390 | { |
| 391 | return PyErr_WarnEx(PyExc_DeprecationWarning, |
| 392 | "The Windows bytes API has been deprecated, " |
| 393 | "use Unicode filenames instead", |
| 394 | 1); |
| 395 | } |
| 396 | #endif |
| 397 | |
| 398 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 399 | #ifndef MS_WINDOWS |
| 400 | PyObject * |
| 401 | _PyLong_FromUid(uid_t uid) |
| 402 | { |
| 403 | if (uid == (uid_t)-1) |
| 404 | return PyLong_FromLong(-1); |
| 405 | return PyLong_FromUnsignedLong(uid); |
| 406 | } |
| 407 | |
| 408 | PyObject * |
| 409 | _PyLong_FromGid(gid_t gid) |
| 410 | { |
| 411 | if (gid == (gid_t)-1) |
| 412 | return PyLong_FromLong(-1); |
| 413 | return PyLong_FromUnsignedLong(gid); |
| 414 | } |
| 415 | |
| 416 | int |
| 417 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 418 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 419 | uid_t uid; |
| 420 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 421 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 422 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 423 | unsigned long uresult; |
| 424 | |
| 425 | index = PyNumber_Index(obj); |
| 426 | if (index == NULL) { |
| 427 | PyErr_Format(PyExc_TypeError, |
| 428 | "uid should be integer, not %.200s", |
| 429 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 430 | return 0; |
| 431 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 432 | |
| 433 | /* |
| 434 | * Handling uid_t is complicated for two reasons: |
| 435 | * * Although uid_t is (always?) unsigned, it still |
| 436 | * accepts -1. |
| 437 | * * We don't know its size in advance--it may be |
| 438 | * bigger than an int, or it may be smaller than |
| 439 | * a long. |
| 440 | * |
| 441 | * So a bit of defensive programming is in order. |
| 442 | * Start with interpreting the value passed |
| 443 | * in as a signed long and see if it works. |
| 444 | */ |
| 445 | |
| 446 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 447 | |
| 448 | if (!overflow) { |
| 449 | uid = (uid_t)result; |
| 450 | |
| 451 | if (result == -1) { |
| 452 | if (PyErr_Occurred()) |
| 453 | goto fail; |
| 454 | /* It's a legitimate -1, we're done. */ |
| 455 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 456 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 457 | |
| 458 | /* Any other negative number is disallowed. */ |
| 459 | if (result < 0) |
| 460 | goto underflow; |
| 461 | |
| 462 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 463 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 464 | (long)uid != result) |
| 465 | goto underflow; |
| 466 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 467 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 468 | |
| 469 | if (overflow < 0) |
| 470 | goto underflow; |
| 471 | |
| 472 | /* |
| 473 | * Okay, the value overflowed a signed long. If it |
| 474 | * fits in an *unsigned* long, it may still be okay, |
| 475 | * as uid_t may be unsigned long on this platform. |
| 476 | */ |
| 477 | uresult = PyLong_AsUnsignedLong(index); |
| 478 | if (PyErr_Occurred()) { |
| 479 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 480 | goto overflow; |
| 481 | goto fail; |
| 482 | } |
| 483 | |
| 484 | uid = (uid_t)uresult; |
| 485 | |
| 486 | /* |
| 487 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, |
| 488 | * but this value would get interpreted as (uid_t)-1 by chown |
| 489 | * and its siblings. That's not what the user meant! So we |
| 490 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 491 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 492 | */ |
| 493 | if (uid == (uid_t)-1) |
| 494 | goto overflow; |
| 495 | |
| 496 | /* Ensure the value wasn't truncated. */ |
| 497 | if (sizeof(uid_t) < sizeof(long) && |
| 498 | (unsigned long)uid != uresult) |
| 499 | goto overflow; |
| 500 | /* fallthrough */ |
| 501 | |
| 502 | success: |
| 503 | Py_DECREF(index); |
| 504 | *(uid_t *)p = uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 505 | return 1; |
| 506 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 507 | underflow: |
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 less than minimum"); |
| 510 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 511 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 512 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 513 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 514 | "uid is greater than maximum"); |
| 515 | /* fallthrough */ |
| 516 | |
| 517 | fail: |
| 518 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | int |
| 523 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 524 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 525 | gid_t gid; |
| 526 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 527 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 528 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 529 | unsigned long uresult; |
| 530 | |
| 531 | index = PyNumber_Index(obj); |
| 532 | if (index == NULL) { |
| 533 | PyErr_Format(PyExc_TypeError, |
| 534 | "gid should be integer, not %.200s", |
| 535 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 536 | return 0; |
| 537 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 538 | |
| 539 | /* |
| 540 | * Handling gid_t is complicated for two reasons: |
| 541 | * * Although gid_t is (always?) unsigned, it still |
| 542 | * accepts -1. |
| 543 | * * We don't know its size in advance--it may be |
| 544 | * bigger than an int, or it may be smaller than |
| 545 | * a long. |
| 546 | * |
| 547 | * So a bit of defensive programming is in order. |
| 548 | * Start with interpreting the value passed |
| 549 | * in as a signed long and see if it works. |
| 550 | */ |
| 551 | |
| 552 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 553 | |
| 554 | if (!overflow) { |
| 555 | gid = (gid_t)result; |
| 556 | |
| 557 | if (result == -1) { |
| 558 | if (PyErr_Occurred()) |
| 559 | goto fail; |
| 560 | /* It's a legitimate -1, we're done. */ |
| 561 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 562 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 563 | |
| 564 | /* Any other negative number is disallowed. */ |
| 565 | if (result < 0) { |
| 566 | goto underflow; |
| 567 | } |
| 568 | |
| 569 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 570 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 571 | (long)gid != result) |
| 572 | goto underflow; |
| 573 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 574 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 575 | |
| 576 | if (overflow < 0) |
| 577 | goto underflow; |
| 578 | |
| 579 | /* |
| 580 | * Okay, the value overflowed a signed long. If it |
| 581 | * fits in an *unsigned* long, it may still be okay, |
| 582 | * as gid_t may be unsigned long on this platform. |
| 583 | */ |
| 584 | uresult = PyLong_AsUnsignedLong(index); |
| 585 | if (PyErr_Occurred()) { |
| 586 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 587 | goto overflow; |
| 588 | goto fail; |
| 589 | } |
| 590 | |
| 591 | gid = (gid_t)uresult; |
| 592 | |
| 593 | /* |
| 594 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, |
| 595 | * but this value would get interpreted as (gid_t)-1 by chown |
| 596 | * and its siblings. That's not what the user meant! So we |
| 597 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 598 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 599 | */ |
| 600 | if (gid == (gid_t)-1) |
| 601 | goto overflow; |
| 602 | |
| 603 | /* Ensure the value wasn't truncated. */ |
| 604 | if (sizeof(gid_t) < sizeof(long) && |
| 605 | (unsigned long)gid != uresult) |
| 606 | goto overflow; |
| 607 | /* fallthrough */ |
| 608 | |
| 609 | success: |
| 610 | Py_DECREF(index); |
| 611 | *(gid_t *)p = gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 612 | return 1; |
| 613 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 614 | underflow: |
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 less than minimum"); |
| 617 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 618 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 619 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 620 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 621 | "gid is greater than maximum"); |
| 622 | /* fallthrough */ |
| 623 | |
| 624 | fail: |
| 625 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 626 | return 0; |
| 627 | } |
| 628 | #endif /* MS_WINDOWS */ |
| 629 | |
| 630 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 631 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 632 | /* |
| 633 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 634 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 635 | * which doesn't play nicely with all the initializer lines in this file that |
| 636 | * look like this: |
| 637 | * int dir_fd = DEFAULT_DIR_FD; |
| 638 | */ |
| 639 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 640 | #else |
| 641 | #define DEFAULT_DIR_FD (-100) |
| 642 | #endif |
| 643 | |
| 644 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 645 | _fd_converter(PyObject *o, int *p, const char *allowed) |
| 646 | { |
| 647 | int overflow; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 648 | long long_value; |
| 649 | |
| 650 | PyObject *index = PyNumber_Index(o); |
| 651 | if (index == NULL) { |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 652 | PyErr_Format(PyExc_TypeError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 653 | "argument should be %s, not %.200s", |
| 654 | allowed, Py_TYPE(o)->tp_name); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 655 | return 0; |
| 656 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 657 | |
| 658 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
| 659 | Py_DECREF(index); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 660 | if (overflow > 0 || long_value > INT_MAX) { |
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 greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 663 | return 0; |
| 664 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 665 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 666 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 667 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 668 | return 0; |
| 669 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 670 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 671 | *p = (int)long_value; |
| 672 | return 1; |
| 673 | } |
| 674 | |
| 675 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 676 | dir_fd_converter(PyObject *o, void *p) |
| 677 | { |
| 678 | if (o == Py_None) { |
| 679 | *(int *)p = DEFAULT_DIR_FD; |
| 680 | return 1; |
| 681 | } |
| 682 | return _fd_converter(o, (int *)p, "integer"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | |
| 686 | |
| 687 | /* |
| 688 | * A PyArg_ParseTuple "converter" function |
| 689 | * that handles filesystem paths in the manner |
| 690 | * preferred by the os module. |
| 691 | * |
| 692 | * path_converter accepts (Unicode) strings and their |
| 693 | * subclasses, and bytes and their subclasses. What |
| 694 | * it does with the argument depends on the platform: |
| 695 | * |
| 696 | * * On Windows, if we get a (Unicode) string we |
| 697 | * extract the wchar_t * and return it; if we get |
| 698 | * bytes we extract the char * and return that. |
| 699 | * |
| 700 | * * On all other platforms, strings are encoded |
| 701 | * to bytes using PyUnicode_FSConverter, then we |
| 702 | * extract the char * from the bytes object and |
| 703 | * return that. |
| 704 | * |
| 705 | * path_converter also optionally accepts signed |
| 706 | * integers (representing open file descriptors) instead |
| 707 | * of path strings. |
| 708 | * |
| 709 | * Input fields: |
| 710 | * path.nullable |
| 711 | * If nonzero, the path is permitted to be None. |
| 712 | * path.allow_fd |
| 713 | * If nonzero, the path is permitted to be a file handle |
| 714 | * (a signed int) instead of a string. |
| 715 | * path.function_name |
| 716 | * If non-NULL, path_converter will use that as the name |
| 717 | * of the function in error messages. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 718 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 719 | * path.argument_name |
| 720 | * If non-NULL, path_converter will use that as the name |
| 721 | * of the parameter in error messages. |
| 722 | * (If path.argument_name is NULL it uses "path".) |
| 723 | * |
| 724 | * Output fields: |
| 725 | * path.wide |
| 726 | * Points to the path if it was expressed as Unicode |
| 727 | * and was not encoded. (Only used on Windows.) |
| 728 | * path.narrow |
| 729 | * Points to the path if it was expressed as bytes, |
| 730 | * or it was Unicode and was encoded to bytes. |
| 731 | * path.fd |
| 732 | * Contains a file descriptor if path.accept_fd was true |
| 733 | * and the caller provided a signed integer instead of any |
| 734 | * sort of string. |
| 735 | * |
| 736 | * WARNING: if your "path" parameter is optional, and is |
| 737 | * unspecified, path_converter will never get called. |
| 738 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 739 | * yourself! |
| 740 | * path.length |
| 741 | * The length of the path in characters, if specified as |
| 742 | * a string. |
| 743 | * path.object |
| 744 | * The original object passed in. |
| 745 | * path.cleanup |
| 746 | * For internal use only. May point to a temporary object. |
| 747 | * (Pay no attention to the man behind the curtain.) |
| 748 | * |
| 749 | * At most one of path.wide or path.narrow will be non-NULL. |
| 750 | * If path was None and path.nullable was set, |
| 751 | * or if path was an integer and path.allow_fd was set, |
| 752 | * both path.wide and path.narrow will be NULL |
| 753 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 754 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 755 | * path_converter takes care to not write to the path_t |
| 756 | * unless it's successful. However it must reset the |
| 757 | * "cleanup" field each time it's called. |
| 758 | * |
| 759 | * Use as follows: |
| 760 | * path_t path; |
| 761 | * memset(&path, 0, sizeof(path)); |
| 762 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 763 | * // ... use values from path ... |
| 764 | * path_cleanup(&path); |
| 765 | * |
| 766 | * (Note that if PyArg_Parse fails you don't need to call |
| 767 | * path_cleanup(). However it is safe to do so.) |
| 768 | */ |
| 769 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 770 | const char *function_name; |
| 771 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 772 | int nullable; |
| 773 | int allow_fd; |
| 774 | wchar_t *wide; |
| 775 | char *narrow; |
| 776 | int fd; |
| 777 | Py_ssize_t length; |
| 778 | PyObject *object; |
| 779 | PyObject *cleanup; |
| 780 | } path_t; |
| 781 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 782 | #define PATH_T_INITIALIZE(function_name, nullable, allow_fd) \ |
| 783 | {function_name, NULL, nullable, allow_fd, NULL, NULL, 0, 0, NULL, NULL} |
| 784 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 785 | static void |
| 786 | path_cleanup(path_t *path) { |
| 787 | if (path->cleanup) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 788 | Py_CLEAR(path->cleanup); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
| 792 | static int |
| 793 | path_converter(PyObject *o, void *p) { |
| 794 | path_t *path = (path_t *)p; |
| 795 | PyObject *unicode, *bytes; |
| 796 | Py_ssize_t length; |
| 797 | char *narrow; |
| 798 | |
| 799 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 800 | PyErr_Format(exc, "%s%s" fmt, \ |
| 801 | path->function_name ? path->function_name : "", \ |
| 802 | path->function_name ? ": " : "", \ |
| 803 | path->argument_name ? path->argument_name : "path") |
| 804 | |
| 805 | /* Py_CLEANUP_SUPPORTED support */ |
| 806 | if (o == NULL) { |
| 807 | path_cleanup(path); |
| 808 | return 1; |
| 809 | } |
| 810 | |
| 811 | /* ensure it's always safe to call path_cleanup() */ |
| 812 | path->cleanup = NULL; |
| 813 | |
| 814 | if (o == Py_None) { |
| 815 | if (!path->nullable) { |
| 816 | FORMAT_EXCEPTION(PyExc_TypeError, |
| 817 | "can't specify None for %s argument"); |
| 818 | return 0; |
| 819 | } |
| 820 | path->wide = NULL; |
| 821 | path->narrow = NULL; |
| 822 | path->length = 0; |
| 823 | path->object = o; |
| 824 | path->fd = -1; |
| 825 | return 1; |
| 826 | } |
| 827 | |
| 828 | unicode = PyUnicode_FromObject(o); |
| 829 | if (unicode) { |
| 830 | #ifdef MS_WINDOWS |
| 831 | wchar_t *wide; |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 832 | |
| 833 | wide = PyUnicode_AsUnicodeAndSize(unicode, &length); |
| 834 | if (!wide) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 835 | Py_DECREF(unicode); |
| 836 | return 0; |
| 837 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 838 | if (length > 32767) { |
| 839 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 840 | Py_DECREF(unicode); |
| 841 | return 0; |
| 842 | } |
| 843 | |
| 844 | path->wide = wide; |
| 845 | path->narrow = NULL; |
| 846 | path->length = length; |
| 847 | path->object = o; |
| 848 | path->fd = -1; |
| 849 | path->cleanup = unicode; |
| 850 | return Py_CLEANUP_SUPPORTED; |
| 851 | #else |
| 852 | int converted = PyUnicode_FSConverter(unicode, &bytes); |
| 853 | Py_DECREF(unicode); |
| 854 | if (!converted) |
| 855 | bytes = NULL; |
| 856 | #endif |
| 857 | } |
| 858 | else { |
| 859 | PyErr_Clear(); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 860 | if (PyObject_CheckBuffer(o)) |
| 861 | bytes = PyBytes_FromObject(o); |
| 862 | else |
| 863 | bytes = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 864 | if (!bytes) { |
| 865 | PyErr_Clear(); |
| 866 | if (path->allow_fd) { |
| 867 | int fd; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 868 | int result = _fd_converter(o, &fd, |
| 869 | "string, bytes or integer"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 870 | if (result) { |
| 871 | path->wide = NULL; |
| 872 | path->narrow = NULL; |
| 873 | path->length = 0; |
| 874 | path->object = o; |
| 875 | path->fd = fd; |
| 876 | return result; |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | if (!bytes) { |
| 883 | if (!PyErr_Occurred()) |
| 884 | FORMAT_EXCEPTION(PyExc_TypeError, "illegal type for %s parameter"); |
| 885 | return 0; |
| 886 | } |
| 887 | |
| 888 | #ifdef MS_WINDOWS |
| 889 | if (win32_warn_bytes_api()) { |
| 890 | Py_DECREF(bytes); |
| 891 | return 0; |
| 892 | } |
| 893 | #endif |
| 894 | |
| 895 | length = PyBytes_GET_SIZE(bytes); |
| 896 | #ifdef MS_WINDOWS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 897 | if (length > MAX_PATH-1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 898 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
| 899 | Py_DECREF(bytes); |
| 900 | return 0; |
| 901 | } |
| 902 | #endif |
| 903 | |
| 904 | narrow = PyBytes_AS_STRING(bytes); |
| 905 | if (length != strlen(narrow)) { |
| 906 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded NUL character in %s"); |
| 907 | Py_DECREF(bytes); |
| 908 | return 0; |
| 909 | } |
| 910 | |
| 911 | path->wide = NULL; |
| 912 | path->narrow = narrow; |
| 913 | path->length = length; |
| 914 | path->object = o; |
| 915 | path->fd = -1; |
| 916 | path->cleanup = bytes; |
| 917 | return Py_CLEANUP_SUPPORTED; |
| 918 | } |
| 919 | |
| 920 | static void |
| 921 | argument_unavailable_error(char *function_name, char *argument_name) { |
| 922 | PyErr_Format(PyExc_NotImplementedError, |
| 923 | "%s%s%s unavailable on this platform", |
| 924 | (function_name != NULL) ? function_name : "", |
| 925 | (function_name != NULL) ? ": ": "", |
| 926 | argument_name); |
| 927 | } |
| 928 | |
| 929 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 930 | dir_fd_unavailable(PyObject *o, void *p) |
| 931 | { |
| 932 | int dir_fd; |
| 933 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 934 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 935 | if (dir_fd != DEFAULT_DIR_FD) { |
| 936 | argument_unavailable_error(NULL, "dir_fd"); |
| 937 | return 0; |
| 938 | } |
| 939 | *(int *)p = dir_fd; |
| 940 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | static int |
| 944 | fd_specified(char *function_name, int fd) { |
| 945 | if (fd == -1) |
| 946 | return 0; |
| 947 | |
| 948 | argument_unavailable_error(function_name, "fd"); |
| 949 | return 1; |
| 950 | } |
| 951 | |
| 952 | static int |
| 953 | follow_symlinks_specified(char *function_name, int follow_symlinks) { |
| 954 | if (follow_symlinks) |
| 955 | return 0; |
| 956 | |
| 957 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 958 | return 1; |
| 959 | } |
| 960 | |
| 961 | static int |
| 962 | path_and_dir_fd_invalid(char *function_name, path_t *path, int dir_fd) { |
| 963 | if (!path->narrow && !path->wide && (dir_fd != DEFAULT_DIR_FD)) { |
| 964 | PyErr_Format(PyExc_ValueError, |
| 965 | "%s: can't specify dir_fd without matching path", |
| 966 | function_name); |
| 967 | return 1; |
| 968 | } |
| 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | static int |
| 973 | dir_fd_and_fd_invalid(char *function_name, int dir_fd, int fd) { |
| 974 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 975 | PyErr_Format(PyExc_ValueError, |
| 976 | "%s: can't specify both dir_fd and fd", |
| 977 | function_name); |
| 978 | return 1; |
| 979 | } |
| 980 | return 0; |
| 981 | } |
| 982 | |
| 983 | static int |
| 984 | fd_and_follow_symlinks_invalid(char *function_name, int fd, |
| 985 | int follow_symlinks) { |
| 986 | if ((fd > 0) && (!follow_symlinks)) { |
| 987 | PyErr_Format(PyExc_ValueError, |
| 988 | "%s: cannot use fd and follow_symlinks together", |
| 989 | function_name); |
| 990 | return 1; |
| 991 | } |
| 992 | return 0; |
| 993 | } |
| 994 | |
| 995 | static int |
| 996 | dir_fd_and_follow_symlinks_invalid(char *function_name, int dir_fd, |
| 997 | int follow_symlinks) { |
| 998 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 999 | PyErr_Format(PyExc_ValueError, |
| 1000 | "%s: cannot use dir_fd and follow_symlinks together", |
| 1001 | function_name); |
| 1002 | return 1; |
| 1003 | } |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1007 | /* A helper used by a number of POSIX-only functions */ |
| 1008 | #ifndef MS_WINDOWS |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 1009 | static int |
| 1010 | _parse_off_t(PyObject* arg, void* addr) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1011 | { |
| 1012 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 1013 | *((off_t*)addr) = PyLong_AsLong(arg); |
| 1014 | #else |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 1015 | *((off_t*)addr) = PyLong_AsLongLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1016 | #endif |
| 1017 | if (PyErr_Occurred()) |
| 1018 | return 0; |
| 1019 | return 1; |
| 1020 | } |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1021 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1022 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1023 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 1024 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 1025 | * valid and raise an assertion if it isn't. |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1026 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 1027 | * an assertion can be useful, but it does contradict the POSIX standard |
| 1028 | * which for write(2) states: |
| 1029 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 1030 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 1031 | * writing." |
| 1032 | * Furthermore, python allows the user to enter any old integer |
| 1033 | * as a fd and should merely raise a python exception on error. |
| 1034 | * The Microsoft CRT doesn't provide an official way to check for the |
| 1035 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1036 | * 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] | 1037 | * internal structures involved. |
| 1038 | * The structures below must be updated for each version of visual studio |
| 1039 | * according to the file internal.h in the CRT source, until MS comes |
| 1040 | * up with a less hacky way to do this. |
| 1041 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 1042 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 1043 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 1044 | /* The actual size of the structure is determined at runtime. |
| 1045 | * Only the first items must be present. |
| 1046 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1047 | typedef struct { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1048 | intptr_t osfhnd; |
| 1049 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 1050 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1051 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 1052 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1053 | #define IOINFO_L2E 5 |
| 1054 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 1055 | #define IOINFO_ARRAYS 64 |
| 1056 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 1057 | #define FOPEN 0x01 |
| 1058 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 1059 | |
| 1060 | /* This function emulates what the windows CRT does to validate file handles */ |
| 1061 | int |
| 1062 | _PyVerify_fd(int fd) |
| 1063 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1064 | const int i1 = fd >> IOINFO_L2E; |
| 1065 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 1066 | |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 1067 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1068 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1069 | /* Determine the actual size of the ioinfo structure, |
| 1070 | * as used by the CRT loaded in memory |
| 1071 | */ |
| 1072 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 1073 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 1074 | } |
| 1075 | if (sizeof_ioinfo == 0) { |
| 1076 | /* This should not happen... */ |
| 1077 | goto fail; |
| 1078 | } |
| 1079 | |
| 1080 | /* See that it isn't a special CLEAR fileno */ |
| 1081 | if (fd != _NO_CONSOLE_FILENO) { |
| 1082 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 1083 | * we check pointer validity and other info |
| 1084 | */ |
| 1085 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 1086 | /* finally, check that the file is open */ |
| 1087 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 1088 | if (info->osfile & FOPEN) { |
| 1089 | return 1; |
| 1090 | } |
| 1091 | } |
| 1092 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 1093 | fail: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1094 | errno = EBADF; |
| 1095 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 1099 | static int |
| 1100 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 1101 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1102 | if (!_PyVerify_fd(fd1)) |
| 1103 | return 0; |
| 1104 | if (fd2 == _NO_CONSOLE_FILENO) |
| 1105 | return 0; |
| 1106 | if ((unsigned)fd2 < _NHANDLE_) |
| 1107 | return 1; |
| 1108 | else |
| 1109 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1110 | } |
| 1111 | #else |
| 1112 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 1113 | #define _PyVerify_fd_dup2(A, B) (1) |
| 1114 | #endif |
| 1115 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1116 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1117 | |
| 1118 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1119 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1120 | { |
| 1121 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1122 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 1123 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1124 | |
| 1125 | if (0 == DeviceIoControl( |
| 1126 | reparse_point_handle, |
| 1127 | FSCTL_GET_REPARSE_POINT, |
| 1128 | NULL, 0, /* in buffer */ |
| 1129 | target_buffer, sizeof(target_buffer), |
| 1130 | &n_bytes_returned, |
| 1131 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1132 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1133 | |
| 1134 | if (reparse_tag) |
| 1135 | *reparse_tag = rdb->ReparseTag; |
| 1136 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1137 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1138 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1139 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1140 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1141 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1142 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1143 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1144 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1145 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1146 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1147 | */ |
| 1148 | #include <crt_externs.h> |
| 1149 | static char **environ; |
| 1150 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1151 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1152 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1153 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1154 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1155 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1156 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1157 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1158 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1159 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1160 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1161 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1162 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1163 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1164 | d = PyDict_New(); |
| 1165 | if (d == NULL) |
| 1166 | return NULL; |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1167 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1168 | if (environ == NULL) |
| 1169 | environ = *_NSGetEnviron(); |
| 1170 | #endif |
| 1171 | #ifdef MS_WINDOWS |
| 1172 | /* _wenviron must be initialized in this way if the program is started |
| 1173 | through main() instead of wmain(). */ |
| 1174 | _wgetenv(L""); |
| 1175 | if (_wenviron == NULL) |
| 1176 | return d; |
| 1177 | /* This part ignores errors */ |
| 1178 | for (e = _wenviron; *e != NULL; e++) { |
| 1179 | PyObject *k; |
| 1180 | PyObject *v; |
| 1181 | wchar_t *p = wcschr(*e, L'='); |
| 1182 | if (p == NULL) |
| 1183 | continue; |
| 1184 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1185 | if (k == NULL) { |
| 1186 | PyErr_Clear(); |
| 1187 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1188 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1189 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1190 | if (v == NULL) { |
| 1191 | PyErr_Clear(); |
| 1192 | Py_DECREF(k); |
| 1193 | continue; |
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 | if (PyDict_GetItem(d, k) == NULL) { |
| 1196 | if (PyDict_SetItem(d, k, v) != 0) |
| 1197 | PyErr_Clear(); |
| 1198 | } |
| 1199 | Py_DECREF(k); |
| 1200 | Py_DECREF(v); |
| 1201 | } |
| 1202 | #else |
| 1203 | if (environ == NULL) |
| 1204 | return d; |
| 1205 | /* This part ignores errors */ |
| 1206 | for (e = environ; *e != NULL; e++) { |
| 1207 | PyObject *k; |
| 1208 | PyObject *v; |
| 1209 | char *p = strchr(*e, '='); |
| 1210 | if (p == NULL) |
| 1211 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1212 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1213 | if (k == NULL) { |
| 1214 | PyErr_Clear(); |
| 1215 | continue; |
| 1216 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1217 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1218 | if (v == NULL) { |
| 1219 | PyErr_Clear(); |
| 1220 | Py_DECREF(k); |
| 1221 | continue; |
| 1222 | } |
| 1223 | if (PyDict_GetItem(d, k) == NULL) { |
| 1224 | if (PyDict_SetItem(d, k, v) != 0) |
| 1225 | PyErr_Clear(); |
| 1226 | } |
| 1227 | Py_DECREF(k); |
| 1228 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1229 | } |
| 1230 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1231 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1234 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1235 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1236 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1237 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1238 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1239 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1240 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1241 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1242 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1243 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1244 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1245 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1246 | /* XXX We should pass the function name along in the future. |
| 1247 | (winreg.c also wants to pass the function name.) |
| 1248 | This would however require an additional param to the |
| 1249 | Windows error object, which is non-trivial. |
| 1250 | */ |
| 1251 | errno = GetLastError(); |
| 1252 | if (filename) |
| 1253 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1254 | else |
| 1255 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1256 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1257 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1258 | static PyObject * |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1259 | win32_error_object(char* function, PyObject* filename) |
| 1260 | { |
| 1261 | /* XXX - see win32_error for comments on 'function' */ |
| 1262 | errno = GetLastError(); |
| 1263 | if (filename) |
| 1264 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1265 | PyExc_OSError, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1266 | errno, |
| 1267 | filename); |
| 1268 | else |
| 1269 | return PyErr_SetFromWindowsErr(errno); |
| 1270 | } |
| 1271 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1272 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1273 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1274 | static PyObject * |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1275 | path_error(path_t *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1276 | { |
| 1277 | #ifdef MS_WINDOWS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1278 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 1279 | 0, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1280 | #else |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1281 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1282 | #endif |
| 1283 | } |
| 1284 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1285 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1286 | static PyObject * |
| 1287 | path_error2(path_t *path, path_t *path2) |
| 1288 | { |
| 1289 | #ifdef MS_WINDOWS |
| 1290 | return PyErr_SetExcFromWindowsErrWithFilenameObjects(PyExc_OSError, |
| 1291 | 0, path->object, path2->object); |
| 1292 | #else |
| 1293 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, |
| 1294 | path->object, path2->object); |
| 1295 | #endif |
| 1296 | } |
| 1297 | |
| 1298 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1299 | /* POSIX generic methods */ |
| 1300 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1301 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1302 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 1303 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1304 | int fd; |
| 1305 | int res; |
| 1306 | fd = PyObject_AsFileDescriptor(fdobj); |
| 1307 | if (fd < 0) |
| 1308 | return NULL; |
| 1309 | if (!_PyVerify_fd(fd)) |
| 1310 | return posix_error(); |
| 1311 | Py_BEGIN_ALLOW_THREADS |
| 1312 | res = (*func)(fd); |
| 1313 | Py_END_ALLOW_THREADS |
| 1314 | if (res < 0) |
| 1315 | return posix_error(); |
| 1316 | Py_INCREF(Py_None); |
| 1317 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1318 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1319 | |
| 1320 | static PyObject * |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1321 | posix_1str(const char *func_name, PyObject *args, char *format, |
| 1322 | int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1323 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1324 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1325 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1326 | memset(&path, 0, sizeof(path)); |
| 1327 | path.function_name = func_name; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1328 | if (!PyArg_ParseTuple(args, format, |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1329 | path_converter, &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1330 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1331 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1332 | res = (*func)(path.narrow); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1333 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1334 | if (res < 0) { |
| 1335 | path_error(&path); |
| 1336 | path_cleanup(&path); |
| 1337 | return NULL; |
| 1338 | } |
| 1339 | path_cleanup(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1340 | Py_INCREF(Py_None); |
| 1341 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1344 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1345 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1346 | /* This is a reimplementation of the C library's chdir function, |
| 1347 | but one that produces Win32 errors instead of DOS error codes. |
| 1348 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1349 | it also needs to set "magic" environment variables indicating |
| 1350 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1351 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1352 | win32_chdir(LPCSTR path) |
| 1353 | { |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1354 | char new_path[MAX_PATH]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1355 | int result; |
| 1356 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1357 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1358 | if(!SetCurrentDirectoryA(path)) |
| 1359 | return FALSE; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1360 | result = GetCurrentDirectoryA(Py_ARRAY_LENGTH(new_path), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1361 | if (!result) |
| 1362 | return FALSE; |
| 1363 | /* In the ANSI API, there should not be any paths longer |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1364 | than MAX_PATH-1 (not including the final null character). */ |
| 1365 | assert(result < Py_ARRAY_LENGTH(new_path)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1366 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 1367 | strncmp(new_path, "//", 2) == 0) |
| 1368 | /* UNC path, nothing to do. */ |
| 1369 | return TRUE; |
| 1370 | env[1] = new_path[0]; |
| 1371 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | /* The Unicode version differs from the ANSI version |
| 1375 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1376 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1377 | win32_wchdir(LPCWSTR path) |
| 1378 | { |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1379 | wchar_t _new_path[MAX_PATH], *new_path = _new_path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1380 | int result; |
| 1381 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1382 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1383 | if(!SetCurrentDirectoryW(path)) |
| 1384 | return FALSE; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1385 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(new_path), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1386 | if (!result) |
| 1387 | return FALSE; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1388 | if (result > Py_ARRAY_LENGTH(new_path)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1389 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1390 | if (!new_path) { |
| 1391 | SetLastError(ERROR_OUTOFMEMORY); |
| 1392 | return FALSE; |
| 1393 | } |
| 1394 | result = GetCurrentDirectoryW(result, new_path); |
| 1395 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1396 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1397 | return FALSE; |
| 1398 | } |
| 1399 | } |
| 1400 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1401 | wcsncmp(new_path, L"//", 2) == 0) |
| 1402 | /* UNC path, nothing to do. */ |
| 1403 | return TRUE; |
| 1404 | env[1] = new_path[0]; |
| 1405 | result = SetEnvironmentVariableW(env, new_path); |
| 1406 | if (new_path != _new_path) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1407 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1408 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1409 | } |
| 1410 | #endif |
| 1411 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1412 | #ifdef MS_WINDOWS |
| 1413 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1414 | - time stamps are restricted to second resolution |
| 1415 | - file modification times suffer from forth-and-back conversions between |
| 1416 | UTC and local time |
| 1417 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1418 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1419 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1420 | |
| 1421 | struct win32_stat{ |
Brian Curtin | 87e63a2 | 2012-12-31 11:59:48 -0600 | [diff] [blame] | 1422 | unsigned long st_dev; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1423 | __int64 st_ino; |
| 1424 | unsigned short st_mode; |
| 1425 | int st_nlink; |
| 1426 | int st_uid; |
| 1427 | int st_gid; |
Brian Curtin | 87e63a2 | 2012-12-31 11:59:48 -0600 | [diff] [blame] | 1428 | unsigned long st_rdev; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1429 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1430 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1431 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1432 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1433 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1434 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1435 | int st_ctime_nsec; |
| 1436 | }; |
| 1437 | |
| 1438 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 1439 | |
| 1440 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1441 | 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] | 1442 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1443 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 1444 | /* Cannot simply cast and dereference in_ptr, |
| 1445 | since it might not be aligned properly */ |
| 1446 | __int64 in; |
| 1447 | memcpy(&in, in_ptr, sizeof(in)); |
| 1448 | *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] | 1449 | *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] | 1450 | } |
| 1451 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1452 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1453 | 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] | 1454 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1455 | /* XXX endianness */ |
| 1456 | __int64 out; |
| 1457 | out = time_in + secs_between_epochs; |
| 1458 | out = out * 10000000 + nsec_in / 100; |
| 1459 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1462 | /* Below, we *know* that ugo+r is 0444 */ |
| 1463 | #if _S_IREAD != 0400 |
| 1464 | #error Unsupported C library |
| 1465 | #endif |
| 1466 | static int |
| 1467 | attributes_to_mode(DWORD attr) |
| 1468 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1469 | int m = 0; |
| 1470 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1471 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1472 | else |
| 1473 | m |= _S_IFREG; |
| 1474 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1475 | m |= 0444; |
| 1476 | else |
| 1477 | m |= 0666; |
| 1478 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1482 | 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] | 1483 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1484 | memset(result, 0, sizeof(*result)); |
| 1485 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1486 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
Brian Curtin | 490b32a | 2012-12-26 07:03:03 -0600 | [diff] [blame] | 1487 | result->st_dev = info->dwVolumeSerialNumber; |
| 1488 | result->st_rdev = result->st_dev; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1489 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1490 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1491 | 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] | 1492 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1493 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1494 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1495 | /* first clear the S_IFMT bits */ |
Christian Heimes | 99d6135 | 2013-06-23 23:56:05 +0200 | [diff] [blame] | 1496 | result->st_mode ^= (result->st_mode & S_IFMT); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1497 | /* now set the bits that make this a symlink */ |
Christian Heimes | 99d6135 | 2013-06-23 23:56:05 +0200 | [diff] [blame] | 1498 | result->st_mode |= S_IFLNK; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1499 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1500 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1501 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1504 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1505 | 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] | 1506 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1507 | HANDLE hFindFile; |
| 1508 | WIN32_FIND_DATAA FileData; |
| 1509 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1510 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1511 | return FALSE; |
| 1512 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1513 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1514 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1515 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1516 | info->ftCreationTime = FileData.ftCreationTime; |
| 1517 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1518 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1519 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1520 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1521 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1522 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1523 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1524 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1528 | 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] | 1529 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1530 | HANDLE hFindFile; |
| 1531 | WIN32_FIND_DATAW FileData; |
| 1532 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1533 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1534 | return FALSE; |
| 1535 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1536 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1537 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1538 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1539 | info->ftCreationTime = FileData.ftCreationTime; |
| 1540 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1541 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1542 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1543 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1544 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1545 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1546 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1547 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1550 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
Antoine Pitrou | 06eecea | 2012-10-21 16:33:33 +0200 | [diff] [blame] | 1551 | static int has_GetFinalPathNameByHandle = -1; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1552 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 1553 | DWORD); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1554 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1555 | check_GetFinalPathNameByHandle() |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1556 | { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1557 | HINSTANCE hKernel32; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1558 | DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 1559 | DWORD); |
| 1560 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1561 | /* only recheck */ |
Antoine Pitrou | 06eecea | 2012-10-21 16:33:33 +0200 | [diff] [blame] | 1562 | if (-1 == has_GetFinalPathNameByHandle) |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1563 | { |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1564 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1565 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 1566 | "GetFinalPathNameByHandleA"); |
| 1567 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 1568 | "GetFinalPathNameByHandleW"); |
| 1569 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 1570 | Py_GetFinalPathNameByHandleW; |
| 1571 | } |
| 1572 | return has_GetFinalPathNameByHandle; |
| 1573 | } |
| 1574 | |
| 1575 | static BOOL |
| 1576 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1577 | { |
| 1578 | int buf_size, result_length; |
| 1579 | wchar_t *buf; |
| 1580 | |
| 1581 | /* We have a good handle to the target, use it to determine |
| 1582 | the target path name (then we'll call lstat on it). */ |
| 1583 | buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1584 | VOLUME_NAME_DOS); |
| 1585 | if(!buf_size) |
| 1586 | return FALSE; |
| 1587 | |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1588 | buf = (wchar_t *)PyMem_Malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1589 | if (!buf) { |
| 1590 | SetLastError(ERROR_OUTOFMEMORY); |
| 1591 | return FALSE; |
| 1592 | } |
| 1593 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1594 | result_length = Py_GetFinalPathNameByHandleW(hdl, |
| 1595 | buf, buf_size, VOLUME_NAME_DOS); |
| 1596 | |
| 1597 | if(!result_length) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1598 | PyMem_Free(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1599 | return FALSE; |
| 1600 | } |
| 1601 | |
| 1602 | if(!CloseHandle(hdl)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1603 | PyMem_Free(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1604 | return FALSE; |
| 1605 | } |
| 1606 | |
| 1607 | buf[result_length] = 0; |
| 1608 | |
| 1609 | *target_path = buf; |
| 1610 | return TRUE; |
| 1611 | } |
| 1612 | |
| 1613 | static int |
| 1614 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1615 | BOOL traverse); |
| 1616 | static int |
| 1617 | win32_xstat_impl(const char *path, struct win32_stat *result, |
| 1618 | BOOL traverse) |
| 1619 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1620 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1621 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1622 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1623 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1624 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1625 | const char *dot; |
| 1626 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1627 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1628 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1629 | traverse reparse point. */ |
| 1630 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1633 | hFile = CreateFileA( |
| 1634 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1635 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1636 | 0, /* share mode */ |
| 1637 | NULL, /* security attributes */ |
| 1638 | OPEN_EXISTING, |
| 1639 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1640 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1641 | Because of this, calls like GetFinalPathNameByHandle will return |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 1642 | the symlink path again and not the actual final path. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1643 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1644 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1645 | NULL); |
| 1646 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1647 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1648 | /* Either the target doesn't exist, or we don't have access to |
| 1649 | get a handle to it. If the former, we need to return an error. |
| 1650 | If the latter, we can use attributes_from_dir. */ |
| 1651 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1652 | return -1; |
| 1653 | /* Could not get attributes on open file. Fall back to |
| 1654 | reading the directory. */ |
| 1655 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1656 | /* Very strange. This should not fail now */ |
| 1657 | return -1; |
| 1658 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1659 | if (traverse) { |
| 1660 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1661 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1662 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1663 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1664 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1665 | } else { |
| 1666 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1667 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1668 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1669 | } |
| 1670 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1671 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1672 | return -1; |
| 1673 | |
| 1674 | /* Close the outer open file handle now that we're about to |
| 1675 | reopen it with different flags. */ |
| 1676 | if (!CloseHandle(hFile)) |
| 1677 | return -1; |
| 1678 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1679 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1680 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1681 | the file without the reparse handling flag set. */ |
| 1682 | hFile2 = CreateFileA( |
| 1683 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1684 | NULL, OPEN_EXISTING, |
| 1685 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1686 | NULL); |
| 1687 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1688 | return -1; |
| 1689 | |
| 1690 | if (!get_target_path(hFile2, &target_path)) |
| 1691 | return -1; |
| 1692 | |
| 1693 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1694 | PyMem_Free(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1695 | return code; |
| 1696 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1697 | } else |
| 1698 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1699 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1700 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1701 | |
| 1702 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1703 | dot = strrchr(path, '.'); |
| 1704 | if (dot) { |
| 1705 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1706 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1707 | result->st_mode |= 0111; |
| 1708 | } |
| 1709 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1713 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1714 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1715 | { |
| 1716 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1717 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1718 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1719 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1720 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1721 | const wchar_t *dot; |
| 1722 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1723 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1724 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1725 | traverse reparse point. */ |
| 1726 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1729 | hFile = CreateFileW( |
| 1730 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1731 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1732 | 0, /* share mode */ |
| 1733 | NULL, /* security attributes */ |
| 1734 | OPEN_EXISTING, |
| 1735 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1736 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1737 | Because of this, calls like GetFinalPathNameByHandle will return |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 1738 | the symlink path again and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1739 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1740 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1741 | NULL); |
| 1742 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1743 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1744 | /* Either the target doesn't exist, or we don't have access to |
| 1745 | get a handle to it. If the former, we need to return an error. |
| 1746 | If the latter, we can use attributes_from_dir. */ |
| 1747 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1748 | return -1; |
| 1749 | /* Could not get attributes on open file. Fall back to |
| 1750 | reading the directory. */ |
| 1751 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1752 | /* Very strange. This should not fail now */ |
| 1753 | return -1; |
| 1754 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1755 | if (traverse) { |
| 1756 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1757 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1758 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1759 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1760 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1761 | } else { |
| 1762 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1763 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1764 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1765 | } |
| 1766 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1767 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1768 | return -1; |
| 1769 | |
| 1770 | /* Close the outer open file handle now that we're about to |
| 1771 | reopen it with different flags. */ |
| 1772 | if (!CloseHandle(hFile)) |
| 1773 | return -1; |
| 1774 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1775 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1776 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1777 | the file without the reparse handling flag set. */ |
| 1778 | hFile2 = CreateFileW( |
| 1779 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1780 | NULL, OPEN_EXISTING, |
| 1781 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1782 | NULL); |
| 1783 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1784 | return -1; |
| 1785 | |
| 1786 | if (!get_target_path(hFile2, &target_path)) |
| 1787 | return -1; |
| 1788 | |
| 1789 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1790 | PyMem_Free(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1791 | return code; |
| 1792 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1793 | } else |
| 1794 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1795 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1796 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1797 | |
| 1798 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1799 | dot = wcsrchr(path, '.'); |
| 1800 | if (dot) { |
| 1801 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1802 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1803 | result->st_mode |= 0111; |
| 1804 | } |
| 1805 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
| 1808 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1809 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1810 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1811 | /* Protocol violation: we explicitly clear errno, instead of |
| 1812 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1813 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1814 | errno = 0; |
| 1815 | return code; |
| 1816 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1817 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1818 | static int |
| 1819 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1820 | { |
| 1821 | /* Protocol violation: we explicitly clear errno, instead of |
| 1822 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1823 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1824 | errno = 0; |
| 1825 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1826 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1827 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1828 | |
| 1829 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1830 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1831 | default does not traverse symlinks and instead returns attributes for |
| 1832 | the symlink. |
| 1833 | |
| 1834 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1835 | win32_stat will first explicitly resolve the symlink target and then will |
| 1836 | call win32_lstat on that result. |
| 1837 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1838 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1839 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1840 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1841 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1842 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1843 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1846 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1847 | 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] | 1848 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1849 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | static int |
| 1853 | win32_stat(const char* path, struct win32_stat *result) |
| 1854 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1855 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1856 | } |
| 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_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1860 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1861 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | static int |
| 1865 | win32_fstat(int file_number, struct win32_stat *result) |
| 1866 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1867 | BY_HANDLE_FILE_INFORMATION info; |
| 1868 | HANDLE h; |
| 1869 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1870 | |
Richard Oudkerk | 2240ac1 | 2012-07-06 12:05:32 +0100 | [diff] [blame] | 1871 | if (!_PyVerify_fd(file_number)) |
| 1872 | h = INVALID_HANDLE_VALUE; |
| 1873 | else |
| 1874 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1875 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1876 | /* Protocol violation: we explicitly clear errno, instead of |
| 1877 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1878 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1879 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1880 | if (h == INVALID_HANDLE_VALUE) { |
| 1881 | /* This is really a C library error (invalid file handle). |
| 1882 | We set the Win32 error to the closes one matching. */ |
| 1883 | SetLastError(ERROR_INVALID_HANDLE); |
| 1884 | return -1; |
| 1885 | } |
| 1886 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1887 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1888 | type = GetFileType(h); |
| 1889 | if (type == FILE_TYPE_UNKNOWN) { |
| 1890 | DWORD error = GetLastError(); |
| 1891 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1892 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1893 | } |
| 1894 | /* else: valid but unknown file */ |
| 1895 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1896 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1897 | if (type != FILE_TYPE_DISK) { |
| 1898 | if (type == FILE_TYPE_CHAR) |
| 1899 | result->st_mode = _S_IFCHR; |
| 1900 | else if (type == FILE_TYPE_PIPE) |
| 1901 | result->st_mode = _S_IFIFO; |
| 1902 | return 0; |
| 1903 | } |
| 1904 | |
| 1905 | if (!GetFileInformationByHandle(h, &info)) { |
| 1906 | return -1; |
| 1907 | } |
| 1908 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1909 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1910 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1911 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1912 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | #endif /* MS_WINDOWS */ |
| 1916 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1917 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1918 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1919 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1920 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1921 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1922 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1923 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1924 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1925 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1926 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1927 | |
| 1928 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1929 | {"st_mode", "protection bits"}, |
| 1930 | {"st_ino", "inode"}, |
| 1931 | {"st_dev", "device"}, |
| 1932 | {"st_nlink", "number of hard links"}, |
| 1933 | {"st_uid", "user ID of owner"}, |
| 1934 | {"st_gid", "group ID of owner"}, |
| 1935 | {"st_size", "total size, in bytes"}, |
| 1936 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1937 | {NULL, "integer time of last access"}, |
| 1938 | {NULL, "integer time of last modification"}, |
| 1939 | {NULL, "integer time of last change"}, |
| 1940 | {"st_atime", "time of last access"}, |
| 1941 | {"st_mtime", "time of last modification"}, |
| 1942 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1943 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1944 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1945 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1946 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1947 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1948 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1949 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1950 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1951 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1952 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1953 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1954 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1955 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1956 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1957 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1958 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1959 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1960 | #endif |
| 1961 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1962 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1963 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1964 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1965 | }; |
| 1966 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1967 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1968 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1969 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1970 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1971 | #endif |
| 1972 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1973 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1974 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1975 | #else |
| 1976 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1977 | #endif |
| 1978 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1979 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1980 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1981 | #else |
| 1982 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1983 | #endif |
| 1984 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1985 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1986 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1987 | #else |
| 1988 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1989 | #endif |
| 1990 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1991 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1992 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1993 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1994 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1995 | #endif |
| 1996 | |
| 1997 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1998 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1999 | #else |
| 2000 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 2001 | #endif |
| 2002 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2003 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2004 | "stat_result", /* name */ |
| 2005 | stat_result__doc__, /* doc */ |
| 2006 | stat_result_fields, |
| 2007 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2008 | }; |
| 2009 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2010 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2011 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 2012 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2013 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 2014 | 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] | 2015 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2016 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2017 | |
| 2018 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2019 | {"f_bsize", }, |
| 2020 | {"f_frsize", }, |
| 2021 | {"f_blocks", }, |
| 2022 | {"f_bfree", }, |
| 2023 | {"f_bavail", }, |
| 2024 | {"f_files", }, |
| 2025 | {"f_ffree", }, |
| 2026 | {"f_favail", }, |
| 2027 | {"f_flag", }, |
| 2028 | {"f_namemax",}, |
| 2029 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2030 | }; |
| 2031 | |
| 2032 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2033 | "statvfs_result", /* name */ |
| 2034 | statvfs_result__doc__, /* doc */ |
| 2035 | statvfs_result_fields, |
| 2036 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2037 | }; |
| 2038 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2039 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 2040 | PyDoc_STRVAR(waitid_result__doc__, |
| 2041 | "waitid_result: Result from waitid.\n\n\ |
| 2042 | This object may be accessed either as a tuple of\n\ |
| 2043 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 2044 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 2045 | \n\ |
| 2046 | See os.waitid for more information."); |
| 2047 | |
| 2048 | static PyStructSequence_Field waitid_result_fields[] = { |
| 2049 | {"si_pid", }, |
| 2050 | {"si_uid", }, |
| 2051 | {"si_signo", }, |
| 2052 | {"si_status", }, |
| 2053 | {"si_code", }, |
| 2054 | {0} |
| 2055 | }; |
| 2056 | |
| 2057 | static PyStructSequence_Desc waitid_result_desc = { |
| 2058 | "waitid_result", /* name */ |
| 2059 | waitid_result__doc__, /* doc */ |
| 2060 | waitid_result_fields, |
| 2061 | 5 |
| 2062 | }; |
| 2063 | static PyTypeObject WaitidResultType; |
| 2064 | #endif |
| 2065 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2066 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2067 | static PyTypeObject StatResultType; |
| 2068 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2069 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 2070 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2071 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2072 | static newfunc structseq_new; |
| 2073 | |
| 2074 | static PyObject * |
| 2075 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2076 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2077 | PyStructSequence *result; |
| 2078 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2079 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2080 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 2081 | if (!result) |
| 2082 | return NULL; |
| 2083 | /* If we have been initialized from a tuple, |
| 2084 | st_?time might be set to None. Initialize it |
| 2085 | from the int slots. */ |
| 2086 | for (i = 7; i <= 9; i++) { |
| 2087 | if (result->ob_item[i+3] == Py_None) { |
| 2088 | Py_DECREF(Py_None); |
| 2089 | Py_INCREF(result->ob_item[i]); |
| 2090 | result->ob_item[i+3] = result->ob_item[i]; |
| 2091 | } |
| 2092 | } |
| 2093 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | |
| 2097 | |
| 2098 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 2099 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2100 | |
| 2101 | PyDoc_STRVAR(stat_float_times__doc__, |
| 2102 | "stat_float_times([newval]) -> oldval\n\n\ |
| 2103 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 2104 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 2105 | future calls return ints. \n\ |
| 2106 | If newval is omitted, return the current setting.\n"); |
| 2107 | |
| 2108 | static PyObject* |
| 2109 | stat_float_times(PyObject* self, PyObject *args) |
| 2110 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2111 | int newval = -1; |
| 2112 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 2113 | return NULL; |
Victor Stinner | 034d0aa | 2012-06-05 01:22:15 +0200 | [diff] [blame] | 2114 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2115 | "stat_float_times() is deprecated", |
| 2116 | 1)) |
| 2117 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2118 | if (newval == -1) |
| 2119 | /* Return old value */ |
| 2120 | return PyBool_FromLong(_stat_float_times); |
| 2121 | _stat_float_times = newval; |
| 2122 | Py_INCREF(Py_None); |
| 2123 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2124 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2125 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2126 | static PyObject *billion = NULL; |
| 2127 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2128 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2129 | 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] | 2130 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2131 | PyObject *s = _PyLong_FromTime_t(sec); |
| 2132 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 2133 | PyObject *s_in_ns = NULL; |
| 2134 | PyObject *ns_total = NULL; |
| 2135 | PyObject *float_s = NULL; |
| 2136 | |
| 2137 | if (!(s && ns_fractional)) |
| 2138 | goto exit; |
| 2139 | |
| 2140 | s_in_ns = PyNumber_Multiply(s, billion); |
| 2141 | if (!s_in_ns) |
| 2142 | goto exit; |
| 2143 | |
| 2144 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 2145 | if (!ns_total) |
| 2146 | goto exit; |
| 2147 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2148 | if (_stat_float_times) { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2149 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2150 | if (!float_s) |
| 2151 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2152 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2153 | else { |
| 2154 | float_s = s; |
| 2155 | Py_INCREF(float_s); |
| 2156 | } |
| 2157 | |
| 2158 | PyStructSequence_SET_ITEM(v, index, s); |
| 2159 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2160 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2161 | s = NULL; |
| 2162 | float_s = NULL; |
| 2163 | ns_total = NULL; |
| 2164 | exit: |
| 2165 | Py_XDECREF(s); |
| 2166 | Py_XDECREF(ns_fractional); |
| 2167 | Py_XDECREF(s_in_ns); |
| 2168 | Py_XDECREF(ns_total); |
| 2169 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2170 | } |
| 2171 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2172 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2173 | (used by posix_stat() and posix_fstat()) */ |
| 2174 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2175 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2176 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2177 | unsigned long ansec, mnsec, cnsec; |
| 2178 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 2179 | if (v == NULL) |
| 2180 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2181 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2182 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2183 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2184 | PyStructSequence_SET_ITEM(v, 1, |
| 2185 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2186 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2187 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2188 | #endif |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2189 | #ifdef MS_WINDOWS |
| 2190 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
| 2191 | #elif defined(HAVE_LONG_LONG) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2192 | PyStructSequence_SET_ITEM(v, 2, |
| 2193 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2194 | #else |
Brian Curtin | 9cc4321 | 2013-01-01 12:31:06 -0600 | [diff] [blame] | 2195 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2196 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2197 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2198 | #if defined(MS_WINDOWS) |
| 2199 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2200 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2201 | #else |
| 2202 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2203 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2204 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2205 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2206 | PyStructSequence_SET_ITEM(v, 6, |
| 2207 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2208 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2209 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2210 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2211 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2212 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2213 | ansec = st->st_atim.tv_nsec; |
| 2214 | mnsec = st->st_mtim.tv_nsec; |
| 2215 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2216 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2217 | ansec = st->st_atimespec.tv_nsec; |
| 2218 | mnsec = st->st_mtimespec.tv_nsec; |
| 2219 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2220 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2221 | ansec = st->st_atime_nsec; |
| 2222 | mnsec = st->st_mtime_nsec; |
| 2223 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2224 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2225 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2226 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2227 | fill_time(v, 7, st->st_atime, ansec); |
| 2228 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2229 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2230 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2231 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2232 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2233 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2234 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2235 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2236 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2237 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2238 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2239 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2240 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2241 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2242 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2243 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2244 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2245 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2246 | #endif |
| 2247 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2248 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2249 | PyObject *val; |
| 2250 | unsigned long bsec,bnsec; |
| 2251 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2252 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2253 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2254 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2255 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2256 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2257 | if (_stat_float_times) { |
| 2258 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 2259 | } else { |
| 2260 | val = PyLong_FromLong((long)bsec); |
| 2261 | } |
| 2262 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2263 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2264 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2265 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2266 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2267 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2268 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2269 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2270 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2271 | if (PyErr_Occurred()) { |
| 2272 | Py_DECREF(v); |
| 2273 | return NULL; |
| 2274 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2275 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2276 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2279 | /* POSIX methods */ |
| 2280 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2281 | |
| 2282 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2283 | posix_do_stat(char *function_name, path_t *path, |
| 2284 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2285 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2286 | STRUCT_STAT st; |
| 2287 | int result; |
| 2288 | |
| 2289 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2290 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2291 | return NULL; |
| 2292 | #endif |
| 2293 | |
| 2294 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2295 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2296 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2297 | return NULL; |
| 2298 | |
| 2299 | Py_BEGIN_ALLOW_THREADS |
| 2300 | if (path->fd != -1) |
| 2301 | result = FSTAT(path->fd, &st); |
| 2302 | else |
| 2303 | #ifdef MS_WINDOWS |
| 2304 | if (path->wide) { |
| 2305 | if (follow_symlinks) |
| 2306 | result = win32_stat_w(path->wide, &st); |
| 2307 | else |
| 2308 | result = win32_lstat_w(path->wide, &st); |
| 2309 | } |
| 2310 | else |
| 2311 | #endif |
| 2312 | #if defined(HAVE_LSTAT) || defined(MS_WINDOWS) |
| 2313 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2314 | result = LSTAT(path->narrow, &st); |
| 2315 | else |
| 2316 | #endif |
| 2317 | #ifdef HAVE_FSTATAT |
| 2318 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2319 | result = fstatat(dir_fd, path->narrow, &st, |
| 2320 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2321 | else |
| 2322 | #endif |
| 2323 | result = STAT(path->narrow, &st); |
| 2324 | Py_END_ALLOW_THREADS |
| 2325 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2326 | if (result != 0) { |
| 2327 | return path_error(path); |
| 2328 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2329 | |
| 2330 | return _pystat_fromstructstat(&st); |
| 2331 | } |
| 2332 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2333 | #ifdef HAVE_FSTATAT |
| 2334 | #define OS_STAT_DIR_FD_CONVERTER dir_fd_converter |
| 2335 | #else |
| 2336 | #define OS_STAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2337 | #endif |
| 2338 | |
| 2339 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2340 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2341 | |
| 2342 | class path_t_converter(CConverter): |
| 2343 | |
| 2344 | type = "path_t" |
| 2345 | impl_by_reference = True |
| 2346 | parse_by_reference = True |
| 2347 | |
| 2348 | converter = 'path_converter' |
| 2349 | |
| 2350 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2351 | # right now path_t doesn't support default values. |
| 2352 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2353 | if self.default is not unspecified: |
| 2354 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2355 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2356 | if self.c_default is not None: |
| 2357 | fail("Can't specify a c_default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2358 | |
| 2359 | self.nullable = nullable |
| 2360 | self.allow_fd = allow_fd |
| 2361 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2362 | def pre_render(self): |
| 2363 | def strify(value): |
| 2364 | return str(int(bool(value))) |
| 2365 | |
| 2366 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2367 | self.c_default = 'PATH_T_INITIALIZE("{}", {}, {})'.format( |
| 2368 | self.function.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2369 | strify(self.nullable), |
| 2370 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2371 | ) |
| 2372 | |
| 2373 | def cleanup(self): |
| 2374 | return "path_cleanup(&" + self.name + ");\n" |
| 2375 | |
| 2376 | |
| 2377 | class dir_fd_converter(CConverter): |
| 2378 | type = 'int' |
| 2379 | converter = 'OS_STAT_DIR_FD_CONVERTER' |
| 2380 | |
| 2381 | def converter_init(self): |
| 2382 | if self.default in (unspecified, None): |
| 2383 | self.c_default = 'DEFAULT_DIR_FD' |
| 2384 | |
| 2385 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2386 | [python start generated code]*/ |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2387 | /*[python end generated code: output=da39a3ee5e6b4b0d input=5c9f456f53244fc3]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2388 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2389 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2390 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2391 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2392 | |
| 2393 | path : path_t(allow_fd=True) |
| 2394 | Path to be examined; can be string, bytes, or open-file-descriptor int. |
| 2395 | |
| 2396 | * |
| 2397 | |
| 2398 | dir_fd : dir_fd = None |
| 2399 | If not None, it should be a file descriptor open to a directory, |
| 2400 | and path should be a relative string; path will then be relative to |
| 2401 | that directory. |
| 2402 | |
| 2403 | follow_symlinks: bool = True |
| 2404 | If False, and the last element of the path is a symbolic link, |
| 2405 | stat will examine the symbolic link itself instead of the file |
| 2406 | the link points to. |
| 2407 | |
| 2408 | Perform a stat system call on the given path. |
| 2409 | |
| 2410 | dir_fd and follow_symlinks may not be implemented |
| 2411 | on your platform. If they are unavailable, using them will raise a |
| 2412 | NotImplementedError. |
| 2413 | |
| 2414 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2415 | an open file descriptor. |
| 2416 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2417 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2418 | |
| 2419 | PyDoc_STRVAR(os_stat__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2420 | "stat($module, /, path, *, dir_fd=None, follow_symlinks=True)\n" |
| 2421 | "--\n" |
| 2422 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2423 | "Perform a stat system call on the given path.\n" |
| 2424 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2425 | " path\n" |
| 2426 | " Path to be examined; can be string, bytes, or open-file-descriptor int.\n" |
| 2427 | " dir_fd\n" |
| 2428 | " If not None, it should be a file descriptor open to a directory,\n" |
| 2429 | " and path should be a relative string; path will then be relative to\n" |
| 2430 | " that directory.\n" |
| 2431 | " follow_symlinks\n" |
| 2432 | " If False, and the last element of the path is a symbolic link,\n" |
| 2433 | " stat will examine the symbolic link itself instead of the file\n" |
| 2434 | " the link points to.\n" |
| 2435 | "\n" |
| 2436 | "dir_fd and follow_symlinks may not be implemented\n" |
| 2437 | " on your platform. If they are unavailable, using them will raise a\n" |
| 2438 | " NotImplementedError.\n" |
| 2439 | "\n" |
| 2440 | "It\'s an error to use dir_fd or follow_symlinks when specifying path as\n" |
| 2441 | " an open file descriptor."); |
| 2442 | |
| 2443 | #define OS_STAT_METHODDEF \ |
| 2444 | {"stat", (PyCFunction)os_stat, METH_VARARGS|METH_KEYWORDS, os_stat__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2445 | |
| 2446 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2447 | 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] | 2448 | |
| 2449 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2450 | os_stat(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2451 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2452 | PyObject *return_value = NULL; |
| 2453 | static char *_keywords[] = {"path", "dir_fd", "follow_symlinks", NULL}; |
| 2454 | path_t path = PATH_T_INITIALIZE("stat", 0, 1); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2455 | int dir_fd = DEFAULT_DIR_FD; |
| 2456 | int follow_symlinks = 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2457 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2458 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2459 | "O&|$O&p:stat", _keywords, |
| 2460 | path_converter, &path, OS_STAT_DIR_FD_CONVERTER, &dir_fd, &follow_symlinks)) |
| 2461 | goto exit; |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 2462 | return_value = os_stat_impl(module, &path, dir_fd, follow_symlinks); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2463 | |
| 2464 | exit: |
| 2465 | /* Cleanup for path */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2466 | path_cleanup(&path); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2467 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2468 | return return_value; |
| 2469 | } |
| 2470 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2471 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2472 | os_stat_impl(PyModuleDef *module, path_t *path, int dir_fd, int follow_symlinks) |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2473 | /*[clinic end generated code: output=f1dcaa5e24db9882 input=5ae155bd475fd20a]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2474 | { |
| 2475 | return posix_do_stat("stat", path, dir_fd, follow_symlinks); |
| 2476 | } |
| 2477 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2478 | PyDoc_STRVAR(posix_lstat__doc__, |
| 2479 | "lstat(path, *, dir_fd=None) -> stat result\n\n\ |
| 2480 | Like stat(), but do not follow symbolic links.\n\ |
| 2481 | Equivalent to stat(path, follow_symlinks=False)."); |
| 2482 | |
| 2483 | static PyObject * |
| 2484 | posix_lstat(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2485 | { |
| 2486 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 2487 | path_t path; |
| 2488 | int dir_fd = DEFAULT_DIR_FD; |
| 2489 | int follow_symlinks = 0; |
| 2490 | PyObject *return_value; |
| 2491 | |
| 2492 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2493 | path.function_name = "lstat"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2494 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:lstat", keywords, |
| 2495 | path_converter, &path, |
| 2496 | #ifdef HAVE_FSTATAT |
| 2497 | dir_fd_converter, &dir_fd |
| 2498 | #else |
| 2499 | dir_fd_unavailable, &dir_fd |
| 2500 | #endif |
| 2501 | )) |
| 2502 | return NULL; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2503 | return_value = posix_do_stat("lstat", &path, dir_fd, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2504 | path_cleanup(&path); |
| 2505 | return return_value; |
| 2506 | } |
| 2507 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2508 | |
| 2509 | #ifdef HAVE_FACCESSAT |
| 2510 | #define OS_ACCESS_DIR_FD_CONVERTER dir_fd_converter |
| 2511 | #else |
| 2512 | #define OS_ACCESS_DIR_FD_CONVERTER dir_fd_unavailable |
| 2513 | #endif |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2514 | /*[clinic input] |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2515 | os.access |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2516 | |
| 2517 | path: path_t(allow_fd=True) |
| 2518 | Path to be tested; can be string, bytes, or open-file-descriptor int. |
| 2519 | |
| 2520 | mode: int |
| 2521 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2522 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2523 | |
| 2524 | * |
| 2525 | |
| 2526 | dir_fd : dir_fd = None |
| 2527 | If not None, it should be a file descriptor open to a directory, |
| 2528 | and path should be relative; path will then be relative to that |
| 2529 | directory. |
| 2530 | |
| 2531 | effective_ids: bool = False |
| 2532 | If True, access will use the effective uid/gid instead of |
| 2533 | the real uid/gid. |
| 2534 | |
| 2535 | follow_symlinks: bool = True |
| 2536 | If False, and the last element of the path is a symbolic link, |
| 2537 | access will examine the symbolic link itself instead of the file |
| 2538 | the link points to. |
| 2539 | |
| 2540 | Use the real uid/gid to test for access to a path. |
| 2541 | |
| 2542 | {parameters} |
| 2543 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2544 | on your platform. If they are unavailable, using them will raise a |
| 2545 | NotImplementedError. |
| 2546 | |
| 2547 | Note that most operations will use the effective uid/gid, therefore this |
| 2548 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2549 | has the specified access to the path. |
| 2550 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2551 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2552 | |
| 2553 | PyDoc_STRVAR(os_access__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2554 | "access($module, /, path, mode, *, dir_fd=None, effective_ids=False,\n" |
| 2555 | " follow_symlinks=True)\n" |
| 2556 | "--\n" |
| 2557 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2558 | "Use the real uid/gid to test for access to a path.\n" |
| 2559 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2560 | " path\n" |
| 2561 | " Path to be tested; can be string, bytes, or open-file-descriptor int.\n" |
| 2562 | " mode\n" |
| 2563 | " Operating-system mode bitfield. Can be F_OK to test existence,\n" |
| 2564 | " or the inclusive-OR of R_OK, W_OK, and X_OK.\n" |
| 2565 | " dir_fd\n" |
| 2566 | " If not None, it should be a file descriptor open to a directory,\n" |
| 2567 | " and path should be relative; path will then be relative to that\n" |
| 2568 | " directory.\n" |
| 2569 | " effective_ids\n" |
| 2570 | " If True, access will use the effective uid/gid instead of\n" |
| 2571 | " the real uid/gid.\n" |
| 2572 | " follow_symlinks\n" |
| 2573 | " If False, and the last element of the path is a symbolic link,\n" |
| 2574 | " access will examine the symbolic link itself instead of the file\n" |
| 2575 | " the link points to.\n" |
| 2576 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2577 | "dir_fd, effective_ids, and follow_symlinks may not be implemented\n" |
| 2578 | " on your platform. If they are unavailable, using them will raise a\n" |
| 2579 | " NotImplementedError.\n" |
| 2580 | "\n" |
| 2581 | "Note that most operations will use the effective uid/gid, therefore this\n" |
| 2582 | " routine can be used in a suid/sgid environment to test if the invoking user\n" |
| 2583 | " has the specified access to the path."); |
| 2584 | |
| 2585 | #define OS_ACCESS_METHODDEF \ |
| 2586 | {"access", (PyCFunction)os_access, METH_VARARGS|METH_KEYWORDS, os_access__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2587 | |
| 2588 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2589 | 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] | 2590 | |
| 2591 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2592 | os_access(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2593 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2594 | PyObject *return_value = NULL; |
| 2595 | static char *_keywords[] = {"path", "mode", "dir_fd", "effective_ids", "follow_symlinks", NULL}; |
| 2596 | path_t path = PATH_T_INITIALIZE("access", 0, 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2597 | int mode; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2598 | int dir_fd = DEFAULT_DIR_FD; |
| 2599 | int effective_ids = 0; |
| 2600 | int follow_symlinks = 1; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2601 | |
| 2602 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2603 | "O&i|$O&pp:access", _keywords, |
| 2604 | path_converter, &path, &mode, OS_STAT_DIR_FD_CONVERTER, &dir_fd, &effective_ids, &follow_symlinks)) |
| 2605 | goto exit; |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 2606 | 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] | 2607 | |
| 2608 | exit: |
| 2609 | /* Cleanup for path */ |
| 2610 | path_cleanup(&path); |
| 2611 | |
| 2612 | return return_value; |
| 2613 | } |
| 2614 | |
| 2615 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2616 | os_access_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, int effective_ids, int follow_symlinks) |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2617 | /*[clinic end generated code: output=a6ed4f151be9df0f input=2e2e7594371f5b7e]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2618 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2619 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2620 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2621 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2622 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2623 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2624 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2625 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2626 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2627 | #ifndef HAVE_FACCESSAT |
| 2628 | if (follow_symlinks_specified("access", follow_symlinks)) |
| 2629 | goto exit; |
| 2630 | |
| 2631 | if (effective_ids) { |
| 2632 | argument_unavailable_error("access", "effective_ids"); |
| 2633 | goto exit; |
| 2634 | } |
| 2635 | #endif |
| 2636 | |
| 2637 | #ifdef MS_WINDOWS |
| 2638 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | ebe83f9 | 2013-10-19 22:36:17 +0200 | [diff] [blame] | 2639 | if (path->wide != NULL) |
| 2640 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2641 | else |
Christian Heimes | ebe83f9 | 2013-10-19 22:36:17 +0200 | [diff] [blame] | 2642 | attr = GetFileAttributesA(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2643 | Py_END_ALLOW_THREADS |
| 2644 | |
| 2645 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2646 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2647 | * * we didn't get a -1, and |
| 2648 | * * write access wasn't requested, |
| 2649 | * * or the file isn't read-only, |
| 2650 | * * or it's a directory. |
| 2651 | * (Directories cannot be read-only on Windows.) |
| 2652 | */ |
| 2653 | return_value = PyBool_FromLong( |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 2654 | (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2655 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2656 | !(attr & FILE_ATTRIBUTE_READONLY) || |
| 2657 | (attr & FILE_ATTRIBUTE_DIRECTORY))); |
| 2658 | #else |
| 2659 | |
| 2660 | Py_BEGIN_ALLOW_THREADS |
| 2661 | #ifdef HAVE_FACCESSAT |
| 2662 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2663 | effective_ids || |
| 2664 | !follow_symlinks) { |
| 2665 | int flags = 0; |
| 2666 | if (!follow_symlinks) |
| 2667 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2668 | if (effective_ids) |
| 2669 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2670 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2671 | } |
| 2672 | else |
| 2673 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2674 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2675 | Py_END_ALLOW_THREADS |
| 2676 | return_value = PyBool_FromLong(!result); |
| 2677 | #endif |
| 2678 | |
| 2679 | #ifndef HAVE_FACCESSAT |
| 2680 | exit: |
| 2681 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2682 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2685 | #ifndef F_OK |
| 2686 | #define F_OK 0 |
| 2687 | #endif |
| 2688 | #ifndef R_OK |
| 2689 | #define R_OK 4 |
| 2690 | #endif |
| 2691 | #ifndef W_OK |
| 2692 | #define W_OK 2 |
| 2693 | #endif |
| 2694 | #ifndef X_OK |
| 2695 | #define X_OK 1 |
| 2696 | #endif |
| 2697 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2698 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2699 | #ifdef HAVE_TTYNAME |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2700 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2701 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2702 | os.ttyname -> DecodeFSDefault |
| 2703 | |
| 2704 | fd: int |
| 2705 | Integer file descriptor handle. |
| 2706 | |
| 2707 | / |
| 2708 | |
| 2709 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2710 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2711 | |
| 2712 | PyDoc_STRVAR(os_ttyname__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2713 | "ttyname($module, fd, /)\n" |
| 2714 | "--\n" |
| 2715 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2716 | "Return the name of the terminal device connected to \'fd\'.\n" |
| 2717 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2718 | " fd\n" |
| 2719 | " Integer file descriptor handle."); |
| 2720 | |
| 2721 | #define OS_TTYNAME_METHODDEF \ |
| 2722 | {"ttyname", (PyCFunction)os_ttyname, METH_VARARGS, os_ttyname__doc__}, |
| 2723 | |
| 2724 | static char * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2725 | os_ttyname_impl(PyModuleDef *module, int fd); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2726 | |
| 2727 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2728 | os_ttyname(PyModuleDef *module, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2729 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2730 | PyObject *return_value = NULL; |
| 2731 | int fd; |
| 2732 | char *_return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2733 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2734 | if (!PyArg_ParseTuple(args, |
| 2735 | "i:ttyname", |
| 2736 | &fd)) |
| 2737 | goto exit; |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 2738 | _return_value = os_ttyname_impl(module, fd); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2739 | if (_return_value == NULL) |
| 2740 | goto exit; |
| 2741 | return_value = PyUnicode_DecodeFSDefault(_return_value); |
| 2742 | |
| 2743 | exit: |
| 2744 | return return_value; |
| 2745 | } |
| 2746 | |
| 2747 | static char * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2748 | os_ttyname_impl(PyModuleDef *module, int fd) |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2749 | /*[clinic end generated code: output=cee7bc4cffec01a2 input=5f72ca83e76b3b45]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2750 | { |
| 2751 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2752 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2753 | ret = ttyname(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2754 | if (ret == NULL) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2755 | posix_error(); |
| 2756 | return ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2757 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2758 | #else |
| 2759 | #define OS_TTYNAME_METHODDEF |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2760 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2761 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2762 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2763 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2764 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2765 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2766 | |
| 2767 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2768 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2769 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2770 | char *ret; |
| 2771 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2772 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2773 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2774 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2775 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2776 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2777 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2778 | if (ret == NULL) |
| 2779 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2780 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2781 | } |
| 2782 | #endif |
| 2783 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2784 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2785 | "chdir(path)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2786 | Change the current working directory to the specified path.\n\ |
| 2787 | \n\ |
| 2788 | path may always be specified as a string.\n\ |
| 2789 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2790 | If this functionality is unavailable, using it raises an exception."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2791 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2792 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2793 | posix_chdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2794 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2795 | path_t path; |
| 2796 | int result; |
| 2797 | PyObject *return_value = NULL; |
| 2798 | static char *keywords[] = {"path", NULL}; |
| 2799 | |
| 2800 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2801 | path.function_name = "chdir"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2802 | #ifdef HAVE_FCHDIR |
| 2803 | path.allow_fd = 1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2804 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2805 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:chdir", keywords, |
| 2806 | path_converter, &path |
| 2807 | )) |
| 2808 | return NULL; |
| 2809 | |
| 2810 | Py_BEGIN_ALLOW_THREADS |
| 2811 | #ifdef MS_WINDOWS |
| 2812 | if (path.wide) |
| 2813 | result = win32_wchdir(path.wide); |
| 2814 | else |
| 2815 | result = win32_chdir(path.narrow); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 2816 | result = !result; /* on unix, success = 0, on windows, success = !0 */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2817 | #else |
| 2818 | #ifdef HAVE_FCHDIR |
| 2819 | if (path.fd != -1) |
| 2820 | result = fchdir(path.fd); |
| 2821 | else |
| 2822 | #endif |
| 2823 | result = chdir(path.narrow); |
| 2824 | #endif |
| 2825 | Py_END_ALLOW_THREADS |
| 2826 | |
| 2827 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2828 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2829 | goto exit; |
| 2830 | } |
| 2831 | |
| 2832 | return_value = Py_None; |
| 2833 | Py_INCREF(Py_None); |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2834 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2835 | exit: |
| 2836 | path_cleanup(&path); |
| 2837 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2840 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2841 | PyDoc_STRVAR(posix_fchdir__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2842 | "fchdir(fd)\n\n\ |
| 2843 | Change to the directory of the given file descriptor. fd must be\n\ |
| 2844 | opened on a directory, not a file. Equivalent to os.chdir(fd)."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2845 | |
| 2846 | static PyObject * |
| 2847 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 2848 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2849 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2850 | } |
| 2851 | #endif /* HAVE_FCHDIR */ |
| 2852 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2853 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2854 | PyDoc_STRVAR(posix_chmod__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2855 | "chmod(path, mode, *, dir_fd=None, follow_symlinks=True)\n\n\ |
| 2856 | Change the access permissions of a file.\n\ |
| 2857 | \n\ |
| 2858 | path may always be specified as a string.\n\ |
| 2859 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2860 | If this functionality is unavailable, using it raises an exception.\n\ |
| 2861 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2862 | and path should be relative; path will then be relative to that directory.\n\ |
| 2863 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2864 | link, chmod will modify the symbolic link itself instead of the file the\n\ |
| 2865 | link points to.\n\ |
| 2866 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2867 | an open file descriptor.\n\ |
| 2868 | dir_fd and follow_symlinks may not be implemented on your platform.\n\ |
| 2869 | If they are unavailable, using them will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2870 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2871 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2872 | posix_chmod(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2873 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2874 | path_t path; |
| 2875 | int mode; |
| 2876 | int dir_fd = DEFAULT_DIR_FD; |
| 2877 | int follow_symlinks = 1; |
| 2878 | int result; |
| 2879 | PyObject *return_value = NULL; |
| 2880 | static char *keywords[] = {"path", "mode", "dir_fd", |
| 2881 | "follow_symlinks", NULL}; |
| 2882 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2883 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2884 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2885 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2886 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2887 | #ifdef HAVE_FCHMODAT |
| 2888 | int fchmodat_nofollow_unsupported = 0; |
| 2889 | #endif |
| 2890 | |
| 2891 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2892 | path.function_name = "chmod"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2893 | #ifdef HAVE_FCHMOD |
| 2894 | path.allow_fd = 1; |
| 2895 | #endif |
| 2896 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&p:chmod", keywords, |
| 2897 | path_converter, &path, |
| 2898 | &mode, |
| 2899 | #ifdef HAVE_FCHMODAT |
| 2900 | dir_fd_converter, &dir_fd, |
| 2901 | #else |
| 2902 | dir_fd_unavailable, &dir_fd, |
| 2903 | #endif |
| 2904 | &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2905 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2906 | |
| 2907 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2908 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
| 2909 | goto exit; |
| 2910 | #endif |
| 2911 | |
| 2912 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2913 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2914 | if (path.wide) |
| 2915 | attr = GetFileAttributesW(path.wide); |
| 2916 | else |
| 2917 | attr = GetFileAttributesA(path.narrow); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 2918 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2919 | result = 0; |
| 2920 | else { |
| 2921 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2922 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2923 | else |
| 2924 | attr |= FILE_ATTRIBUTE_READONLY; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2925 | if (path.wide) |
| 2926 | result = SetFileAttributesW(path.wide, attr); |
| 2927 | else |
| 2928 | result = SetFileAttributesA(path.narrow, attr); |
| 2929 | } |
| 2930 | Py_END_ALLOW_THREADS |
| 2931 | |
| 2932 | if (!result) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 2933 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2934 | goto exit; |
| 2935 | } |
| 2936 | #else /* MS_WINDOWS */ |
| 2937 | Py_BEGIN_ALLOW_THREADS |
| 2938 | #ifdef HAVE_FCHMOD |
| 2939 | if (path.fd != -1) |
| 2940 | result = fchmod(path.fd, mode); |
| 2941 | else |
| 2942 | #endif |
| 2943 | #ifdef HAVE_LCHMOD |
| 2944 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2945 | result = lchmod(path.narrow, mode); |
| 2946 | else |
| 2947 | #endif |
| 2948 | #ifdef HAVE_FCHMODAT |
| 2949 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2950 | /* |
| 2951 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2952 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2953 | * and then says it isn't implemented yet. |
| 2954 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2955 | * |
| 2956 | * Once it is supported, os.chmod will automatically |
| 2957 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2958 | * Until then, we need to be careful what exception we raise. |
| 2959 | */ |
| 2960 | result = fchmodat(dir_fd, path.narrow, mode, |
| 2961 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2962 | /* |
| 2963 | * But wait! We can't throw the exception without allowing threads, |
| 2964 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2965 | */ |
| 2966 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2967 | result && |
| 2968 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2969 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2970 | } |
| 2971 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2972 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2973 | result = chmod(path.narrow, mode); |
| 2974 | Py_END_ALLOW_THREADS |
| 2975 | |
| 2976 | if (result) { |
| 2977 | #ifdef HAVE_FCHMODAT |
| 2978 | if (fchmodat_nofollow_unsupported) { |
| 2979 | if (dir_fd != DEFAULT_DIR_FD) |
| 2980 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 2981 | dir_fd, follow_symlinks); |
| 2982 | else |
| 2983 | follow_symlinks_specified("chmod", follow_symlinks); |
| 2984 | } |
| 2985 | else |
| 2986 | #endif |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2987 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2988 | goto exit; |
| 2989 | } |
| 2990 | #endif |
| 2991 | |
| 2992 | Py_INCREF(Py_None); |
| 2993 | return_value = Py_None; |
| 2994 | exit: |
| 2995 | path_cleanup(&path); |
| 2996 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2999 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3000 | #ifdef HAVE_FCHMOD |
| 3001 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 3002 | "fchmod(fd, mode)\n\n\ |
| 3003 | Change the access permissions of the file given by file\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3004 | descriptor fd. Equivalent to os.chmod(fd, mode)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3005 | |
| 3006 | static PyObject * |
| 3007 | posix_fchmod(PyObject *self, PyObject *args) |
| 3008 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3009 | int fd, mode, res; |
| 3010 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 3011 | return NULL; |
| 3012 | Py_BEGIN_ALLOW_THREADS |
| 3013 | res = fchmod(fd, mode); |
| 3014 | Py_END_ALLOW_THREADS |
| 3015 | if (res < 0) |
| 3016 | return posix_error(); |
| 3017 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3018 | } |
| 3019 | #endif /* HAVE_FCHMOD */ |
| 3020 | |
| 3021 | #ifdef HAVE_LCHMOD |
| 3022 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 3023 | "lchmod(path, mode)\n\n\ |
| 3024 | 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] | 3025 | affects the link itself rather than the target.\n\ |
| 3026 | Equivalent to chmod(path, mode, follow_symlinks=False)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3027 | |
| 3028 | static PyObject * |
| 3029 | posix_lchmod(PyObject *self, PyObject *args) |
| 3030 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3031 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3032 | int i; |
| 3033 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3034 | memset(&path, 0, sizeof(path)); |
| 3035 | path.function_name = "lchmod"; |
| 3036 | if (!PyArg_ParseTuple(args, "O&i:lchmod", |
| 3037 | path_converter, &path, &i)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3038 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3039 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3040 | res = lchmod(path.narrow, i); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3041 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3042 | if (res < 0) { |
| 3043 | path_error(&path); |
| 3044 | path_cleanup(&path); |
| 3045 | return NULL; |
| 3046 | } |
| 3047 | path_cleanup(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3048 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3049 | } |
| 3050 | #endif /* HAVE_LCHMOD */ |
| 3051 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3052 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3053 | #ifdef HAVE_CHFLAGS |
| 3054 | PyDoc_STRVAR(posix_chflags__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3055 | "chflags(path, flags, *, follow_symlinks=True)\n\n\ |
| 3056 | Set file flags.\n\ |
| 3057 | \n\ |
| 3058 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 3059 | link, chflags will change flags on the symbolic link itself instead of the\n\ |
| 3060 | file the link points to.\n\ |
| 3061 | follow_symlinks may not be implemented on your platform. If it is\n\ |
| 3062 | unavailable, using it will raise a NotImplementedError."); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3063 | |
| 3064 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3065 | posix_chflags(PyObject *self, PyObject *args, PyObject *kwargs) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3066 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3067 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3068 | unsigned long flags; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3069 | int follow_symlinks = 1; |
| 3070 | int result; |
Victor Stinner | 45e9039 | 2013-07-18 23:57:35 +0200 | [diff] [blame] | 3071 | PyObject *return_value = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3072 | static char *keywords[] = {"path", "flags", "follow_symlinks", NULL}; |
| 3073 | |
| 3074 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3075 | path.function_name = "chflags"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3076 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&k|$i:chflags", keywords, |
| 3077 | path_converter, &path, |
| 3078 | &flags, &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3079 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3080 | |
| 3081 | #ifndef HAVE_LCHFLAGS |
| 3082 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
| 3083 | goto exit; |
| 3084 | #endif |
| 3085 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3086 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3087 | #ifdef HAVE_LCHFLAGS |
| 3088 | if (!follow_symlinks) |
| 3089 | result = lchflags(path.narrow, flags); |
| 3090 | else |
| 3091 | #endif |
| 3092 | result = chflags(path.narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3093 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3094 | |
| 3095 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3096 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3097 | goto exit; |
| 3098 | } |
| 3099 | |
| 3100 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3101 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3102 | |
| 3103 | exit: |
| 3104 | path_cleanup(&path); |
| 3105 | return return_value; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3106 | } |
| 3107 | #endif /* HAVE_CHFLAGS */ |
| 3108 | |
| 3109 | #ifdef HAVE_LCHFLAGS |
| 3110 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 3111 | "lchflags(path, flags)\n\n\ |
| 3112 | Set file flags.\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3113 | This function will not follow symbolic links.\n\ |
| 3114 | Equivalent to chflags(path, flags, follow_symlinks=False)."); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3115 | |
| 3116 | static PyObject * |
| 3117 | posix_lchflags(PyObject *self, PyObject *args) |
| 3118 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3119 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3120 | unsigned long flags; |
| 3121 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3122 | memset(&path, 0, sizeof(path)); |
| 3123 | path.function_name = "lchflags"; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3124 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3125 | path_converter, &path, &flags)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3126 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3127 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3128 | res = lchflags(path.narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3129 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3130 | if (res < 0) { |
| 3131 | path_error(&path); |
| 3132 | path_cleanup(&path); |
| 3133 | return NULL; |
| 3134 | } |
| 3135 | path_cleanup(&path); |
| 3136 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3137 | } |
| 3138 | #endif /* HAVE_LCHFLAGS */ |
| 3139 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3140 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3141 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3142 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3143 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3144 | |
| 3145 | static PyObject * |
| 3146 | posix_chroot(PyObject *self, PyObject *args) |
| 3147 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3148 | return posix_1str("chroot", args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3149 | } |
| 3150 | #endif |
| 3151 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3152 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3153 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3154 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3155 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3156 | |
| 3157 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 3158 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3159 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 3160 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3161 | } |
| 3162 | #endif /* HAVE_FSYNC */ |
| 3163 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3164 | #ifdef HAVE_SYNC |
| 3165 | PyDoc_STRVAR(posix_sync__doc__, |
| 3166 | "sync()\n\n\ |
| 3167 | Force write of everything to disk."); |
| 3168 | |
| 3169 | static PyObject * |
| 3170 | posix_sync(PyObject *self, PyObject *noargs) |
| 3171 | { |
| 3172 | Py_BEGIN_ALLOW_THREADS |
| 3173 | sync(); |
| 3174 | Py_END_ALLOW_THREADS |
| 3175 | Py_RETURN_NONE; |
| 3176 | } |
| 3177 | #endif |
| 3178 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3179 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3180 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3181 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3182 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3183 | #endif |
| 3184 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3185 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3186 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3187 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3188 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3189 | |
| 3190 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 3191 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3192 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 3193 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3194 | } |
| 3195 | #endif /* HAVE_FDATASYNC */ |
| 3196 | |
| 3197 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3198 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3199 | PyDoc_STRVAR(posix_chown__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3200 | "chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True)\n\n\ |
| 3201 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 3202 | \n\ |
| 3203 | path may always be specified as a string.\n\ |
| 3204 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 3205 | If this functionality is unavailable, using it raises an exception.\n\ |
| 3206 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 3207 | and path should be relative; path will then be relative to that directory.\n\ |
| 3208 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 3209 | link, chown will modify the symbolic link itself instead of the file the\n\ |
| 3210 | link points to.\n\ |
| 3211 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 3212 | an open file descriptor.\n\ |
| 3213 | dir_fd and follow_symlinks may not be implemented on your platform.\n\ |
| 3214 | If they are unavailable, using them will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3215 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3216 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3217 | posix_chown(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3218 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3219 | path_t path; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3220 | uid_t uid; |
| 3221 | gid_t gid; |
| 3222 | int dir_fd = DEFAULT_DIR_FD; |
| 3223 | int follow_symlinks = 1; |
| 3224 | int result; |
| 3225 | PyObject *return_value = NULL; |
| 3226 | static char *keywords[] = {"path", "uid", "gid", "dir_fd", |
| 3227 | "follow_symlinks", NULL}; |
| 3228 | |
| 3229 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3230 | path.function_name = "chown"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3231 | #ifdef HAVE_FCHOWN |
| 3232 | path.allow_fd = 1; |
| 3233 | #endif |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3234 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&O&|$O&p:chown", keywords, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3235 | path_converter, &path, |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3236 | _Py_Uid_Converter, &uid, |
| 3237 | _Py_Gid_Converter, &gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3238 | #ifdef HAVE_FCHOWNAT |
| 3239 | dir_fd_converter, &dir_fd, |
| 3240 | #else |
| 3241 | dir_fd_unavailable, &dir_fd, |
| 3242 | #endif |
| 3243 | &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3244 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3245 | |
| 3246 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3247 | if (follow_symlinks_specified("chown", follow_symlinks)) |
| 3248 | goto exit; |
| 3249 | #endif |
| 3250 | if (dir_fd_and_fd_invalid("chown", dir_fd, path.fd) || |
| 3251 | fd_and_follow_symlinks_invalid("chown", path.fd, follow_symlinks)) |
| 3252 | goto exit; |
| 3253 | |
| 3254 | #ifdef __APPLE__ |
| 3255 | /* |
| 3256 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3257 | * (But we still have an lchown symbol because of weak-linking.) |
| 3258 | * It doesn't have fchownat either. So there's no possibility |
| 3259 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3260 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3261 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3262 | follow_symlinks_specified("chown", follow_symlinks); |
| 3263 | goto exit; |
| 3264 | } |
| 3265 | #endif |
| 3266 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3267 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3268 | #ifdef HAVE_FCHOWN |
| 3269 | if (path.fd != -1) |
| 3270 | result = fchown(path.fd, uid, gid); |
| 3271 | else |
| 3272 | #endif |
| 3273 | #ifdef HAVE_LCHOWN |
| 3274 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 3275 | result = lchown(path.narrow, uid, gid); |
| 3276 | else |
| 3277 | #endif |
| 3278 | #ifdef HAVE_FCHOWNAT |
| 3279 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
| 3280 | result = fchownat(dir_fd, path.narrow, uid, gid, |
| 3281 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3282 | else |
| 3283 | #endif |
| 3284 | result = chown(path.narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3285 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3286 | |
| 3287 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3288 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3289 | goto exit; |
| 3290 | } |
| 3291 | |
| 3292 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3293 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3294 | |
| 3295 | exit: |
| 3296 | path_cleanup(&path); |
| 3297 | return return_value; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3298 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3299 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3300 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3301 | #ifdef HAVE_FCHOWN |
| 3302 | PyDoc_STRVAR(posix_fchown__doc__, |
| 3303 | "fchown(fd, uid, gid)\n\n\ |
| 3304 | 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] | 3305 | 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] | 3306 | |
| 3307 | static PyObject * |
| 3308 | posix_fchown(PyObject *self, PyObject *args) |
| 3309 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3310 | int fd; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3311 | uid_t uid; |
| 3312 | gid_t gid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3313 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3314 | if (!PyArg_ParseTuple(args, "iO&O&:fchown", &fd, |
| 3315 | _Py_Uid_Converter, &uid, |
| 3316 | _Py_Gid_Converter, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3317 | return NULL; |
| 3318 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3319 | res = fchown(fd, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3320 | Py_END_ALLOW_THREADS |
| 3321 | if (res < 0) |
| 3322 | return posix_error(); |
| 3323 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3324 | } |
| 3325 | #endif /* HAVE_FCHOWN */ |
| 3326 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3327 | #ifdef HAVE_LCHOWN |
| 3328 | PyDoc_STRVAR(posix_lchown__doc__, |
| 3329 | "lchown(path, uid, gid)\n\n\ |
| 3330 | 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] | 3331 | This function will not follow symbolic links.\n\ |
| 3332 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False)."); |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3333 | |
| 3334 | static PyObject * |
| 3335 | posix_lchown(PyObject *self, PyObject *args) |
| 3336 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3337 | path_t path; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3338 | uid_t uid; |
| 3339 | gid_t gid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3340 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3341 | memset(&path, 0, sizeof(path)); |
| 3342 | path.function_name = "lchown"; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3343 | if (!PyArg_ParseTuple(args, "O&O&O&:lchown", |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3344 | path_converter, &path, |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3345 | _Py_Uid_Converter, &uid, |
| 3346 | _Py_Gid_Converter, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3347 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3348 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | c2d0200 | 2013-02-10 22:03:08 +0200 | [diff] [blame] | 3349 | res = lchown(path.narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3350 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3351 | if (res < 0) { |
| 3352 | path_error(&path); |
| 3353 | path_cleanup(&path); |
| 3354 | return NULL; |
| 3355 | } |
| 3356 | path_cleanup(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3357 | Py_INCREF(Py_None); |
| 3358 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3359 | } |
| 3360 | #endif /* HAVE_LCHOWN */ |
| 3361 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3362 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3363 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3364 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3365 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3366 | char buf[1026]; |
| 3367 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3368 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3369 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3370 | if (!use_bytes) { |
| 3371 | wchar_t wbuf[1026]; |
| 3372 | wchar_t *wbuf2 = wbuf; |
| 3373 | PyObject *resobj; |
| 3374 | DWORD len; |
| 3375 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3376 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3377 | /* If the buffer is large enough, len does not include the |
| 3378 | terminating \0. If the buffer is too small, len includes |
| 3379 | the space needed for the terminator. */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3380 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3381 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3382 | if (wbuf2) |
| 3383 | len = GetCurrentDirectoryW(len, wbuf2); |
| 3384 | } |
| 3385 | Py_END_ALLOW_THREADS |
| 3386 | if (!wbuf2) { |
| 3387 | PyErr_NoMemory(); |
| 3388 | return NULL; |
| 3389 | } |
| 3390 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3391 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3392 | PyMem_RawFree(wbuf2); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3393 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3394 | } |
| 3395 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3396 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3397 | PyMem_RawFree(wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3398 | return resobj; |
| 3399 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3400 | |
| 3401 | if (win32_warn_bytes_api()) |
| 3402 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3403 | #endif |
| 3404 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3405 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3406 | res = getcwd(buf, sizeof buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3407 | Py_END_ALLOW_THREADS |
| 3408 | if (res == NULL) |
| 3409 | return posix_error(); |
| 3410 | if (use_bytes) |
| 3411 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 3412 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3413 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3414 | |
| 3415 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 3416 | "getcwd() -> path\n\n\ |
| 3417 | Return a unicode string representing the current working directory."); |
| 3418 | |
| 3419 | static PyObject * |
| 3420 | posix_getcwd_unicode(PyObject *self) |
| 3421 | { |
| 3422 | return posix_getcwd(0); |
| 3423 | } |
| 3424 | |
| 3425 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 3426 | "getcwdb() -> path\n\n\ |
| 3427 | Return a bytes string representing the current working directory."); |
| 3428 | |
| 3429 | static PyObject * |
| 3430 | posix_getcwd_bytes(PyObject *self) |
| 3431 | { |
| 3432 | return posix_getcwd(1); |
| 3433 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3434 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3435 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3436 | #define HAVE_LINK 1 |
| 3437 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3438 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3439 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3440 | PyDoc_STRVAR(posix_link__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3441 | "link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True)\n\n\ |
| 3442 | Create a hard link to a file.\n\ |
| 3443 | \n\ |
| 3444 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 3445 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 3446 | should be relative; the path will then be relative to that directory.\n\ |
| 3447 | If follow_symlinks is False, and the last element of src is a symbolic\n\ |
| 3448 | link, link will create a link to the symbolic link itself instead of the\n\ |
| 3449 | file the link points to.\n\ |
| 3450 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your\n\ |
| 3451 | platform. If they are unavailable, using them will raise a\n\ |
| 3452 | NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3453 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3454 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3455 | posix_link(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3456 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3457 | path_t src, dst; |
| 3458 | int src_dir_fd = DEFAULT_DIR_FD; |
| 3459 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 3460 | int follow_symlinks = 1; |
| 3461 | PyObject *return_value = NULL; |
| 3462 | static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", |
| 3463 | "follow_symlinks", NULL}; |
| 3464 | #ifdef MS_WINDOWS |
| 3465 | BOOL result; |
| 3466 | #else |
| 3467 | int result; |
| 3468 | #endif |
| 3469 | |
| 3470 | memset(&src, 0, sizeof(src)); |
| 3471 | memset(&dst, 0, sizeof(dst)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3472 | src.function_name = "link"; |
| 3473 | dst.function_name = "link"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3474 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|O&O&p:link", keywords, |
| 3475 | path_converter, &src, |
| 3476 | path_converter, &dst, |
| 3477 | dir_fd_converter, &src_dir_fd, |
| 3478 | dir_fd_converter, &dst_dir_fd, |
| 3479 | &follow_symlinks)) |
| 3480 | return NULL; |
| 3481 | |
| 3482 | #ifndef HAVE_LINKAT |
| 3483 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3484 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
| 3485 | goto exit; |
| 3486 | } |
| 3487 | #endif |
| 3488 | |
| 3489 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 3490 | PyErr_SetString(PyExc_NotImplementedError, |
| 3491 | "link: src and dst must be the same type"); |
| 3492 | goto exit; |
| 3493 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3494 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3495 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3496 | Py_BEGIN_ALLOW_THREADS |
| 3497 | if (src.wide) |
| 3498 | result = CreateHardLinkW(dst.wide, src.wide, NULL); |
| 3499 | else |
| 3500 | result = CreateHardLinkA(dst.narrow, src.narrow, NULL); |
| 3501 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3502 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3503 | if (!result) { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 3504 | return_value = path_error2(&src, &dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3505 | goto exit; |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3506 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3507 | #else |
| 3508 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3509 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3510 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3511 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3512 | (!follow_symlinks)) |
| 3513 | result = linkat(src_dir_fd, src.narrow, |
| 3514 | dst_dir_fd, dst.narrow, |
| 3515 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3516 | else |
| 3517 | #endif |
| 3518 | result = link(src.narrow, dst.narrow); |
| 3519 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3520 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3521 | if (result) { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 3522 | return_value = path_error2(&src, &dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3523 | goto exit; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3524 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3525 | #endif |
| 3526 | |
| 3527 | return_value = Py_None; |
| 3528 | Py_INCREF(Py_None); |
| 3529 | |
| 3530 | exit: |
| 3531 | path_cleanup(&src); |
| 3532 | path_cleanup(&dst); |
| 3533 | return return_value; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3534 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3535 | #endif |
| 3536 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3537 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3538 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3539 | PyDoc_STRVAR(posix_listdir__doc__, |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3540 | "listdir(path='.') -> list_of_filenames\n\n\ |
| 3541 | 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] | 3542 | 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] | 3543 | entries '.' and '..' even if they are present in the directory.\n\ |
| 3544 | \n\ |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3545 | path can be specified as either str or bytes. If path is bytes,\n\ |
| 3546 | the filenames returned will also be bytes; in all other circumstances\n\ |
| 3547 | the filenames returned will be str.\n\ |
| 3548 | On some platforms, path may also be specified as an open file descriptor;\n\ |
| 3549 | the file descriptor must refer to a directory.\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3550 | If this functionality is unavailable, using it raises NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3551 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3552 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3553 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3554 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3555 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3556 | static char *keywords[] = {"path", NULL}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3557 | PyObject *v; |
| 3558 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3559 | BOOL result; |
| 3560 | WIN32_FIND_DATA FileData; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3561 | char namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3562 | char *bufptr = namebuf; |
| 3563 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3564 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3565 | PyObject *po = NULL; |
| 3566 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3567 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3568 | if (!path->narrow) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3569 | WIN32_FIND_DATAW wFileData; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3570 | wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3571 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3572 | if (!path->wide) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3573 | po_wchars = L"."; |
| 3574 | len = 1; |
| 3575 | } else { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3576 | po_wchars = path->wide; |
| 3577 | len = wcslen(path->wide); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3578 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3579 | /* The +5 is so we can append "\\*.*\0" */ |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3580 | wnamebuf = PyMem_Malloc((len + 5) * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3581 | if (!wnamebuf) { |
| 3582 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3583 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3584 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3585 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3586 | if (len > 0) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3587 | wchar_t wch = wnamebuf[len-1]; |
Tim Golden | 781bbeb | 2013-10-25 20:24:06 +0100 | [diff] [blame] | 3588 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3589 | wnamebuf[len++] = SEP; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3590 | wcscpy(wnamebuf + len, L"*.*"); |
| 3591 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3592 | if ((list = PyList_New(0)) == NULL) { |
| 3593 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3594 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3595 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3596 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3597 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3598 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3599 | int error = GetLastError(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3600 | if (error == ERROR_FILE_NOT_FOUND) |
| 3601 | goto exit; |
| 3602 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3603 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3604 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3605 | } |
| 3606 | do { |
| 3607 | /* Skip over . and .. */ |
| 3608 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3609 | wcscmp(wFileData.cFileName, L"..") != 0) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3610 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3611 | wcslen(wFileData.cFileName)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3612 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3613 | Py_DECREF(list); |
| 3614 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3615 | break; |
| 3616 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3617 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3618 | Py_DECREF(v); |
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 | } |
| 3623 | Py_DECREF(v); |
| 3624 | } |
| 3625 | Py_BEGIN_ALLOW_THREADS |
| 3626 | result = FindNextFileW(hFindFile, &wFileData); |
| 3627 | Py_END_ALLOW_THREADS |
| 3628 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3629 | it got to the end of the directory. */ |
| 3630 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3631 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3632 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3633 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3634 | } |
| 3635 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3636 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3637 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3638 | } |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3639 | strcpy(namebuf, path->narrow); |
| 3640 | len = path->length; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3641 | if (len > 0) { |
| 3642 | char ch = namebuf[len-1]; |
Tim Golden | 781bbeb | 2013-10-25 20:24:06 +0100 | [diff] [blame] | 3643 | if (ch != '\\' && ch != '/' && ch != ':') |
| 3644 | namebuf[len++] = '\\'; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3645 | strcpy(namebuf + len, "*.*"); |
| 3646 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3647 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3648 | if ((list = PyList_New(0)) == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3649 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3650 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3651 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3652 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3653 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3654 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3655 | int error = GetLastError(); |
| 3656 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3657 | goto exit; |
| 3658 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3659 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3660 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3661 | } |
| 3662 | do { |
| 3663 | /* Skip over . and .. */ |
| 3664 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 3665 | strcmp(FileData.cFileName, "..") != 0) { |
| 3666 | v = PyBytes_FromString(FileData.cFileName); |
| 3667 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3668 | Py_DECREF(list); |
| 3669 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3670 | break; |
| 3671 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3672 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3673 | Py_DECREF(v); |
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 | } |
| 3678 | Py_DECREF(v); |
| 3679 | } |
| 3680 | Py_BEGIN_ALLOW_THREADS |
| 3681 | result = FindNextFile(hFindFile, &FileData); |
| 3682 | Py_END_ALLOW_THREADS |
| 3683 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3684 | it got to the end of the directory. */ |
| 3685 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3686 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3687 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3688 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3689 | } |
| 3690 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3691 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3692 | exit: |
| 3693 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3694 | if (FindClose(hFindFile) == FALSE) { |
| 3695 | if (list != NULL) { |
| 3696 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3697 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3698 | } |
| 3699 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3700 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3701 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3702 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3703 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3704 | } /* end of _listdir_windows_no_opendir */ |
| 3705 | |
| 3706 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3707 | |
| 3708 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3709 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3710 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3711 | PyObject *v; |
| 3712 | DIR *dirp = NULL; |
| 3713 | struct dirent *ep; |
| 3714 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3715 | #ifdef HAVE_FDOPENDIR |
| 3716 | int fd = -1; |
| 3717 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3718 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3719 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3720 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3721 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3722 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3723 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3724 | if (fd == -1) |
| 3725 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3726 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3727 | return_str = 1; |
| 3728 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3729 | Py_BEGIN_ALLOW_THREADS |
| 3730 | dirp = fdopendir(fd); |
| 3731 | Py_END_ALLOW_THREADS |
| 3732 | } |
| 3733 | else |
| 3734 | #endif |
| 3735 | { |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3736 | char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3737 | if (path->narrow) { |
| 3738 | name = path->narrow; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3739 | /* only return bytes if they specified a bytes object */ |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3740 | return_str = !(PyBytes_Check(path->object)); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3741 | } |
| 3742 | else { |
| 3743 | name = "."; |
| 3744 | return_str = 1; |
| 3745 | } |
| 3746 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3747 | Py_BEGIN_ALLOW_THREADS |
| 3748 | dirp = opendir(name); |
| 3749 | Py_END_ALLOW_THREADS |
| 3750 | } |
| 3751 | |
| 3752 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3753 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3754 | #ifdef HAVE_FDOPENDIR |
| 3755 | if (fd != -1) { |
| 3756 | Py_BEGIN_ALLOW_THREADS |
| 3757 | close(fd); |
| 3758 | Py_END_ALLOW_THREADS |
| 3759 | } |
| 3760 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3761 | goto exit; |
| 3762 | } |
| 3763 | if ((list = PyList_New(0)) == NULL) { |
| 3764 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3765 | } |
| 3766 | for (;;) { |
| 3767 | errno = 0; |
| 3768 | Py_BEGIN_ALLOW_THREADS |
| 3769 | ep = readdir(dirp); |
| 3770 | Py_END_ALLOW_THREADS |
| 3771 | if (ep == NULL) { |
| 3772 | if (errno == 0) { |
| 3773 | break; |
| 3774 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3775 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3776 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3777 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3778 | } |
| 3779 | } |
| 3780 | if (ep->d_name[0] == '.' && |
| 3781 | (NAMLEN(ep) == 1 || |
| 3782 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3783 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3784 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3785 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3786 | else |
| 3787 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3788 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3789 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3790 | break; |
| 3791 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3792 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3793 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3794 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3795 | break; |
| 3796 | } |
| 3797 | Py_DECREF(v); |
| 3798 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3799 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3800 | exit: |
| 3801 | if (dirp != NULL) { |
| 3802 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3803 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3804 | if (fd > -1) |
| 3805 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3806 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3807 | closedir(dirp); |
| 3808 | Py_END_ALLOW_THREADS |
| 3809 | } |
| 3810 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3811 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3812 | } /* end of _posix_listdir */ |
| 3813 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3814 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3815 | static PyObject * |
| 3816 | posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs) |
| 3817 | { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3818 | path_t path; |
| 3819 | PyObject *list = NULL; |
| 3820 | static char *keywords[] = {"path", NULL}; |
| 3821 | PyObject *return_value; |
| 3822 | |
| 3823 | memset(&path, 0, sizeof(path)); |
| 3824 | path.function_name = "listdir"; |
| 3825 | path.nullable = 1; |
| 3826 | #ifdef HAVE_FDOPENDIR |
| 3827 | path.allow_fd = 1; |
| 3828 | path.fd = -1; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3829 | #endif |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3830 | |
| 3831 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:listdir", keywords, |
| 3832 | path_converter, &path)) { |
| 3833 | return NULL; |
| 3834 | } |
| 3835 | |
| 3836 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3837 | return_value = _listdir_windows_no_opendir(&path, list); |
| 3838 | #else |
| 3839 | return_value = _posix_listdir(&path, list); |
| 3840 | #endif |
| 3841 | path_cleanup(&path); |
| 3842 | return return_value; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3843 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3844 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3845 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3846 | /* A helper function for abspath on win32 */ |
| 3847 | static PyObject * |
| 3848 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 3849 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3850 | const char *path; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3851 | char outbuf[MAX_PATH]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3852 | char *temp; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3853 | PyObject *po; |
| 3854 | |
| 3855 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) |
| 3856 | { |
| 3857 | wchar_t *wpath; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3858 | wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3859 | wchar_t *wtemp; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3860 | DWORD result; |
| 3861 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3862 | |
| 3863 | wpath = PyUnicode_AsUnicode(po); |
| 3864 | if (wpath == NULL) |
| 3865 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3866 | result = GetFullPathNameW(wpath, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3867 | Py_ARRAY_LENGTH(woutbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3868 | woutbuf, &wtemp); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3869 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3870 | woutbufp = PyMem_Malloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3871 | if (!woutbufp) |
| 3872 | return PyErr_NoMemory(); |
| 3873 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 3874 | } |
| 3875 | if (result) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3876 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3877 | else |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3878 | v = win32_error_object("GetFullPathNameW", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3879 | if (woutbufp != woutbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3880 | PyMem_Free(woutbufp); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3881 | return v; |
| 3882 | } |
| 3883 | /* Drop the argument parsing error as narrow strings |
| 3884 | are also valid. */ |
| 3885 | PyErr_Clear(); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3886 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3887 | if (!PyArg_ParseTuple (args, "y:_getfullpathname", |
| 3888 | &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3889 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3890 | if (win32_warn_bytes_api()) |
| 3891 | return NULL; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3892 | if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3893 | outbuf, &temp)) { |
| 3894 | win32_error("GetFullPathName", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3895 | return NULL; |
| 3896 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3897 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 3898 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 3899 | Py_FileSystemDefaultEncoding, NULL); |
| 3900 | } |
| 3901 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3902 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3903 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3904 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3905 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3906 | /* A helper function for samepath on windows */ |
| 3907 | static PyObject * |
| 3908 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 3909 | { |
| 3910 | HANDLE hFile; |
| 3911 | int buf_size; |
| 3912 | wchar_t *target_path; |
| 3913 | int result_length; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3914 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3915 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3916 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3917 | if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po)) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3918 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3919 | path = PyUnicode_AsUnicode(po); |
| 3920 | if (path == NULL) |
| 3921 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3922 | |
| 3923 | if(!check_GetFinalPathNameByHandle()) { |
| 3924 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 3925 | NotImplementedError. */ |
| 3926 | return PyErr_Format(PyExc_NotImplementedError, |
| 3927 | "GetFinalPathNameByHandle not available on this platform"); |
| 3928 | } |
| 3929 | |
| 3930 | hFile = CreateFileW( |
| 3931 | path, |
| 3932 | 0, /* desired access */ |
| 3933 | 0, /* share mode */ |
| 3934 | NULL, /* security attributes */ |
| 3935 | OPEN_EXISTING, |
| 3936 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3937 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3938 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3939 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3940 | if(hFile == INVALID_HANDLE_VALUE) |
| 3941 | return win32_error_object("CreateFileW", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3942 | |
| 3943 | /* We have a good handle to the target, use it to determine the |
| 3944 | target path name. */ |
| 3945 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 3946 | |
| 3947 | if(!buf_size) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3948 | return win32_error_object("GetFinalPathNameByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3949 | |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3950 | target_path = (wchar_t *)PyMem_Malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3951 | if(!target_path) |
| 3952 | return PyErr_NoMemory(); |
| 3953 | |
| 3954 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 3955 | buf_size, VOLUME_NAME_DOS); |
| 3956 | if(!result_length) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3957 | return win32_error_object("GetFinalPathNamyByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3958 | |
| 3959 | if(!CloseHandle(hFile)) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3960 | return win32_error_object("CloseHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3961 | |
| 3962 | target_path[result_length] = 0; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3963 | result = PyUnicode_FromWideChar(target_path, result_length); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3964 | PyMem_Free(target_path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3965 | return result; |
| 3966 | |
| 3967 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3968 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3969 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3970 | "Return true if the pathname refers to an existing directory."); |
| 3971 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3972 | static PyObject * |
| 3973 | posix__isdir(PyObject *self, PyObject *args) |
| 3974 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3975 | const char *path; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3976 | PyObject *po; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3977 | DWORD attributes; |
| 3978 | |
| 3979 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3980 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 3981 | if (wpath == NULL) |
| 3982 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3983 | |
| 3984 | attributes = GetFileAttributesW(wpath); |
| 3985 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3986 | Py_RETURN_FALSE; |
| 3987 | goto check; |
| 3988 | } |
| 3989 | /* Drop the argument parsing error as narrow strings |
| 3990 | are also valid. */ |
| 3991 | PyErr_Clear(); |
| 3992 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3993 | if (!PyArg_ParseTuple(args, "y:_isdir", &path)) |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3994 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3995 | if (win32_warn_bytes_api()) |
| 3996 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3997 | attributes = GetFileAttributesA(path); |
| 3998 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3999 | Py_RETURN_FALSE; |
| 4000 | |
| 4001 | check: |
| 4002 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 4003 | Py_RETURN_TRUE; |
| 4004 | else |
| 4005 | Py_RETURN_FALSE; |
| 4006 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4007 | |
| 4008 | PyDoc_STRVAR(posix__getvolumepathname__doc__, |
| 4009 | "Return volume mount point of the specified path."); |
| 4010 | |
| 4011 | /* A helper function for ismount on windows */ |
| 4012 | static PyObject * |
| 4013 | posix__getvolumepathname(PyObject *self, PyObject *args) |
| 4014 | { |
| 4015 | PyObject *po, *result; |
| 4016 | wchar_t *path, *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4017 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4018 | BOOL ret; |
| 4019 | |
| 4020 | if (!PyArg_ParseTuple(args, "U|:_getvolumepathname", &po)) |
| 4021 | return NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4022 | path = PyUnicode_AsUnicodeAndSize(po, &buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4023 | if (path == NULL) |
| 4024 | return NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4025 | buflen += 1; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4026 | |
| 4027 | /* Volume path should be shorter than entire path */ |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4028 | buflen = Py_MAX(buflen, MAX_PATH); |
| 4029 | |
| 4030 | if (buflen > DWORD_MAX) { |
| 4031 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 4032 | return NULL; |
| 4033 | } |
| 4034 | |
| 4035 | mountpath = (wchar_t *)PyMem_Malloc(buflen * sizeof(wchar_t)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4036 | if (mountpath == NULL) |
| 4037 | return PyErr_NoMemory(); |
| 4038 | |
| 4039 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | 85ba92a | 2013-11-18 10:30:42 +0100 | [diff] [blame] | 4040 | ret = GetVolumePathNameW(path, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4041 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4042 | Py_END_ALLOW_THREADS |
| 4043 | |
| 4044 | if (!ret) { |
| 4045 | result = win32_error_object("_getvolumepathname", po); |
| 4046 | goto exit; |
| 4047 | } |
| 4048 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
| 4049 | |
| 4050 | exit: |
| 4051 | PyMem_Free(mountpath); |
| 4052 | return result; |
| 4053 | } |
| 4054 | /* end of posix__getvolumepathname */ |
| 4055 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4056 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4057 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4058 | PyDoc_STRVAR(posix_mkdir__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4059 | "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 4060 | Create a directory.\n\ |
| 4061 | \n\ |
| 4062 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4063 | and path should be relative; path will then be relative to that directory.\n\ |
| 4064 | dir_fd may not be implemented on your platform.\n\ |
| 4065 | If it is unavailable, using it will raise a NotImplementedError.\n\ |
| 4066 | \n\ |
| 4067 | The mode argument is ignored on Windows."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4068 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4069 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4070 | posix_mkdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4071 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4072 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4073 | int mode = 0777; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4074 | int dir_fd = DEFAULT_DIR_FD; |
| 4075 | static char *keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 4076 | PyObject *return_value = NULL; |
| 4077 | int result; |
| 4078 | |
| 4079 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4080 | path.function_name = "mkdir"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4081 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkdir", keywords, |
| 4082 | path_converter, &path, &mode, |
| 4083 | #ifdef HAVE_MKDIRAT |
| 4084 | dir_fd_converter, &dir_fd |
| 4085 | #else |
| 4086 | dir_fd_unavailable, &dir_fd |
| 4087 | #endif |
| 4088 | )) |
| 4089 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4090 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4091 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4092 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4093 | if (path.wide) |
| 4094 | result = CreateDirectoryW(path.wide, NULL); |
| 4095 | else |
| 4096 | result = CreateDirectoryA(path.narrow, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4097 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4098 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4099 | if (!result) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4100 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4101 | goto exit; |
| 4102 | } |
| 4103 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4104 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4105 | #if HAVE_MKDIRAT |
| 4106 | if (dir_fd != DEFAULT_DIR_FD) |
| 4107 | result = mkdirat(dir_fd, path.narrow, mode); |
| 4108 | else |
| 4109 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4110 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4111 | result = mkdir(path.narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4112 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4113 | result = mkdir(path.narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4114 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4115 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4116 | if (result < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4117 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4118 | goto exit; |
| 4119 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4120 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4121 | return_value = Py_None; |
| 4122 | Py_INCREF(Py_None); |
| 4123 | exit: |
| 4124 | path_cleanup(&path); |
| 4125 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4126 | } |
| 4127 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4128 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4129 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 4130 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4131 | #include <sys/resource.h> |
| 4132 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4133 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4134 | |
| 4135 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4136 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4137 | "nice(inc) -> new_priority\n\n\ |
| 4138 | 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] | 4139 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4140 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4141 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4142 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4143 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4144 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4145 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 4146 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4147 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4148 | /* There are two flavours of 'nice': one that returns the new |
| 4149 | priority (as required by almost all standards out there) and the |
| 4150 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 4151 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4152 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4153 | If we are of the nice family that returns the new priority, we |
| 4154 | need to clear errno before the call, and check if errno is filled |
| 4155 | before calling posix_error() on a returnvalue of -1, because the |
| 4156 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4157 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4158 | errno = 0; |
| 4159 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4160 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4161 | if (value == 0) |
| 4162 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4163 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4164 | if (value == -1 && errno != 0) |
| 4165 | /* either nice() or getpriority() returned an error */ |
| 4166 | return posix_error(); |
| 4167 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4168 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4169 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4170 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4171 | |
| 4172 | #ifdef HAVE_GETPRIORITY |
| 4173 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 4174 | "getpriority(which, who) -> current_priority\n\n\ |
| 4175 | Get program scheduling priority."); |
| 4176 | |
| 4177 | static PyObject * |
| 4178 | posix_getpriority(PyObject *self, PyObject *args) |
| 4179 | { |
| 4180 | int which, who, retval; |
| 4181 | |
| 4182 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 4183 | return NULL; |
| 4184 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4185 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4186 | if (errno != 0) |
| 4187 | return posix_error(); |
| 4188 | return PyLong_FromLong((long)retval); |
| 4189 | } |
| 4190 | #endif /* HAVE_GETPRIORITY */ |
| 4191 | |
| 4192 | |
| 4193 | #ifdef HAVE_SETPRIORITY |
| 4194 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 4195 | "setpriority(which, who, prio) -> None\n\n\ |
| 4196 | Set program scheduling priority."); |
| 4197 | |
| 4198 | static PyObject * |
| 4199 | posix_setpriority(PyObject *self, PyObject *args) |
| 4200 | { |
| 4201 | int which, who, prio, retval; |
| 4202 | |
| 4203 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 4204 | return NULL; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4205 | retval = setpriority(which, who, prio); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4206 | if (retval == -1) |
| 4207 | return posix_error(); |
| 4208 | Py_RETURN_NONE; |
| 4209 | } |
| 4210 | #endif /* HAVE_SETPRIORITY */ |
| 4211 | |
| 4212 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4213 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4214 | internal_rename(PyObject *args, PyObject *kwargs, int is_replace) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4215 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4216 | char *function_name = is_replace ? "replace" : "rename"; |
| 4217 | path_t src; |
| 4218 | path_t dst; |
| 4219 | int src_dir_fd = DEFAULT_DIR_FD; |
| 4220 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 4221 | int dir_fd_specified; |
| 4222 | PyObject *return_value = NULL; |
| 4223 | char format[24]; |
| 4224 | static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL}; |
| 4225 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4226 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4227 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4228 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4229 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4230 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4231 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4232 | |
| 4233 | memset(&src, 0, sizeof(src)); |
| 4234 | memset(&dst, 0, sizeof(dst)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4235 | src.function_name = function_name; |
| 4236 | dst.function_name = function_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4237 | strcpy(format, "O&O&|$O&O&:"); |
| 4238 | strcat(format, function_name); |
| 4239 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, keywords, |
| 4240 | path_converter, &src, |
| 4241 | path_converter, &dst, |
| 4242 | dir_fd_converter, &src_dir_fd, |
| 4243 | dir_fd_converter, &dst_dir_fd)) |
| 4244 | return NULL; |
| 4245 | |
| 4246 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4247 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4248 | #ifndef HAVE_RENAMEAT |
| 4249 | if (dir_fd_specified) { |
| 4250 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
| 4251 | goto exit; |
| 4252 | } |
| 4253 | #endif |
| 4254 | |
| 4255 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 4256 | PyErr_Format(PyExc_ValueError, |
| 4257 | "%s: src and dst must be the same type", function_name); |
| 4258 | goto exit; |
| 4259 | } |
| 4260 | |
| 4261 | #ifdef MS_WINDOWS |
| 4262 | Py_BEGIN_ALLOW_THREADS |
| 4263 | if (src.wide) |
| 4264 | result = MoveFileExW(src.wide, dst.wide, flags); |
| 4265 | else |
| 4266 | result = MoveFileExA(src.narrow, dst.narrow, flags); |
| 4267 | Py_END_ALLOW_THREADS |
| 4268 | |
| 4269 | if (!result) { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 4270 | return_value = path_error2(&src, &dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4271 | goto exit; |
| 4272 | } |
| 4273 | |
| 4274 | #else |
| 4275 | Py_BEGIN_ALLOW_THREADS |
| 4276 | #ifdef HAVE_RENAMEAT |
| 4277 | if (dir_fd_specified) |
| 4278 | result = renameat(src_dir_fd, src.narrow, dst_dir_fd, dst.narrow); |
| 4279 | else |
| 4280 | #endif |
| 4281 | result = rename(src.narrow, dst.narrow); |
| 4282 | Py_END_ALLOW_THREADS |
| 4283 | |
| 4284 | if (result) { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 4285 | return_value = path_error2(&src, &dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4286 | goto exit; |
| 4287 | } |
| 4288 | #endif |
| 4289 | |
| 4290 | Py_INCREF(Py_None); |
| 4291 | return_value = Py_None; |
| 4292 | exit: |
| 4293 | path_cleanup(&src); |
| 4294 | path_cleanup(&dst); |
| 4295 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4296 | } |
| 4297 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4298 | PyDoc_STRVAR(posix_rename__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4299 | "rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\ |
| 4300 | Rename a file or directory.\n\ |
| 4301 | \n\ |
| 4302 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 4303 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 4304 | should be relative; the path will then be relative to that directory.\n\ |
| 4305 | src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\ |
| 4306 | If they are unavailable, using them will raise a NotImplementedError."); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4307 | |
| 4308 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4309 | posix_rename(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4310 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4311 | return internal_rename(args, kwargs, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4312 | } |
| 4313 | |
| 4314 | PyDoc_STRVAR(posix_replace__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4315 | "replace(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\ |
| 4316 | Rename a file or directory, overwriting the destination.\n\ |
| 4317 | \n\ |
| 4318 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 4319 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 4320 | should be relative; the path will then be relative to that directory.\n\ |
| 4321 | src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\ |
| 4322 | If they are unavailable, using them will raise a NotImplementedError."); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4323 | |
| 4324 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4325 | posix_replace(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4326 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4327 | return internal_rename(args, kwargs, 1); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4328 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4329 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4330 | PyDoc_STRVAR(posix_rmdir__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4331 | "rmdir(path, *, dir_fd=None)\n\n\ |
| 4332 | Remove a directory.\n\ |
| 4333 | \n\ |
| 4334 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4335 | and path should be relative; path will then be relative to that directory.\n\ |
| 4336 | dir_fd may not be implemented on your platform.\n\ |
| 4337 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4338 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4339 | static PyObject * |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4340 | posix_rmdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4341 | { |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4342 | path_t path; |
| 4343 | int dir_fd = DEFAULT_DIR_FD; |
| 4344 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 4345 | int result; |
| 4346 | PyObject *return_value = NULL; |
| 4347 | |
| 4348 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4349 | path.function_name = "rmdir"; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4350 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:rmdir", keywords, |
| 4351 | path_converter, &path, |
| 4352 | #ifdef HAVE_UNLINKAT |
| 4353 | dir_fd_converter, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4354 | #else |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4355 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4356 | #endif |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4357 | )) |
| 4358 | return NULL; |
| 4359 | |
| 4360 | Py_BEGIN_ALLOW_THREADS |
| 4361 | #ifdef MS_WINDOWS |
| 4362 | if (path.wide) |
| 4363 | result = RemoveDirectoryW(path.wide); |
| 4364 | else |
| 4365 | result = RemoveDirectoryA(path.narrow); |
| 4366 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4367 | #else |
| 4368 | #ifdef HAVE_UNLINKAT |
| 4369 | if (dir_fd != DEFAULT_DIR_FD) |
| 4370 | result = unlinkat(dir_fd, path.narrow, AT_REMOVEDIR); |
| 4371 | else |
| 4372 | #endif |
| 4373 | result = rmdir(path.narrow); |
| 4374 | #endif |
| 4375 | Py_END_ALLOW_THREADS |
| 4376 | |
| 4377 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4378 | return_value = path_error(&path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4379 | goto exit; |
| 4380 | } |
| 4381 | |
| 4382 | return_value = Py_None; |
| 4383 | Py_INCREF(Py_None); |
| 4384 | |
| 4385 | exit: |
| 4386 | path_cleanup(&path); |
| 4387 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4388 | } |
| 4389 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4390 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4391 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4392 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4393 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4394 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4395 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4396 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4397 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4398 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4399 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 4400 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4401 | wchar_t *command; |
| 4402 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 4403 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4404 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4405 | Py_BEGIN_ALLOW_THREADS |
| 4406 | sts = _wsystem(command); |
| 4407 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4408 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4409 | PyObject *command_obj; |
| 4410 | char *command; |
| 4411 | if (!PyArg_ParseTuple(args, "O&:system", |
| 4412 | PyUnicode_FSConverter, &command_obj)) |
| 4413 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4414 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4415 | command = PyBytes_AsString(command_obj); |
| 4416 | Py_BEGIN_ALLOW_THREADS |
| 4417 | sts = system(command); |
| 4418 | Py_END_ALLOW_THREADS |
| 4419 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4420 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4421 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4422 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4423 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4424 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4425 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4426 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4427 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4428 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4429 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4430 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4431 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4432 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4433 | int i; |
| 4434 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 4435 | return NULL; |
| 4436 | i = (int)umask(i); |
| 4437 | if (i < 0) |
| 4438 | return posix_error(); |
| 4439 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4440 | } |
| 4441 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4442 | #ifdef MS_WINDOWS |
| 4443 | |
| 4444 | /* override the default DeleteFileW behavior so that directory |
| 4445 | symlinks can be removed with this function, the same as with |
| 4446 | Unix symlinks */ |
| 4447 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4448 | { |
| 4449 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4450 | WIN32_FIND_DATAW find_data; |
| 4451 | HANDLE find_data_handle; |
| 4452 | int is_directory = 0; |
| 4453 | int is_link = 0; |
| 4454 | |
| 4455 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4456 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4457 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4458 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4459 | it is a symlink */ |
| 4460 | if(is_directory && |
| 4461 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4462 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4463 | |
| 4464 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4465 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 4466 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 4467 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 4468 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4469 | FindClose(find_data_handle); |
| 4470 | } |
| 4471 | } |
| 4472 | } |
| 4473 | |
| 4474 | if (is_directory && is_link) |
| 4475 | return RemoveDirectoryW(lpFileName); |
| 4476 | |
| 4477 | return DeleteFileW(lpFileName); |
| 4478 | } |
| 4479 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4480 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4481 | PyDoc_STRVAR(posix_unlink__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4482 | "unlink(path, *, dir_fd=None)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4483 | Remove a file (same as remove()).\n\ |
| 4484 | \n\ |
| 4485 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4486 | and path should be relative; path will then be relative to that directory.\n\ |
| 4487 | dir_fd may not be implemented on your platform.\n\ |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4488 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4489 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4490 | PyDoc_STRVAR(posix_remove__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4491 | "remove(path, *, dir_fd=None)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4492 | Remove a file (same as unlink()).\n\ |
| 4493 | \n\ |
| 4494 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4495 | and path should be relative; path will then be relative to that directory.\n\ |
| 4496 | dir_fd may not be implemented on your platform.\n\ |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4497 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4498 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4499 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4500 | posix_unlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4501 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4502 | path_t path; |
| 4503 | int dir_fd = DEFAULT_DIR_FD; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4504 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4505 | int result; |
| 4506 | PyObject *return_value = NULL; |
| 4507 | |
| 4508 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4509 | path.function_name = "unlink"; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4510 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:unlink", keywords, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4511 | path_converter, &path, |
| 4512 | #ifdef HAVE_UNLINKAT |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4513 | dir_fd_converter, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4514 | #else |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4515 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4516 | #endif |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4517 | )) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4518 | return NULL; |
| 4519 | |
| 4520 | Py_BEGIN_ALLOW_THREADS |
| 4521 | #ifdef MS_WINDOWS |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4522 | if (path.wide) |
| 4523 | result = Py_DeleteFileW(path.wide); |
| 4524 | else |
| 4525 | result = DeleteFileA(path.narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4526 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4527 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4528 | #ifdef HAVE_UNLINKAT |
| 4529 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4530 | result = unlinkat(dir_fd, path.narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4531 | else |
| 4532 | #endif /* HAVE_UNLINKAT */ |
| 4533 | result = unlink(path.narrow); |
| 4534 | #endif |
| 4535 | Py_END_ALLOW_THREADS |
| 4536 | |
| 4537 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4538 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4539 | goto exit; |
| 4540 | } |
| 4541 | |
| 4542 | return_value = Py_None; |
| 4543 | Py_INCREF(Py_None); |
| 4544 | |
| 4545 | exit: |
| 4546 | path_cleanup(&path); |
| 4547 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4548 | } |
| 4549 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4550 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4551 | PyDoc_STRVAR(posix_uname__doc__, |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4552 | "uname() -> uname_result\n\n\ |
| 4553 | Return an object identifying the current operating system.\n\ |
| 4554 | The object behaves like a named tuple with the following fields:\n\ |
| 4555 | (sysname, nodename, release, version, machine)"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4556 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4557 | static PyStructSequence_Field uname_result_fields[] = { |
| 4558 | {"sysname", "operating system name"}, |
| 4559 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4560 | {"release", "operating system release"}, |
| 4561 | {"version", "operating system version"}, |
| 4562 | {"machine", "hardware identifier"}, |
| 4563 | {NULL} |
| 4564 | }; |
| 4565 | |
| 4566 | PyDoc_STRVAR(uname_result__doc__, |
| 4567 | "uname_result: Result from os.uname().\n\n\ |
| 4568 | This object may be accessed either as a tuple of\n\ |
| 4569 | (sysname, nodename, release, version, machine),\n\ |
| 4570 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4571 | \n\ |
| 4572 | See os.uname for more information."); |
| 4573 | |
| 4574 | static PyStructSequence_Desc uname_result_desc = { |
| 4575 | "uname_result", /* name */ |
| 4576 | uname_result__doc__, /* doc */ |
| 4577 | uname_result_fields, |
| 4578 | 5 |
| 4579 | }; |
| 4580 | |
| 4581 | static PyTypeObject UnameResultType; |
| 4582 | |
| 4583 | |
| 4584 | #ifdef HAVE_UNAME |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4585 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4586 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4587 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4588 | struct utsname u; |
| 4589 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4590 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4591 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4592 | Py_BEGIN_ALLOW_THREADS |
| 4593 | res = uname(&u); |
| 4594 | Py_END_ALLOW_THREADS |
| 4595 | if (res < 0) |
| 4596 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4597 | |
| 4598 | value = PyStructSequence_New(&UnameResultType); |
| 4599 | if (value == NULL) |
| 4600 | return NULL; |
| 4601 | |
| 4602 | #define SET(i, field) \ |
| 4603 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4604 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4605 | if (!o) { \ |
| 4606 | Py_DECREF(value); \ |
| 4607 | return NULL; \ |
| 4608 | } \ |
| 4609 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4610 | } \ |
| 4611 | |
| 4612 | SET(0, u.sysname); |
| 4613 | SET(1, u.nodename); |
| 4614 | SET(2, u.release); |
| 4615 | SET(3, u.version); |
| 4616 | SET(4, u.machine); |
| 4617 | |
| 4618 | #undef SET |
| 4619 | |
| 4620 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4621 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4622 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4623 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4624 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4625 | PyDoc_STRVAR(posix_utime__doc__, |
| 4626 | "utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True)\n\ |
| 4627 | Set the access and modified time of path.\n\ |
| 4628 | \n\ |
| 4629 | path may always be specified as a string.\n\ |
| 4630 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 4631 | If this functionality is unavailable, using it raises an exception.\n\ |
| 4632 | \n\ |
| 4633 | If times is not None, it must be a tuple (atime, mtime);\n\ |
| 4634 | atime and mtime should be expressed as float seconds since the epoch.\n\ |
| 4635 | If ns is not None, it must be a tuple (atime_ns, mtime_ns);\n\ |
| 4636 | atime_ns and mtime_ns should be expressed as integer nanoseconds\n\ |
| 4637 | since the epoch.\n\ |
| 4638 | If both times and ns are None, utime uses the current time.\n\ |
| 4639 | Specifying tuples for both times and ns is an error.\n\ |
| 4640 | \n\ |
| 4641 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4642 | and path should be relative; path will then be relative to that directory.\n\ |
| 4643 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 4644 | link, utime will modify the symbolic link itself instead of the file the\n\ |
| 4645 | link points to.\n\ |
| 4646 | It is an error to use dir_fd or follow_symlinks when specifying path\n\ |
| 4647 | as an open file descriptor.\n\ |
| 4648 | dir_fd and follow_symlinks may not be available on your platform.\n\ |
| 4649 | If they are unavailable, using them will raise a NotImplementedError."); |
| 4650 | |
| 4651 | typedef struct { |
| 4652 | int now; |
| 4653 | time_t atime_s; |
| 4654 | long atime_ns; |
| 4655 | time_t mtime_s; |
| 4656 | long mtime_ns; |
| 4657 | } utime_t; |
| 4658 | |
| 4659 | /* |
| 4660 | * these macros assume that "utime" is a pointer to a utime_t |
| 4661 | * they also intentionally leak the declaration of a pointer named "time" |
| 4662 | */ |
| 4663 | #define UTIME_TO_TIMESPEC \ |
| 4664 | struct timespec ts[2]; \ |
| 4665 | struct timespec *time; \ |
| 4666 | if (utime->now) \ |
| 4667 | time = NULL; \ |
| 4668 | else { \ |
| 4669 | ts[0].tv_sec = utime->atime_s; \ |
| 4670 | ts[0].tv_nsec = utime->atime_ns; \ |
| 4671 | ts[1].tv_sec = utime->mtime_s; \ |
| 4672 | ts[1].tv_nsec = utime->mtime_ns; \ |
| 4673 | time = ts; \ |
| 4674 | } \ |
| 4675 | |
| 4676 | #define UTIME_TO_TIMEVAL \ |
| 4677 | struct timeval tv[2]; \ |
| 4678 | struct timeval *time; \ |
| 4679 | if (utime->now) \ |
| 4680 | time = NULL; \ |
| 4681 | else { \ |
| 4682 | tv[0].tv_sec = utime->atime_s; \ |
| 4683 | tv[0].tv_usec = utime->atime_ns / 1000; \ |
| 4684 | tv[1].tv_sec = utime->mtime_s; \ |
| 4685 | tv[1].tv_usec = utime->mtime_ns / 1000; \ |
| 4686 | time = tv; \ |
| 4687 | } \ |
| 4688 | |
| 4689 | #define UTIME_TO_UTIMBUF \ |
| 4690 | struct utimbuf u[2]; \ |
| 4691 | struct utimbuf *time; \ |
| 4692 | if (utime->now) \ |
| 4693 | time = NULL; \ |
| 4694 | else { \ |
| 4695 | u.actime = utime->atime_s; \ |
| 4696 | u.modtime = utime->mtime_s; \ |
| 4697 | time = u; \ |
| 4698 | } |
| 4699 | |
| 4700 | #define UTIME_TO_TIME_T \ |
| 4701 | time_t timet[2]; \ |
| 4702 | struct timet time; \ |
| 4703 | if (utime->now) \ |
| 4704 | time = NULL; \ |
| 4705 | else { \ |
| 4706 | timet[0] = utime->atime_s; \ |
| 4707 | timet[1] = utime->mtime_s; \ |
| 4708 | time = &timet; \ |
| 4709 | } \ |
| 4710 | |
| 4711 | |
| 4712 | #define UTIME_HAVE_DIR_FD (defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)) |
| 4713 | |
| 4714 | #if UTIME_HAVE_DIR_FD |
| 4715 | |
| 4716 | static int |
| 4717 | utime_dir_fd(utime_t *utime, int dir_fd, char *path, int follow_symlinks) |
| 4718 | { |
| 4719 | #ifdef HAVE_UTIMENSAT |
| 4720 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4721 | UTIME_TO_TIMESPEC; |
| 4722 | return utimensat(dir_fd, path, time, flags); |
| 4723 | #elif defined(HAVE_FUTIMESAT) |
| 4724 | UTIME_TO_TIMEVAL; |
| 4725 | /* |
| 4726 | * follow_symlinks will never be false here; |
| 4727 | * we only allow !follow_symlinks and dir_fd together |
| 4728 | * if we have utimensat() |
| 4729 | */ |
| 4730 | assert(follow_symlinks); |
| 4731 | return futimesat(dir_fd, path, time); |
| 4732 | #endif |
| 4733 | } |
| 4734 | |
| 4735 | #endif |
| 4736 | |
| 4737 | #define UTIME_HAVE_FD (defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)) |
| 4738 | |
| 4739 | #if UTIME_HAVE_FD |
| 4740 | |
| 4741 | static int |
| 4742 | utime_fd(utime_t *utime, int fd) |
| 4743 | { |
| 4744 | #ifdef HAVE_FUTIMENS |
| 4745 | UTIME_TO_TIMESPEC; |
| 4746 | return futimens(fd, time); |
| 4747 | #else |
| 4748 | UTIME_TO_TIMEVAL; |
| 4749 | return futimes(fd, time); |
| 4750 | #endif |
| 4751 | } |
| 4752 | |
| 4753 | #endif |
| 4754 | |
| 4755 | |
| 4756 | #define UTIME_HAVE_NOFOLLOW_SYMLINKS \ |
| 4757 | (defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)) |
| 4758 | |
| 4759 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4760 | |
| 4761 | static int |
| 4762 | utime_nofollow_symlinks(utime_t *utime, char *path) |
| 4763 | { |
| 4764 | #ifdef HAVE_UTIMENSAT |
| 4765 | UTIME_TO_TIMESPEC; |
| 4766 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4767 | #else |
| 4768 | UTIME_TO_TIMEVAL; |
| 4769 | return lutimes(path, time); |
| 4770 | #endif |
| 4771 | } |
| 4772 | |
| 4773 | #endif |
| 4774 | |
| 4775 | #ifndef MS_WINDOWS |
| 4776 | |
| 4777 | static int |
| 4778 | utime_default(utime_t *utime, char *path) |
| 4779 | { |
| 4780 | #ifdef HAVE_UTIMENSAT |
| 4781 | UTIME_TO_TIMESPEC; |
| 4782 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4783 | #elif defined(HAVE_UTIMES) |
| 4784 | UTIME_TO_TIMEVAL; |
| 4785 | return utimes(path, time); |
| 4786 | #elif defined(HAVE_UTIME_H) |
| 4787 | UTIME_TO_UTIMBUF; |
| 4788 | return utime(path, time); |
| 4789 | #else |
| 4790 | UTIME_TO_TIME_T; |
| 4791 | return utime(path, time); |
| 4792 | #endif |
| 4793 | } |
| 4794 | |
| 4795 | #endif |
| 4796 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4797 | static int |
| 4798 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4799 | { |
| 4800 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4801 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4802 | divmod = PyNumber_Divmod(py_long, billion); |
| 4803 | if (!divmod) |
| 4804 | goto exit; |
| 4805 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4806 | if ((*s == -1) && PyErr_Occurred()) |
| 4807 | goto exit; |
| 4808 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4809 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4810 | goto exit; |
| 4811 | |
| 4812 | result = 1; |
| 4813 | exit: |
| 4814 | Py_XDECREF(divmod); |
| 4815 | return result; |
| 4816 | } |
| 4817 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4818 | static PyObject * |
| 4819 | posix_utime(PyObject *self, PyObject *args, PyObject *kwargs) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4820 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4821 | path_t path; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4822 | PyObject *times = NULL; |
| 4823 | PyObject *ns = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4824 | int dir_fd = DEFAULT_DIR_FD; |
| 4825 | int follow_symlinks = 1; |
| 4826 | char *keywords[] = {"path", "times", "ns", "dir_fd", |
| 4827 | "follow_symlinks", NULL}; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4828 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4829 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4830 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4831 | #ifdef MS_WINDOWS |
| 4832 | HANDLE hFile; |
| 4833 | FILETIME atime, mtime; |
| 4834 | #else |
| 4835 | int result; |
| 4836 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4837 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4838 | PyObject *return_value = NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4839 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4840 | memset(&path, 0, sizeof(path)); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4841 | path.function_name = "utime"; |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4842 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4843 | #if UTIME_HAVE_FD |
| 4844 | path.allow_fd = 1; |
| 4845 | #endif |
| 4846 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 4847 | "O&|O$OO&p:utime", keywords, |
| 4848 | path_converter, &path, |
| 4849 | ×, &ns, |
| 4850 | #if UTIME_HAVE_DIR_FD |
| 4851 | dir_fd_converter, &dir_fd, |
| 4852 | #else |
| 4853 | dir_fd_unavailable, &dir_fd, |
| 4854 | #endif |
| 4855 | &follow_symlinks |
| 4856 | )) |
| 4857 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4858 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4859 | if (times && (times != Py_None) && ns) { |
| 4860 | PyErr_SetString(PyExc_ValueError, |
| 4861 | "utime: you may specify either 'times'" |
| 4862 | " or 'ns' but not both"); |
| 4863 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4864 | } |
| 4865 | |
| 4866 | if (times && (times != Py_None)) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4867 | time_t a_sec, m_sec; |
| 4868 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4869 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4870 | PyErr_SetString(PyExc_TypeError, |
| 4871 | "utime: 'times' must be either" |
| 4872 | " a tuple of two ints or None"); |
| 4873 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4874 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4875 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4876 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 4877 | &a_sec, &a_nsec, _PyTime_ROUND_DOWN) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4878 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 4879 | &m_sec, &m_nsec, _PyTime_ROUND_DOWN) == -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4880 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4881 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4882 | utime.atime_s = a_sec; |
| 4883 | utime.atime_ns = a_nsec; |
| 4884 | utime.mtime_s = m_sec; |
| 4885 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4886 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4887 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4888 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4889 | PyErr_SetString(PyExc_TypeError, |
| 4890 | "utime: 'ns' must be a tuple of two ints"); |
| 4891 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4892 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4893 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4894 | 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] | 4895 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4896 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4897 | &utime.mtime_s, &utime.mtime_ns)) { |
| 4898 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4899 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4900 | } |
| 4901 | else { |
| 4902 | /* times and ns are both None/unspecified. use "now". */ |
| 4903 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4904 | } |
| 4905 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4906 | #if !UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4907 | if (follow_symlinks_specified("utime", follow_symlinks)) |
| 4908 | goto exit; |
| 4909 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4910 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4911 | if (path_and_dir_fd_invalid("utime", &path, dir_fd) || |
| 4912 | dir_fd_and_fd_invalid("utime", dir_fd, path.fd) || |
| 4913 | fd_and_follow_symlinks_invalid("utime", path.fd, follow_symlinks)) |
| 4914 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4915 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4916 | #if !defined(HAVE_UTIMENSAT) |
| 4917 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4918 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4919 | "utime: cannot use dir_fd and follow_symlinks " |
| 4920 | "together on this platform"); |
| 4921 | goto exit; |
| 4922 | } |
| 4923 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4924 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4925 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4926 | Py_BEGIN_ALLOW_THREADS |
| 4927 | if (path.wide) |
| 4928 | hFile = CreateFileW(path.wide, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4929 | NULL, OPEN_EXISTING, |
| 4930 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4931 | else |
| 4932 | hFile = CreateFileA(path.narrow, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4933 | NULL, OPEN_EXISTING, |
| 4934 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4935 | Py_END_ALLOW_THREADS |
| 4936 | if (hFile == INVALID_HANDLE_VALUE) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4937 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4938 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4939 | } |
| 4940 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4941 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 4942 | GetSystemTimeAsFileTime(&mtime); |
| 4943 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4944 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4945 | else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4946 | time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4947 | time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4948 | } |
| 4949 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4950 | /* Avoid putting the file name into the error here, |
| 4951 | as that may confuse the user into believing that |
| 4952 | something is wrong with the file, when it also |
| 4953 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4954 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4955 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4956 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4957 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4958 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4959 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4960 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4961 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 4962 | result = utime_nofollow_symlinks(&utime, path.narrow); |
| 4963 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4964 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4965 | |
| 4966 | #if UTIME_HAVE_DIR_FD |
| 4967 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
| 4968 | result = utime_dir_fd(&utime, dir_fd, path.narrow, follow_symlinks); |
| 4969 | else |
| 4970 | #endif |
| 4971 | |
| 4972 | #if UTIME_HAVE_FD |
| 4973 | if (path.fd != -1) |
| 4974 | result = utime_fd(&utime, path.fd); |
| 4975 | else |
| 4976 | #endif |
| 4977 | |
| 4978 | result = utime_default(&utime, path.narrow); |
| 4979 | |
| 4980 | Py_END_ALLOW_THREADS |
| 4981 | |
| 4982 | if (result < 0) { |
| 4983 | /* see previous comment about not putting filename in error here */ |
| 4984 | return_value = posix_error(); |
| 4985 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4986 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4987 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4988 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4989 | |
| 4990 | Py_INCREF(Py_None); |
| 4991 | return_value = Py_None; |
| 4992 | |
| 4993 | exit: |
| 4994 | path_cleanup(&path); |
| 4995 | #ifdef MS_WINDOWS |
| 4996 | if (hFile != INVALID_HANDLE_VALUE) |
| 4997 | CloseHandle(hFile); |
| 4998 | #endif |
| 4999 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5000 | } |
| 5001 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5002 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5003 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5004 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5005 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5006 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5007 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5008 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5009 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5010 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5011 | int sts; |
| 5012 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 5013 | return NULL; |
| 5014 | _exit(sts); |
| 5015 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5016 | } |
| 5017 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5018 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 5019 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 5020 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5021 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5022 | Py_ssize_t i; |
| 5023 | for (i = 0; i < count; i++) |
| 5024 | PyMem_Free(array[i]); |
| 5025 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5026 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5027 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 5028 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5029 | int fsconvert_strdup(PyObject *o, char**out) |
| 5030 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5031 | PyObject *bytes; |
| 5032 | Py_ssize_t size; |
| 5033 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 5034 | return 0; |
| 5035 | size = PyBytes_GET_SIZE(bytes); |
| 5036 | *out = PyMem_Malloc(size+1); |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 5037 | if (!*out) { |
| 5038 | PyErr_NoMemory(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5039 | return 0; |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 5040 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5041 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 5042 | Py_DECREF(bytes); |
| 5043 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5044 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5045 | #endif |
| 5046 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5047 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5048 | static char** |
| 5049 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 5050 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5051 | char **envlist; |
| 5052 | Py_ssize_t i, pos, envc; |
| 5053 | PyObject *keys=NULL, *vals=NULL; |
| 5054 | PyObject *key, *val, *key2, *val2; |
| 5055 | char *p, *k, *v; |
| 5056 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5057 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5058 | i = PyMapping_Size(env); |
| 5059 | if (i < 0) |
| 5060 | return NULL; |
| 5061 | envlist = PyMem_NEW(char *, i + 1); |
| 5062 | if (envlist == NULL) { |
| 5063 | PyErr_NoMemory(); |
| 5064 | return NULL; |
| 5065 | } |
| 5066 | envc = 0; |
| 5067 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5068 | if (!keys) |
| 5069 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5070 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5071 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5072 | goto error; |
| 5073 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 5074 | PyErr_Format(PyExc_TypeError, |
| 5075 | "env.keys() or env.values() is not a list"); |
| 5076 | goto error; |
| 5077 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5078 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5079 | for (pos = 0; pos < i; pos++) { |
| 5080 | key = PyList_GetItem(keys, pos); |
| 5081 | val = PyList_GetItem(vals, pos); |
| 5082 | if (!key || !val) |
| 5083 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5084 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5085 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 5086 | goto error; |
| 5087 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 5088 | Py_DECREF(key2); |
| 5089 | goto error; |
| 5090 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5091 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5092 | k = PyBytes_AsString(key2); |
| 5093 | v = PyBytes_AsString(val2); |
| 5094 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5095 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5096 | p = PyMem_NEW(char, len); |
| 5097 | if (p == NULL) { |
| 5098 | PyErr_NoMemory(); |
| 5099 | Py_DECREF(key2); |
| 5100 | Py_DECREF(val2); |
| 5101 | goto error; |
| 5102 | } |
| 5103 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 5104 | envlist[envc++] = p; |
| 5105 | Py_DECREF(key2); |
| 5106 | Py_DECREF(val2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5107 | } |
| 5108 | Py_DECREF(vals); |
| 5109 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5110 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5111 | envlist[envc] = 0; |
| 5112 | *envc_ptr = envc; |
| 5113 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5114 | |
| 5115 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5116 | Py_XDECREF(keys); |
| 5117 | Py_XDECREF(vals); |
| 5118 | while (--envc >= 0) |
| 5119 | PyMem_DEL(envlist[envc]); |
| 5120 | PyMem_DEL(envlist); |
| 5121 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5122 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5123 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5124 | static char** |
| 5125 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 5126 | { |
| 5127 | int i; |
| 5128 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 5129 | if (argvlist == NULL) { |
| 5130 | PyErr_NoMemory(); |
| 5131 | return NULL; |
| 5132 | } |
| 5133 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5134 | PyObject* item = PySequence_ITEM(argv, i); |
| 5135 | if (item == NULL) |
| 5136 | goto fail; |
| 5137 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 5138 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5139 | goto fail; |
| 5140 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5141 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5142 | } |
| 5143 | argvlist[*argc] = NULL; |
| 5144 | return argvlist; |
| 5145 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5146 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5147 | free_string_array(argvlist, *argc); |
| 5148 | return NULL; |
| 5149 | } |
| 5150 | #endif |
| 5151 | |
| 5152 | #ifdef HAVE_EXECV |
| 5153 | PyDoc_STRVAR(posix_execv__doc__, |
| 5154 | "execv(path, args)\n\n\ |
| 5155 | Execute an executable path with arguments, replacing current process.\n\ |
| 5156 | \n\ |
| 5157 | path: path of executable file\n\ |
| 5158 | args: tuple or list of strings"); |
| 5159 | |
| 5160 | static PyObject * |
| 5161 | posix_execv(PyObject *self, PyObject *args) |
| 5162 | { |
| 5163 | PyObject *opath; |
| 5164 | char *path; |
| 5165 | PyObject *argv; |
| 5166 | char **argvlist; |
| 5167 | Py_ssize_t argc; |
| 5168 | |
| 5169 | /* execv has two arguments: (path, argv), where |
| 5170 | argv is a list or tuple of strings. */ |
| 5171 | |
| 5172 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 5173 | PyUnicode_FSConverter, |
| 5174 | &opath, &argv)) |
| 5175 | return NULL; |
| 5176 | path = PyBytes_AsString(opath); |
| 5177 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 5178 | PyErr_SetString(PyExc_TypeError, |
| 5179 | "execv() arg 2 must be a tuple or list"); |
| 5180 | Py_DECREF(opath); |
| 5181 | return NULL; |
| 5182 | } |
| 5183 | argc = PySequence_Size(argv); |
| 5184 | if (argc < 1) { |
| 5185 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 5186 | Py_DECREF(opath); |
| 5187 | return NULL; |
| 5188 | } |
| 5189 | |
| 5190 | argvlist = parse_arglist(argv, &argc); |
| 5191 | if (argvlist == NULL) { |
| 5192 | Py_DECREF(opath); |
| 5193 | return NULL; |
| 5194 | } |
| 5195 | |
| 5196 | execv(path, argvlist); |
| 5197 | |
| 5198 | /* If we get here it's definitely an error */ |
| 5199 | |
| 5200 | free_string_array(argvlist, argc); |
| 5201 | Py_DECREF(opath); |
| 5202 | return posix_error(); |
| 5203 | } |
| 5204 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5205 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5206 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5207 | Execute a path with arguments and environment, replacing current process.\n\ |
| 5208 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5209 | path: path of executable file\n\ |
| 5210 | args: tuple or list of arguments\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5211 | env: dictionary of strings mapping to strings\n\ |
| 5212 | \n\ |
| 5213 | On some platforms, you may specify an open file descriptor for path;\n\ |
| 5214 | execve will execute the program the file descriptor is open to.\n\ |
| 5215 | If this functionality is unavailable, using it raises NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5216 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5217 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5218 | posix_execve(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5219 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5220 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5221 | PyObject *argv, *env; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5222 | char **argvlist = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5223 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5224 | Py_ssize_t argc, envc; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5225 | static char *keywords[] = {"path", "argv", "environment", NULL}; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5226 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5227 | /* execve has three arguments: (path, argv, env), where |
| 5228 | argv is a list or tuple of strings and env is a dictionary |
| 5229 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5230 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5231 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 5232 | path.function_name = "execve"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5233 | #ifdef HAVE_FEXECVE |
| 5234 | path.allow_fd = 1; |
| 5235 | #endif |
| 5236 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&OO:execve", keywords, |
| 5237 | path_converter, &path, |
| 5238 | &argv, &env |
| 5239 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5240 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5241 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5242 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5243 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5244 | "execve: argv must be a tuple or list"); |
| 5245 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5246 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5247 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5248 | if (!PyMapping_Check(env)) { |
| 5249 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5250 | "execve: environment must be a mapping object"); |
| 5251 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5252 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5253 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5254 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5255 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5256 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5257 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5258 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5259 | envlist = parse_envlist(env, &envc); |
| 5260 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5261 | goto fail; |
| 5262 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5263 | #ifdef HAVE_FEXECVE |
| 5264 | if (path.fd > -1) |
| 5265 | fexecve(path.fd, argvlist, envlist); |
| 5266 | else |
| 5267 | #endif |
| 5268 | execve(path.narrow, argvlist, envlist); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5269 | |
| 5270 | /* If we get here it's definitely an error */ |
| 5271 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 5272 | path_error(&path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5273 | |
| 5274 | while (--envc >= 0) |
| 5275 | PyMem_DEL(envlist[envc]); |
| 5276 | PyMem_DEL(envlist); |
| 5277 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5278 | if (argvlist) |
| 5279 | free_string_array(argvlist, argc); |
| 5280 | path_cleanup(&path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5281 | return NULL; |
| 5282 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5283 | #endif /* HAVE_EXECV */ |
| 5284 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5285 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5286 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5287 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5288 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5289 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5290 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5291 | mode: mode of process creation\n\ |
| 5292 | path: path of executable file\n\ |
| 5293 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5294 | |
| 5295 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5296 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5297 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5298 | PyObject *opath; |
| 5299 | char *path; |
| 5300 | PyObject *argv; |
| 5301 | char **argvlist; |
| 5302 | int mode, i; |
| 5303 | Py_ssize_t argc; |
| 5304 | Py_intptr_t spawnval; |
| 5305 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5306 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5307 | /* spawnv has three arguments: (mode, path, argv), where |
| 5308 | argv is a list or tuple of strings. */ |
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 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 5311 | PyUnicode_FSConverter, |
| 5312 | &opath, &argv)) |
| 5313 | return NULL; |
| 5314 | path = PyBytes_AsString(opath); |
| 5315 | if (PyList_Check(argv)) { |
| 5316 | argc = PyList_Size(argv); |
| 5317 | getitem = PyList_GetItem; |
| 5318 | } |
| 5319 | else if (PyTuple_Check(argv)) { |
| 5320 | argc = PyTuple_Size(argv); |
| 5321 | getitem = PyTuple_GetItem; |
| 5322 | } |
| 5323 | else { |
| 5324 | PyErr_SetString(PyExc_TypeError, |
| 5325 | "spawnv() arg 2 must be a tuple or list"); |
| 5326 | Py_DECREF(opath); |
| 5327 | return NULL; |
| 5328 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5329 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5330 | argvlist = PyMem_NEW(char *, argc+1); |
| 5331 | if (argvlist == NULL) { |
| 5332 | Py_DECREF(opath); |
| 5333 | return PyErr_NoMemory(); |
| 5334 | } |
| 5335 | for (i = 0; i < argc; i++) { |
| 5336 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5337 | &argvlist[i])) { |
| 5338 | free_string_array(argvlist, i); |
| 5339 | PyErr_SetString( |
| 5340 | PyExc_TypeError, |
| 5341 | "spawnv() arg 2 must contain only strings"); |
| 5342 | Py_DECREF(opath); |
| 5343 | return NULL; |
| 5344 | } |
| 5345 | } |
| 5346 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5347 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5348 | if (mode == _OLD_P_OVERLAY) |
| 5349 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5350 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5351 | Py_BEGIN_ALLOW_THREADS |
| 5352 | spawnval = _spawnv(mode, path, argvlist); |
| 5353 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5354 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5355 | free_string_array(argvlist, argc); |
| 5356 | Py_DECREF(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5357 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5358 | if (spawnval == -1) |
| 5359 | return posix_error(); |
| 5360 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5361 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5362 | } |
| 5363 | |
| 5364 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5365 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5366 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5367 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5368 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5369 | mode: mode of process creation\n\ |
| 5370 | path: path of executable file\n\ |
| 5371 | args: tuple or list of arguments\n\ |
| 5372 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5373 | |
| 5374 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5375 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5376 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5377 | PyObject *opath; |
| 5378 | char *path; |
| 5379 | PyObject *argv, *env; |
| 5380 | char **argvlist; |
| 5381 | char **envlist; |
| 5382 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5383 | int mode; |
| 5384 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5385 | Py_intptr_t spawnval; |
| 5386 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 5387 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5388 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5389 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5390 | argv is a list or tuple of strings and env is a dictionary |
| 5391 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5392 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5393 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 5394 | PyUnicode_FSConverter, |
| 5395 | &opath, &argv, &env)) |
| 5396 | return NULL; |
| 5397 | path = PyBytes_AsString(opath); |
| 5398 | if (PyList_Check(argv)) { |
| 5399 | argc = PyList_Size(argv); |
| 5400 | getitem = PyList_GetItem; |
| 5401 | } |
| 5402 | else if (PyTuple_Check(argv)) { |
| 5403 | argc = PyTuple_Size(argv); |
| 5404 | getitem = PyTuple_GetItem; |
| 5405 | } |
| 5406 | else { |
| 5407 | PyErr_SetString(PyExc_TypeError, |
| 5408 | "spawnve() arg 2 must be a tuple or list"); |
| 5409 | goto fail_0; |
| 5410 | } |
| 5411 | if (!PyMapping_Check(env)) { |
| 5412 | PyErr_SetString(PyExc_TypeError, |
| 5413 | "spawnve() arg 3 must be a mapping object"); |
| 5414 | goto fail_0; |
| 5415 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5416 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5417 | argvlist = PyMem_NEW(char *, argc+1); |
| 5418 | if (argvlist == NULL) { |
| 5419 | PyErr_NoMemory(); |
| 5420 | goto fail_0; |
| 5421 | } |
| 5422 | for (i = 0; i < argc; i++) { |
| 5423 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5424 | &argvlist[i])) |
| 5425 | { |
| 5426 | lastarg = i; |
| 5427 | goto fail_1; |
| 5428 | } |
| 5429 | } |
| 5430 | lastarg = argc; |
| 5431 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5432 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5433 | envlist = parse_envlist(env, &envc); |
| 5434 | if (envlist == NULL) |
| 5435 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5436 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5437 | if (mode == _OLD_P_OVERLAY) |
| 5438 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5439 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5440 | Py_BEGIN_ALLOW_THREADS |
| 5441 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 5442 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5443 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5444 | if (spawnval == -1) |
| 5445 | (void) posix_error(); |
| 5446 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5447 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5448 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5449 | while (--envc >= 0) |
| 5450 | PyMem_DEL(envlist[envc]); |
| 5451 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5452 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5453 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5454 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5455 | Py_DECREF(opath); |
| 5456 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5457 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5458 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5459 | #endif /* HAVE_SPAWNV */ |
| 5460 | |
| 5461 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5462 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5463 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5464 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5465 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 5466 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5467 | 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] | 5468 | |
| 5469 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5470 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5471 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5472 | pid_t pid; |
| 5473 | int result = 0; |
| 5474 | _PyImport_AcquireLock(); |
| 5475 | pid = fork1(); |
| 5476 | if (pid == 0) { |
| 5477 | /* child: this clobbers and resets the import lock. */ |
| 5478 | PyOS_AfterFork(); |
| 5479 | } else { |
| 5480 | /* parent: release the import lock. */ |
| 5481 | result = _PyImport_ReleaseLock(); |
| 5482 | } |
| 5483 | if (pid == -1) |
| 5484 | return posix_error(); |
| 5485 | if (result < 0) { |
| 5486 | /* Don't clobber the OSError if the fork failed. */ |
| 5487 | PyErr_SetString(PyExc_RuntimeError, |
| 5488 | "not holding the import lock"); |
| 5489 | return NULL; |
| 5490 | } |
| 5491 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5492 | } |
| 5493 | #endif |
| 5494 | |
| 5495 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5496 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5497 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5498 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5499 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5500 | 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] | 5501 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5502 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5503 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5504 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5505 | pid_t pid; |
| 5506 | int result = 0; |
| 5507 | _PyImport_AcquireLock(); |
| 5508 | pid = fork(); |
| 5509 | if (pid == 0) { |
| 5510 | /* child: this clobbers and resets the import lock. */ |
| 5511 | PyOS_AfterFork(); |
| 5512 | } else { |
| 5513 | /* parent: release the import lock. */ |
| 5514 | result = _PyImport_ReleaseLock(); |
| 5515 | } |
| 5516 | if (pid == -1) |
| 5517 | return posix_error(); |
| 5518 | if (result < 0) { |
| 5519 | /* Don't clobber the OSError if the fork failed. */ |
| 5520 | PyErr_SetString(PyExc_RuntimeError, |
| 5521 | "not holding the import lock"); |
| 5522 | return NULL; |
| 5523 | } |
| 5524 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5525 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5526 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5527 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5528 | #ifdef HAVE_SCHED_H |
| 5529 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5530 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
| 5531 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5532 | PyDoc_STRVAR(posix_sched_get_priority_max__doc__, |
| 5533 | "sched_get_priority_max(policy)\n\n\ |
| 5534 | Get the maximum scheduling priority for *policy*."); |
| 5535 | |
| 5536 | static PyObject * |
| 5537 | posix_sched_get_priority_max(PyObject *self, PyObject *args) |
| 5538 | { |
| 5539 | int policy, max; |
| 5540 | |
| 5541 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy)) |
| 5542 | return NULL; |
| 5543 | max = sched_get_priority_max(policy); |
| 5544 | if (max < 0) |
| 5545 | return posix_error(); |
| 5546 | return PyLong_FromLong(max); |
| 5547 | } |
| 5548 | |
| 5549 | PyDoc_STRVAR(posix_sched_get_priority_min__doc__, |
| 5550 | "sched_get_priority_min(policy)\n\n\ |
| 5551 | Get the minimum scheduling priority for *policy*."); |
| 5552 | |
| 5553 | static PyObject * |
| 5554 | posix_sched_get_priority_min(PyObject *self, PyObject *args) |
| 5555 | { |
| 5556 | int policy, min; |
| 5557 | |
| 5558 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy)) |
| 5559 | return NULL; |
| 5560 | min = sched_get_priority_min(policy); |
| 5561 | if (min < 0) |
| 5562 | return posix_error(); |
| 5563 | return PyLong_FromLong(min); |
| 5564 | } |
| 5565 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5566 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 5567 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5568 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5569 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5570 | PyDoc_STRVAR(posix_sched_getscheduler__doc__, |
| 5571 | "sched_getscheduler(pid)\n\n\ |
| 5572 | Get the scheduling policy for the process with a PID of *pid*.\n\ |
| 5573 | Passing a PID of 0 returns the scheduling policy for the calling process."); |
| 5574 | |
| 5575 | static PyObject * |
| 5576 | posix_sched_getscheduler(PyObject *self, PyObject *args) |
| 5577 | { |
| 5578 | pid_t pid; |
| 5579 | int policy; |
| 5580 | |
| 5581 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid)) |
| 5582 | return NULL; |
| 5583 | policy = sched_getscheduler(pid); |
| 5584 | if (policy < 0) |
| 5585 | return posix_error(); |
| 5586 | return PyLong_FromLong(policy); |
| 5587 | } |
| 5588 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5589 | #endif |
| 5590 | |
| 5591 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
| 5592 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5593 | static PyObject * |
| 5594 | sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 5595 | { |
| 5596 | PyObject *res, *priority; |
Benjamin Peterson | 4e36d5a | 2011-08-02 19:56:11 -0500 | [diff] [blame] | 5597 | static char *kwlist[] = {"sched_priority", NULL}; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5598 | |
| 5599 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority)) |
| 5600 | return NULL; |
| 5601 | res = PyStructSequence_New(type); |
| 5602 | if (!res) |
| 5603 | return NULL; |
| 5604 | Py_INCREF(priority); |
| 5605 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5606 | return res; |
| 5607 | } |
| 5608 | |
| 5609 | PyDoc_STRVAR(sched_param__doc__, |
| 5610 | "sched_param(sched_priority): A scheduling parameter.\n\n\ |
| 5611 | Current has only one field: sched_priority"); |
| 5612 | |
| 5613 | static PyStructSequence_Field sched_param_fields[] = { |
| 5614 | {"sched_priority", "the scheduling priority"}, |
| 5615 | {0} |
| 5616 | }; |
| 5617 | |
| 5618 | static PyStructSequence_Desc sched_param_desc = { |
| 5619 | "sched_param", /* name */ |
| 5620 | sched_param__doc__, /* doc */ |
| 5621 | sched_param_fields, |
| 5622 | 1 |
| 5623 | }; |
| 5624 | |
| 5625 | static int |
| 5626 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 5627 | { |
| 5628 | long priority; |
| 5629 | |
| 5630 | if (Py_TYPE(param) != &SchedParamType) { |
| 5631 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 5632 | return 0; |
| 5633 | } |
| 5634 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 5635 | if (priority == -1 && PyErr_Occurred()) |
| 5636 | return 0; |
| 5637 | if (priority > INT_MAX || priority < INT_MIN) { |
| 5638 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 5639 | return 0; |
| 5640 | } |
| 5641 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 5642 | return 1; |
| 5643 | } |
| 5644 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5645 | #endif |
| 5646 | |
| 5647 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5648 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5649 | PyDoc_STRVAR(posix_sched_setscheduler__doc__, |
| 5650 | "sched_setscheduler(pid, policy, param)\n\n\ |
| 5651 | Set the scheduling policy, *policy*, for *pid*.\n\ |
| 5652 | If *pid* is 0, the calling process is changed.\n\ |
| 5653 | *param* is an instance of sched_param."); |
| 5654 | |
| 5655 | static PyObject * |
| 5656 | posix_sched_setscheduler(PyObject *self, PyObject *args) |
| 5657 | { |
| 5658 | pid_t pid; |
| 5659 | int policy; |
| 5660 | struct sched_param param; |
| 5661 | |
| 5662 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler", |
| 5663 | &pid, &policy, &convert_sched_param, ¶m)) |
| 5664 | return NULL; |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5665 | |
| 5666 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 5667 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 5668 | ** scheduling policy under Solaris/Illumos, and others. |
| 5669 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5670 | */ |
| 5671 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5672 | return posix_error(); |
| 5673 | Py_RETURN_NONE; |
| 5674 | } |
| 5675 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5676 | #endif |
| 5677 | |
| 5678 | #ifdef HAVE_SCHED_SETPARAM |
| 5679 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5680 | PyDoc_STRVAR(posix_sched_getparam__doc__, |
| 5681 | "sched_getparam(pid) -> sched_param\n\n\ |
| 5682 | Returns scheduling parameters for the process with *pid* as an instance of the\n\ |
| 5683 | sched_param class. A PID of 0 means the calling process."); |
| 5684 | |
| 5685 | static PyObject * |
| 5686 | posix_sched_getparam(PyObject *self, PyObject *args) |
| 5687 | { |
| 5688 | pid_t pid; |
| 5689 | struct sched_param param; |
| 5690 | PyObject *res, *priority; |
| 5691 | |
| 5692 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid)) |
| 5693 | return NULL; |
| 5694 | if (sched_getparam(pid, ¶m)) |
| 5695 | return posix_error(); |
| 5696 | res = PyStructSequence_New(&SchedParamType); |
| 5697 | if (!res) |
| 5698 | return NULL; |
| 5699 | priority = PyLong_FromLong(param.sched_priority); |
| 5700 | if (!priority) { |
| 5701 | Py_DECREF(res); |
| 5702 | return NULL; |
| 5703 | } |
| 5704 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5705 | return res; |
| 5706 | } |
| 5707 | |
| 5708 | PyDoc_STRVAR(posix_sched_setparam__doc__, |
| 5709 | "sched_setparam(pid, param)\n\n\ |
| 5710 | Set scheduling parameters for a process with PID *pid*.\n\ |
| 5711 | A PID of 0 means the calling process."); |
| 5712 | |
| 5713 | static PyObject * |
| 5714 | posix_sched_setparam(PyObject *self, PyObject *args) |
| 5715 | { |
| 5716 | pid_t pid; |
| 5717 | struct sched_param param; |
| 5718 | |
| 5719 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam", |
| 5720 | &pid, &convert_sched_param, ¶m)) |
| 5721 | return NULL; |
| 5722 | if (sched_setparam(pid, ¶m)) |
| 5723 | return posix_error(); |
| 5724 | Py_RETURN_NONE; |
| 5725 | } |
| 5726 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5727 | #endif |
| 5728 | |
| 5729 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
| 5730 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5731 | PyDoc_STRVAR(posix_sched_rr_get_interval__doc__, |
| 5732 | "sched_rr_get_interval(pid) -> float\n\n\ |
| 5733 | Return the round-robin quantum for the process with PID *pid* in seconds."); |
| 5734 | |
| 5735 | static PyObject * |
| 5736 | posix_sched_rr_get_interval(PyObject *self, PyObject *args) |
| 5737 | { |
| 5738 | pid_t pid; |
| 5739 | struct timespec interval; |
| 5740 | |
| 5741 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid)) |
| 5742 | return NULL; |
| 5743 | if (sched_rr_get_interval(pid, &interval)) |
| 5744 | return posix_error(); |
| 5745 | return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec); |
| 5746 | } |
| 5747 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5748 | #endif |
| 5749 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5750 | PyDoc_STRVAR(posix_sched_yield__doc__, |
| 5751 | "sched_yield()\n\n\ |
| 5752 | Voluntarily relinquish the CPU."); |
| 5753 | |
| 5754 | static PyObject * |
| 5755 | posix_sched_yield(PyObject *self, PyObject *noargs) |
| 5756 | { |
| 5757 | if (sched_yield()) |
| 5758 | return posix_error(); |
| 5759 | Py_RETURN_NONE; |
| 5760 | } |
| 5761 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5762 | #ifdef HAVE_SCHED_SETAFFINITY |
| 5763 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5764 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 5765 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5766 | |
| 5767 | PyDoc_STRVAR(posix_sched_setaffinity__doc__, |
| 5768 | "sched_setaffinity(pid, cpu_set)\n\n\ |
| 5769 | Set the affinity of the process with PID *pid* to *cpu_set*."); |
| 5770 | |
| 5771 | static PyObject * |
| 5772 | posix_sched_setaffinity(PyObject *self, PyObject *args) |
| 5773 | { |
| 5774 | pid_t pid; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5775 | int ncpus; |
| 5776 | size_t setsize; |
| 5777 | cpu_set_t *mask = NULL; |
| 5778 | PyObject *iterable, *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5779 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5780 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O:sched_setaffinity", |
| 5781 | &pid, &iterable)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5782 | return NULL; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5783 | |
| 5784 | iterator = PyObject_GetIter(iterable); |
| 5785 | if (iterator == NULL) |
| 5786 | return NULL; |
| 5787 | |
| 5788 | ncpus = NCPUS_START; |
| 5789 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5790 | mask = CPU_ALLOC(ncpus); |
| 5791 | if (mask == NULL) { |
| 5792 | PyErr_NoMemory(); |
| 5793 | goto error; |
| 5794 | } |
| 5795 | CPU_ZERO_S(setsize, mask); |
| 5796 | |
| 5797 | while ((item = PyIter_Next(iterator))) { |
| 5798 | long cpu; |
| 5799 | if (!PyLong_Check(item)) { |
| 5800 | PyErr_Format(PyExc_TypeError, |
| 5801 | "expected an iterator of ints, " |
| 5802 | "but iterator yielded %R", |
| 5803 | Py_TYPE(item)); |
| 5804 | Py_DECREF(item); |
| 5805 | goto error; |
| 5806 | } |
| 5807 | cpu = PyLong_AsLong(item); |
| 5808 | Py_DECREF(item); |
| 5809 | if (cpu < 0) { |
| 5810 | if (!PyErr_Occurred()) |
| 5811 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 5812 | goto error; |
| 5813 | } |
| 5814 | if (cpu > INT_MAX - 1) { |
| 5815 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 5816 | goto error; |
| 5817 | } |
| 5818 | if (cpu >= ncpus) { |
| 5819 | /* Grow CPU mask to fit the CPU number */ |
| 5820 | int newncpus = ncpus; |
| 5821 | cpu_set_t *newmask; |
| 5822 | size_t newsetsize; |
| 5823 | while (newncpus <= cpu) { |
| 5824 | if (newncpus > INT_MAX / 2) |
| 5825 | newncpus = cpu + 1; |
| 5826 | else |
| 5827 | newncpus = newncpus * 2; |
| 5828 | } |
| 5829 | newmask = CPU_ALLOC(newncpus); |
| 5830 | if (newmask == NULL) { |
| 5831 | PyErr_NoMemory(); |
| 5832 | goto error; |
| 5833 | } |
| 5834 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 5835 | CPU_ZERO_S(newsetsize, newmask); |
| 5836 | memcpy(newmask, mask, setsize); |
| 5837 | CPU_FREE(mask); |
| 5838 | setsize = newsetsize; |
| 5839 | mask = newmask; |
| 5840 | ncpus = newncpus; |
| 5841 | } |
| 5842 | CPU_SET_S(cpu, setsize, mask); |
| 5843 | } |
| 5844 | Py_CLEAR(iterator); |
| 5845 | |
| 5846 | if (sched_setaffinity(pid, setsize, mask)) { |
| 5847 | posix_error(); |
| 5848 | goto error; |
| 5849 | } |
| 5850 | CPU_FREE(mask); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5851 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5852 | |
| 5853 | error: |
| 5854 | if (mask) |
| 5855 | CPU_FREE(mask); |
| 5856 | Py_XDECREF(iterator); |
| 5857 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5858 | } |
| 5859 | |
| 5860 | PyDoc_STRVAR(posix_sched_getaffinity__doc__, |
| 5861 | "sched_getaffinity(pid, ncpus) -> cpu_set\n\n\ |
| 5862 | Return the affinity of the process with PID *pid*.\n\ |
| 5863 | The returned cpu_set will be of size *ncpus*."); |
| 5864 | |
| 5865 | static PyObject * |
| 5866 | posix_sched_getaffinity(PyObject *self, PyObject *args) |
| 5867 | { |
| 5868 | pid_t pid; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5869 | int cpu, ncpus, count; |
| 5870 | size_t setsize; |
| 5871 | cpu_set_t *mask = NULL; |
| 5872 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5873 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5874 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getaffinity", |
| 5875 | &pid)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5876 | return NULL; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5877 | |
| 5878 | ncpus = NCPUS_START; |
| 5879 | while (1) { |
| 5880 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5881 | mask = CPU_ALLOC(ncpus); |
| 5882 | if (mask == NULL) |
| 5883 | return PyErr_NoMemory(); |
| 5884 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 5885 | break; |
| 5886 | CPU_FREE(mask); |
| 5887 | if (errno != EINVAL) |
| 5888 | return posix_error(); |
| 5889 | if (ncpus > INT_MAX / 2) { |
| 5890 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 5891 | "a large enough CPU set"); |
| 5892 | return NULL; |
| 5893 | } |
| 5894 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5895 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5896 | |
| 5897 | res = PySet_New(NULL); |
| 5898 | if (res == NULL) |
| 5899 | goto error; |
| 5900 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 5901 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 5902 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 5903 | --count; |
| 5904 | if (cpu_num == NULL) |
| 5905 | goto error; |
| 5906 | if (PySet_Add(res, cpu_num)) { |
| 5907 | Py_DECREF(cpu_num); |
| 5908 | goto error; |
| 5909 | } |
| 5910 | Py_DECREF(cpu_num); |
| 5911 | } |
| 5912 | } |
| 5913 | CPU_FREE(mask); |
| 5914 | return res; |
| 5915 | |
| 5916 | error: |
| 5917 | if (mask) |
| 5918 | CPU_FREE(mask); |
| 5919 | Py_XDECREF(res); |
| 5920 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5921 | } |
| 5922 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5923 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5924 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5925 | #endif /* HAVE_SCHED_H */ |
| 5926 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5927 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5928 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5929 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5930 | #define DEV_PTY_FILE "/dev/ptc" |
| 5931 | #define HAVE_DEV_PTMX |
| 5932 | #else |
| 5933 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5934 | #endif |
| 5935 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5936 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5937 | #ifdef HAVE_PTY_H |
| 5938 | #include <pty.h> |
| 5939 | #else |
| 5940 | #ifdef HAVE_LIBUTIL_H |
| 5941 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5942 | #else |
| 5943 | #ifdef HAVE_UTIL_H |
| 5944 | #include <util.h> |
| 5945 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5946 | #endif /* HAVE_LIBUTIL_H */ |
| 5947 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5948 | #ifdef HAVE_STROPTS_H |
| 5949 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5950 | #endif |
| 5951 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5952 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5953 | #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] | 5954 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5955 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5956 | 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] | 5957 | |
| 5958 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5959 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5960 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5961 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5962 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5963 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5964 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5965 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5966 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5967 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5968 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5969 | #endif |
| 5970 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5971 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5972 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5973 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5974 | goto posix_error; |
| 5975 | |
| 5976 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5977 | goto error; |
| 5978 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 5979 | goto error; |
| 5980 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5981 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5982 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5983 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5984 | goto posix_error; |
| 5985 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5986 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5987 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5988 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5989 | if (slave_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5990 | goto posix_error; |
| 5991 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5992 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 5993 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5994 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5995 | goto posix_error; |
| 5996 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5997 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5998 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5999 | /* change permission of slave */ |
| 6000 | if (grantpt(master_fd) < 0) { |
| 6001 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6002 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6003 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6004 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6005 | /* unlock slave */ |
| 6006 | if (unlockpt(master_fd) < 0) { |
| 6007 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6008 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6009 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6010 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6011 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6012 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6013 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 6014 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6015 | goto posix_error; |
| 6016 | |
| 6017 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6018 | if (slave_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6019 | goto posix_error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6020 | |
| 6021 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6022 | goto posix_error; |
| 6023 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6024 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6025 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 6026 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6027 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6028 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6029 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6030 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 6031 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6032 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6033 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6034 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6035 | posix_error: |
| 6036 | posix_error(); |
Victor Stinner | b9981ba | 2013-08-28 01:51:06 +0200 | [diff] [blame] | 6037 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6038 | error: |
Victor Stinner | b9981ba | 2013-08-28 01:51:06 +0200 | [diff] [blame] | 6039 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6040 | if (master_fd != -1) |
| 6041 | close(master_fd); |
| 6042 | if (slave_fd != -1) |
| 6043 | close(slave_fd); |
| 6044 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6045 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6046 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6047 | |
| 6048 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6049 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6050 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6051 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 6052 | 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] | 6053 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6054 | |
| 6055 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6056 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6057 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6058 | int master_fd = -1, result = 0; |
| 6059 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6060 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6061 | _PyImport_AcquireLock(); |
| 6062 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 6063 | if (pid == 0) { |
| 6064 | /* child: this clobbers and resets the import lock. */ |
| 6065 | PyOS_AfterFork(); |
| 6066 | } else { |
| 6067 | /* parent: release the import lock. */ |
| 6068 | result = _PyImport_ReleaseLock(); |
| 6069 | } |
| 6070 | if (pid == -1) |
| 6071 | return posix_error(); |
| 6072 | if (result < 0) { |
| 6073 | /* Don't clobber the OSError if the fork failed. */ |
| 6074 | PyErr_SetString(PyExc_RuntimeError, |
| 6075 | "not holding the import lock"); |
| 6076 | return NULL; |
| 6077 | } |
| 6078 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6079 | } |
| 6080 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6081 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6082 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6083 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6084 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6085 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6086 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6087 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6088 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6089 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6090 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6091 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6092 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6093 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6094 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6095 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6096 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6097 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6098 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6099 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6100 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6101 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6102 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6103 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6104 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6105 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6106 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6107 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6108 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6109 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6110 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6111 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6112 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6113 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6114 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6115 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6116 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6117 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6118 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6119 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6120 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6121 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6122 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6123 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6124 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6125 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6126 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6127 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6128 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6129 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6130 | } |
| 6131 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6132 | #ifdef HAVE_GETGROUPLIST |
| 6133 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 6134 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 6135 | Returns a list of groups to which a user belongs.\n\n\ |
| 6136 | user: username to lookup\n\ |
| 6137 | group: base group id of the user"); |
| 6138 | |
| 6139 | static PyObject * |
| 6140 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 6141 | { |
| 6142 | #ifdef NGROUPS_MAX |
| 6143 | #define MAX_GROUPS NGROUPS_MAX |
| 6144 | #else |
| 6145 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6146 | #define MAX_GROUPS 64 |
| 6147 | #endif |
| 6148 | |
| 6149 | const char *user; |
| 6150 | int i, ngroups; |
| 6151 | PyObject *list; |
| 6152 | #ifdef __APPLE__ |
| 6153 | int *groups, basegid; |
| 6154 | #else |
| 6155 | gid_t *groups, basegid; |
| 6156 | #endif |
| 6157 | ngroups = MAX_GROUPS; |
| 6158 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6159 | #ifdef __APPLE__ |
| 6160 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6161 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6162 | #else |
| 6163 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 6164 | _Py_Gid_Converter, &basegid)) |
| 6165 | return NULL; |
| 6166 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6167 | |
| 6168 | #ifdef __APPLE__ |
| 6169 | groups = PyMem_Malloc(ngroups * sizeof(int)); |
| 6170 | #else |
| 6171 | groups = PyMem_Malloc(ngroups * sizeof(gid_t)); |
| 6172 | #endif |
| 6173 | if (groups == NULL) |
| 6174 | return PyErr_NoMemory(); |
| 6175 | |
| 6176 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 6177 | PyMem_Del(groups); |
| 6178 | return posix_error(); |
| 6179 | } |
| 6180 | |
| 6181 | list = PyList_New(ngroups); |
| 6182 | if (list == NULL) { |
| 6183 | PyMem_Del(groups); |
| 6184 | return NULL; |
| 6185 | } |
| 6186 | |
| 6187 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6188 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6189 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6190 | #else |
| 6191 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 6192 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6193 | if (o == NULL) { |
| 6194 | Py_DECREF(list); |
| 6195 | PyMem_Del(groups); |
| 6196 | return NULL; |
| 6197 | } |
| 6198 | PyList_SET_ITEM(list, i, o); |
| 6199 | } |
| 6200 | |
| 6201 | PyMem_Del(groups); |
| 6202 | |
| 6203 | return list; |
| 6204 | } |
| 6205 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6206 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6207 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6208 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6209 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6210 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6211 | |
| 6212 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6213 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6214 | { |
| 6215 | PyObject *result = NULL; |
| 6216 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6217 | #ifdef NGROUPS_MAX |
| 6218 | #define MAX_GROUPS NGROUPS_MAX |
| 6219 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6220 | /* 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] | 6221 | #define MAX_GROUPS 64 |
| 6222 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6223 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6224 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6225 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6226 | * This is a helper variable to store the intermediate result when |
| 6227 | * that happens. |
| 6228 | * |
| 6229 | * To keep the code readable the OSX behaviour is unconditional, |
| 6230 | * according to the POSIX spec this should be safe on all unix-y |
| 6231 | * systems. |
| 6232 | */ |
| 6233 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6234 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6235 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6236 | #ifdef __APPLE__ |
| 6237 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 6238 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 6239 | * always first call getgroups with length 0 to get the actual number |
| 6240 | * of groups. |
| 6241 | */ |
| 6242 | n = getgroups(0, NULL); |
| 6243 | if (n < 0) { |
| 6244 | return posix_error(); |
| 6245 | } else if (n <= MAX_GROUPS) { |
| 6246 | /* groups will fit in existing array */ |
| 6247 | alt_grouplist = grouplist; |
| 6248 | } else { |
| 6249 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 6250 | if (alt_grouplist == NULL) { |
| 6251 | errno = EINVAL; |
| 6252 | return posix_error(); |
| 6253 | } |
| 6254 | } |
| 6255 | |
| 6256 | n = getgroups(n, alt_grouplist); |
| 6257 | if (n == -1) { |
| 6258 | if (alt_grouplist != grouplist) { |
| 6259 | PyMem_Free(alt_grouplist); |
| 6260 | } |
| 6261 | return posix_error(); |
| 6262 | } |
| 6263 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6264 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6265 | if (n < 0) { |
| 6266 | if (errno == EINVAL) { |
| 6267 | n = getgroups(0, NULL); |
| 6268 | if (n == -1) { |
| 6269 | return posix_error(); |
| 6270 | } |
| 6271 | if (n == 0) { |
| 6272 | /* Avoid malloc(0) */ |
| 6273 | alt_grouplist = grouplist; |
| 6274 | } else { |
| 6275 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 6276 | if (alt_grouplist == NULL) { |
| 6277 | errno = EINVAL; |
| 6278 | return posix_error(); |
| 6279 | } |
| 6280 | n = getgroups(n, alt_grouplist); |
| 6281 | if (n == -1) { |
| 6282 | PyMem_Free(alt_grouplist); |
| 6283 | return posix_error(); |
| 6284 | } |
| 6285 | } |
| 6286 | } else { |
| 6287 | return posix_error(); |
| 6288 | } |
| 6289 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6290 | #endif |
| 6291 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6292 | result = PyList_New(n); |
| 6293 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6294 | int i; |
| 6295 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6296 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6297 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6298 | Py_DECREF(result); |
| 6299 | result = NULL; |
| 6300 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6301 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6302 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6303 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6304 | } |
| 6305 | |
| 6306 | if (alt_grouplist != grouplist) { |
| 6307 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6308 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6309 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6310 | return result; |
| 6311 | } |
| 6312 | #endif |
| 6313 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6314 | #ifdef HAVE_INITGROUPS |
| 6315 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 6316 | "initgroups(username, gid) -> None\n\n\ |
| 6317 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 6318 | the groups of which the specified username is a member, plus the specified\n\ |
| 6319 | group id."); |
| 6320 | |
| 6321 | static PyObject * |
| 6322 | posix_initgroups(PyObject *self, PyObject *args) |
| 6323 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6324 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6325 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6326 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6327 | #ifdef __APPLE__ |
| 6328 | int gid; |
| 6329 | #else |
| 6330 | gid_t gid; |
| 6331 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6332 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6333 | #ifdef __APPLE__ |
| 6334 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 6335 | PyUnicode_FSConverter, &oname, |
| 6336 | &gid)) |
| 6337 | #else |
| 6338 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 6339 | PyUnicode_FSConverter, &oname, |
| 6340 | _Py_Gid_Converter, &gid)) |
| 6341 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6342 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6343 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6344 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6345 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6346 | Py_DECREF(oname); |
| 6347 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6348 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6349 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6350 | Py_INCREF(Py_None); |
| 6351 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6352 | } |
| 6353 | #endif |
| 6354 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6355 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 6356 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6357 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 6358 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6359 | |
| 6360 | static PyObject * |
| 6361 | posix_getpgid(PyObject *self, PyObject *args) |
| 6362 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6363 | pid_t pid, pgid; |
| 6364 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 6365 | return NULL; |
| 6366 | pgid = getpgid(pid); |
| 6367 | if (pgid < 0) |
| 6368 | return posix_error(); |
| 6369 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6370 | } |
| 6371 | #endif /* HAVE_GETPGID */ |
| 6372 | |
| 6373 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6374 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6375 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6376 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6377 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6378 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6379 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6380 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6381 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6382 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6383 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6384 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6385 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6386 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6387 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6388 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6389 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6390 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6391 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6392 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6393 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 6394 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6395 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6396 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6397 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6398 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6399 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6400 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6401 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6402 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6403 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6404 | return posix_error(); |
| 6405 | Py_INCREF(Py_None); |
| 6406 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6407 | } |
| 6408 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6409 | #endif /* HAVE_SETPGRP */ |
| 6410 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6411 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6412 | |
| 6413 | #ifdef MS_WINDOWS |
| 6414 | #include <tlhelp32.h> |
| 6415 | |
| 6416 | static PyObject* |
| 6417 | win32_getppid() |
| 6418 | { |
| 6419 | HANDLE snapshot; |
| 6420 | pid_t mypid; |
| 6421 | PyObject* result = NULL; |
| 6422 | BOOL have_record; |
| 6423 | PROCESSENTRY32 pe; |
| 6424 | |
| 6425 | mypid = getpid(); /* This function never fails */ |
| 6426 | |
| 6427 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 6428 | if (snapshot == INVALID_HANDLE_VALUE) |
| 6429 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 6430 | |
| 6431 | pe.dwSize = sizeof(pe); |
| 6432 | have_record = Process32First(snapshot, &pe); |
| 6433 | while (have_record) { |
| 6434 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 6435 | /* We could cache the ulong value in a static variable. */ |
| 6436 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 6437 | break; |
| 6438 | } |
| 6439 | |
| 6440 | have_record = Process32Next(snapshot, &pe); |
| 6441 | } |
| 6442 | |
| 6443 | /* If our loop exits and our pid was not found (result will be NULL) |
| 6444 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 6445 | * error anyway, so let's raise it. */ |
| 6446 | if (!result) |
| 6447 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6448 | |
| 6449 | CloseHandle(snapshot); |
| 6450 | |
| 6451 | return result; |
| 6452 | } |
| 6453 | #endif /*MS_WINDOWS*/ |
| 6454 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6455 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6456 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6457 | Return the parent's process id. If the parent process has already exited,\n\ |
| 6458 | Windows machines will still return its id; others systems will return the id\n\ |
| 6459 | of the 'init' process (1)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6460 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6461 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6462 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6463 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6464 | #ifdef MS_WINDOWS |
| 6465 | return win32_getppid(); |
| 6466 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6467 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6468 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6469 | } |
| 6470 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6471 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6472 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6473 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6474 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6475 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6476 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6477 | |
| 6478 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6479 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6480 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6481 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6482 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6483 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6484 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6485 | |
| 6486 | if (GetUserNameW(user_name, &num_chars)) { |
| 6487 | /* num_chars is the number of unicode chars plus null terminator */ |
| 6488 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6489 | } |
| 6490 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6491 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6492 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6493 | char *name; |
| 6494 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6495 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6496 | errno = 0; |
| 6497 | name = getlogin(); |
| 6498 | if (name == NULL) { |
| 6499 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6500 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6501 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6502 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6503 | } |
| 6504 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6505 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6506 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6507 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6508 | return result; |
| 6509 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6510 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6511 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6512 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6513 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6514 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6515 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6516 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6517 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6518 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6519 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6520 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6521 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6522 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6523 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6524 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6525 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6526 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6527 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6528 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6529 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6530 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6531 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6532 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6533 | pid_t pid; |
| 6534 | int sig; |
| 6535 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 6536 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6537 | if (kill(pid, sig) == -1) |
| 6538 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6539 | Py_INCREF(Py_None); |
| 6540 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6541 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6542 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6543 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6544 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6545 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6546 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6547 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6548 | |
| 6549 | static PyObject * |
| 6550 | posix_killpg(PyObject *self, PyObject *args) |
| 6551 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6552 | int sig; |
| 6553 | pid_t pgid; |
| 6554 | /* XXX some man pages make the `pgid` parameter an int, others |
| 6555 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 6556 | take the same type. Moreover, pid_t is always at least as wide as |
| 6557 | int (else compilation of this module fails), which is safe. */ |
| 6558 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 6559 | return NULL; |
| 6560 | if (killpg(pgid, sig) == -1) |
| 6561 | return posix_error(); |
| 6562 | Py_INCREF(Py_None); |
| 6563 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6564 | } |
| 6565 | #endif |
| 6566 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6567 | #ifdef MS_WINDOWS |
| 6568 | PyDoc_STRVAR(win32_kill__doc__, |
| 6569 | "kill(pid, sig)\n\n\ |
| 6570 | Kill a process with a signal."); |
| 6571 | |
| 6572 | static PyObject * |
| 6573 | win32_kill(PyObject *self, PyObject *args) |
| 6574 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 6575 | PyObject *result; |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6576 | pid_t pid; |
| 6577 | DWORD sig, err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6578 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6579 | |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6580 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "k:kill", &pid, &sig)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6581 | return NULL; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6582 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6583 | /* Console processes which share a common console can be sent CTRL+C or |
| 6584 | CTRL+BREAK events, provided they handle said events. */ |
| 6585 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6586 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6587 | err = GetLastError(); |
| 6588 | PyErr_SetFromWindowsErr(err); |
| 6589 | } |
| 6590 | else |
| 6591 | Py_RETURN_NONE; |
| 6592 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6593 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6594 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 6595 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6596 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6597 | if (handle == NULL) { |
| 6598 | err = GetLastError(); |
| 6599 | return PyErr_SetFromWindowsErr(err); |
| 6600 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6601 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6602 | if (TerminateProcess(handle, sig) == 0) { |
| 6603 | err = GetLastError(); |
| 6604 | result = PyErr_SetFromWindowsErr(err); |
| 6605 | } else { |
| 6606 | Py_INCREF(Py_None); |
| 6607 | result = Py_None; |
| 6608 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6609 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6610 | CloseHandle(handle); |
| 6611 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6612 | } |
| 6613 | #endif /* MS_WINDOWS */ |
| 6614 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6615 | #ifdef HAVE_PLOCK |
| 6616 | |
| 6617 | #ifdef HAVE_SYS_LOCK_H |
| 6618 | #include <sys/lock.h> |
| 6619 | #endif |
| 6620 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6621 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6622 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6623 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6624 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6625 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6626 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6627 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6628 | int op; |
| 6629 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 6630 | return NULL; |
| 6631 | if (plock(op) == -1) |
| 6632 | return posix_error(); |
| 6633 | Py_INCREF(Py_None); |
| 6634 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6635 | } |
| 6636 | #endif |
| 6637 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6638 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6639 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6640 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6641 | Set the current process's user id."); |
| 6642 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6643 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6644 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6645 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6646 | uid_t uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6647 | if (!PyArg_ParseTuple(args, "O&:setuid", _Py_Uid_Converter, &uid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6648 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6649 | if (setuid(uid) < 0) |
| 6650 | return posix_error(); |
| 6651 | Py_INCREF(Py_None); |
| 6652 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6653 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6654 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6655 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6656 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6657 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6658 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6659 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6660 | Set the current process's effective user id."); |
| 6661 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6662 | static PyObject * |
| 6663 | posix_seteuid (PyObject *self, PyObject *args) |
| 6664 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6665 | uid_t euid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6666 | if (!PyArg_ParseTuple(args, "O&:seteuid", _Py_Uid_Converter, &euid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6667 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6668 | if (seteuid(euid) < 0) { |
| 6669 | return posix_error(); |
| 6670 | } else { |
| 6671 | Py_INCREF(Py_None); |
| 6672 | return Py_None; |
| 6673 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6674 | } |
| 6675 | #endif /* HAVE_SETEUID */ |
| 6676 | |
| 6677 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6678 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6679 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6680 | Set the current process's effective group id."); |
| 6681 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6682 | static PyObject * |
| 6683 | posix_setegid (PyObject *self, PyObject *args) |
| 6684 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6685 | gid_t egid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6686 | if (!PyArg_ParseTuple(args, "O&:setegid", _Py_Gid_Converter, &egid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6687 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6688 | if (setegid(egid) < 0) { |
| 6689 | return posix_error(); |
| 6690 | } else { |
| 6691 | Py_INCREF(Py_None); |
| 6692 | return Py_None; |
| 6693 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6694 | } |
| 6695 | #endif /* HAVE_SETEGID */ |
| 6696 | |
| 6697 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6698 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6699 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6700 | Set the current process's real and effective user ids."); |
| 6701 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6702 | static PyObject * |
| 6703 | posix_setreuid (PyObject *self, PyObject *args) |
| 6704 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6705 | uid_t ruid, euid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6706 | if (!PyArg_ParseTuple(args, "O&O&:setreuid", |
| 6707 | _Py_Uid_Converter, &ruid, |
| 6708 | _Py_Uid_Converter, &euid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6709 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6710 | if (setreuid(ruid, euid) < 0) { |
| 6711 | return posix_error(); |
| 6712 | } else { |
| 6713 | Py_INCREF(Py_None); |
| 6714 | return Py_None; |
| 6715 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6716 | } |
| 6717 | #endif /* HAVE_SETREUID */ |
| 6718 | |
| 6719 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6720 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6721 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6722 | Set the current process's real and effective group ids."); |
| 6723 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6724 | static PyObject * |
| 6725 | posix_setregid (PyObject *self, PyObject *args) |
| 6726 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6727 | gid_t rgid, egid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6728 | if (!PyArg_ParseTuple(args, "O&O&:setregid", |
| 6729 | _Py_Gid_Converter, &rgid, |
| 6730 | _Py_Gid_Converter, &egid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6731 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6732 | if (setregid(rgid, egid) < 0) { |
| 6733 | return posix_error(); |
| 6734 | } else { |
| 6735 | Py_INCREF(Py_None); |
| 6736 | return Py_None; |
| 6737 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6738 | } |
| 6739 | #endif /* HAVE_SETREGID */ |
| 6740 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6741 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6742 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6743 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6744 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6745 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6746 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6747 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6748 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6749 | gid_t gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6750 | if (!PyArg_ParseTuple(args, "O&:setgid", _Py_Gid_Converter, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6751 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6752 | if (setgid(gid) < 0) |
| 6753 | return posix_error(); |
| 6754 | Py_INCREF(Py_None); |
| 6755 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6756 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6757 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6758 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6759 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6760 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6761 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6762 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6763 | |
| 6764 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6765 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6766 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6767 | int i, len; |
| 6768 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6769 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6770 | if (!PySequence_Check(groups)) { |
| 6771 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6772 | return NULL; |
| 6773 | } |
| 6774 | len = PySequence_Size(groups); |
| 6775 | if (len > MAX_GROUPS) { |
| 6776 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6777 | return NULL; |
| 6778 | } |
| 6779 | for(i = 0; i < len; i++) { |
| 6780 | PyObject *elem; |
| 6781 | elem = PySequence_GetItem(groups, i); |
| 6782 | if (!elem) |
| 6783 | return NULL; |
| 6784 | if (!PyLong_Check(elem)) { |
| 6785 | PyErr_SetString(PyExc_TypeError, |
| 6786 | "groups must be integers"); |
| 6787 | Py_DECREF(elem); |
| 6788 | return NULL; |
| 6789 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6790 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6791 | Py_DECREF(elem); |
| 6792 | return NULL; |
| 6793 | } |
| 6794 | } |
| 6795 | Py_DECREF(elem); |
| 6796 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6797 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6798 | if (setgroups(len, grouplist) < 0) |
| 6799 | return posix_error(); |
| 6800 | Py_INCREF(Py_None); |
| 6801 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6802 | } |
| 6803 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6804 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6805 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6806 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6807 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6808 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6809 | PyObject *result; |
| 6810 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6811 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6812 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6813 | if (pid == -1) |
| 6814 | return posix_error(); |
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 (struct_rusage == NULL) { |
| 6817 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6818 | if (m == NULL) |
| 6819 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6820 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6821 | Py_DECREF(m); |
| 6822 | if (struct_rusage == NULL) |
| 6823 | return NULL; |
| 6824 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6825 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6826 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6827 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6828 | if (!result) |
| 6829 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6830 | |
| 6831 | #ifndef doubletime |
| 6832 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6833 | #endif |
| 6834 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6835 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6836 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6837 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6838 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6839 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6840 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6841 | SET_INT(result, 2, ru->ru_maxrss); |
| 6842 | SET_INT(result, 3, ru->ru_ixrss); |
| 6843 | SET_INT(result, 4, ru->ru_idrss); |
| 6844 | SET_INT(result, 5, ru->ru_isrss); |
| 6845 | SET_INT(result, 6, ru->ru_minflt); |
| 6846 | SET_INT(result, 7, ru->ru_majflt); |
| 6847 | SET_INT(result, 8, ru->ru_nswap); |
| 6848 | SET_INT(result, 9, ru->ru_inblock); |
| 6849 | SET_INT(result, 10, ru->ru_oublock); |
| 6850 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6851 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6852 | SET_INT(result, 13, ru->ru_nsignals); |
| 6853 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6854 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6855 | #undef SET_INT |
| 6856 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6857 | if (PyErr_Occurred()) { |
| 6858 | Py_DECREF(result); |
| 6859 | return NULL; |
| 6860 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6861 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6862 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6863 | } |
| 6864 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6865 | |
| 6866 | #ifdef HAVE_WAIT3 |
| 6867 | PyDoc_STRVAR(posix_wait3__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6868 | "wait3(options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6869 | Wait for completion of a child process."); |
| 6870 | |
| 6871 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6872 | posix_wait3(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6873 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6874 | pid_t pid; |
| 6875 | int options; |
| 6876 | struct rusage ru; |
| 6877 | WAIT_TYPE status; |
| 6878 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6879 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6880 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6881 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6882 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6883 | Py_BEGIN_ALLOW_THREADS |
| 6884 | pid = wait3(&status, options, &ru); |
| 6885 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6886 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6887 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6888 | } |
| 6889 | #endif /* HAVE_WAIT3 */ |
| 6890 | |
| 6891 | #ifdef HAVE_WAIT4 |
| 6892 | PyDoc_STRVAR(posix_wait4__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6893 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6894 | Wait for completion of a given child process."); |
| 6895 | |
| 6896 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6897 | posix_wait4(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6898 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6899 | pid_t pid; |
| 6900 | int options; |
| 6901 | struct rusage ru; |
| 6902 | WAIT_TYPE status; |
| 6903 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6904 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6905 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6906 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6907 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6908 | Py_BEGIN_ALLOW_THREADS |
| 6909 | pid = wait4(pid, &status, options, &ru); |
| 6910 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6911 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6912 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6913 | } |
| 6914 | #endif /* HAVE_WAIT4 */ |
| 6915 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6916 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 6917 | PyDoc_STRVAR(posix_waitid__doc__, |
| 6918 | "waitid(idtype, id, options) -> waitid_result\n\n\ |
| 6919 | Wait for the completion of one or more child processes.\n\n\ |
| 6920 | idtype can be P_PID, P_PGID or P_ALL.\n\ |
| 6921 | id specifies the pid to wait on.\n\ |
| 6922 | options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\ |
| 6923 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\ |
| 6924 | Returns either waitid_result or None if WNOHANG is specified and there are\n\ |
| 6925 | no children in a waitable state."); |
| 6926 | |
| 6927 | static PyObject * |
| 6928 | posix_waitid(PyObject *self, PyObject *args) |
| 6929 | { |
| 6930 | PyObject *result; |
| 6931 | idtype_t idtype; |
| 6932 | id_t id; |
| 6933 | int options, res; |
| 6934 | siginfo_t si; |
| 6935 | si.si_pid = 0; |
| 6936 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options)) |
| 6937 | return NULL; |
| 6938 | Py_BEGIN_ALLOW_THREADS |
| 6939 | res = waitid(idtype, id, &si, options); |
| 6940 | Py_END_ALLOW_THREADS |
| 6941 | if (res == -1) |
| 6942 | return posix_error(); |
| 6943 | |
| 6944 | if (si.si_pid == 0) |
| 6945 | Py_RETURN_NONE; |
| 6946 | |
| 6947 | result = PyStructSequence_New(&WaitidResultType); |
| 6948 | if (!result) |
| 6949 | return NULL; |
| 6950 | |
| 6951 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6952 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6953 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6954 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6955 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6956 | if (PyErr_Occurred()) { |
| 6957 | Py_DECREF(result); |
| 6958 | return NULL; |
| 6959 | } |
| 6960 | |
| 6961 | return result; |
| 6962 | } |
| 6963 | #endif |
| 6964 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6965 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6966 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6967 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6968 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6969 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6970 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6971 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6972 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6973 | pid_t pid; |
| 6974 | int options; |
| 6975 | WAIT_TYPE status; |
| 6976 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6977 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6978 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6979 | return NULL; |
| 6980 | Py_BEGIN_ALLOW_THREADS |
| 6981 | pid = waitpid(pid, &status, options); |
| 6982 | Py_END_ALLOW_THREADS |
| 6983 | if (pid == -1) |
| 6984 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6985 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6986 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6987 | } |
| 6988 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6989 | #elif defined(HAVE_CWAIT) |
| 6990 | |
| 6991 | /* 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] | 6992 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6993 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6994 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6995 | |
| 6996 | static PyObject * |
| 6997 | posix_waitpid(PyObject *self, PyObject *args) |
| 6998 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6999 | Py_intptr_t pid; |
| 7000 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7001 | |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7002 | if (!PyArg_ParseTuple(args, _Py_PARSE_INTPTR "i:waitpid", &pid, &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7003 | return NULL; |
| 7004 | Py_BEGIN_ALLOW_THREADS |
| 7005 | pid = _cwait(&status, pid, options); |
| 7006 | Py_END_ALLOW_THREADS |
| 7007 | if (pid == -1) |
| 7008 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7009 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7010 | /* 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] | 7011 | return Py_BuildValue(_Py_PARSE_INTPTR "i", pid, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7012 | } |
| 7013 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7014 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7015 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7016 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7017 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7018 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7019 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7020 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7021 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7022 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7023 | pid_t pid; |
| 7024 | WAIT_TYPE status; |
| 7025 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7026 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7027 | Py_BEGIN_ALLOW_THREADS |
| 7028 | pid = wait(&status); |
| 7029 | Py_END_ALLOW_THREADS |
| 7030 | if (pid == -1) |
| 7031 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7032 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7033 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7034 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7035 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7036 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7037 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7038 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
| 7039 | PyDoc_STRVAR(readlink__doc__, |
| 7040 | "readlink(path, *, dir_fd=None) -> path\n\n\ |
| 7041 | Return a string representing the path to which the symbolic link points.\n\ |
| 7042 | \n\ |
| 7043 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7044 | and path should be relative; path will then be relative to that directory.\n\ |
| 7045 | dir_fd may not be implemented on your platform.\n\ |
| 7046 | If it is unavailable, using it will raise a NotImplementedError."); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7047 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7048 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7049 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7050 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7051 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7052 | posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7053 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7054 | path_t path; |
| 7055 | int dir_fd = DEFAULT_DIR_FD; |
| 7056 | char buffer[MAXPATHLEN]; |
| 7057 | ssize_t length; |
| 7058 | PyObject *return_value = NULL; |
| 7059 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7060 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7061 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7062 | path.function_name = "readlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7063 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords, |
| 7064 | path_converter, &path, |
| 7065 | #ifdef HAVE_READLINKAT |
| 7066 | dir_fd_converter, &dir_fd |
| 7067 | #else |
| 7068 | dir_fd_unavailable, &dir_fd |
| 7069 | #endif |
| 7070 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7071 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7072 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7073 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7074 | #ifdef HAVE_READLINKAT |
| 7075 | if (dir_fd != DEFAULT_DIR_FD) |
| 7076 | length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer)); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 7077 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7078 | #endif |
| 7079 | length = readlink(path.narrow, buffer, sizeof(buffer)); |
| 7080 | Py_END_ALLOW_THREADS |
| 7081 | |
| 7082 | if (length < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7083 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7084 | goto exit; |
| 7085 | } |
| 7086 | |
| 7087 | if (PyUnicode_Check(path.object)) |
| 7088 | return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 7089 | else |
| 7090 | return_value = PyBytes_FromStringAndSize(buffer, length); |
| 7091 | exit: |
| 7092 | path_cleanup(&path); |
| 7093 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7094 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7095 | |
| 7096 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7097 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7098 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7099 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7100 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7101 | PyDoc_STRVAR(posix_symlink__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7102 | "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 7103 | Create a symbolic link pointing to src named dst.\n\n\ |
| 7104 | target_is_directory is required on Windows if the target is to be\n\ |
| 7105 | interpreted as a directory. (On Windows, symlink requires\n\ |
| 7106 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.)\n\ |
| 7107 | target_is_directory is ignored on non-Windows platforms.\n\ |
| 7108 | \n\ |
| 7109 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7110 | and path should be relative; path will then be relative to that directory.\n\ |
| 7111 | dir_fd may not be implemented on your platform.\n\ |
| 7112 | If it is unavailable, using it will raise a NotImplementedError."); |
| 7113 | |
| 7114 | #if defined(MS_WINDOWS) |
| 7115 | |
| 7116 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 7117 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD) = NULL; |
| 7118 | static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPSTR, LPSTR, DWORD) = NULL; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7119 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7120 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7121 | check_CreateSymbolicLink(void) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7122 | { |
| 7123 | HINSTANCE hKernel32; |
| 7124 | /* only recheck */ |
| 7125 | if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA) |
| 7126 | return 1; |
| 7127 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 7128 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 7129 | "CreateSymbolicLinkW"); |
| 7130 | *(FARPROC*)&Py_CreateSymbolicLinkA = GetProcAddress(hKernel32, |
| 7131 | "CreateSymbolicLinkA"); |
| 7132 | return (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA); |
| 7133 | } |
| 7134 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7135 | /* Remove the last portion of the path */ |
| 7136 | static void |
| 7137 | _dirnameW(WCHAR *path) |
| 7138 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7139 | WCHAR *ptr; |
| 7140 | |
| 7141 | /* walk the path from the end until a backslash is encountered */ |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7142 | for(ptr = path + wcslen(path); ptr != path; ptr--) { |
Victor Stinner | 072318b | 2013-06-05 02:07:46 +0200 | [diff] [blame] | 7143 | if (*ptr == L'\\' || *ptr == L'/') |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7144 | break; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7145 | } |
| 7146 | *ptr = 0; |
| 7147 | } |
| 7148 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7149 | /* Remove the last portion of the path */ |
| 7150 | static void |
| 7151 | _dirnameA(char *path) |
| 7152 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7153 | char *ptr; |
| 7154 | |
| 7155 | /* walk the path from the end until a backslash is encountered */ |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7156 | for(ptr = path + strlen(path); ptr != path; ptr--) { |
| 7157 | if (*ptr == '\\' || *ptr == '/') |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7158 | break; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7159 | } |
| 7160 | *ptr = 0; |
| 7161 | } |
| 7162 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7163 | /* Is this path absolute? */ |
| 7164 | static int |
| 7165 | _is_absW(const WCHAR *path) |
| 7166 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7167 | return path[0] == L'\\' || path[0] == L'/' || path[1] == L':'; |
| 7168 | |
| 7169 | } |
| 7170 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7171 | /* Is this path absolute? */ |
| 7172 | static int |
| 7173 | _is_absA(const char *path) |
| 7174 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7175 | return path[0] == '\\' || path[0] == '/' || path[1] == ':'; |
| 7176 | |
| 7177 | } |
| 7178 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7179 | /* join root and rest with a backslash */ |
| 7180 | static void |
| 7181 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 7182 | { |
Victor Stinner | e7e7eba | 2013-06-05 00:35:54 +0200 | [diff] [blame] | 7183 | size_t root_len; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7184 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7185 | if (_is_absW(rest)) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7186 | wcscpy(dest_path, rest); |
| 7187 | return; |
| 7188 | } |
| 7189 | |
| 7190 | root_len = wcslen(root); |
| 7191 | |
| 7192 | wcscpy(dest_path, root); |
| 7193 | if(root_len) { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7194 | dest_path[root_len] = L'\\'; |
| 7195 | root_len++; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7196 | } |
| 7197 | wcscpy(dest_path+root_len, rest); |
| 7198 | } |
| 7199 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7200 | /* join root and rest with a backslash */ |
| 7201 | static void |
| 7202 | _joinA(char *dest_path, const char *root, const char *rest) |
| 7203 | { |
Victor Stinner | e7e7eba | 2013-06-05 00:35:54 +0200 | [diff] [blame] | 7204 | size_t root_len; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7205 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7206 | if (_is_absA(rest)) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7207 | strcpy(dest_path, rest); |
| 7208 | return; |
| 7209 | } |
| 7210 | |
| 7211 | root_len = strlen(root); |
| 7212 | |
| 7213 | strcpy(dest_path, root); |
| 7214 | if(root_len) { |
| 7215 | dest_path[root_len] = '\\'; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7216 | root_len++; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7217 | } |
| 7218 | strcpy(dest_path+root_len, rest); |
| 7219 | } |
| 7220 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7221 | /* Return True if the path at src relative to dest is a directory */ |
| 7222 | static int |
| 7223 | _check_dirW(WCHAR *src, WCHAR *dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7224 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7225 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7226 | WCHAR dest_parent[MAX_PATH]; |
| 7227 | WCHAR src_resolved[MAX_PATH] = L""; |
| 7228 | |
| 7229 | /* dest_parent = os.path.dirname(dest) */ |
| 7230 | wcscpy(dest_parent, dest); |
| 7231 | _dirnameW(dest_parent); |
| 7232 | /* src_resolved = os.path.join(dest_parent, src) */ |
| 7233 | _joinW(src_resolved, dest_parent, src); |
| 7234 | return ( |
| 7235 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 7236 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7237 | ); |
| 7238 | } |
| 7239 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7240 | /* Return True if the path at src relative to dest is a directory */ |
| 7241 | static int |
| 7242 | _check_dirA(char *src, char *dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7243 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7244 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7245 | char dest_parent[MAX_PATH]; |
| 7246 | char src_resolved[MAX_PATH] = ""; |
| 7247 | |
| 7248 | /* dest_parent = os.path.dirname(dest) */ |
| 7249 | strcpy(dest_parent, dest); |
Victor Stinner | 5a43676 | 2013-06-05 00:37:12 +0200 | [diff] [blame] | 7250 | _dirnameA(dest_parent); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7251 | /* src_resolved = os.path.join(dest_parent, src) */ |
Victor Stinner | 5a43676 | 2013-06-05 00:37:12 +0200 | [diff] [blame] | 7252 | _joinA(src_resolved, dest_parent, src); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7253 | return ( |
| 7254 | GetFileAttributesExA(src_resolved, GetFileExInfoStandard, &src_info) |
| 7255 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7256 | ); |
| 7257 | } |
| 7258 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7259 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7260 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7261 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7262 | posix_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7263 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7264 | path_t src; |
| 7265 | path_t dst; |
| 7266 | int dir_fd = DEFAULT_DIR_FD; |
| 7267 | int target_is_directory = 0; |
| 7268 | static char *keywords[] = {"src", "dst", "target_is_directory", |
| 7269 | "dir_fd", NULL}; |
| 7270 | PyObject *return_value; |
| 7271 | #ifdef MS_WINDOWS |
| 7272 | DWORD result; |
| 7273 | #else |
| 7274 | int result; |
| 7275 | #endif |
| 7276 | |
| 7277 | memset(&src, 0, sizeof(src)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7278 | src.function_name = "symlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7279 | src.argument_name = "src"; |
| 7280 | memset(&dst, 0, sizeof(dst)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7281 | dst.function_name = "symlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7282 | dst.argument_name = "dst"; |
| 7283 | |
| 7284 | #ifdef MS_WINDOWS |
| 7285 | if (!check_CreateSymbolicLink()) { |
| 7286 | PyErr_SetString(PyExc_NotImplementedError, |
| 7287 | "CreateSymbolicLink functions not found"); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7288 | return NULL; |
| 7289 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7290 | if (!win32_can_symlink) { |
| 7291 | PyErr_SetString(PyExc_OSError, "symbolic link privilege not held"); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7292 | return NULL; |
| 7293 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7294 | #endif |
| 7295 | |
| 7296 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|i$O&:symlink", |
| 7297 | keywords, |
| 7298 | path_converter, &src, |
| 7299 | path_converter, &dst, |
| 7300 | &target_is_directory, |
| 7301 | #ifdef HAVE_SYMLINKAT |
| 7302 | dir_fd_converter, &dir_fd |
| 7303 | #else |
| 7304 | dir_fd_unavailable, &dir_fd |
| 7305 | #endif |
| 7306 | )) |
| 7307 | return NULL; |
| 7308 | |
| 7309 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 7310 | PyErr_SetString(PyExc_ValueError, |
| 7311 | "symlink: src and dst must be the same type"); |
| 7312 | return_value = NULL; |
| 7313 | goto exit; |
| 7314 | } |
| 7315 | |
| 7316 | #ifdef MS_WINDOWS |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7317 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7318 | Py_BEGIN_ALLOW_THREADS |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7319 | if (dst.wide) { |
| 7320 | /* if src is a directory, ensure target_is_directory==1 */ |
| 7321 | target_is_directory |= _check_dirW(src.wide, dst.wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7322 | result = Py_CreateSymbolicLinkW(dst.wide, src.wide, |
| 7323 | target_is_directory); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7324 | } |
| 7325 | else { |
| 7326 | /* if src is a directory, ensure target_is_directory==1 */ |
| 7327 | target_is_directory |= _check_dirA(src.narrow, dst.narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7328 | result = Py_CreateSymbolicLinkA(dst.narrow, src.narrow, |
| 7329 | target_is_directory); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7330 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7331 | Py_END_ALLOW_THREADS |
| 7332 | |
| 7333 | if (!result) { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 7334 | return_value = path_error2(&src, &dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7335 | goto exit; |
| 7336 | } |
| 7337 | |
| 7338 | #else |
| 7339 | |
| 7340 | Py_BEGIN_ALLOW_THREADS |
| 7341 | #if HAVE_SYMLINKAT |
| 7342 | if (dir_fd != DEFAULT_DIR_FD) |
| 7343 | result = symlinkat(src.narrow, dir_fd, dst.narrow); |
| 7344 | else |
| 7345 | #endif |
| 7346 | result = symlink(src.narrow, dst.narrow); |
| 7347 | Py_END_ALLOW_THREADS |
| 7348 | |
| 7349 | if (result) { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 7350 | return_value = path_error2(&src, &dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7351 | goto exit; |
| 7352 | } |
| 7353 | #endif |
| 7354 | |
| 7355 | return_value = Py_None; |
| 7356 | Py_INCREF(Py_None); |
| 7357 | goto exit; /* silence "unused label" warning */ |
| 7358 | exit: |
| 7359 | path_cleanup(&src); |
| 7360 | path_cleanup(&dst); |
| 7361 | return return_value; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7362 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7363 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7364 | #endif /* HAVE_SYMLINK */ |
| 7365 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7366 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7367 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 7368 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7369 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7370 | win_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7371 | { |
| 7372 | wchar_t *path; |
| 7373 | DWORD n_bytes_returned; |
| 7374 | DWORD io_result; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7375 | PyObject *po, *result; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7376 | int dir_fd; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7377 | HANDLE reparse_point_handle; |
| 7378 | |
| 7379 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 7380 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 7381 | wchar_t *print_name; |
| 7382 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7383 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 7384 | |
| 7385 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords, |
| 7386 | &po, |
| 7387 | dir_fd_unavailable, &dir_fd |
| 7388 | )) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7389 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7390 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7391 | path = PyUnicode_AsUnicode(po); |
| 7392 | if (path == NULL) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7393 | return NULL; |
| 7394 | |
| 7395 | /* First get a handle to the reparse point */ |
| 7396 | Py_BEGIN_ALLOW_THREADS |
| 7397 | reparse_point_handle = CreateFileW( |
| 7398 | path, |
| 7399 | 0, |
| 7400 | 0, |
| 7401 | 0, |
| 7402 | OPEN_EXISTING, |
| 7403 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 7404 | 0); |
| 7405 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7406 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7407 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7408 | return win32_error_object("readlink", po); |
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 | Py_BEGIN_ALLOW_THREADS |
| 7411 | /* New call DeviceIoControl to read the reparse point */ |
| 7412 | io_result = DeviceIoControl( |
| 7413 | reparse_point_handle, |
| 7414 | FSCTL_GET_REPARSE_POINT, |
| 7415 | 0, 0, /* in buffer */ |
| 7416 | target_buffer, sizeof(target_buffer), |
| 7417 | &n_bytes_returned, |
| 7418 | 0 /* we're not using OVERLAPPED_IO */ |
| 7419 | ); |
| 7420 | CloseHandle(reparse_point_handle); |
| 7421 | Py_END_ALLOW_THREADS |
| 7422 | |
| 7423 | if (io_result==0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7424 | return win32_error_object("readlink", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7425 | |
| 7426 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 7427 | { |
| 7428 | PyErr_SetString(PyExc_ValueError, |
| 7429 | "not a symbolic link"); |
| 7430 | return NULL; |
| 7431 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 7432 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7433 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 7434 | |
| 7435 | result = PyUnicode_FromWideChar(print_name, |
| 7436 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7437 | return result; |
| 7438 | } |
| 7439 | |
| 7440 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 7441 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7442 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7443 | static PyStructSequence_Field times_result_fields[] = { |
| 7444 | {"user", "user time"}, |
| 7445 | {"system", "system time"}, |
| 7446 | {"children_user", "user time of children"}, |
| 7447 | {"children_system", "system time of children"}, |
| 7448 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 7449 | {NULL} |
| 7450 | }; |
| 7451 | |
| 7452 | PyDoc_STRVAR(times_result__doc__, |
| 7453 | "times_result: Result from os.times().\n\n\ |
| 7454 | This object may be accessed either as a tuple of\n\ |
| 7455 | (user, system, children_user, children_system, elapsed),\n\ |
| 7456 | or via the attributes user, system, children_user, children_system,\n\ |
| 7457 | and elapsed.\n\ |
| 7458 | \n\ |
| 7459 | See os.times for more information."); |
| 7460 | |
| 7461 | static PyStructSequence_Desc times_result_desc = { |
| 7462 | "times_result", /* name */ |
| 7463 | times_result__doc__, /* doc */ |
| 7464 | times_result_fields, |
| 7465 | 5 |
| 7466 | }; |
| 7467 | |
| 7468 | static PyTypeObject TimesResultType; |
| 7469 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7470 | #ifdef MS_WINDOWS |
| 7471 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 7472 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7473 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7474 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7475 | |
| 7476 | static PyObject * |
| 7477 | build_times_result(double user, double system, |
| 7478 | double children_user, double children_system, |
| 7479 | double elapsed) |
| 7480 | { |
| 7481 | PyObject *value = PyStructSequence_New(&TimesResultType); |
| 7482 | if (value == NULL) |
| 7483 | return NULL; |
| 7484 | |
| 7485 | #define SET(i, field) \ |
| 7486 | { \ |
| 7487 | PyObject *o = PyFloat_FromDouble(field); \ |
| 7488 | if (!o) { \ |
| 7489 | Py_DECREF(value); \ |
| 7490 | return NULL; \ |
| 7491 | } \ |
| 7492 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 7493 | } \ |
| 7494 | |
| 7495 | SET(0, user); |
| 7496 | SET(1, system); |
| 7497 | SET(2, children_user); |
| 7498 | SET(3, children_system); |
| 7499 | SET(4, elapsed); |
| 7500 | |
| 7501 | #undef SET |
| 7502 | |
| 7503 | return value; |
| 7504 | } |
| 7505 | |
| 7506 | PyDoc_STRVAR(posix_times__doc__, |
| 7507 | "times() -> times_result\n\n\ |
| 7508 | Return an object containing floating point numbers indicating process\n\ |
| 7509 | times. The object behaves like a named tuple with these fields:\n\ |
| 7510 | (utime, stime, cutime, cstime, elapsed_time)"); |
| 7511 | |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 7512 | #if defined(MS_WINDOWS) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7513 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7514 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7515 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7516 | FILETIME create, exit, kernel, user; |
| 7517 | HANDLE hProc; |
| 7518 | hProc = GetCurrentProcess(); |
| 7519 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 7520 | /* The fields of a FILETIME structure are the hi and lo part |
| 7521 | of a 64-bit value expressed in 100 nanosecond units. |
| 7522 | 1e7 is one second in such units; 1e-7 the inverse. |
| 7523 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 7524 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7525 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7526 | (double)(user.dwHighDateTime*429.4967296 + |
| 7527 | user.dwLowDateTime*1e-7), |
| 7528 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 7529 | kernel.dwLowDateTime*1e-7), |
| 7530 | (double)0, |
| 7531 | (double)0, |
| 7532 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7533 | } |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 7534 | #else /* Not Windows */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7535 | #define NEED_TICKS_PER_SECOND |
| 7536 | static long ticks_per_second = -1; |
| 7537 | static PyObject * |
| 7538 | posix_times(PyObject *self, PyObject *noargs) |
| 7539 | { |
| 7540 | struct tms t; |
| 7541 | clock_t c; |
| 7542 | errno = 0; |
| 7543 | c = times(&t); |
| 7544 | if (c == (clock_t) -1) |
| 7545 | return posix_error(); |
| 7546 | return build_times_result( |
| 7547 | (double)t.tms_utime / ticks_per_second, |
| 7548 | (double)t.tms_stime / ticks_per_second, |
| 7549 | (double)t.tms_cutime / ticks_per_second, |
| 7550 | (double)t.tms_cstime / ticks_per_second, |
| 7551 | (double)c / ticks_per_second); |
| 7552 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7553 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7554 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7555 | #endif /* HAVE_TIMES */ |
| 7556 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7557 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7558 | #ifdef HAVE_GETSID |
| 7559 | PyDoc_STRVAR(posix_getsid__doc__, |
| 7560 | "getsid(pid) -> sid\n\n\ |
| 7561 | Call the system call getsid()."); |
| 7562 | |
| 7563 | static PyObject * |
| 7564 | posix_getsid(PyObject *self, PyObject *args) |
| 7565 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7566 | pid_t pid; |
| 7567 | int sid; |
| 7568 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 7569 | return NULL; |
| 7570 | sid = getsid(pid); |
| 7571 | if (sid < 0) |
| 7572 | return posix_error(); |
| 7573 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7574 | } |
| 7575 | #endif /* HAVE_GETSID */ |
| 7576 | |
| 7577 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7578 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7579 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7580 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7581 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7582 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7583 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7584 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7585 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7586 | if (setsid() < 0) |
| 7587 | return posix_error(); |
| 7588 | Py_INCREF(Py_None); |
| 7589 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7590 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7591 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7592 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7593 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7594 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7595 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7596 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7597 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7598 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7599 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7600 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7601 | pid_t pid; |
| 7602 | int pgrp; |
| 7603 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 7604 | return NULL; |
| 7605 | if (setpgid(pid, pgrp) < 0) |
| 7606 | return posix_error(); |
| 7607 | Py_INCREF(Py_None); |
| 7608 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7609 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7610 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7611 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7612 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7613 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7614 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7615 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7616 | 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] | 7617 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7618 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7619 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7620 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7621 | int fd; |
| 7622 | pid_t pgid; |
| 7623 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 7624 | return NULL; |
| 7625 | pgid = tcgetpgrp(fd); |
| 7626 | if (pgid < 0) |
| 7627 | return posix_error(); |
| 7628 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7629 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7630 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7631 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7632 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7633 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7634 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7635 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7636 | 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] | 7637 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7638 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7639 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7640 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7641 | int fd; |
| 7642 | pid_t pgid; |
| 7643 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 7644 | return NULL; |
| 7645 | if (tcsetpgrp(fd, pgid) < 0) |
| 7646 | return posix_error(); |
| 7647 | Py_INCREF(Py_None); |
| 7648 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7649 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7650 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7651 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7652 | /* Functions acting on file descriptors */ |
| 7653 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7654 | #ifdef O_CLOEXEC |
| 7655 | extern int _Py_open_cloexec_works; |
| 7656 | #endif |
| 7657 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7658 | PyDoc_STRVAR(posix_open__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7659 | "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 7660 | Open a file for low level IO. Returns a file handle (integer).\n\ |
| 7661 | \n\ |
| 7662 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7663 | and path should be relative; path will then be relative to that directory.\n\ |
| 7664 | dir_fd may not be implemented on your platform.\n\ |
| 7665 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7666 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7667 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7668 | posix_open(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7669 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7670 | path_t path; |
| 7671 | int flags; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7672 | int mode = 0777; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7673 | int dir_fd = DEFAULT_DIR_FD; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7674 | int fd; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7675 | PyObject *return_value = NULL; |
| 7676 | static char *keywords[] = {"path", "flags", "mode", "dir_fd", NULL}; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7677 | #ifdef O_CLOEXEC |
| 7678 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 7679 | #elif !defined(MS_WINDOWS) |
| 7680 | int *atomic_flag_works = NULL; |
| 7681 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7682 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7683 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7684 | path.function_name = "open"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7685 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|i$O&:open", keywords, |
| 7686 | path_converter, &path, |
| 7687 | &flags, &mode, |
| 7688 | #ifdef HAVE_OPENAT |
| 7689 | dir_fd_converter, &dir_fd |
| 7690 | #else |
| 7691 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7692 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7693 | )) |
| 7694 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7695 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7696 | #ifdef MS_WINDOWS |
| 7697 | flags |= O_NOINHERIT; |
| 7698 | #elif defined(O_CLOEXEC) |
| 7699 | flags |= O_CLOEXEC; |
| 7700 | #endif |
| 7701 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7702 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7703 | #ifdef MS_WINDOWS |
| 7704 | if (path.wide) |
| 7705 | fd = _wopen(path.wide, flags, mode); |
| 7706 | else |
| 7707 | #endif |
| 7708 | #ifdef HAVE_OPENAT |
| 7709 | if (dir_fd != DEFAULT_DIR_FD) |
| 7710 | fd = openat(dir_fd, path.narrow, flags, mode); |
| 7711 | else |
| 7712 | #endif |
| 7713 | fd = open(path.narrow, flags, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7714 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7715 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7716 | if (fd == -1) { |
Victor Stinner | 4e7d2d4 | 2012-11-05 01:20:58 +0100 | [diff] [blame] | 7717 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path.object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7718 | goto exit; |
| 7719 | } |
| 7720 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7721 | #ifndef MS_WINDOWS |
| 7722 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 7723 | close(fd); |
| 7724 | goto exit; |
| 7725 | } |
| 7726 | #endif |
| 7727 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7728 | return_value = PyLong_FromLong((long)fd); |
| 7729 | |
| 7730 | exit: |
| 7731 | path_cleanup(&path); |
| 7732 | return return_value; |
| 7733 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7734 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7735 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7736 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7737 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7738 | |
Benjamin Peterson | 932bba3 | 2014-02-11 10:16:16 -0500 | [diff] [blame] | 7739 | /* |
| 7740 | The underscore at end of function name avoids a name clash with the libc |
| 7741 | function posix_close. |
| 7742 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7743 | static PyObject * |
Benjamin Peterson | 932bba3 | 2014-02-11 10:16:16 -0500 | [diff] [blame] | 7744 | posix_close_(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7745 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7746 | int fd, res; |
| 7747 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 7748 | return NULL; |
| 7749 | if (!_PyVerify_fd(fd)) |
| 7750 | return posix_error(); |
| 7751 | Py_BEGIN_ALLOW_THREADS |
| 7752 | res = close(fd); |
| 7753 | Py_END_ALLOW_THREADS |
| 7754 | if (res < 0) |
| 7755 | return posix_error(); |
| 7756 | Py_INCREF(Py_None); |
| 7757 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7758 | } |
| 7759 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7760 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7761 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7762 | "closerange(fd_low, fd_high)\n\n\ |
| 7763 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 7764 | |
| 7765 | static PyObject * |
| 7766 | posix_closerange(PyObject *self, PyObject *args) |
| 7767 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7768 | int fd_from, fd_to, i; |
| 7769 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 7770 | return NULL; |
| 7771 | Py_BEGIN_ALLOW_THREADS |
| 7772 | for (i = fd_from; i < fd_to; i++) |
| 7773 | if (_PyVerify_fd(i)) |
| 7774 | close(i); |
| 7775 | Py_END_ALLOW_THREADS |
| 7776 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7777 | } |
| 7778 | |
| 7779 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7780 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7781 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7782 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7783 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7784 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7785 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7786 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7787 | int fd; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7788 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7789 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 7790 | return NULL; |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 7791 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7792 | fd = _Py_dup(fd); |
| 7793 | if (fd == -1) |
| 7794 | return NULL; |
| 7795 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7796 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7797 | } |
| 7798 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7799 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7800 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 7801 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7802 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7803 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7804 | static PyObject * |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7805 | posix_dup2(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7806 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7807 | static char *keywords[] = {"fd", "fd2", "inheritable", NULL}; |
| 7808 | int fd, fd2; |
| 7809 | int inheritable = 1; |
| 7810 | int res; |
| 7811 | #if defined(HAVE_DUP3) && \ |
| 7812 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 7813 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
| 7814 | int dup3_works = -1; |
| 7815 | #endif |
| 7816 | |
| 7817 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|i:dup2", keywords, |
| 7818 | &fd, &fd2, &inheritable)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7819 | return NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7820 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7821 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 7822 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7823 | |
| 7824 | #ifdef MS_WINDOWS |
| 7825 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7826 | res = dup2(fd, fd2); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7827 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7828 | if (res < 0) |
| 7829 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7830 | |
| 7831 | /* Character files like console cannot be make non-inheritable */ |
| 7832 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7833 | close(fd2); |
| 7834 | return NULL; |
| 7835 | } |
| 7836 | |
| 7837 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 7838 | Py_BEGIN_ALLOW_THREADS |
| 7839 | if (!inheritable) |
| 7840 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 7841 | else |
| 7842 | res = dup2(fd, fd2); |
| 7843 | Py_END_ALLOW_THREADS |
| 7844 | if (res < 0) |
| 7845 | return posix_error(); |
| 7846 | |
| 7847 | #else |
| 7848 | |
| 7849 | #ifdef HAVE_DUP3 |
| 7850 | if (!inheritable && dup3_works != 0) { |
| 7851 | Py_BEGIN_ALLOW_THREADS |
| 7852 | res = dup3(fd, fd2, O_CLOEXEC); |
| 7853 | Py_END_ALLOW_THREADS |
| 7854 | if (res < 0) { |
| 7855 | if (dup3_works == -1) |
| 7856 | dup3_works = (errno != ENOSYS); |
| 7857 | if (dup3_works) |
| 7858 | return posix_error(); |
| 7859 | } |
| 7860 | } |
| 7861 | |
| 7862 | if (inheritable || dup3_works == 0) |
| 7863 | { |
| 7864 | #endif |
| 7865 | Py_BEGIN_ALLOW_THREADS |
| 7866 | res = dup2(fd, fd2); |
| 7867 | Py_END_ALLOW_THREADS |
| 7868 | if (res < 0) |
| 7869 | return posix_error(); |
| 7870 | |
| 7871 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7872 | close(fd2); |
| 7873 | return NULL; |
| 7874 | } |
| 7875 | #ifdef HAVE_DUP3 |
| 7876 | } |
| 7877 | #endif |
| 7878 | |
| 7879 | #endif |
| 7880 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7881 | Py_INCREF(Py_None); |
| 7882 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7883 | } |
| 7884 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7885 | #ifdef HAVE_LOCKF |
| 7886 | PyDoc_STRVAR(posix_lockf__doc__, |
| 7887 | "lockf(fd, cmd, len)\n\n\ |
| 7888 | Apply, test or remove a POSIX lock on an open file descriptor.\n\n\ |
| 7889 | fd is an open file descriptor.\n\ |
| 7890 | cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\ |
| 7891 | F_TEST.\n\ |
| 7892 | len specifies the section of the file to lock."); |
| 7893 | |
| 7894 | static PyObject * |
| 7895 | posix_lockf(PyObject *self, PyObject *args) |
| 7896 | { |
| 7897 | int fd, cmd, res; |
| 7898 | off_t len; |
| 7899 | if (!PyArg_ParseTuple(args, "iiO&:lockf", |
| 7900 | &fd, &cmd, _parse_off_t, &len)) |
| 7901 | return NULL; |
| 7902 | |
| 7903 | Py_BEGIN_ALLOW_THREADS |
| 7904 | res = lockf(fd, cmd, len); |
| 7905 | Py_END_ALLOW_THREADS |
| 7906 | |
| 7907 | if (res < 0) |
| 7908 | return posix_error(); |
| 7909 | |
| 7910 | Py_RETURN_NONE; |
| 7911 | } |
| 7912 | #endif |
| 7913 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7914 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7915 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7916 | "lseek(fd, pos, how) -> newpos\n\n\ |
Victor Stinner | e83f899 | 2011-12-17 23:15:09 +0100 | [diff] [blame] | 7917 | Set the current position of a file descriptor.\n\ |
| 7918 | Return the new cursor position in bytes, starting from the beginning."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7919 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7920 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7921 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7922 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7923 | int fd, how; |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 7924 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7925 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7926 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7927 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7928 | #endif |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7929 | PyObject *posobj; |
| 7930 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7931 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7932 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7933 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 7934 | switch (how) { |
| 7935 | case 0: how = SEEK_SET; break; |
| 7936 | case 1: how = SEEK_CUR; break; |
| 7937 | case 2: how = SEEK_END; break; |
| 7938 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7939 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7940 | |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7941 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7942 | pos = PyLong_AsLong(posobj); |
| 7943 | #else |
| 7944 | pos = PyLong_AsLongLong(posobj); |
| 7945 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7946 | if (PyErr_Occurred()) |
| 7947 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7948 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7949 | if (!_PyVerify_fd(fd)) |
| 7950 | return posix_error(); |
| 7951 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 7952 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7953 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7954 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7955 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7956 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7957 | Py_END_ALLOW_THREADS |
| 7958 | if (res < 0) |
| 7959 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7960 | |
| 7961 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7962 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7963 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7964 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7965 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7966 | } |
| 7967 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7968 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7969 | PyDoc_STRVAR(posix_read__doc__, |
Benjamin Peterson | 3b08a29 | 2013-05-24 14:35:57 -0700 | [diff] [blame] | 7970 | "read(fd, buffersize) -> bytes\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7971 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7972 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7973 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7974 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7975 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7976 | int fd, size; |
| 7977 | Py_ssize_t n; |
| 7978 | PyObject *buffer; |
| 7979 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 7980 | return NULL; |
| 7981 | if (size < 0) { |
| 7982 | errno = EINVAL; |
| 7983 | return posix_error(); |
| 7984 | } |
| 7985 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7986 | if (buffer == NULL) |
| 7987 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7988 | if (!_PyVerify_fd(fd)) { |
| 7989 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7990 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7991 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7992 | Py_BEGIN_ALLOW_THREADS |
| 7993 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 7994 | Py_END_ALLOW_THREADS |
| 7995 | if (n < 0) { |
| 7996 | Py_DECREF(buffer); |
| 7997 | return posix_error(); |
| 7998 | } |
| 7999 | if (n != size) |
| 8000 | _PyBytes_Resize(&buffer, n); |
| 8001 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8002 | } |
| 8003 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8004 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 8005 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8006 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8007 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 8008 | { |
| 8009 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8010 | Py_ssize_t blen, total = 0; |
| 8011 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8012 | *iov = PyMem_New(struct iovec, cnt); |
| 8013 | if (*iov == NULL) { |
| 8014 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8015 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8016 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8017 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8018 | *buf = PyMem_New(Py_buffer, cnt); |
| 8019 | if (*buf == NULL) { |
| 8020 | PyMem_Del(*iov); |
| 8021 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8022 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8023 | } |
| 8024 | |
| 8025 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8026 | PyObject *item = PySequence_GetItem(seq, i); |
| 8027 | if (item == NULL) |
| 8028 | goto fail; |
| 8029 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 8030 | Py_DECREF(item); |
| 8031 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8032 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8033 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8034 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8035 | blen = (*buf)[i].len; |
| 8036 | (*iov)[i].iov_len = blen; |
| 8037 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8038 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8039 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8040 | |
| 8041 | fail: |
| 8042 | PyMem_Del(*iov); |
| 8043 | for (j = 0; j < i; j++) { |
| 8044 | PyBuffer_Release(&(*buf)[j]); |
| 8045 | } |
| 8046 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8047 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8048 | } |
| 8049 | |
| 8050 | static void |
| 8051 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 8052 | { |
| 8053 | int i; |
| 8054 | PyMem_Del(iov); |
| 8055 | for (i = 0; i < cnt; i++) { |
| 8056 | PyBuffer_Release(&buf[i]); |
| 8057 | } |
| 8058 | PyMem_Del(buf); |
| 8059 | } |
| 8060 | #endif |
| 8061 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8062 | #ifdef HAVE_READV |
| 8063 | PyDoc_STRVAR(posix_readv__doc__, |
| 8064 | "readv(fd, buffers) -> bytesread\n\n\ |
Benjamin Peterson | e83ed43 | 2014-01-18 22:54:59 -0500 | [diff] [blame] | 8065 | Read from a file descriptor fd into a number of mutable, bytes-like\n\ |
| 8066 | objects (\"buffers\"). readv will transfer data into each buffer\n\ |
| 8067 | until it is full and then move on to the next buffer in the sequence\n\ |
| 8068 | to hold the rest of the data.\n\n\ |
| 8069 | readv returns the total number of bytes read (which may be less than\n\ |
| 8070 | the total capacity of all the buffers."); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8071 | |
| 8072 | static PyObject * |
| 8073 | posix_readv(PyObject *self, PyObject *args) |
| 8074 | { |
| 8075 | int fd, cnt; |
| 8076 | Py_ssize_t n; |
| 8077 | PyObject *seq; |
| 8078 | struct iovec *iov; |
| 8079 | Py_buffer *buf; |
| 8080 | |
| 8081 | if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq)) |
| 8082 | return NULL; |
| 8083 | if (!PySequence_Check(seq)) { |
| 8084 | PyErr_SetString(PyExc_TypeError, |
| 8085 | "readv() arg 2 must be a sequence"); |
| 8086 | return NULL; |
| 8087 | } |
| 8088 | cnt = PySequence_Size(seq); |
| 8089 | |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8090 | if (iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE) < 0) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8091 | return NULL; |
| 8092 | |
| 8093 | Py_BEGIN_ALLOW_THREADS |
| 8094 | n = readv(fd, iov, cnt); |
| 8095 | Py_END_ALLOW_THREADS |
| 8096 | |
| 8097 | iov_cleanup(iov, buf, cnt); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8098 | if (n < 0) |
| 8099 | return posix_error(); |
| 8100 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8101 | return PyLong_FromSsize_t(n); |
| 8102 | } |
| 8103 | #endif |
| 8104 | |
| 8105 | #ifdef HAVE_PREAD |
| 8106 | PyDoc_STRVAR(posix_pread__doc__, |
| 8107 | "pread(fd, buffersize, offset) -> string\n\n\ |
| 8108 | Read from a file descriptor, fd, at a position of offset. It will read up\n\ |
| 8109 | to buffersize number of bytes. The file offset remains unchanged."); |
| 8110 | |
| 8111 | static PyObject * |
| 8112 | posix_pread(PyObject *self, PyObject *args) |
| 8113 | { |
| 8114 | int fd, size; |
| 8115 | off_t offset; |
| 8116 | Py_ssize_t n; |
| 8117 | PyObject *buffer; |
| 8118 | if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset)) |
| 8119 | return NULL; |
| 8120 | |
| 8121 | if (size < 0) { |
| 8122 | errno = EINVAL; |
| 8123 | return posix_error(); |
| 8124 | } |
| 8125 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 8126 | if (buffer == NULL) |
| 8127 | return NULL; |
| 8128 | if (!_PyVerify_fd(fd)) { |
| 8129 | Py_DECREF(buffer); |
| 8130 | return posix_error(); |
| 8131 | } |
| 8132 | Py_BEGIN_ALLOW_THREADS |
| 8133 | n = pread(fd, PyBytes_AS_STRING(buffer), size, offset); |
| 8134 | Py_END_ALLOW_THREADS |
| 8135 | if (n < 0) { |
| 8136 | Py_DECREF(buffer); |
| 8137 | return posix_error(); |
| 8138 | } |
| 8139 | if (n != size) |
| 8140 | _PyBytes_Resize(&buffer, n); |
| 8141 | return buffer; |
| 8142 | } |
| 8143 | #endif |
| 8144 | |
| 8145 | PyDoc_STRVAR(posix_write__doc__, |
Benjamin Peterson | 3b08a29 | 2013-05-24 14:35:57 -0700 | [diff] [blame] | 8146 | "write(fd, data) -> byteswritten\n\n\ |
| 8147 | Write bytes to a file descriptor."); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8148 | |
| 8149 | static PyObject * |
| 8150 | posix_write(PyObject *self, PyObject *args) |
| 8151 | { |
| 8152 | Py_buffer pbuf; |
| 8153 | int fd; |
| 8154 | Py_ssize_t size, len; |
| 8155 | |
| 8156 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 8157 | return NULL; |
| 8158 | if (!_PyVerify_fd(fd)) { |
| 8159 | PyBuffer_Release(&pbuf); |
| 8160 | return posix_error(); |
| 8161 | } |
| 8162 | len = pbuf.len; |
| 8163 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 8164 | #ifdef MS_WINDOWS |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8165 | if (len > INT_MAX) |
| 8166 | len = INT_MAX; |
| 8167 | size = write(fd, pbuf.buf, (int)len); |
| 8168 | #else |
| 8169 | size = write(fd, pbuf.buf, len); |
| 8170 | #endif |
| 8171 | Py_END_ALLOW_THREADS |
| 8172 | PyBuffer_Release(&pbuf); |
| 8173 | if (size < 0) |
| 8174 | return posix_error(); |
| 8175 | return PyLong_FromSsize_t(size); |
| 8176 | } |
| 8177 | |
| 8178 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8179 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 8180 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 8181 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 8182 | -> byteswritten\n\ |
| 8183 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 8184 | |
| 8185 | static PyObject * |
| 8186 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 8187 | { |
| 8188 | int in, out; |
| 8189 | Py_ssize_t ret; |
| 8190 | off_t offset; |
| 8191 | |
| 8192 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 8193 | #ifndef __APPLE__ |
| 8194 | Py_ssize_t len; |
| 8195 | #endif |
| 8196 | PyObject *headers = NULL, *trailers = NULL; |
| 8197 | Py_buffer *hbuf, *tbuf; |
| 8198 | off_t sbytes; |
| 8199 | struct sf_hdtr sf; |
| 8200 | int flags = 0; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 8201 | static char *keywords[] = {"out", "in", |
| 8202 | "offset", "count", |
| 8203 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8204 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 8205 | sf.headers = NULL; |
| 8206 | sf.trailers = NULL; |
| 8207 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8208 | #ifdef __APPLE__ |
| 8209 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 8210 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8211 | #else |
| 8212 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 8213 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8214 | #endif |
| 8215 | &headers, &trailers, &flags)) |
| 8216 | return NULL; |
| 8217 | if (headers != NULL) { |
| 8218 | if (!PySequence_Check(headers)) { |
| 8219 | PyErr_SetString(PyExc_TypeError, |
| 8220 | "sendfile() headers must be a sequence or None"); |
| 8221 | return NULL; |
| 8222 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8223 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8224 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8225 | if (sf.hdr_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8226 | (i = iov_setup(&(sf.headers), &hbuf, |
| 8227 | headers, sf.hdr_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8228 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8229 | #ifdef __APPLE__ |
| 8230 | sbytes += i; |
| 8231 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8232 | } |
| 8233 | } |
| 8234 | if (trailers != NULL) { |
| 8235 | if (!PySequence_Check(trailers)) { |
| 8236 | PyErr_SetString(PyExc_TypeError, |
| 8237 | "sendfile() trailers must be a sequence or None"); |
| 8238 | return NULL; |
| 8239 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8240 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8241 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8242 | if (sf.trl_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8243 | (i = iov_setup(&(sf.trailers), &tbuf, |
| 8244 | trailers, sf.trl_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8245 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8246 | #ifdef __APPLE__ |
| 8247 | sbytes += i; |
| 8248 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8249 | } |
| 8250 | } |
| 8251 | |
| 8252 | Py_BEGIN_ALLOW_THREADS |
| 8253 | #ifdef __APPLE__ |
| 8254 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 8255 | #else |
| 8256 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 8257 | #endif |
| 8258 | Py_END_ALLOW_THREADS |
| 8259 | |
| 8260 | if (sf.headers != NULL) |
| 8261 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 8262 | if (sf.trailers != NULL) |
| 8263 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 8264 | |
| 8265 | if (ret < 0) { |
| 8266 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 8267 | if (sbytes != 0) { |
| 8268 | // some data has been sent |
| 8269 | goto done; |
| 8270 | } |
| 8271 | else { |
| 8272 | // no data has been sent; upper application is supposed |
| 8273 | // to retry on EAGAIN or EBUSY |
| 8274 | return posix_error(); |
| 8275 | } |
| 8276 | } |
| 8277 | return posix_error(); |
| 8278 | } |
| 8279 | goto done; |
| 8280 | |
| 8281 | done: |
| 8282 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 8283 | return Py_BuildValue("l", sbytes); |
| 8284 | #else |
| 8285 | return Py_BuildValue("L", sbytes); |
| 8286 | #endif |
| 8287 | |
| 8288 | #else |
| 8289 | Py_ssize_t count; |
| 8290 | PyObject *offobj; |
| 8291 | static char *keywords[] = {"out", "in", |
| 8292 | "offset", "count", NULL}; |
| 8293 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 8294 | keywords, &out, &in, &offobj, &count)) |
| 8295 | return NULL; |
| 8296 | #ifdef linux |
| 8297 | if (offobj == Py_None) { |
| 8298 | Py_BEGIN_ALLOW_THREADS |
| 8299 | ret = sendfile(out, in, NULL, count); |
| 8300 | Py_END_ALLOW_THREADS |
| 8301 | if (ret < 0) |
| 8302 | return posix_error(); |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 8303 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8304 | } |
| 8305 | #endif |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 8306 | if (!_parse_off_t(offobj, &offset)) |
| 8307 | return NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8308 | Py_BEGIN_ALLOW_THREADS |
| 8309 | ret = sendfile(out, in, &offset, count); |
| 8310 | Py_END_ALLOW_THREADS |
| 8311 | if (ret < 0) |
| 8312 | return posix_error(); |
| 8313 | return Py_BuildValue("n", ret); |
| 8314 | #endif |
| 8315 | } |
| 8316 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8317 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8318 | PyDoc_STRVAR(posix_fstat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8319 | "fstat(fd) -> stat result\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8320 | Like stat(), but for an open file descriptor.\n\ |
| 8321 | Equivalent to stat(fd=fd)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8322 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8323 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8324 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8325 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8326 | int fd; |
| 8327 | STRUCT_STAT st; |
| 8328 | int res; |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8329 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8330 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8331 | Py_BEGIN_ALLOW_THREADS |
| 8332 | res = FSTAT(fd, &st); |
| 8333 | Py_END_ALLOW_THREADS |
| 8334 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8335 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8336 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8337 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8338 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8339 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8340 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8341 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8342 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8343 | } |
| 8344 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8345 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8346 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8347 | 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] | 8348 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8349 | |
| 8350 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 8351 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8352 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8353 | int fd; |
| 8354 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 8355 | return NULL; |
| 8356 | if (!_PyVerify_fd(fd)) |
| 8357 | return PyBool_FromLong(0); |
| 8358 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8359 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8360 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8361 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8362 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8363 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8364 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8365 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8366 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8367 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8368 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8369 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8370 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8371 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8372 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8373 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8374 | #else |
| 8375 | int res; |
| 8376 | #endif |
| 8377 | |
| 8378 | #ifdef MS_WINDOWS |
| 8379 | attr.nLength = sizeof(attr); |
| 8380 | attr.lpSecurityDescriptor = NULL; |
| 8381 | attr.bInheritHandle = FALSE; |
| 8382 | |
| 8383 | Py_BEGIN_ALLOW_THREADS |
| 8384 | ok = CreatePipe(&read, &write, &attr, 0); |
| 8385 | if (ok) { |
| 8386 | fds[0] = _open_osfhandle((Py_intptr_t)read, _O_RDONLY); |
| 8387 | fds[1] = _open_osfhandle((Py_intptr_t)write, _O_WRONLY); |
| 8388 | if (fds[0] == -1 || fds[1] == -1) { |
| 8389 | CloseHandle(read); |
| 8390 | CloseHandle(write); |
| 8391 | ok = 0; |
| 8392 | } |
| 8393 | } |
| 8394 | Py_END_ALLOW_THREADS |
| 8395 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8396 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8397 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8398 | #else |
| 8399 | |
| 8400 | #ifdef HAVE_PIPE2 |
| 8401 | Py_BEGIN_ALLOW_THREADS |
| 8402 | res = pipe2(fds, O_CLOEXEC); |
| 8403 | Py_END_ALLOW_THREADS |
| 8404 | |
| 8405 | if (res != 0 && errno == ENOSYS) |
| 8406 | { |
| 8407 | #endif |
| 8408 | Py_BEGIN_ALLOW_THREADS |
| 8409 | res = pipe(fds); |
| 8410 | Py_END_ALLOW_THREADS |
| 8411 | |
| 8412 | if (res == 0) { |
| 8413 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 8414 | close(fds[0]); |
| 8415 | close(fds[1]); |
| 8416 | return NULL; |
| 8417 | } |
| 8418 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 8419 | close(fds[0]); |
| 8420 | close(fds[1]); |
| 8421 | return NULL; |
| 8422 | } |
| 8423 | } |
| 8424 | #ifdef HAVE_PIPE2 |
| 8425 | } |
| 8426 | #endif |
| 8427 | |
| 8428 | if (res != 0) |
| 8429 | return PyErr_SetFromErrno(PyExc_OSError); |
| 8430 | #endif /* !MS_WINDOWS */ |
| 8431 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8432 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8433 | #endif /* HAVE_PIPE */ |
| 8434 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8435 | #ifdef HAVE_PIPE2 |
| 8436 | PyDoc_STRVAR(posix_pipe2__doc__, |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8437 | "pipe2(flags) -> (read_end, write_end)\n\n\ |
| 8438 | Create a pipe with flags set atomically.\n\ |
| 8439 | flags can be constructed by ORing together one or more of these values:\n\ |
| 8440 | O_NONBLOCK, O_CLOEXEC.\n\ |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8441 | "); |
| 8442 | |
| 8443 | static PyObject * |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8444 | posix_pipe2(PyObject *self, PyObject *arg) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8445 | { |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8446 | int flags; |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8447 | int fds[2]; |
| 8448 | int res; |
| 8449 | |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 8450 | flags = _PyLong_AsInt(arg); |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8451 | if (flags == -1 && PyErr_Occurred()) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8452 | return NULL; |
| 8453 | |
| 8454 | res = pipe2(fds, flags); |
| 8455 | if (res != 0) |
| 8456 | return posix_error(); |
| 8457 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 8458 | } |
| 8459 | #endif /* HAVE_PIPE2 */ |
| 8460 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8461 | #ifdef HAVE_WRITEV |
| 8462 | PyDoc_STRVAR(posix_writev__doc__, |
| 8463 | "writev(fd, buffers) -> byteswritten\n\n\ |
Benjamin Peterson | e83ed43 | 2014-01-18 22:54:59 -0500 | [diff] [blame] | 8464 | Write the contents of *buffers* to file descriptor *fd*. *buffers*\n\ |
| 8465 | must be a sequence of bytes-like objects.\n\n\ |
| 8466 | writev writes the contents of each object to the file descriptor\n\ |
| 8467 | and returns the total number of bytes written."); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8468 | |
| 8469 | static PyObject * |
| 8470 | posix_writev(PyObject *self, PyObject *args) |
| 8471 | { |
| 8472 | int fd, cnt; |
| 8473 | Py_ssize_t res; |
| 8474 | PyObject *seq; |
| 8475 | struct iovec *iov; |
| 8476 | Py_buffer *buf; |
| 8477 | if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq)) |
| 8478 | return NULL; |
| 8479 | if (!PySequence_Check(seq)) { |
| 8480 | PyErr_SetString(PyExc_TypeError, |
| 8481 | "writev() arg 2 must be a sequence"); |
| 8482 | return NULL; |
| 8483 | } |
| 8484 | cnt = PySequence_Size(seq); |
| 8485 | |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8486 | if (iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE) < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8487 | return NULL; |
| 8488 | } |
| 8489 | |
| 8490 | Py_BEGIN_ALLOW_THREADS |
| 8491 | res = writev(fd, iov, cnt); |
| 8492 | Py_END_ALLOW_THREADS |
| 8493 | |
| 8494 | iov_cleanup(iov, buf, cnt); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8495 | if (res < 0) |
| 8496 | return posix_error(); |
| 8497 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8498 | return PyLong_FromSsize_t(res); |
| 8499 | } |
| 8500 | #endif |
| 8501 | |
| 8502 | #ifdef HAVE_PWRITE |
| 8503 | PyDoc_STRVAR(posix_pwrite__doc__, |
| 8504 | "pwrite(fd, string, offset) -> byteswritten\n\n\ |
| 8505 | Write string to a file descriptor, fd, from offset, leaving the file\n\ |
| 8506 | offset unchanged."); |
| 8507 | |
| 8508 | static PyObject * |
| 8509 | posix_pwrite(PyObject *self, PyObject *args) |
| 8510 | { |
| 8511 | Py_buffer pbuf; |
| 8512 | int fd; |
| 8513 | off_t offset; |
| 8514 | Py_ssize_t size; |
| 8515 | |
| 8516 | if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset)) |
| 8517 | return NULL; |
| 8518 | |
| 8519 | if (!_PyVerify_fd(fd)) { |
| 8520 | PyBuffer_Release(&pbuf); |
| 8521 | return posix_error(); |
| 8522 | } |
| 8523 | Py_BEGIN_ALLOW_THREADS |
| 8524 | size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset); |
| 8525 | Py_END_ALLOW_THREADS |
| 8526 | PyBuffer_Release(&pbuf); |
| 8527 | if (size < 0) |
| 8528 | return posix_error(); |
| 8529 | return PyLong_FromSsize_t(size); |
| 8530 | } |
| 8531 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8532 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8533 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8534 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8535 | "mkfifo(path, mode=0o666, *, dir_fd=None)\n\n\ |
| 8536 | Create a FIFO (a POSIX named pipe).\n\ |
| 8537 | \n\ |
| 8538 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 8539 | and path should be relative; path will then be relative to that directory.\n\ |
| 8540 | dir_fd may not be implemented on your platform.\n\ |
| 8541 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8542 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8543 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8544 | posix_mkfifo(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8545 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8546 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8547 | int mode = 0666; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8548 | int dir_fd = DEFAULT_DIR_FD; |
| 8549 | int result; |
| 8550 | PyObject *return_value = NULL; |
| 8551 | static char *keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 8552 | |
| 8553 | memset(&path, 0, sizeof(path)); |
| 8554 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkfifo", keywords, |
| 8555 | path_converter, &path, |
| 8556 | &mode, |
| 8557 | #ifdef HAVE_MKFIFOAT |
| 8558 | dir_fd_converter, &dir_fd |
| 8559 | #else |
| 8560 | dir_fd_unavailable, &dir_fd |
| 8561 | #endif |
| 8562 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8563 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8564 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8565 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8566 | #ifdef HAVE_MKFIFOAT |
| 8567 | if (dir_fd != DEFAULT_DIR_FD) |
| 8568 | result = mkfifoat(dir_fd, path.narrow, mode); |
| 8569 | else |
| 8570 | #endif |
| 8571 | result = mkfifo(path.narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8572 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8573 | |
| 8574 | if (result < 0) { |
| 8575 | return_value = posix_error(); |
| 8576 | goto exit; |
| 8577 | } |
| 8578 | |
| 8579 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8580 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8581 | |
| 8582 | exit: |
| 8583 | path_cleanup(&path); |
| 8584 | return return_value; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8585 | } |
| 8586 | #endif |
| 8587 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 8588 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8589 | PyDoc_STRVAR(posix_mknod__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8590 | "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] | 8591 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 8592 | named filename. mode specifies both the permissions to use and the\n\ |
| 8593 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 8594 | 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] | 8595 | device defines the newly created device special file (probably using\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8596 | os.makedev()), otherwise it is ignored.\n\ |
| 8597 | \n\ |
| 8598 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 8599 | and path should be relative; path will then be relative to that directory.\n\ |
| 8600 | dir_fd may not be implemented on your platform.\n\ |
| 8601 | If it is unavailable, using it will raise a NotImplementedError."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8602 | |
| 8603 | |
| 8604 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8605 | posix_mknod(PyObject *self, PyObject *args, PyObject *kwargs) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8606 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8607 | path_t path; |
| 8608 | int mode = 0666; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8609 | int device = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8610 | int dir_fd = DEFAULT_DIR_FD; |
| 8611 | int result; |
| 8612 | PyObject *return_value = NULL; |
| 8613 | static char *keywords[] = {"path", "mode", "device", "dir_fd", NULL}; |
| 8614 | |
| 8615 | memset(&path, 0, sizeof(path)); |
| 8616 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|ii$O&:mknod", keywords, |
| 8617 | path_converter, &path, |
| 8618 | &mode, &device, |
| 8619 | #ifdef HAVE_MKNODAT |
| 8620 | dir_fd_converter, &dir_fd |
| 8621 | #else |
| 8622 | dir_fd_unavailable, &dir_fd |
| 8623 | #endif |
| 8624 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8625 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8626 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8627 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8628 | #ifdef HAVE_MKNODAT |
| 8629 | if (dir_fd != DEFAULT_DIR_FD) |
| 8630 | result = mknodat(dir_fd, path.narrow, mode, device); |
| 8631 | else |
| 8632 | #endif |
| 8633 | result = mknod(path.narrow, mode, device); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8634 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8635 | |
| 8636 | if (result < 0) { |
| 8637 | return_value = posix_error(); |
| 8638 | goto exit; |
| 8639 | } |
| 8640 | |
| 8641 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8642 | Py_INCREF(Py_None); |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 8643 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8644 | exit: |
| 8645 | path_cleanup(&path); |
| 8646 | return return_value; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8647 | } |
| 8648 | #endif |
| 8649 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8650 | #ifdef HAVE_DEVICE_MACROS |
| 8651 | PyDoc_STRVAR(posix_major__doc__, |
| 8652 | "major(device) -> major number\n\ |
| 8653 | Extracts a device major number from a raw device number."); |
| 8654 | |
| 8655 | static PyObject * |
| 8656 | posix_major(PyObject *self, PyObject *args) |
| 8657 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8658 | int device; |
| 8659 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 8660 | return NULL; |
| 8661 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8662 | } |
| 8663 | |
| 8664 | PyDoc_STRVAR(posix_minor__doc__, |
| 8665 | "minor(device) -> minor number\n\ |
| 8666 | Extracts a device minor number from a raw device number."); |
| 8667 | |
| 8668 | static PyObject * |
| 8669 | posix_minor(PyObject *self, PyObject *args) |
| 8670 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8671 | int device; |
| 8672 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 8673 | return NULL; |
| 8674 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8675 | } |
| 8676 | |
| 8677 | PyDoc_STRVAR(posix_makedev__doc__, |
| 8678 | "makedev(major, minor) -> device number\n\ |
| 8679 | Composes a raw device number from the major and minor device numbers."); |
| 8680 | |
| 8681 | static PyObject * |
| 8682 | posix_makedev(PyObject *self, PyObject *args) |
| 8683 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8684 | int major, minor; |
| 8685 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 8686 | return NULL; |
| 8687 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8688 | } |
| 8689 | #endif /* device macros */ |
| 8690 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8691 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8692 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8693 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8694 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8695 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8696 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8697 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8698 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8699 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8700 | int fd; |
| 8701 | off_t length; |
| 8702 | int res; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8703 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8704 | if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8705 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8706 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8707 | Py_BEGIN_ALLOW_THREADS |
| 8708 | res = ftruncate(fd, length); |
| 8709 | Py_END_ALLOW_THREADS |
| 8710 | if (res < 0) |
| 8711 | return posix_error(); |
| 8712 | Py_INCREF(Py_None); |
| 8713 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8714 | } |
| 8715 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8716 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8717 | #ifdef HAVE_TRUNCATE |
| 8718 | PyDoc_STRVAR(posix_truncate__doc__, |
| 8719 | "truncate(path, length)\n\n\ |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8720 | Truncate the file given by path to length bytes.\n\ |
| 8721 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 8722 | If this functionality is unavailable, using it raises an exception."); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8723 | |
| 8724 | static PyObject * |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8725 | posix_truncate(PyObject *self, PyObject *args, PyObject *kwargs) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8726 | { |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8727 | path_t path; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8728 | off_t length; |
| 8729 | int res; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8730 | PyObject *result = NULL; |
| 8731 | static char *keywords[] = {"path", "length", NULL}; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8732 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8733 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8734 | path.function_name = "truncate"; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8735 | #ifdef HAVE_FTRUNCATE |
| 8736 | path.allow_fd = 1; |
| 8737 | #endif |
| 8738 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:truncate", keywords, |
| 8739 | path_converter, &path, |
| 8740 | _parse_off_t, &length)) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8741 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8742 | |
| 8743 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8744 | #ifdef HAVE_FTRUNCATE |
| 8745 | if (path.fd != -1) |
| 8746 | res = ftruncate(path.fd, length); |
| 8747 | else |
| 8748 | #endif |
| 8749 | res = truncate(path.narrow, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8750 | Py_END_ALLOW_THREADS |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8751 | if (res < 0) |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8752 | result = path_error(&path); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8753 | else { |
| 8754 | Py_INCREF(Py_None); |
| 8755 | result = Py_None; |
| 8756 | } |
| 8757 | path_cleanup(&path); |
| 8758 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8759 | } |
| 8760 | #endif |
| 8761 | |
| 8762 | #ifdef HAVE_POSIX_FALLOCATE |
| 8763 | PyDoc_STRVAR(posix_posix_fallocate__doc__, |
| 8764 | "posix_fallocate(fd, offset, len)\n\n\ |
| 8765 | Ensures that enough disk space is allocated for the file specified by fd\n\ |
| 8766 | starting from offset and continuing for len bytes."); |
| 8767 | |
| 8768 | static PyObject * |
| 8769 | posix_posix_fallocate(PyObject *self, PyObject *args) |
| 8770 | { |
| 8771 | off_t len, offset; |
| 8772 | int res, fd; |
| 8773 | |
| 8774 | if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate", |
| 8775 | &fd, _parse_off_t, &offset, _parse_off_t, &len)) |
| 8776 | return NULL; |
| 8777 | |
| 8778 | Py_BEGIN_ALLOW_THREADS |
| 8779 | res = posix_fallocate(fd, offset, len); |
| 8780 | Py_END_ALLOW_THREADS |
| 8781 | if (res != 0) { |
| 8782 | errno = res; |
| 8783 | return posix_error(); |
| 8784 | } |
| 8785 | Py_RETURN_NONE; |
| 8786 | } |
| 8787 | #endif |
| 8788 | |
| 8789 | #ifdef HAVE_POSIX_FADVISE |
| 8790 | PyDoc_STRVAR(posix_posix_fadvise__doc__, |
| 8791 | "posix_fadvise(fd, offset, len, advice)\n\n\ |
| 8792 | Announces an intention to access data in a specific pattern thus allowing\n\ |
| 8793 | the kernel to make optimizations.\n\ |
| 8794 | The advice applies to the region of the file specified by fd starting at\n\ |
| 8795 | offset and continuing for len bytes.\n\ |
| 8796 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\ |
| 8797 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\ |
| 8798 | POSIX_FADV_DONTNEED."); |
| 8799 | |
| 8800 | static PyObject * |
| 8801 | posix_posix_fadvise(PyObject *self, PyObject *args) |
| 8802 | { |
| 8803 | off_t len, offset; |
| 8804 | int res, fd, advice; |
| 8805 | |
| 8806 | if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise", |
| 8807 | &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice)) |
| 8808 | return NULL; |
| 8809 | |
| 8810 | Py_BEGIN_ALLOW_THREADS |
| 8811 | res = posix_fadvise(fd, offset, len, advice); |
| 8812 | Py_END_ALLOW_THREADS |
| 8813 | if (res != 0) { |
| 8814 | errno = res; |
| 8815 | return posix_error(); |
| 8816 | } |
| 8817 | Py_RETURN_NONE; |
| 8818 | } |
| 8819 | #endif |
| 8820 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8821 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8822 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8823 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8824 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8825 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8826 | /* Save putenv() parameters as values here, so we can collect them when they |
| 8827 | * get re-set with another call for the same key. */ |
| 8828 | static PyObject *posix_putenv_garbage; |
| 8829 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8830 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8831 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8832 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8833 | PyObject *newstr = NULL; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8834 | #ifdef MS_WINDOWS |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8835 | PyObject *os1, *os2; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8836 | wchar_t *newenv; |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8837 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8838 | if (!PyArg_ParseTuple(args, |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8839 | "UU:putenv", |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8840 | &os1, &os2)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8841 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8842 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8843 | newstr = PyUnicode_FromFormat("%U=%U", os1, os2); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8844 | if (newstr == NULL) { |
| 8845 | PyErr_NoMemory(); |
| 8846 | goto error; |
| 8847 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8848 | if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) { |
| 8849 | PyErr_Format(PyExc_ValueError, |
| 8850 | "the environment variable is longer than %u characters", |
| 8851 | _MAX_ENV); |
| 8852 | goto error; |
| 8853 | } |
| 8854 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8855 | newenv = PyUnicode_AsUnicode(newstr); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 8856 | if (newenv == NULL) |
| 8857 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8858 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8859 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8860 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8861 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8862 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8863 | PyObject *os1, *os2; |
| 8864 | char *s1, *s2; |
| 8865 | char *newenv; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8866 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8867 | if (!PyArg_ParseTuple(args, |
| 8868 | "O&O&:putenv", |
| 8869 | PyUnicode_FSConverter, &os1, |
| 8870 | PyUnicode_FSConverter, &os2)) |
| 8871 | return NULL; |
| 8872 | s1 = PyBytes_AsString(os1); |
| 8873 | s2 = PyBytes_AsString(os2); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8874 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8875 | newstr = PyBytes_FromFormat("%s=%s", s1, s2); |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8876 | if (newstr == NULL) { |
| 8877 | PyErr_NoMemory(); |
| 8878 | goto error; |
| 8879 | } |
| 8880 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8881 | newenv = PyBytes_AS_STRING(newstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8882 | if (putenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8883 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8884 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8885 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8886 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8887 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8888 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 8889 | * this will cause previous value to be collected. This has to |
| 8890 | * happen after the real putenv() call because the old value |
| 8891 | * was still accessible until then. */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8892 | if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8893 | /* really not much we can do; just leak */ |
| 8894 | PyErr_Clear(); |
| 8895 | } |
| 8896 | else { |
| 8897 | Py_DECREF(newstr); |
| 8898 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8899 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8900 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8901 | Py_DECREF(os1); |
| 8902 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8903 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8904 | Py_RETURN_NONE; |
| 8905 | |
| 8906 | error: |
| 8907 | #ifndef MS_WINDOWS |
| 8908 | Py_DECREF(os1); |
| 8909 | Py_DECREF(os2); |
| 8910 | #endif |
| 8911 | Py_XDECREF(newstr); |
| 8912 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8913 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8914 | #endif /* putenv */ |
| 8915 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8916 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8917 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8918 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8919 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8920 | |
| 8921 | static PyObject * |
| 8922 | posix_unsetenv(PyObject *self, PyObject *args) |
| 8923 | { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8924 | PyObject *name; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8925 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8926 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8927 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8928 | |
| 8929 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8930 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8931 | PyUnicode_FSConverter, &name)) |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8932 | return NULL; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8933 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8934 | #ifdef HAVE_BROKEN_UNSETENV |
| 8935 | unsetenv(PyBytes_AS_STRING(name)); |
| 8936 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8937 | err = unsetenv(PyBytes_AS_STRING(name)); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8938 | if (err) { |
Benjamin Peterson | e8eb0e8 | 2011-11-22 23:14:47 -0600 | [diff] [blame] | 8939 | Py_DECREF(name); |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8940 | return posix_error(); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8941 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8942 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8943 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8944 | /* Remove the key from posix_putenv_garbage; |
| 8945 | * this will cause it to be collected. This has to |
| 8946 | * happen after the real unsetenv() call because the |
| 8947 | * old value was still accessible until then. |
| 8948 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8949 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8950 | /* really not much we can do; just leak */ |
| 8951 | PyErr_Clear(); |
| 8952 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8953 | Py_DECREF(name); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8954 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8955 | } |
| 8956 | #endif /* unsetenv */ |
| 8957 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8958 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8959 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8960 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8961 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 8962 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8963 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8964 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8965 | int code; |
| 8966 | char *message; |
| 8967 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 8968 | return NULL; |
| 8969 | message = strerror(code); |
| 8970 | if (message == NULL) { |
| 8971 | PyErr_SetString(PyExc_ValueError, |
| 8972 | "strerror() argument out of range"); |
| 8973 | return NULL; |
| 8974 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 8975 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8976 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8977 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8978 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8979 | #ifdef HAVE_SYS_WAIT_H |
| 8980 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8981 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8982 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8983 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8984 | 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] | 8985 | |
| 8986 | static PyObject * |
| 8987 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 8988 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8989 | WAIT_TYPE status; |
| 8990 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8991 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8992 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 8993 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8994 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8995 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8996 | } |
| 8997 | #endif /* WCOREDUMP */ |
| 8998 | |
| 8999 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9000 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9001 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9002 | 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] | 9003 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9004 | |
| 9005 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 9006 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9007 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9008 | WAIT_TYPE status; |
| 9009 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9010 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9011 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 9012 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9013 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9014 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9015 | } |
| 9016 | #endif /* WIFCONTINUED */ |
| 9017 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9018 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9019 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9020 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9021 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9022 | |
| 9023 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9024 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9025 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9026 | WAIT_TYPE status; |
| 9027 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9028 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9029 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 9030 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9031 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9032 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9033 | } |
| 9034 | #endif /* WIFSTOPPED */ |
| 9035 | |
| 9036 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9037 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9038 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9039 | 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] | 9040 | |
| 9041 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9042 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9043 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9044 | WAIT_TYPE status; |
| 9045 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9046 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9047 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 9048 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9049 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9050 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9051 | } |
| 9052 | #endif /* WIFSIGNALED */ |
| 9053 | |
| 9054 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9055 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9056 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 9057 | 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] | 9058 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9059 | |
| 9060 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9061 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9062 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9063 | WAIT_TYPE status; |
| 9064 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9065 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9066 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 9067 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9068 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9069 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9070 | } |
| 9071 | #endif /* WIFEXITED */ |
| 9072 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 9073 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9074 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9075 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9076 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9077 | |
| 9078 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9079 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9080 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9081 | WAIT_TYPE status; |
| 9082 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9083 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9084 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 9085 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9086 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9087 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9088 | } |
| 9089 | #endif /* WEXITSTATUS */ |
| 9090 | |
| 9091 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9092 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9093 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 9094 | 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] | 9095 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9096 | |
| 9097 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9098 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9099 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9100 | WAIT_TYPE status; |
| 9101 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9102 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9103 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 9104 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9105 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9106 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9107 | } |
| 9108 | #endif /* WTERMSIG */ |
| 9109 | |
| 9110 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9111 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9112 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9113 | Return the signal that stopped the process that provided\n\ |
| 9114 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9115 | |
| 9116 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9117 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9118 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9119 | WAIT_TYPE status; |
| 9120 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9121 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9122 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 9123 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9124 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9125 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9126 | } |
| 9127 | #endif /* WSTOPSIG */ |
| 9128 | |
| 9129 | #endif /* HAVE_SYS_WAIT_H */ |
| 9130 | |
| 9131 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9132 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 9133 | #ifdef _SCO_DS |
| 9134 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 9135 | needed definitions in sys/statvfs.h */ |
| 9136 | #define _SVID3 |
| 9137 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9138 | #include <sys/statvfs.h> |
| 9139 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9140 | static PyObject* |
| 9141 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9142 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 9143 | if (v == NULL) |
| 9144 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9145 | |
| 9146 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9147 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9148 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9149 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 9150 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 9151 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 9152 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 9153 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 9154 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 9155 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9156 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9157 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9158 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9159 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9160 | PyStructSequence_SET_ITEM(v, 2, |
| 9161 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 9162 | PyStructSequence_SET_ITEM(v, 3, |
| 9163 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 9164 | PyStructSequence_SET_ITEM(v, 4, |
| 9165 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 9166 | PyStructSequence_SET_ITEM(v, 5, |
| 9167 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 9168 | PyStructSequence_SET_ITEM(v, 6, |
| 9169 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 9170 | PyStructSequence_SET_ITEM(v, 7, |
| 9171 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 9172 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9173 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9174 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 9175 | if (PyErr_Occurred()) { |
| 9176 | Py_DECREF(v); |
| 9177 | return NULL; |
| 9178 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9179 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9180 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9181 | } |
| 9182 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9183 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9184 | "fstatvfs(fd) -> statvfs result\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9185 | Perform an fstatvfs system call on the given fd.\n\ |
| 9186 | Equivalent to statvfs(fd)."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9187 | |
| 9188 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9189 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9190 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9191 | int fd, res; |
| 9192 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9193 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9194 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 9195 | return NULL; |
| 9196 | Py_BEGIN_ALLOW_THREADS |
| 9197 | res = fstatvfs(fd, &st); |
| 9198 | Py_END_ALLOW_THREADS |
| 9199 | if (res != 0) |
| 9200 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9201 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9202 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9203 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9204 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9205 | |
| 9206 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9207 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9208 | #include <sys/statvfs.h> |
| 9209 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9210 | PyDoc_STRVAR(posix_statvfs__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9211 | "statvfs(path)\n\n\ |
| 9212 | Perform a statvfs system call on the given path.\n\ |
| 9213 | \n\ |
| 9214 | path may always be specified as a string.\n\ |
| 9215 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 9216 | If this functionality is unavailable, using it raises an exception."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9217 | |
| 9218 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9219 | posix_statvfs(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9220 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9221 | static char *keywords[] = {"path", NULL}; |
| 9222 | path_t path; |
| 9223 | int result; |
| 9224 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9225 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9226 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9227 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9228 | path.function_name = "statvfs"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9229 | #ifdef HAVE_FSTATVFS |
| 9230 | path.allow_fd = 1; |
| 9231 | #endif |
| 9232 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:statvfs", keywords, |
| 9233 | path_converter, &path |
| 9234 | )) |
| 9235 | return NULL; |
| 9236 | |
| 9237 | Py_BEGIN_ALLOW_THREADS |
| 9238 | #ifdef HAVE_FSTATVFS |
| 9239 | if (path.fd != -1) { |
| 9240 | #ifdef __APPLE__ |
| 9241 | /* handle weak-linking on Mac OS X 10.3 */ |
| 9242 | if (fstatvfs == NULL) { |
| 9243 | fd_specified("statvfs", path.fd); |
| 9244 | goto exit; |
| 9245 | } |
| 9246 | #endif |
| 9247 | result = fstatvfs(path.fd, &st); |
| 9248 | } |
| 9249 | else |
| 9250 | #endif |
| 9251 | result = statvfs(path.narrow, &st); |
| 9252 | Py_END_ALLOW_THREADS |
| 9253 | |
| 9254 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9255 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9256 | goto exit; |
| 9257 | } |
| 9258 | |
| 9259 | return_value = _pystatvfs_fromstructstatvfs(st); |
| 9260 | |
| 9261 | exit: |
| 9262 | path_cleanup(&path); |
| 9263 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9264 | } |
| 9265 | #endif /* HAVE_STATVFS */ |
| 9266 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9267 | #ifdef MS_WINDOWS |
| 9268 | PyDoc_STRVAR(win32__getdiskusage__doc__, |
| 9269 | "_getdiskusage(path) -> (total, free)\n\n\ |
| 9270 | Return disk usage statistics about the given path as (total, free) tuple."); |
| 9271 | |
| 9272 | static PyObject * |
| 9273 | win32__getdiskusage(PyObject *self, PyObject *args) |
| 9274 | { |
| 9275 | BOOL retval; |
| 9276 | ULARGE_INTEGER _, total, free; |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 9277 | const wchar_t *path; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9278 | |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 9279 | if (! PyArg_ParseTuple(args, "u", &path)) |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9280 | return NULL; |
| 9281 | |
| 9282 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 9283 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9284 | Py_END_ALLOW_THREADS |
| 9285 | if (retval == 0) |
| 9286 | return PyErr_SetFromWindowsErr(0); |
| 9287 | |
| 9288 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 9289 | } |
| 9290 | #endif |
| 9291 | |
| 9292 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9293 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 9294 | * It maps strings representing configuration variable names to |
| 9295 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 9296 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9297 | * rarely-used constants. There are three separate tables that use |
| 9298 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9299 | * |
| 9300 | * This code is always included, even if none of the interfaces that |
| 9301 | * need it are included. The #if hackery needed to avoid it would be |
| 9302 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9303 | */ |
| 9304 | struct constdef { |
| 9305 | char *name; |
| 9306 | long value; |
| 9307 | }; |
| 9308 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9309 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9310 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 9311 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9312 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9313 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9314 | *valuep = PyLong_AS_LONG(arg); |
| 9315 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9316 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 9317 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9318 | /* look up the value in the table using a binary search */ |
| 9319 | size_t lo = 0; |
| 9320 | size_t mid; |
| 9321 | size_t hi = tablesize; |
| 9322 | int cmp; |
| 9323 | const char *confname; |
| 9324 | if (!PyUnicode_Check(arg)) { |
| 9325 | PyErr_SetString(PyExc_TypeError, |
| 9326 | "configuration names must be strings or integers"); |
| 9327 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9328 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9329 | confname = _PyUnicode_AsString(arg); |
| 9330 | if (confname == NULL) |
| 9331 | return 0; |
| 9332 | while (lo < hi) { |
| 9333 | mid = (lo + hi) / 2; |
| 9334 | cmp = strcmp(confname, table[mid].name); |
| 9335 | if (cmp < 0) |
| 9336 | hi = mid; |
| 9337 | else if (cmp > 0) |
| 9338 | lo = mid + 1; |
| 9339 | else { |
| 9340 | *valuep = table[mid].value; |
| 9341 | return 1; |
| 9342 | } |
| 9343 | } |
| 9344 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 9345 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9346 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9347 | } |
| 9348 | |
| 9349 | |
| 9350 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 9351 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9352 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9353 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9354 | #endif |
| 9355 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9356 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9357 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9358 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9359 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9360 | #endif |
| 9361 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9362 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9363 | #endif |
| 9364 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9365 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9366 | #endif |
| 9367 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9368 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9369 | #endif |
| 9370 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9371 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9372 | #endif |
| 9373 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9374 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9375 | #endif |
| 9376 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9377 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9378 | #endif |
| 9379 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9380 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9381 | #endif |
| 9382 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9383 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9384 | #endif |
| 9385 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9386 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9387 | #endif |
| 9388 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9389 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9390 | #endif |
| 9391 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9392 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9393 | #endif |
| 9394 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9395 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9396 | #endif |
| 9397 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9398 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9399 | #endif |
| 9400 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9401 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9402 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 9403 | #ifdef _PC_ACL_ENABLED |
| 9404 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 9405 | #endif |
| 9406 | #ifdef _PC_MIN_HOLE_SIZE |
| 9407 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 9408 | #endif |
| 9409 | #ifdef _PC_ALLOC_SIZE_MIN |
| 9410 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 9411 | #endif |
| 9412 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 9413 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 9414 | #endif |
| 9415 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 9416 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 9417 | #endif |
| 9418 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 9419 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 9420 | #endif |
| 9421 | #ifdef _PC_REC_XFER_ALIGN |
| 9422 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 9423 | #endif |
| 9424 | #ifdef _PC_SYMLINK_MAX |
| 9425 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 9426 | #endif |
| 9427 | #ifdef _PC_XATTR_ENABLED |
| 9428 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 9429 | #endif |
| 9430 | #ifdef _PC_XATTR_EXISTS |
| 9431 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 9432 | #endif |
| 9433 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 9434 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 9435 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9436 | }; |
| 9437 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9438 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9439 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9440 | { |
| 9441 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 9442 | sizeof(posix_constants_pathconf) |
| 9443 | / sizeof(struct constdef)); |
| 9444 | } |
| 9445 | #endif |
| 9446 | |
| 9447 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9448 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9449 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9450 | 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] | 9451 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9452 | |
| 9453 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9454 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9455 | { |
| 9456 | PyObject *result = NULL; |
| 9457 | int name, fd; |
| 9458 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9459 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 9460 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9461 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9462 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9463 | errno = 0; |
| 9464 | limit = fpathconf(fd, name); |
| 9465 | if (limit == -1 && errno != 0) |
| 9466 | posix_error(); |
| 9467 | else |
| 9468 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9469 | } |
| 9470 | return result; |
| 9471 | } |
| 9472 | #endif |
| 9473 | |
| 9474 | |
| 9475 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9476 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9477 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9478 | Return the configuration limit name for the file or directory path.\n\ |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9479 | If there is no limit, return -1.\n\ |
| 9480 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 9481 | If this functionality is unavailable, using it raises an exception."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9482 | |
| 9483 | static PyObject * |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9484 | posix_pathconf(PyObject *self, PyObject *args, PyObject *kwargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9485 | { |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9486 | path_t path; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9487 | PyObject *result = NULL; |
| 9488 | int name; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9489 | static char *keywords[] = {"path", "name", NULL}; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9490 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9491 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9492 | path.function_name = "pathconf"; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9493 | #ifdef HAVE_FPATHCONF |
| 9494 | path.allow_fd = 1; |
| 9495 | #endif |
| 9496 | if (PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:pathconf", keywords, |
| 9497 | path_converter, &path, |
| 9498 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9499 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9500 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9501 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9502 | #ifdef HAVE_FPATHCONF |
| 9503 | if (path.fd != -1) |
| 9504 | limit = fpathconf(path.fd, name); |
| 9505 | else |
| 9506 | #endif |
| 9507 | limit = pathconf(path.narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9508 | if (limit == -1 && errno != 0) { |
| 9509 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 9510 | /* could be a path or name problem */ |
| 9511 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9512 | else |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9513 | result = path_error(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9514 | } |
| 9515 | else |
| 9516 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9517 | } |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9518 | path_cleanup(&path); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9519 | return result; |
| 9520 | } |
| 9521 | #endif |
| 9522 | |
| 9523 | #ifdef HAVE_CONFSTR |
| 9524 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9525 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9526 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9527 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9528 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9529 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9530 | #endif |
| 9531 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9532 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9533 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9534 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9535 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9536 | #endif |
| 9537 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9538 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9539 | #endif |
| 9540 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9541 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9542 | #endif |
| 9543 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9544 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9545 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9546 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9547 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9548 | #endif |
| 9549 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9550 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9551 | #endif |
| 9552 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9553 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9554 | #endif |
| 9555 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9556 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9557 | #endif |
| 9558 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9559 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9560 | #endif |
| 9561 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9562 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9563 | #endif |
| 9564 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9565 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9566 | #endif |
| 9567 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9568 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9569 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9570 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9571 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9572 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9573 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9574 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9575 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9576 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9577 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9578 | #endif |
| 9579 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9580 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9581 | #endif |
| 9582 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9583 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9584 | #endif |
| 9585 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9586 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9587 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9588 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9589 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9590 | #endif |
| 9591 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9592 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9593 | #endif |
| 9594 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9595 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9596 | #endif |
| 9597 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9598 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9599 | #endif |
| 9600 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9601 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9602 | #endif |
| 9603 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9604 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9605 | #endif |
| 9606 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9607 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9608 | #endif |
| 9609 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9610 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9611 | #endif |
| 9612 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9613 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9614 | #endif |
| 9615 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9616 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9617 | #endif |
| 9618 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9619 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9620 | #endif |
| 9621 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9622 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9623 | #endif |
| 9624 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9625 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9626 | #endif |
| 9627 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9628 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9629 | #endif |
| 9630 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9631 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9632 | #endif |
| 9633 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9634 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9635 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9636 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9637 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9638 | #endif |
| 9639 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9640 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9641 | #endif |
| 9642 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9643 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9644 | #endif |
| 9645 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9646 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9647 | #endif |
| 9648 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9649 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9650 | #endif |
| 9651 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9652 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9653 | #endif |
| 9654 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9655 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9656 | #endif |
| 9657 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9658 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9659 | #endif |
| 9660 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9661 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9662 | #endif |
| 9663 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9664 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9665 | #endif |
| 9666 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9667 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9668 | #endif |
| 9669 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9670 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9671 | #endif |
| 9672 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9673 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9674 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9675 | }; |
| 9676 | |
| 9677 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9678 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9679 | { |
| 9680 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 9681 | sizeof(posix_constants_confstr) |
| 9682 | / sizeof(struct constdef)); |
| 9683 | } |
| 9684 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9685 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9686 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9687 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9688 | |
| 9689 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9690 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9691 | { |
| 9692 | PyObject *result = NULL; |
| 9693 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9694 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9695 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9696 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9697 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 9698 | return NULL; |
| 9699 | |
| 9700 | errno = 0; |
| 9701 | len = confstr(name, buffer, sizeof(buffer)); |
| 9702 | if (len == 0) { |
| 9703 | if (errno) { |
| 9704 | posix_error(); |
| 9705 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9706 | } |
| 9707 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9708 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9709 | } |
| 9710 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9711 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9712 | if (len >= sizeof(buffer)) { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9713 | char *buf = PyMem_Malloc(len); |
| 9714 | if (buf == NULL) |
| 9715 | return PyErr_NoMemory(); |
| 9716 | confstr(name, buf, len); |
| 9717 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 9718 | PyMem_Free(buf); |
| 9719 | } |
| 9720 | else |
| 9721 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9722 | return result; |
| 9723 | } |
| 9724 | #endif |
| 9725 | |
| 9726 | |
| 9727 | #ifdef HAVE_SYSCONF |
| 9728 | static struct constdef posix_constants_sysconf[] = { |
| 9729 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9730 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9731 | #endif |
| 9732 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9733 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9734 | #endif |
| 9735 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9736 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9737 | #endif |
| 9738 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9739 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9740 | #endif |
| 9741 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9742 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9743 | #endif |
| 9744 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9745 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9746 | #endif |
| 9747 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9748 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9749 | #endif |
| 9750 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9751 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9752 | #endif |
| 9753 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9754 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9755 | #endif |
| 9756 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9757 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9758 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9759 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9760 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9761 | #endif |
| 9762 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9763 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9764 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9765 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9766 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9767 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9768 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9769 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9770 | #endif |
| 9771 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9772 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9773 | #endif |
| 9774 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9775 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9776 | #endif |
| 9777 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9778 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9779 | #endif |
| 9780 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9781 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9782 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9783 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9784 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9785 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9786 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9787 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9788 | #endif |
| 9789 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9790 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9791 | #endif |
| 9792 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9793 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9794 | #endif |
| 9795 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9796 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9797 | #endif |
| 9798 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9799 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9800 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9801 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9802 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9803 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9804 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9805 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9806 | #endif |
| 9807 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9808 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9809 | #endif |
| 9810 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9811 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9812 | #endif |
| 9813 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9814 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9815 | #endif |
| 9816 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9817 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9818 | #endif |
| 9819 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9820 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9821 | #endif |
| 9822 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9823 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9824 | #endif |
| 9825 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9826 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9827 | #endif |
| 9828 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9829 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9830 | #endif |
| 9831 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9832 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9833 | #endif |
| 9834 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9835 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9836 | #endif |
| 9837 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9838 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9839 | #endif |
| 9840 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9841 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9842 | #endif |
| 9843 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9844 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9845 | #endif |
| 9846 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9847 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9848 | #endif |
| 9849 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9850 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9851 | #endif |
| 9852 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9853 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9854 | #endif |
| 9855 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9856 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9857 | #endif |
| 9858 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9859 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9860 | #endif |
| 9861 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9862 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9863 | #endif |
| 9864 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9865 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9866 | #endif |
| 9867 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9868 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9869 | #endif |
| 9870 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9871 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9872 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9873 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9874 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9875 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9876 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9877 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9878 | #endif |
| 9879 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9880 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9881 | #endif |
| 9882 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9883 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9884 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9885 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9886 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9887 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9888 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9889 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9890 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9891 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9892 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9893 | #endif |
| 9894 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9895 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9896 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9897 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9898 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9899 | #endif |
| 9900 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9901 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9902 | #endif |
| 9903 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9904 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9905 | #endif |
| 9906 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9907 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9908 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9909 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9910 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9911 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9912 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9913 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9914 | #endif |
| 9915 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9916 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9917 | #endif |
| 9918 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9919 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9920 | #endif |
| 9921 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9922 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9923 | #endif |
| 9924 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9925 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9926 | #endif |
| 9927 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9928 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9929 | #endif |
| 9930 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9931 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9932 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9933 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9934 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9935 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9936 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9937 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9938 | #endif |
| 9939 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9940 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9941 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9942 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9943 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9944 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9945 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9946 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9947 | #endif |
| 9948 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9949 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9950 | #endif |
| 9951 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9952 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9953 | #endif |
| 9954 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9955 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9956 | #endif |
| 9957 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9958 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9959 | #endif |
| 9960 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9961 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9962 | #endif |
| 9963 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9964 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9965 | #endif |
| 9966 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9967 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9968 | #endif |
| 9969 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9970 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9971 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9972 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9973 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9974 | #endif |
| 9975 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9976 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9977 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9978 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9979 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9980 | #endif |
| 9981 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9982 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9983 | #endif |
| 9984 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9985 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9986 | #endif |
| 9987 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9988 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9989 | #endif |
| 9990 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9991 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9992 | #endif |
| 9993 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9994 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9995 | #endif |
| 9996 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9997 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9998 | #endif |
| 9999 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10000 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10001 | #endif |
| 10002 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10003 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10004 | #endif |
| 10005 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10006 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10007 | #endif |
| 10008 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10009 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10010 | #endif |
| 10011 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10012 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10013 | #endif |
| 10014 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10015 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10016 | #endif |
| 10017 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10018 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10019 | #endif |
| 10020 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10021 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10022 | #endif |
| 10023 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10024 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10025 | #endif |
| 10026 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10027 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10028 | #endif |
| 10029 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10030 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10031 | #endif |
| 10032 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10033 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10034 | #endif |
| 10035 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10036 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10037 | #endif |
| 10038 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10039 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10040 | #endif |
| 10041 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10042 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10043 | #endif |
| 10044 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10045 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10046 | #endif |
| 10047 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10048 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10049 | #endif |
| 10050 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10051 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10052 | #endif |
| 10053 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10054 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10055 | #endif |
| 10056 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10057 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10058 | #endif |
| 10059 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10060 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10061 | #endif |
| 10062 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10063 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10064 | #endif |
| 10065 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10066 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10067 | #endif |
| 10068 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10069 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10070 | #endif |
| 10071 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10072 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10073 | #endif |
| 10074 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10075 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10076 | #endif |
| 10077 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10078 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10079 | #endif |
| 10080 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10081 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10082 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10083 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10084 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10085 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10086 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10087 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10088 | #endif |
| 10089 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10090 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10091 | #endif |
| 10092 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10093 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10094 | #endif |
| 10095 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10096 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10097 | #endif |
| 10098 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10099 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10100 | #endif |
| 10101 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10102 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10103 | #endif |
| 10104 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10105 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10106 | #endif |
| 10107 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10108 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10109 | #endif |
| 10110 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10111 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10112 | #endif |
| 10113 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10114 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10115 | #endif |
| 10116 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10117 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10118 | #endif |
| 10119 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10120 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10121 | #endif |
| 10122 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10123 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10124 | #endif |
| 10125 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10126 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10127 | #endif |
| 10128 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10129 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10130 | #endif |
| 10131 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10132 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10133 | #endif |
| 10134 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10135 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10136 | #endif |
| 10137 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10138 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10139 | #endif |
| 10140 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10141 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10142 | #endif |
| 10143 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10144 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10145 | #endif |
| 10146 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10147 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10148 | #endif |
| 10149 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10150 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10151 | #endif |
| 10152 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10153 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10154 | #endif |
| 10155 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10156 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10157 | #endif |
| 10158 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10159 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10160 | #endif |
| 10161 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10162 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10163 | #endif |
| 10164 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10165 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10166 | #endif |
| 10167 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10168 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10169 | #endif |
| 10170 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10171 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10172 | #endif |
| 10173 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10174 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10175 | #endif |
| 10176 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10177 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10178 | #endif |
| 10179 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10180 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10181 | #endif |
| 10182 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10183 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10184 | #endif |
| 10185 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10186 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10187 | #endif |
| 10188 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10189 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10190 | #endif |
| 10191 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10192 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10193 | #endif |
| 10194 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10195 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10196 | #endif |
| 10197 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10198 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10199 | #endif |
| 10200 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10201 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10202 | #endif |
| 10203 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10204 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10205 | #endif |
| 10206 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10207 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10208 | #endif |
| 10209 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10210 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10211 | #endif |
| 10212 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10213 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10214 | #endif |
| 10215 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10216 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10217 | #endif |
| 10218 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10219 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10220 | #endif |
| 10221 | }; |
| 10222 | |
| 10223 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10224 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10225 | { |
| 10226 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 10227 | sizeof(posix_constants_sysconf) |
| 10228 | / sizeof(struct constdef)); |
| 10229 | } |
| 10230 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10231 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 10232 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10233 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10234 | |
| 10235 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10236 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10237 | { |
| 10238 | PyObject *result = NULL; |
| 10239 | int name; |
| 10240 | |
| 10241 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
Victor Stinner | 6fdd7b8 | 2013-05-16 22:26:29 +0200 | [diff] [blame] | 10242 | long value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10243 | |
| 10244 | errno = 0; |
| 10245 | value = sysconf(name); |
| 10246 | if (value == -1 && errno != 0) |
| 10247 | posix_error(); |
| 10248 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10249 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10250 | } |
| 10251 | return result; |
| 10252 | } |
| 10253 | #endif |
| 10254 | |
| 10255 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10256 | /* This code is used to ensure that the tables of configuration value names |
| 10257 | * are in sorted order as required by conv_confname(), and also to build the |
| 10258 | * the exported dictionaries that are used to publish information about the |
| 10259 | * names available on the host platform. |
| 10260 | * |
| 10261 | * Sorting the table at runtime ensures that the table is properly ordered |
| 10262 | * when used, even for platforms we're not able to test on. It also makes |
| 10263 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10264 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10265 | |
| 10266 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10267 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10268 | { |
| 10269 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10270 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10271 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10272 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10273 | |
| 10274 | return strcmp(c1->name, c2->name); |
| 10275 | } |
| 10276 | |
| 10277 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10278 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10279 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10280 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10281 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10282 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10283 | |
| 10284 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 10285 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10286 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10287 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10288 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10289 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10290 | PyObject *o = PyLong_FromLong(table[i].value); |
| 10291 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 10292 | Py_XDECREF(o); |
| 10293 | Py_DECREF(d); |
| 10294 | return -1; |
| 10295 | } |
| 10296 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10297 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10298 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10299 | } |
| 10300 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10301 | /* Return -1 on failure, 0 on success. */ |
| 10302 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10303 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10304 | { |
| 10305 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10306 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10307 | sizeof(posix_constants_pathconf) |
| 10308 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10309 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10310 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10311 | #endif |
| 10312 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10313 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10314 | sizeof(posix_constants_confstr) |
| 10315 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10316 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10317 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10318 | #endif |
| 10319 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10320 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10321 | sizeof(posix_constants_sysconf) |
| 10322 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10323 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10324 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10325 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10326 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10327 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10328 | |
| 10329 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10330 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 10331 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10332 | 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] | 10333 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10334 | |
| 10335 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 10336 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10337 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10338 | abort(); |
| 10339 | /*NOTREACHED*/ |
| 10340 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 10341 | return NULL; |
| 10342 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10343 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10344 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10345 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 10346 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 10347 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10348 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 10349 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 10350 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 10351 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 10352 | application (if any) its extension is associated.\n\ |
| 10353 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 10354 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10355 | \n\ |
| 10356 | startfile returns as soon as the associated application is launched.\n\ |
| 10357 | There is no option to wait for the application to close, and no way\n\ |
| 10358 | to retrieve the application's exit status.\n\ |
| 10359 | \n\ |
| 10360 | The filepath is relative to the current directory. If you want to use\n\ |
| 10361 | 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] | 10362 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10363 | |
| 10364 | static PyObject * |
| 10365 | win32_startfile(PyObject *self, PyObject *args) |
| 10366 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10367 | PyObject *ofilepath; |
| 10368 | char *filepath; |
| 10369 | char *operation = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10370 | wchar_t *wpath, *woperation; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10371 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 10372 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10373 | PyObject *unipath, *uoperation = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10374 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 10375 | &unipath, &operation)) { |
| 10376 | PyErr_Clear(); |
| 10377 | goto normal; |
| 10378 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10379 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10380 | if (operation) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10381 | uoperation = PyUnicode_DecodeASCII(operation, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10382 | strlen(operation), NULL); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10383 | if (!uoperation) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10384 | PyErr_Clear(); |
| 10385 | operation = NULL; |
| 10386 | goto normal; |
| 10387 | } |
| 10388 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10389 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10390 | wpath = PyUnicode_AsUnicode(unipath); |
| 10391 | if (wpath == NULL) |
| 10392 | goto normal; |
| 10393 | if (uoperation) { |
| 10394 | woperation = PyUnicode_AsUnicode(uoperation); |
| 10395 | if (woperation == NULL) |
| 10396 | goto normal; |
| 10397 | } |
| 10398 | else |
| 10399 | woperation = NULL; |
| 10400 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10401 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10402 | rc = ShellExecuteW((HWND)0, woperation, wpath, |
| 10403 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10404 | Py_END_ALLOW_THREADS |
| 10405 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10406 | Py_XDECREF(uoperation); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10407 | if (rc <= (HINSTANCE)32) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10408 | win32_error_object("startfile", unipath); |
| 10409 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10410 | } |
| 10411 | Py_INCREF(Py_None); |
| 10412 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10413 | |
| 10414 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10415 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 10416 | PyUnicode_FSConverter, &ofilepath, |
| 10417 | &operation)) |
| 10418 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 10419 | if (win32_warn_bytes_api()) { |
| 10420 | Py_DECREF(ofilepath); |
| 10421 | return NULL; |
| 10422 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10423 | filepath = PyBytes_AsString(ofilepath); |
| 10424 | Py_BEGIN_ALLOW_THREADS |
| 10425 | rc = ShellExecute((HWND)0, operation, filepath, |
| 10426 | NULL, NULL, SW_SHOWNORMAL); |
| 10427 | Py_END_ALLOW_THREADS |
| 10428 | if (rc <= (HINSTANCE)32) { |
| 10429 | PyObject *errval = win32_error("startfile", filepath); |
| 10430 | Py_DECREF(ofilepath); |
| 10431 | return errval; |
| 10432 | } |
| 10433 | Py_DECREF(ofilepath); |
| 10434 | Py_INCREF(Py_None); |
| 10435 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10436 | } |
| 10437 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10438 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10439 | #ifdef HAVE_GETLOADAVG |
| 10440 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 10441 | "getloadavg() -> (float, float, float)\n\n\ |
| 10442 | Return the number of processes in the system run queue averaged over\n\ |
| 10443 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 10444 | was unobtainable"); |
| 10445 | |
| 10446 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 10447 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10448 | { |
| 10449 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10450 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10451 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 10452 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10453 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10454 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10455 | } |
| 10456 | #endif |
| 10457 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 10458 | PyDoc_STRVAR(device_encoding__doc__, |
| 10459 | "device_encoding(fd) -> str\n\n\ |
| 10460 | Return a string describing the encoding of the device\n\ |
| 10461 | if the output is a terminal; else return None."); |
| 10462 | |
| 10463 | static PyObject * |
| 10464 | device_encoding(PyObject *self, PyObject *args) |
| 10465 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10466 | int fd; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10467 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10468 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 10469 | return NULL; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10470 | |
| 10471 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 10472 | } |
| 10473 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10474 | #ifdef HAVE_SETRESUID |
| 10475 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 10476 | "setresuid(ruid, euid, suid)\n\n\ |
| 10477 | Set the current process's real, effective, and saved user ids."); |
| 10478 | |
| 10479 | static PyObject* |
| 10480 | posix_setresuid (PyObject *self, PyObject *args) |
| 10481 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10482 | /* We assume uid_t is no larger than a long. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10483 | uid_t ruid, euid, suid; |
| 10484 | if (!PyArg_ParseTuple(args, "O&O&O&:setresuid", |
| 10485 | _Py_Uid_Converter, &ruid, |
| 10486 | _Py_Uid_Converter, &euid, |
| 10487 | _Py_Uid_Converter, &suid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10488 | return NULL; |
| 10489 | if (setresuid(ruid, euid, suid) < 0) |
| 10490 | return posix_error(); |
| 10491 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10492 | } |
| 10493 | #endif |
| 10494 | |
| 10495 | #ifdef HAVE_SETRESGID |
| 10496 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 10497 | "setresgid(rgid, egid, sgid)\n\n\ |
| 10498 | Set the current process's real, effective, and saved group ids."); |
| 10499 | |
| 10500 | static PyObject* |
| 10501 | posix_setresgid (PyObject *self, PyObject *args) |
| 10502 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10503 | gid_t rgid, egid, sgid; |
| 10504 | if (!PyArg_ParseTuple(args, "O&O&O&:setresgid", |
| 10505 | _Py_Gid_Converter, &rgid, |
| 10506 | _Py_Gid_Converter, &egid, |
| 10507 | _Py_Gid_Converter, &sgid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10508 | return NULL; |
| 10509 | if (setresgid(rgid, egid, sgid) < 0) |
| 10510 | return posix_error(); |
| 10511 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10512 | } |
| 10513 | #endif |
| 10514 | |
| 10515 | #ifdef HAVE_GETRESUID |
| 10516 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 10517 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 10518 | Get tuple of the current process's real, effective, and saved user ids."); |
| 10519 | |
| 10520 | static PyObject* |
| 10521 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 10522 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10523 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10524 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 10525 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10526 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 10527 | _PyLong_FromUid(euid), |
| 10528 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10529 | } |
| 10530 | #endif |
| 10531 | |
| 10532 | #ifdef HAVE_GETRESGID |
| 10533 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 10534 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 10535 | 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] | 10536 | |
| 10537 | static PyObject* |
| 10538 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 10539 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10540 | uid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10541 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 10542 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10543 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 10544 | _PyLong_FromGid(egid), |
| 10545 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10546 | } |
| 10547 | #endif |
| 10548 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10549 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10550 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10551 | PyDoc_STRVAR(posix_getxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10552 | "getxattr(path, attribute, *, follow_symlinks=True) -> value\n\n\ |
| 10553 | Return the value of extended attribute attribute on path.\n\ |
| 10554 | \n\ |
| 10555 | path may be either a string or an open file descriptor.\n\ |
| 10556 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10557 | link, getxattr will examine the symbolic link itself instead of the file\n\ |
| 10558 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10559 | |
| 10560 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10561 | posix_getxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10562 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10563 | path_t path; |
| 10564 | path_t attribute; |
| 10565 | int follow_symlinks = 1; |
| 10566 | PyObject *buffer = NULL; |
| 10567 | int i; |
| 10568 | static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10569 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10570 | memset(&path, 0, sizeof(path)); |
| 10571 | memset(&attribute, 0, sizeof(attribute)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10572 | path.function_name = "getxattr"; |
| 10573 | attribute.function_name = "getxattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10574 | path.allow_fd = 1; |
| 10575 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:getxattr", keywords, |
| 10576 | path_converter, &path, |
| 10577 | path_converter, &attribute, |
| 10578 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10579 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10580 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10581 | if (fd_and_follow_symlinks_invalid("getxattr", path.fd, follow_symlinks)) |
| 10582 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10583 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10584 | for (i = 0; ; i++) { |
| 10585 | void *ptr; |
| 10586 | ssize_t result; |
| 10587 | static Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
| 10588 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10589 | if (!buffer_size) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10590 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10591 | goto exit; |
| 10592 | } |
| 10593 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 10594 | if (!buffer) |
| 10595 | goto exit; |
| 10596 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10597 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10598 | Py_BEGIN_ALLOW_THREADS; |
| 10599 | if (path.fd >= 0) |
| 10600 | result = fgetxattr(path.fd, attribute.narrow, ptr, buffer_size); |
| 10601 | else if (follow_symlinks) |
| 10602 | result = getxattr(path.narrow, attribute.narrow, ptr, buffer_size); |
| 10603 | else |
| 10604 | result = lgetxattr(path.narrow, attribute.narrow, ptr, buffer_size); |
| 10605 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10606 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10607 | if (result < 0) { |
| 10608 | Py_DECREF(buffer); |
| 10609 | buffer = NULL; |
| 10610 | if (errno == ERANGE) |
| 10611 | continue; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10612 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10613 | goto exit; |
| 10614 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10615 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10616 | if (result != buffer_size) { |
| 10617 | /* Can only shrink. */ |
| 10618 | _PyBytes_Resize(&buffer, result); |
| 10619 | } |
| 10620 | break; |
| 10621 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10622 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10623 | exit: |
| 10624 | path_cleanup(&path); |
| 10625 | path_cleanup(&attribute); |
| 10626 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10627 | } |
| 10628 | |
| 10629 | PyDoc_STRVAR(posix_setxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10630 | "setxattr(path, attribute, value, flags=0, *, follow_symlinks=True)\n\n\ |
| 10631 | Set extended attribute attribute on path to value.\n\ |
| 10632 | path may be either a string or an open file descriptor.\n\ |
| 10633 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10634 | link, setxattr will modify the symbolic link itself instead of the file\n\ |
| 10635 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10636 | |
| 10637 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10638 | posix_setxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10639 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10640 | path_t path; |
| 10641 | path_t attribute; |
| 10642 | Py_buffer value; |
| 10643 | int flags = 0; |
| 10644 | int follow_symlinks = 1; |
| 10645 | int result; |
| 10646 | PyObject *return_value = NULL; |
| 10647 | static char *keywords[] = {"path", "attribute", "value", |
| 10648 | "flags", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10649 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10650 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10651 | path.function_name = "setxattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10652 | path.allow_fd = 1; |
| 10653 | memset(&attribute, 0, sizeof(attribute)); |
| 10654 | memset(&value, 0, sizeof(value)); |
| 10655 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&y*|i$p:setxattr", |
| 10656 | keywords, |
| 10657 | path_converter, &path, |
| 10658 | path_converter, &attribute, |
| 10659 | &value, &flags, |
| 10660 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10661 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10662 | |
| 10663 | if (fd_and_follow_symlinks_invalid("setxattr", path.fd, follow_symlinks)) |
| 10664 | goto exit; |
| 10665 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10666 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10667 | if (path.fd > -1) |
| 10668 | result = fsetxattr(path.fd, attribute.narrow, |
| 10669 | value.buf, value.len, flags); |
| 10670 | else if (follow_symlinks) |
| 10671 | result = setxattr(path.narrow, attribute.narrow, |
| 10672 | value.buf, value.len, flags); |
| 10673 | else |
| 10674 | result = lsetxattr(path.narrow, attribute.narrow, |
| 10675 | value.buf, value.len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10676 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10677 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10678 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10679 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10680 | goto exit; |
| 10681 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10682 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10683 | return_value = Py_None; |
| 10684 | Py_INCREF(return_value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10685 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10686 | exit: |
| 10687 | path_cleanup(&path); |
| 10688 | path_cleanup(&attribute); |
| 10689 | PyBuffer_Release(&value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10690 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10691 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10692 | } |
| 10693 | |
| 10694 | PyDoc_STRVAR(posix_removexattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10695 | "removexattr(path, attribute, *, follow_symlinks=True)\n\n\ |
| 10696 | Remove extended attribute attribute on path.\n\ |
| 10697 | path may be either a string or an open file descriptor.\n\ |
| 10698 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10699 | link, removexattr will modify the symbolic link itself instead of the file\n\ |
| 10700 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10701 | |
| 10702 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10703 | posix_removexattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10704 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10705 | path_t path; |
| 10706 | path_t attribute; |
| 10707 | int follow_symlinks = 1; |
| 10708 | int result; |
| 10709 | PyObject *return_value = NULL; |
| 10710 | static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10711 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10712 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10713 | path.function_name = "removexattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10714 | memset(&attribute, 0, sizeof(attribute)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10715 | attribute.function_name = "removexattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10716 | path.allow_fd = 1; |
| 10717 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:removexattr", |
| 10718 | keywords, |
| 10719 | path_converter, &path, |
| 10720 | path_converter, &attribute, |
| 10721 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10722 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10723 | |
| 10724 | if (fd_and_follow_symlinks_invalid("removexattr", path.fd, follow_symlinks)) |
| 10725 | goto exit; |
| 10726 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10727 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10728 | if (path.fd > -1) |
| 10729 | result = fremovexattr(path.fd, attribute.narrow); |
| 10730 | else if (follow_symlinks) |
| 10731 | result = removexattr(path.narrow, attribute.narrow); |
| 10732 | else |
| 10733 | result = lremovexattr(path.narrow, attribute.narrow); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10734 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10735 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10736 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10737 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10738 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10739 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10740 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10741 | return_value = Py_None; |
| 10742 | Py_INCREF(return_value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10743 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10744 | exit: |
| 10745 | path_cleanup(&path); |
| 10746 | path_cleanup(&attribute); |
| 10747 | |
| 10748 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10749 | } |
| 10750 | |
| 10751 | PyDoc_STRVAR(posix_listxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10752 | "listxattr(path='.', *, follow_symlinks=True)\n\n\ |
| 10753 | Return a list of extended attributes on path.\n\ |
| 10754 | \n\ |
| 10755 | path may be either None, a string, or an open file descriptor.\n\ |
| 10756 | if path is None, listxattr will examine the current directory.\n\ |
| 10757 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10758 | link, listxattr will examine the symbolic link itself instead of the file\n\ |
| 10759 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10760 | |
| 10761 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10762 | posix_listxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10763 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10764 | path_t path; |
| 10765 | int follow_symlinks = 1; |
| 10766 | Py_ssize_t i; |
| 10767 | PyObject *result = NULL; |
| 10768 | char *buffer = NULL; |
| 10769 | char *name; |
| 10770 | static char *keywords[] = {"path", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10771 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10772 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10773 | path.function_name = "listxattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10774 | path.allow_fd = 1; |
| 10775 | path.fd = -1; |
| 10776 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&$p:listxattr", keywords, |
| 10777 | path_converter, &path, |
| 10778 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10779 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10780 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10781 | if (fd_and_follow_symlinks_invalid("listxattr", path.fd, follow_symlinks)) |
| 10782 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10783 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10784 | name = path.narrow ? path.narrow : "."; |
| 10785 | for (i = 0; ; i++) { |
| 10786 | char *start, *trace, *end; |
| 10787 | ssize_t length; |
| 10788 | static Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
| 10789 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10790 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 10791 | /* ERANGE */ |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10792 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10793 | break; |
| 10794 | } |
| 10795 | buffer = PyMem_MALLOC(buffer_size); |
| 10796 | if (!buffer) { |
| 10797 | PyErr_NoMemory(); |
| 10798 | break; |
| 10799 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10800 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10801 | Py_BEGIN_ALLOW_THREADS; |
| 10802 | if (path.fd > -1) |
| 10803 | length = flistxattr(path.fd, buffer, buffer_size); |
| 10804 | else if (follow_symlinks) |
| 10805 | length = listxattr(name, buffer, buffer_size); |
| 10806 | else |
| 10807 | length = llistxattr(name, buffer, buffer_size); |
| 10808 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10809 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10810 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 10811 | if (errno == ERANGE) { |
| 10812 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 10813 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10814 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 10815 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10816 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10817 | break; |
| 10818 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10819 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10820 | result = PyList_New(0); |
| 10821 | if (!result) { |
| 10822 | goto exit; |
| 10823 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10824 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10825 | end = buffer + length; |
| 10826 | for (trace = start = buffer; trace != end; trace++) { |
| 10827 | if (!*trace) { |
| 10828 | int error; |
| 10829 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 10830 | trace - start); |
| 10831 | if (!attribute) { |
| 10832 | Py_DECREF(result); |
| 10833 | result = NULL; |
| 10834 | goto exit; |
| 10835 | } |
| 10836 | error = PyList_Append(result, attribute); |
| 10837 | Py_DECREF(attribute); |
| 10838 | if (error) { |
| 10839 | Py_DECREF(result); |
| 10840 | result = NULL; |
| 10841 | goto exit; |
| 10842 | } |
| 10843 | start = trace + 1; |
| 10844 | } |
| 10845 | } |
| 10846 | break; |
| 10847 | } |
| 10848 | exit: |
| 10849 | path_cleanup(&path); |
| 10850 | if (buffer) |
| 10851 | PyMem_FREE(buffer); |
| 10852 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10853 | } |
| 10854 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10855 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10856 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10857 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10858 | PyDoc_STRVAR(posix_urandom__doc__, |
| 10859 | "urandom(n) -> str\n\n\ |
| 10860 | Return n random bytes suitable for cryptographic use."); |
| 10861 | |
| 10862 | static PyObject * |
| 10863 | posix_urandom(PyObject *self, PyObject *args) |
| 10864 | { |
| 10865 | Py_ssize_t size; |
| 10866 | PyObject *result; |
| 10867 | int ret; |
| 10868 | |
| 10869 | /* Read arguments */ |
| 10870 | if (!PyArg_ParseTuple(args, "n:urandom", &size)) |
| 10871 | return NULL; |
| 10872 | if (size < 0) |
| 10873 | return PyErr_Format(PyExc_ValueError, |
| 10874 | "negative argument not allowed"); |
| 10875 | result = PyBytes_FromStringAndSize(NULL, size); |
| 10876 | if (result == NULL) |
| 10877 | return NULL; |
| 10878 | |
| 10879 | ret = _PyOS_URandom(PyBytes_AS_STRING(result), |
| 10880 | PyBytes_GET_SIZE(result)); |
| 10881 | if (ret == -1) { |
| 10882 | Py_DECREF(result); |
| 10883 | return NULL; |
| 10884 | } |
| 10885 | return result; |
| 10886 | } |
| 10887 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10888 | /* Terminal size querying */ |
| 10889 | |
| 10890 | static PyTypeObject TerminalSizeType; |
| 10891 | |
| 10892 | PyDoc_STRVAR(TerminalSize_docstring, |
| 10893 | "A tuple of (columns, lines) for holding terminal window size"); |
| 10894 | |
| 10895 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 10896 | {"columns", "width of the terminal window in characters"}, |
| 10897 | {"lines", "height of the terminal window in characters"}, |
| 10898 | {NULL, NULL} |
| 10899 | }; |
| 10900 | |
| 10901 | static PyStructSequence_Desc TerminalSize_desc = { |
| 10902 | "os.terminal_size", |
| 10903 | TerminalSize_docstring, |
| 10904 | TerminalSize_fields, |
| 10905 | 2, |
| 10906 | }; |
| 10907 | |
| 10908 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 10909 | PyDoc_STRVAR(termsize__doc__, |
| 10910 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 10911 | "\n" \ |
| 10912 | "The optional argument fd (default standard output) specifies\n" \ |
| 10913 | "which file descriptor should be queried.\n" \ |
| 10914 | "\n" \ |
| 10915 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 10916 | "is thrown.\n" \ |
| 10917 | "\n" \ |
| 10918 | "This function will only be defined if an implementation is\n" \ |
| 10919 | "available for this system.\n" \ |
| 10920 | "\n" \ |
| 10921 | "shutil.get_terminal_size is the high-level function which should \n" \ |
| 10922 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 10923 | |
| 10924 | static PyObject* |
| 10925 | get_terminal_size(PyObject *self, PyObject *args) |
| 10926 | { |
| 10927 | int columns, lines; |
| 10928 | PyObject *termsize; |
| 10929 | |
| 10930 | int fd = fileno(stdout); |
| 10931 | /* Under some conditions stdout may not be connected and |
| 10932 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 10933 | * GUI apps don't have valid standard streams by default. |
| 10934 | * |
| 10935 | * If this happens, and the optional fd argument is not present, |
| 10936 | * the ioctl below will fail returning EBADF. This is what we want. |
| 10937 | */ |
| 10938 | |
| 10939 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 10940 | return NULL; |
| 10941 | |
| 10942 | #ifdef TERMSIZE_USE_IOCTL |
| 10943 | { |
| 10944 | struct winsize w; |
| 10945 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 10946 | return PyErr_SetFromErrno(PyExc_OSError); |
| 10947 | columns = w.ws_col; |
| 10948 | lines = w.ws_row; |
| 10949 | } |
| 10950 | #endif /* TERMSIZE_USE_IOCTL */ |
| 10951 | |
| 10952 | #ifdef TERMSIZE_USE_CONIO |
| 10953 | { |
| 10954 | DWORD nhandle; |
| 10955 | HANDLE handle; |
| 10956 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 10957 | switch (fd) { |
| 10958 | case 0: nhandle = STD_INPUT_HANDLE; |
| 10959 | break; |
| 10960 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 10961 | break; |
| 10962 | case 2: nhandle = STD_ERROR_HANDLE; |
| 10963 | break; |
| 10964 | default: |
| 10965 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 10966 | } |
| 10967 | handle = GetStdHandle(nhandle); |
| 10968 | if (handle == NULL) |
| 10969 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 10970 | if (handle == INVALID_HANDLE_VALUE) |
| 10971 | return PyErr_SetFromWindowsErr(0); |
| 10972 | |
| 10973 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 10974 | return PyErr_SetFromWindowsErr(0); |
| 10975 | |
| 10976 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 10977 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 10978 | } |
| 10979 | #endif /* TERMSIZE_USE_CONIO */ |
| 10980 | |
| 10981 | termsize = PyStructSequence_New(&TerminalSizeType); |
| 10982 | if (termsize == NULL) |
| 10983 | return NULL; |
| 10984 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 10985 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 10986 | if (PyErr_Occurred()) { |
| 10987 | Py_DECREF(termsize); |
| 10988 | return NULL; |
| 10989 | } |
| 10990 | return termsize; |
| 10991 | } |
| 10992 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 10993 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10994 | PyDoc_STRVAR(posix_cpu_count__doc__, |
| 10995 | "cpu_count() -> integer\n\n\ |
| 10996 | Return the number of CPUs in the system, or None if this value cannot be\n\ |
| 10997 | established."); |
| 10998 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10999 | static PyObject * |
| 11000 | posix_cpu_count(PyObject *self) |
| 11001 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11002 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11003 | #ifdef MS_WINDOWS |
| 11004 | SYSTEM_INFO sysinfo; |
| 11005 | GetSystemInfo(&sysinfo); |
| 11006 | ncpu = sysinfo.dwNumberOfProcessors; |
| 11007 | #elif defined(__hpux) |
| 11008 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 11009 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 11010 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11011 | #elif defined(__DragonFly__) || \ |
| 11012 | defined(__OpenBSD__) || \ |
| 11013 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11014 | defined(__NetBSD__) || \ |
| 11015 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 11016 | int mib[2]; |
| 11017 | size_t len = sizeof(ncpu); |
| 11018 | mib[0] = CTL_HW; |
| 11019 | mib[1] = HW_NCPU; |
| 11020 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 11021 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11022 | #endif |
| 11023 | if (ncpu >= 1) |
| 11024 | return PyLong_FromLong(ncpu); |
| 11025 | else |
| 11026 | Py_RETURN_NONE; |
| 11027 | } |
| 11028 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11029 | PyDoc_STRVAR(get_inheritable__doc__, |
| 11030 | "get_inheritable(fd) -> bool\n" \ |
| 11031 | "\n" \ |
| 11032 | "Get the close-on-exe flag of the specified file descriptor."); |
| 11033 | |
| 11034 | static PyObject* |
| 11035 | posix_get_inheritable(PyObject *self, PyObject *args) |
| 11036 | { |
| 11037 | int fd; |
| 11038 | int inheritable; |
| 11039 | |
| 11040 | if (!PyArg_ParseTuple(args, "i:get_inheritable", &fd)) |
| 11041 | return NULL; |
| 11042 | |
| 11043 | if (!_PyVerify_fd(fd)) |
| 11044 | return posix_error(); |
| 11045 | |
| 11046 | inheritable = _Py_get_inheritable(fd); |
| 11047 | if (inheritable < 0) |
| 11048 | return NULL; |
| 11049 | return PyBool_FromLong(inheritable); |
| 11050 | } |
| 11051 | |
| 11052 | PyDoc_STRVAR(set_inheritable__doc__, |
| 11053 | "set_inheritable(fd, inheritable)\n" \ |
| 11054 | "\n" \ |
| 11055 | "Set the inheritable flag of the specified file descriptor."); |
| 11056 | |
| 11057 | static PyObject* |
| 11058 | posix_set_inheritable(PyObject *self, PyObject *args) |
| 11059 | { |
| 11060 | int fd, inheritable; |
| 11061 | |
| 11062 | if (!PyArg_ParseTuple(args, "ii:set_inheritable", &fd, &inheritable)) |
| 11063 | return NULL; |
| 11064 | |
| 11065 | if (!_PyVerify_fd(fd)) |
| 11066 | return posix_error(); |
| 11067 | |
| 11068 | if (_Py_set_inheritable(fd, inheritable, NULL) < 0) |
| 11069 | return NULL; |
| 11070 | Py_RETURN_NONE; |
| 11071 | } |
| 11072 | |
| 11073 | |
| 11074 | #ifdef MS_WINDOWS |
| 11075 | PyDoc_STRVAR(get_handle_inheritable__doc__, |
| 11076 | "get_handle_inheritable(fd) -> bool\n" \ |
| 11077 | "\n" \ |
| 11078 | "Get the close-on-exe flag of the specified file descriptor."); |
| 11079 | |
| 11080 | static PyObject* |
| 11081 | posix_get_handle_inheritable(PyObject *self, PyObject *args) |
| 11082 | { |
| 11083 | Py_intptr_t handle; |
| 11084 | DWORD flags; |
| 11085 | |
| 11086 | if (!PyArg_ParseTuple(args, _Py_PARSE_INTPTR ":get_handle_inheritable", &handle)) |
| 11087 | return NULL; |
| 11088 | |
| 11089 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 11090 | PyErr_SetFromWindowsErr(0); |
| 11091 | return NULL; |
| 11092 | } |
| 11093 | |
| 11094 | return PyBool_FromLong(flags & HANDLE_FLAG_INHERIT); |
| 11095 | } |
| 11096 | |
| 11097 | PyDoc_STRVAR(set_handle_inheritable__doc__, |
| 11098 | "set_handle_inheritable(fd, inheritable)\n" \ |
| 11099 | "\n" \ |
| 11100 | "Set the inheritable flag of the specified handle."); |
| 11101 | |
| 11102 | static PyObject* |
| 11103 | posix_set_handle_inheritable(PyObject *self, PyObject *args) |
| 11104 | { |
| 11105 | int inheritable = 1; |
| 11106 | Py_intptr_t handle; |
| 11107 | DWORD flags; |
| 11108 | |
| 11109 | if (!PyArg_ParseTuple(args, _Py_PARSE_INTPTR "i:set_handle_inheritable", |
| 11110 | &handle, &inheritable)) |
| 11111 | return NULL; |
| 11112 | |
| 11113 | if (inheritable) |
| 11114 | flags = HANDLE_FLAG_INHERIT; |
| 11115 | else |
| 11116 | flags = 0; |
| 11117 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 11118 | PyErr_SetFromWindowsErr(0); |
| 11119 | return NULL; |
| 11120 | } |
| 11121 | Py_RETURN_NONE; |
| 11122 | } |
| 11123 | #endif /* MS_WINDOWS */ |
| 11124 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11125 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 11126 | /*[clinic input] |
| 11127 | dump buffer |
| 11128 | [clinic start generated code]*/ |
| 11129 | |
| 11130 | #ifndef OS_TTYNAME_METHODDEF |
| 11131 | #define OS_TTYNAME_METHODDEF |
| 11132 | #endif /* !defined(OS_TTYNAME_METHODDEF) */ |
| 11133 | /*[clinic end generated code: output=5d071bbc8f49ea12 input=524ce2e021e4eba6]*/ |
| 11134 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 11135 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11136 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 11137 | |
| 11138 | OS_STAT_METHODDEF |
| 11139 | OS_ACCESS_METHODDEF |
| 11140 | OS_TTYNAME_METHODDEF |
| 11141 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11142 | {"chdir", (PyCFunction)posix_chdir, |
| 11143 | METH_VARARGS | METH_KEYWORDS, |
| 11144 | posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 11145 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11146 | {"chflags", (PyCFunction)posix_chflags, |
| 11147 | METH_VARARGS | METH_KEYWORDS, |
| 11148 | posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 11149 | #endif /* HAVE_CHFLAGS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11150 | {"chmod", (PyCFunction)posix_chmod, |
| 11151 | METH_VARARGS | METH_KEYWORDS, |
| 11152 | posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 11153 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11154 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 11155 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11156 | #ifdef HAVE_CHOWN |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11157 | {"chown", (PyCFunction)posix_chown, |
| 11158 | METH_VARARGS | METH_KEYWORDS, |
| 11159 | posix_chown__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 11160 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 11161 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11162 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 11163 | #endif /* HAVE_LCHMOD */ |
| 11164 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11165 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 11166 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 11167 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11168 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 11169 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 11170 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11171 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 11172 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 11173 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11174 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 11175 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11176 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11177 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11178 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11179 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 11180 | METH_NOARGS, posix_getcwd__doc__}, |
| 11181 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 11182 | METH_NOARGS, posix_getcwdb__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11183 | #if defined(HAVE_LINK) || defined(MS_WINDOWS) |
| 11184 | {"link", (PyCFunction)posix_link, |
| 11185 | METH_VARARGS | METH_KEYWORDS, |
| 11186 | posix_link__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11187 | #endif /* HAVE_LINK */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11188 | {"listdir", (PyCFunction)posix_listdir, |
| 11189 | METH_VARARGS | METH_KEYWORDS, |
| 11190 | posix_listdir__doc__}, |
| 11191 | {"lstat", (PyCFunction)posix_lstat, |
| 11192 | METH_VARARGS | METH_KEYWORDS, |
| 11193 | posix_lstat__doc__}, |
| 11194 | {"mkdir", (PyCFunction)posix_mkdir, |
| 11195 | METH_VARARGS | METH_KEYWORDS, |
| 11196 | posix_mkdir__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 11197 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11198 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 11199 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11200 | #ifdef HAVE_GETPRIORITY |
| 11201 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 11202 | #endif /* HAVE_GETPRIORITY */ |
| 11203 | #ifdef HAVE_SETPRIORITY |
| 11204 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 11205 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 11206 | #ifdef HAVE_READLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11207 | {"readlink", (PyCFunction)posix_readlink, |
| 11208 | METH_VARARGS | METH_KEYWORDS, |
| 11209 | readlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 11210 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 11211 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11212 | {"readlink", (PyCFunction)win_readlink, |
| 11213 | METH_VARARGS | METH_KEYWORDS, |
| 11214 | readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 11215 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11216 | {"rename", (PyCFunction)posix_rename, |
| 11217 | METH_VARARGS | METH_KEYWORDS, |
| 11218 | posix_rename__doc__}, |
| 11219 | {"replace", (PyCFunction)posix_replace, |
| 11220 | METH_VARARGS | METH_KEYWORDS, |
| 11221 | posix_replace__doc__}, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 11222 | {"rmdir", (PyCFunction)posix_rmdir, |
| 11223 | METH_VARARGS | METH_KEYWORDS, |
| 11224 | posix_rmdir__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11225 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11226 | #if defined(HAVE_SYMLINK) |
| 11227 | {"symlink", (PyCFunction)posix_symlink, |
| 11228 | METH_VARARGS | METH_KEYWORDS, |
| 11229 | posix_symlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 11230 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 11231 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11232 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11233 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11234 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11235 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11236 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11237 | #endif /* HAVE_UNAME */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11238 | {"unlink", (PyCFunction)posix_unlink, |
| 11239 | METH_VARARGS | METH_KEYWORDS, |
| 11240 | posix_unlink__doc__}, |
| 11241 | {"remove", (PyCFunction)posix_unlink, |
| 11242 | METH_VARARGS | METH_KEYWORDS, |
| 11243 | posix_remove__doc__}, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 11244 | {"utime", (PyCFunction)posix_utime, |
| 11245 | METH_VARARGS | METH_KEYWORDS, posix_utime__doc__}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 11246 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11247 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 11248 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11249 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 11250 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11251 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11252 | {"execve", (PyCFunction)posix_execve, |
| 11253 | METH_VARARGS | METH_KEYWORDS, |
| 11254 | posix_execve__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 11255 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 11256 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11257 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 11258 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 11259 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 11260 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11261 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 11262 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11263 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11264 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11265 | #endif /* HAVE_FORK */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11266 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 11267 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11268 | {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__}, |
| 11269 | {"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] | 11270 | #endif |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11271 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11272 | {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11273 | #endif |
| 11274 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11275 | {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11276 | #endif |
| 11277 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11278 | {"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] | 11279 | #endif |
| 11280 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11281 | {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11282 | #endif |
| 11283 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11284 | {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11285 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11286 | {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__}, |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11287 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11288 | {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__}, |
| 11289 | {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__}, |
| 11290 | #endif |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 11291 | #endif /* HAVE_SCHED_H */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 11292 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11293 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 11294 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 11295 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11296 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 11297 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 11298 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11299 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 11300 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11301 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11302 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 11303 | #endif /* HAVE_GETEUID */ |
| 11304 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11305 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11306 | #endif /* HAVE_GETGID */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 11307 | #ifdef HAVE_GETGROUPLIST |
| 11308 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 11309 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11310 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11311 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11312 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11313 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11314 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11315 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11316 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11317 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11318 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11319 | #endif /* HAVE_GETPPID */ |
| 11320 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11321 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11322 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 11323 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11324 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 11325 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11326 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11327 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11328 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 11329 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11330 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 11331 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 11332 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11333 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 11334 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 11335 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11336 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 11337 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 11338 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11339 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11340 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11341 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11342 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11343 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11344 | #endif /* HAVE_SETEUID */ |
| 11345 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11346 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11347 | #endif /* HAVE_SETEGID */ |
| 11348 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11349 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11350 | #endif /* HAVE_SETREUID */ |
| 11351 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11352 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11353 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11354 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11355 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11356 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 11357 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11358 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 11359 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 11360 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11361 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 11362 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 11363 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11364 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 11365 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11366 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11367 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11368 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11369 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11370 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11371 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11372 | #ifdef HAVE_WAIT3 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 11373 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11374 | #endif /* HAVE_WAIT3 */ |
| 11375 | #ifdef HAVE_WAIT4 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 11376 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11377 | #endif /* HAVE_WAIT4 */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11378 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11379 | {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__}, |
| 11380 | #endif |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 11381 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11382 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11383 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 11384 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11385 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 11386 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11387 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11388 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11389 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11390 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11391 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11392 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11393 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11394 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11395 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11396 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11397 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11398 | #endif /* HAVE_TCSETPGRP */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11399 | {"open", (PyCFunction)posix_open,\ |
| 11400 | METH_VARARGS | METH_KEYWORDS, |
| 11401 | posix_open__doc__}, |
Benjamin Peterson | 932bba3 | 2014-02-11 10:16:16 -0500 | [diff] [blame] | 11402 | {"close", posix_close_, METH_VARARGS, posix_close__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11403 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 11404 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 11405 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11406 | {"dup2", (PyCFunction)posix_dup2, |
| 11407 | METH_VARARGS | METH_KEYWORDS, posix_dup2__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11408 | #ifdef HAVE_LOCKF |
| 11409 | {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__}, |
| 11410 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11411 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 11412 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11413 | #ifdef HAVE_READV |
| 11414 | {"readv", posix_readv, METH_VARARGS, posix_readv__doc__}, |
| 11415 | #endif |
| 11416 | #ifdef HAVE_PREAD |
| 11417 | {"pread", posix_pread, METH_VARARGS, posix_pread__doc__}, |
| 11418 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11419 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11420 | #ifdef HAVE_WRITEV |
| 11421 | {"writev", posix_writev, METH_VARARGS, posix_writev__doc__}, |
| 11422 | #endif |
| 11423 | #ifdef HAVE_PWRITE |
| 11424 | {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__}, |
| 11425 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11426 | #ifdef HAVE_SENDFILE |
| 11427 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 11428 | posix_sendfile__doc__}, |
| 11429 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 11430 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11431 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11432 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11433 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11434 | #endif |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 11435 | #ifdef HAVE_PIPE2 |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 11436 | {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__}, |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 11437 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11438 | #ifdef HAVE_MKFIFO |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11439 | {"mkfifo", (PyCFunction)posix_mkfifo, |
| 11440 | METH_VARARGS | METH_KEYWORDS, |
| 11441 | posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11442 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 11443 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11444 | {"mknod", (PyCFunction)posix_mknod, |
| 11445 | METH_VARARGS | METH_KEYWORDS, |
| 11446 | posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 11447 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 11448 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11449 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 11450 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 11451 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 11452 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11453 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11454 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11455 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11456 | #ifdef HAVE_TRUNCATE |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11457 | {"truncate", (PyCFunction)posix_truncate, |
| 11458 | METH_VARARGS | METH_KEYWORDS, |
| 11459 | posix_truncate__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11460 | #endif |
| 11461 | #ifdef HAVE_POSIX_FALLOCATE |
| 11462 | {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__}, |
| 11463 | #endif |
| 11464 | #ifdef HAVE_POSIX_FADVISE |
| 11465 | {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__}, |
| 11466 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 11467 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11468 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 11469 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 11470 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11471 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 11472 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11473 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11474 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11475 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11476 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11477 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11478 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11479 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11480 | #ifdef HAVE_SYNC |
| 11481 | {"sync", posix_sync, METH_NOARGS, posix_sync__doc__}, |
| 11482 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11483 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11484 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11485 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11486 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11487 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11488 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11489 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 11490 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11491 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 11492 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11493 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11494 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11495 | #endif /* WIFSTOPPED */ |
| 11496 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11497 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11498 | #endif /* WIFSIGNALED */ |
| 11499 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11500 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11501 | #endif /* WIFEXITED */ |
| 11502 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11503 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11504 | #endif /* WEXITSTATUS */ |
| 11505 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11506 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11507 | #endif /* WTERMSIG */ |
| 11508 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11509 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11510 | #endif /* WSTOPSIG */ |
| 11511 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11512 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11513 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11514 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11515 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11516 | {"statvfs", (PyCFunction)posix_statvfs, |
| 11517 | METH_VARARGS | METH_KEYWORDS, |
| 11518 | posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11519 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11520 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11521 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11522 | #endif |
| 11523 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11524 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11525 | #endif |
| 11526 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11527 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11528 | #endif |
| 11529 | #ifdef HAVE_PATHCONF |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11530 | {"pathconf", (PyCFunction)posix_pathconf, |
| 11531 | METH_VARARGS | METH_KEYWORDS, |
| 11532 | posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11533 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11534 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11535 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11536 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 11537 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 11538 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 11539 | {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__}, |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 11540 | {"_getvolumepathname", posix__getvolumepathname, METH_VARARGS, posix__getvolumepathname__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 11541 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11542 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11543 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11544 | #endif |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 11545 | {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11546 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11547 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11548 | #endif |
| 11549 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11550 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11551 | #endif |
| 11552 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11553 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11554 | #endif |
| 11555 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11556 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11557 | #endif |
| 11558 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11559 | #ifdef USE_XATTRS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11560 | {"setxattr", (PyCFunction)posix_setxattr, |
| 11561 | METH_VARARGS | METH_KEYWORDS, |
| 11562 | posix_setxattr__doc__}, |
| 11563 | {"getxattr", (PyCFunction)posix_getxattr, |
| 11564 | METH_VARARGS | METH_KEYWORDS, |
| 11565 | posix_getxattr__doc__}, |
| 11566 | {"removexattr", (PyCFunction)posix_removexattr, |
| 11567 | METH_VARARGS | METH_KEYWORDS, |
| 11568 | posix_removexattr__doc__}, |
| 11569 | {"listxattr", (PyCFunction)posix_listxattr, |
| 11570 | METH_VARARGS | METH_KEYWORDS, |
| 11571 | posix_listxattr__doc__}, |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11572 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11573 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 11574 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 11575 | #endif |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11576 | {"cpu_count", (PyCFunction)posix_cpu_count, |
| 11577 | METH_NOARGS, posix_cpu_count__doc__}, |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11578 | {"get_inheritable", posix_get_inheritable, METH_VARARGS, get_inheritable__doc__}, |
| 11579 | {"set_inheritable", posix_set_inheritable, METH_VARARGS, set_inheritable__doc__}, |
| 11580 | #ifdef MS_WINDOWS |
| 11581 | {"get_handle_inheritable", posix_get_handle_inheritable, |
| 11582 | METH_VARARGS, get_handle_inheritable__doc__}, |
| 11583 | {"set_handle_inheritable", posix_set_handle_inheritable, |
| 11584 | METH_VARARGS, set_handle_inheritable__doc__}, |
| 11585 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11586 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11587 | }; |
| 11588 | |
| 11589 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11590 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11591 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11592 | enable_symlink() |
| 11593 | { |
| 11594 | HANDLE tok; |
| 11595 | TOKEN_PRIVILEGES tok_priv; |
| 11596 | LUID luid; |
| 11597 | int meth_idx = 0; |
| 11598 | |
| 11599 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11600 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11601 | |
| 11602 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11603 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11604 | |
| 11605 | tok_priv.PrivilegeCount = 1; |
| 11606 | tok_priv.Privileges[0].Luid = luid; |
| 11607 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 11608 | |
| 11609 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 11610 | sizeof(TOKEN_PRIVILEGES), |
| 11611 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11612 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11613 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11614 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 11615 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11616 | } |
| 11617 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 11618 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11619 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11620 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11621 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11622 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11623 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11624 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11625 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11626 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11627 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11628 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11629 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11630 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11631 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11632 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11633 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11634 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11635 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11636 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11637 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11638 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11639 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11640 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11641 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11642 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11643 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11644 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11645 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11646 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11647 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11648 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11649 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11650 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11651 | #endif |
| 11652 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11653 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11654 | #endif |
| 11655 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11656 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11657 | #endif |
| 11658 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11659 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11660 | #endif |
| 11661 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11662 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11663 | #endif |
| 11664 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11665 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11666 | #endif |
| 11667 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11668 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11669 | #endif |
| 11670 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11671 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11672 | #endif |
| 11673 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11674 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11675 | #endif |
| 11676 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11677 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11678 | #endif |
| 11679 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11680 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11681 | #endif |
| 11682 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11683 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11684 | #endif |
| 11685 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11686 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11687 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11688 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11689 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11690 | #endif |
| 11691 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11692 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11693 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11694 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11695 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11696 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11697 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11698 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11699 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11700 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11701 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11702 | #endif |
| 11703 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11704 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11705 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11706 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11707 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11708 | #endif |
| 11709 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11710 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11711 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 11712 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11713 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 11714 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11715 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11716 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11717 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 11718 | #ifdef O_TMPFILE |
| 11719 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 11720 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11721 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11722 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11723 | #endif |
| 11724 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11725 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11726 | #endif |
| 11727 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11728 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11729 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 11730 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11731 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 11732 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11733 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11734 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11735 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11736 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11737 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 11738 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11739 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 11740 | #endif |
| 11741 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11742 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 11743 | #endif |
| 11744 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11745 | /* MS Windows */ |
| 11746 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11747 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11748 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11749 | #endif |
| 11750 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11751 | /* Optimize for short life (keep in memory). */ |
| 11752 | /* 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] | 11753 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11754 | #endif |
| 11755 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11756 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11757 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11758 | #endif |
| 11759 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11760 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11761 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11762 | #endif |
| 11763 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11764 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11765 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11766 | #endif |
| 11767 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11768 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11769 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11770 | /* Send a SIGIO signal whenever input or output |
| 11771 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11772 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11773 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11774 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11775 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11776 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11777 | #endif |
| 11778 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11779 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11780 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11781 | #endif |
| 11782 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11783 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11784 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11785 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11786 | #ifdef O_NOLINKS |
| 11787 | /* 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] | 11788 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11789 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11790 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11791 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11792 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11793 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11794 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11795 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11796 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11797 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11798 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11799 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11800 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11801 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11802 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11803 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11804 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11805 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11806 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11807 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11808 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11809 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11810 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11811 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11812 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11813 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11814 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11815 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11816 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11817 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11818 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11819 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11820 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11821 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11822 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11823 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11824 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11825 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11826 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11827 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11828 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11829 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11830 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11831 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11832 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11833 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11834 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11835 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11836 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11837 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11838 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11839 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11840 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11841 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11842 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11843 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11844 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11845 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11846 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11847 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11848 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11849 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11850 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11851 | #endif /* ST_RDONLY */ |
| 11852 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11853 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11854 | #endif /* ST_NOSUID */ |
| 11855 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 11856 | /* GNU extensions */ |
| 11857 | #ifdef ST_NODEV |
| 11858 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 11859 | #endif /* ST_NODEV */ |
| 11860 | #ifdef ST_NOEXEC |
| 11861 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 11862 | #endif /* ST_NOEXEC */ |
| 11863 | #ifdef ST_SYNCHRONOUS |
| 11864 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 11865 | #endif /* ST_SYNCHRONOUS */ |
| 11866 | #ifdef ST_MANDLOCK |
| 11867 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 11868 | #endif /* ST_MANDLOCK */ |
| 11869 | #ifdef ST_WRITE |
| 11870 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 11871 | #endif /* ST_WRITE */ |
| 11872 | #ifdef ST_APPEND |
| 11873 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 11874 | #endif /* ST_APPEND */ |
| 11875 | #ifdef ST_NOATIME |
| 11876 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 11877 | #endif /* ST_NOATIME */ |
| 11878 | #ifdef ST_NODIRATIME |
| 11879 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 11880 | #endif /* ST_NODIRATIME */ |
| 11881 | #ifdef ST_RELATIME |
| 11882 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 11883 | #endif /* ST_RELATIME */ |
| 11884 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11885 | /* FreeBSD sendfile() constants */ |
| 11886 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11887 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11888 | #endif |
| 11889 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11890 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11891 | #endif |
| 11892 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11893 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11894 | #endif |
| 11895 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11896 | /* constants for posix_fadvise */ |
| 11897 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11898 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11899 | #endif |
| 11900 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11901 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11902 | #endif |
| 11903 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11904 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11905 | #endif |
| 11906 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11907 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11908 | #endif |
| 11909 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11910 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11911 | #endif |
| 11912 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11913 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11914 | #endif |
| 11915 | |
| 11916 | /* constants for waitid */ |
| 11917 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11918 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 11919 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 11920 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11921 | #endif |
| 11922 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11923 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11924 | #endif |
| 11925 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11926 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11927 | #endif |
| 11928 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11929 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11930 | #endif |
| 11931 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11932 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11933 | #endif |
| 11934 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11935 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11936 | #endif |
| 11937 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11938 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11939 | #endif |
| 11940 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11941 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11942 | #endif |
| 11943 | |
| 11944 | /* constants for lockf */ |
| 11945 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11946 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11947 | #endif |
| 11948 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11949 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11950 | #endif |
| 11951 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11952 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11953 | #endif |
| 11954 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11955 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11956 | #endif |
| 11957 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11958 | #ifdef HAVE_SPAWNV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11959 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 11960 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
| 11961 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
| 11962 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
| 11963 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11964 | #endif |
| 11965 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11966 | #ifdef HAVE_SCHED_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11967 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
| 11968 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
| 11969 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11970 | #ifdef SCHED_SPORADIC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11971 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11972 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11973 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11974 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11975 | #endif |
| 11976 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11977 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11978 | #endif |
| 11979 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11980 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11981 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11982 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11983 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11984 | #endif |
| 11985 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11986 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11987 | #endif |
| 11988 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11989 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11990 | #endif |
| 11991 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11992 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11993 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11994 | #endif |
| 11995 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11996 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11997 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 11998 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 11999 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12000 | #endif |
| 12001 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12002 | #ifdef RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12003 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12004 | #endif |
| 12005 | #ifdef RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12006 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12007 | #endif |
| 12008 | #ifdef RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12009 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12010 | #endif |
| 12011 | #ifdef RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12012 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12013 | #endif |
| 12014 | #ifdef RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12015 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12016 | #endif |
| 12017 | #ifdef RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12018 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12019 | #endif |
| 12020 | #ifdef RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12021 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12022 | #endif |
| 12023 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12024 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12025 | } |
| 12026 | |
| 12027 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12028 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12029 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 12030 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 12031 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 12032 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12033 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 12034 | #define MODNAME "posix" |
| 12035 | #endif |
| 12036 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12037 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12038 | PyModuleDef_HEAD_INIT, |
| 12039 | MODNAME, |
| 12040 | posix__doc__, |
| 12041 | -1, |
| 12042 | posix_methods, |
| 12043 | NULL, |
| 12044 | NULL, |
| 12045 | NULL, |
| 12046 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12047 | }; |
| 12048 | |
| 12049 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12050 | static char *have_functions[] = { |
| 12051 | |
| 12052 | #ifdef HAVE_FACCESSAT |
| 12053 | "HAVE_FACCESSAT", |
| 12054 | #endif |
| 12055 | |
| 12056 | #ifdef HAVE_FCHDIR |
| 12057 | "HAVE_FCHDIR", |
| 12058 | #endif |
| 12059 | |
| 12060 | #ifdef HAVE_FCHMOD |
| 12061 | "HAVE_FCHMOD", |
| 12062 | #endif |
| 12063 | |
| 12064 | #ifdef HAVE_FCHMODAT |
| 12065 | "HAVE_FCHMODAT", |
| 12066 | #endif |
| 12067 | |
| 12068 | #ifdef HAVE_FCHOWN |
| 12069 | "HAVE_FCHOWN", |
| 12070 | #endif |
| 12071 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 12072 | #ifdef HAVE_FCHOWNAT |
| 12073 | "HAVE_FCHOWNAT", |
| 12074 | #endif |
| 12075 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12076 | #ifdef HAVE_FEXECVE |
| 12077 | "HAVE_FEXECVE", |
| 12078 | #endif |
| 12079 | |
| 12080 | #ifdef HAVE_FDOPENDIR |
| 12081 | "HAVE_FDOPENDIR", |
| 12082 | #endif |
| 12083 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12084 | #ifdef HAVE_FPATHCONF |
| 12085 | "HAVE_FPATHCONF", |
| 12086 | #endif |
| 12087 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12088 | #ifdef HAVE_FSTATAT |
| 12089 | "HAVE_FSTATAT", |
| 12090 | #endif |
| 12091 | |
| 12092 | #ifdef HAVE_FSTATVFS |
| 12093 | "HAVE_FSTATVFS", |
| 12094 | #endif |
| 12095 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12096 | #ifdef HAVE_FTRUNCATE |
| 12097 | "HAVE_FTRUNCATE", |
| 12098 | #endif |
| 12099 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12100 | #ifdef HAVE_FUTIMENS |
| 12101 | "HAVE_FUTIMENS", |
| 12102 | #endif |
| 12103 | |
| 12104 | #ifdef HAVE_FUTIMES |
| 12105 | "HAVE_FUTIMES", |
| 12106 | #endif |
| 12107 | |
| 12108 | #ifdef HAVE_FUTIMESAT |
| 12109 | "HAVE_FUTIMESAT", |
| 12110 | #endif |
| 12111 | |
| 12112 | #ifdef HAVE_LINKAT |
| 12113 | "HAVE_LINKAT", |
| 12114 | #endif |
| 12115 | |
| 12116 | #ifdef HAVE_LCHFLAGS |
| 12117 | "HAVE_LCHFLAGS", |
| 12118 | #endif |
| 12119 | |
| 12120 | #ifdef HAVE_LCHMOD |
| 12121 | "HAVE_LCHMOD", |
| 12122 | #endif |
| 12123 | |
| 12124 | #ifdef HAVE_LCHOWN |
| 12125 | "HAVE_LCHOWN", |
| 12126 | #endif |
| 12127 | |
| 12128 | #ifdef HAVE_LSTAT |
| 12129 | "HAVE_LSTAT", |
| 12130 | #endif |
| 12131 | |
| 12132 | #ifdef HAVE_LUTIMES |
| 12133 | "HAVE_LUTIMES", |
| 12134 | #endif |
| 12135 | |
| 12136 | #ifdef HAVE_MKDIRAT |
| 12137 | "HAVE_MKDIRAT", |
| 12138 | #endif |
| 12139 | |
| 12140 | #ifdef HAVE_MKFIFOAT |
| 12141 | "HAVE_MKFIFOAT", |
| 12142 | #endif |
| 12143 | |
| 12144 | #ifdef HAVE_MKNODAT |
| 12145 | "HAVE_MKNODAT", |
| 12146 | #endif |
| 12147 | |
| 12148 | #ifdef HAVE_OPENAT |
| 12149 | "HAVE_OPENAT", |
| 12150 | #endif |
| 12151 | |
| 12152 | #ifdef HAVE_READLINKAT |
| 12153 | "HAVE_READLINKAT", |
| 12154 | #endif |
| 12155 | |
| 12156 | #ifdef HAVE_RENAMEAT |
| 12157 | "HAVE_RENAMEAT", |
| 12158 | #endif |
| 12159 | |
| 12160 | #ifdef HAVE_SYMLINKAT |
| 12161 | "HAVE_SYMLINKAT", |
| 12162 | #endif |
| 12163 | |
| 12164 | #ifdef HAVE_UNLINKAT |
| 12165 | "HAVE_UNLINKAT", |
| 12166 | #endif |
| 12167 | |
| 12168 | #ifdef HAVE_UTIMENSAT |
| 12169 | "HAVE_UTIMENSAT", |
| 12170 | #endif |
| 12171 | |
| 12172 | #ifdef MS_WINDOWS |
| 12173 | "MS_WINDOWS", |
| 12174 | #endif |
| 12175 | |
| 12176 | NULL |
| 12177 | }; |
| 12178 | |
| 12179 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 12180 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 12181 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12182 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12183 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12184 | PyObject *list; |
| 12185 | char **trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12186 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12187 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12188 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12189 | #endif |
| 12190 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12191 | m = PyModule_Create(&posixmodule); |
| 12192 | if (m == NULL) |
| 12193 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12194 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12195 | /* Initialize environ dictionary */ |
| 12196 | v = convertenviron(); |
| 12197 | Py_XINCREF(v); |
| 12198 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 12199 | return NULL; |
| 12200 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12201 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12202 | if (all_ins(m)) |
| 12203 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12204 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12205 | if (setup_confname_tables(m)) |
| 12206 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12207 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12208 | Py_INCREF(PyExc_OSError); |
| 12209 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 12210 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12211 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12212 | if (posix_putenv_garbage == NULL) |
| 12213 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12214 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 12215 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12216 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12217 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 12218 | waitid_result_desc.name = MODNAME ".waitid_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12219 | if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0) |
| 12220 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12221 | #endif |
| 12222 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 12223 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12224 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 12225 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 12226 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12227 | if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0) |
| 12228 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12229 | structseq_new = StatResultType.tp_new; |
| 12230 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12231 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 12232 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12233 | if (PyStructSequence_InitType2(&StatVFSResultType, |
| 12234 | &statvfs_result_desc) < 0) |
| 12235 | return NULL; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12236 | #ifdef NEED_TICKS_PER_SECOND |
| 12237 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12238 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12239 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12240 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12241 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12242 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12243 | # endif |
| 12244 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12245 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 12246 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12247 | sched_param_desc.name = MODNAME ".sched_param"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12248 | if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0) |
| 12249 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12250 | SchedParamType.tp_new = sched_param_new; |
| 12251 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12252 | |
| 12253 | /* initialize TerminalSize_info */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12254 | if (PyStructSequence_InitType2(&TerminalSizeType, |
| 12255 | &TerminalSize_desc) < 0) |
| 12256 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12257 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12258 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 12259 | Py_INCREF((PyObject*) &WaitidResultType); |
| 12260 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 12261 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12262 | Py_INCREF((PyObject*) &StatResultType); |
| 12263 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 12264 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 12265 | PyModule_AddObject(m, "statvfs_result", |
| 12266 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 12267 | |
| 12268 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12269 | Py_INCREF(&SchedParamType); |
| 12270 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 12271 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12272 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 12273 | times_result_desc.name = MODNAME ".times_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12274 | if (PyStructSequence_InitType2(&TimesResultType, ×_result_desc) < 0) |
| 12275 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 12276 | PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType); |
| 12277 | |
| 12278 | uname_result_desc.name = MODNAME ".uname_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12279 | if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0) |
| 12280 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 12281 | PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType); |
| 12282 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12283 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12284 | /* |
| 12285 | * Step 2 of weak-linking support on Mac OS X. |
| 12286 | * |
| 12287 | * The code below removes functions that are not available on the |
| 12288 | * currently active platform. |
| 12289 | * |
| 12290 | * 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] | 12291 | * 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] | 12292 | * OSX 10.4. |
| 12293 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12294 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12295 | if (fstatvfs == NULL) { |
| 12296 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 12297 | return NULL; |
| 12298 | } |
| 12299 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12300 | #endif /* HAVE_FSTATVFS */ |
| 12301 | |
| 12302 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12303 | if (statvfs == NULL) { |
| 12304 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 12305 | return NULL; |
| 12306 | } |
| 12307 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12308 | #endif /* HAVE_STATVFS */ |
| 12309 | |
| 12310 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12311 | if (lchown == NULL) { |
| 12312 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 12313 | return NULL; |
| 12314 | } |
| 12315 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12316 | #endif /* HAVE_LCHOWN */ |
| 12317 | |
| 12318 | |
| 12319 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12320 | |
Antoine Pitrou | 9d20e0e | 2012-09-12 18:01:36 +0200 | [diff] [blame] | 12321 | Py_INCREF(&TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12322 | PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); |
| 12323 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 12324 | billion = PyLong_FromLong(1000000000); |
| 12325 | if (!billion) |
| 12326 | return NULL; |
| 12327 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12328 | /* suppress "function not used" warnings */ |
| 12329 | { |
| 12330 | int ignored; |
| 12331 | fd_specified("", -1); |
| 12332 | follow_symlinks_specified("", 1); |
| 12333 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 12334 | dir_fd_converter(Py_None, &ignored); |
| 12335 | dir_fd_unavailable(Py_None, &ignored); |
| 12336 | } |
| 12337 | |
| 12338 | /* |
| 12339 | * provide list of locally available functions |
| 12340 | * so os.py can populate support_* lists |
| 12341 | */ |
| 12342 | list = PyList_New(0); |
| 12343 | if (!list) |
| 12344 | return NULL; |
| 12345 | for (trace = have_functions; *trace; trace++) { |
| 12346 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 12347 | if (!unicode) |
| 12348 | return NULL; |
| 12349 | if (PyList_Append(list, unicode)) |
| 12350 | return NULL; |
| 12351 | Py_DECREF(unicode); |
| 12352 | } |
| 12353 | PyModule_AddObject(m, "_have_functions", list); |
| 12354 | |
| 12355 | initialized = 1; |
| 12356 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12357 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12358 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12359 | |
| 12360 | #ifdef __cplusplus |
| 12361 | } |
| 12362 | #endif |