Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win. In that case the |
| 5 | module actually calls itself 'nt', not 'posix', and a few |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 11 | |
| 12 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13 | #ifdef __APPLE__ |
| 14 | /* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15 | * Step 1 of support for weak-linking a number of symbols existing on |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 16 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 17 | * at the end of this file for more information. |
| 18 | */ |
| 19 | # pragma weak lchown |
| 20 | # pragma weak statvfs |
| 21 | # pragma weak fstatvfs |
| 22 | |
| 23 | #endif /* __APPLE__ */ |
| 24 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 25 | #define PY_SSIZE_T_CLEAN |
| 26 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 27 | #include "Python.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 28 | #ifndef MS_WINDOWS |
| 29 | #include "posixmodule.h" |
| 30 | #endif |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 31 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 32 | #if defined(__VMS) |
Victor Stinner | b90db4c | 2011-04-26 22:48:24 +0200 | [diff] [blame] | 33 | # error "PEP 11: VMS is now unsupported, code will be removed in Python 3.4" |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 34 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 35 | #endif /* defined(__VMS) */ |
| 36 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 37 | #ifdef __cplusplus |
| 38 | extern "C" { |
| 39 | #endif |
| 40 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 41 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 42 | "This module provides access to operating system functionality that is\n\ |
| 43 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 44 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 45 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 47 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 48 | #ifdef HAVE_SYS_UIO_H |
| 49 | #include <sys/uio.h> |
| 50 | #endif |
| 51 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 52 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 53 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 54 | #endif /* HAVE_SYS_TYPES_H */ |
| 55 | |
| 56 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 57 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 58 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 60 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 61 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 62 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 63 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 64 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 65 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 66 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 67 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 68 | #ifdef HAVE_FCNTL_H |
| 69 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 70 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 72 | #ifdef HAVE_GRP_H |
| 73 | #include <grp.h> |
| 74 | #endif |
| 75 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 76 | #ifdef HAVE_SYSEXITS_H |
| 77 | #include <sysexits.h> |
| 78 | #endif /* HAVE_SYSEXITS_H */ |
| 79 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 80 | #ifdef HAVE_SYS_LOADAVG_H |
| 81 | #include <sys/loadavg.h> |
| 82 | #endif |
| 83 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 84 | #ifdef HAVE_LANGINFO_H |
| 85 | #include <langinfo.h> |
| 86 | #endif |
| 87 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 88 | #ifdef HAVE_SYS_SENDFILE_H |
| 89 | #include <sys/sendfile.h> |
| 90 | #endif |
| 91 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 92 | #ifdef HAVE_SCHED_H |
| 93 | #include <sched.h> |
| 94 | #endif |
| 95 | |
Benjamin Peterson | 2dbda07 | 2012-03-16 10:12:55 -0500 | [diff] [blame] | 96 | #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) |
Benjamin Peterson | 7b51b8d | 2012-03-14 22:28:25 -0500 | [diff] [blame] | 97 | #undef HAVE_SCHED_SETAFFINITY |
| 98 | #endif |
| 99 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 100 | #if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) |
| 101 | #define USE_XATTRS |
| 102 | #endif |
| 103 | |
| 104 | #ifdef USE_XATTRS |
Benjamin Peterson | b77fe17 | 2011-09-13 17:20:47 -0400 | [diff] [blame] | 105 | #include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 106 | #endif |
| 107 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 108 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 109 | #ifdef HAVE_SYS_SOCKET_H |
| 110 | #include <sys/socket.h> |
| 111 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 112 | #endif |
| 113 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 114 | #ifdef HAVE_DLFCN_H |
| 115 | #include <dlfcn.h> |
| 116 | #endif |
| 117 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 118 | #ifdef __hpux |
| 119 | #include <sys/mpctl.h> |
| 120 | #endif |
| 121 | |
| 122 | #if defined(__DragonFly__) || \ |
| 123 | defined(__OpenBSD__) || \ |
| 124 | defined(__FreeBSD__) || \ |
| 125 | defined(__NetBSD__) || \ |
| 126 | defined(__APPLE__) |
| 127 | #include <sys/sysctl.h> |
| 128 | #endif |
| 129 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 130 | #if defined(MS_WINDOWS) |
| 131 | # define TERMSIZE_USE_CONIO |
| 132 | #elif defined(HAVE_SYS_IOCTL_H) |
| 133 | # include <sys/ioctl.h> |
| 134 | # if defined(HAVE_TERMIOS_H) |
| 135 | # include <termios.h> |
| 136 | # endif |
| 137 | # if defined(TIOCGWINSZ) |
| 138 | # define TERMSIZE_USE_IOCTL |
| 139 | # endif |
| 140 | #endif /* MS_WINDOWS */ |
| 141 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 142 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 143 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 144 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 145 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 146 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 147 | #include <process.h> |
| 148 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 149 | #ifdef __BORLANDC__ /* Borland compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 150 | #define HAVE_EXECV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 151 | #define HAVE_OPENDIR 1 |
| 152 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 153 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 154 | #define HAVE_WAIT 1 |
| 155 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 156 | #ifdef _MSC_VER /* Microsoft compiler */ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 157 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 158 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 159 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 160 | #define HAVE_EXECV 1 |
| 161 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 162 | #define HAVE_SYSTEM 1 |
| 163 | #define HAVE_CWAIT 1 |
| 164 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 165 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 166 | #else |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 167 | #if defined(__VMS) |
| 168 | /* Everything needed is defined in vms/pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 169 | #else /* all other compilers */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 170 | /* Unix functions that the configure script doesn't check for */ |
| 171 | #define HAVE_EXECV 1 |
| 172 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 173 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 174 | #define HAVE_FORK1 1 |
| 175 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 176 | #define HAVE_GETEGID 1 |
| 177 | #define HAVE_GETEUID 1 |
| 178 | #define HAVE_GETGID 1 |
| 179 | #define HAVE_GETPPID 1 |
| 180 | #define HAVE_GETUID 1 |
| 181 | #define HAVE_KILL 1 |
| 182 | #define HAVE_OPENDIR 1 |
| 183 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 184 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 185 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 186 | #define HAVE_TTYNAME 1 |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 187 | #endif /* __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 188 | #endif /* _MSC_VER */ |
| 189 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 190 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 191 | |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 192 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 193 | /*[clinic] |
| 194 | module os |
| 195 | [clinic]*/ |
| 196 | /*[clinic checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 197 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 198 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 199 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 200 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 201 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 202 | (default) */ |
| 203 | extern char *ctermid_r(char *); |
| 204 | #endif |
| 205 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 206 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 207 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 208 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 209 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 210 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 211 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 212 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 213 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 214 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 215 | #endif |
| 216 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 217 | extern int chdir(char *); |
| 218 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 219 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 220 | extern int chdir(const char *); |
| 221 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 222 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 223 | #ifdef __BORLANDC__ |
| 224 | extern int chmod(const char *, int); |
| 225 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 226 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 227 | #endif |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 228 | /*#ifdef HAVE_FCHMOD |
| 229 | extern int fchmod(int, mode_t); |
| 230 | #endif*/ |
| 231 | /*#ifdef HAVE_LCHMOD |
| 232 | extern int lchmod(const char *, mode_t); |
| 233 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 234 | extern int chown(const char *, uid_t, gid_t); |
| 235 | extern char *getcwd(char *, int); |
| 236 | extern char *strerror(int); |
| 237 | extern int link(const char *, const char *); |
| 238 | extern int rename(const char *, const char *); |
| 239 | extern int stat(const char *, struct stat *); |
| 240 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 241 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 242 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 243 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 244 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 245 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 246 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 247 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 248 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 249 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 250 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | #ifdef HAVE_UTIME_H |
| 252 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 253 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 255 | #ifdef HAVE_SYS_UTIME_H |
| 256 | #include <sys/utime.h> |
| 257 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 258 | #endif /* HAVE_SYS_UTIME_H */ |
| 259 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 260 | #ifdef HAVE_SYS_TIMES_H |
| 261 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 262 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 263 | |
| 264 | #ifdef HAVE_SYS_PARAM_H |
| 265 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 266 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 267 | |
| 268 | #ifdef HAVE_SYS_UTSNAME_H |
| 269 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 270 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 271 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 272 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 273 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 274 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 275 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 276 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 277 | #include <direct.h> |
| 278 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 279 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 280 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 281 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 282 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 283 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 284 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 285 | #endif |
| 286 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 287 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 288 | #endif |
| 289 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 290 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 291 | #endif |
| 292 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 293 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 294 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 295 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 296 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 297 | #endif |
| 298 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 299 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 300 | #endif |
| 301 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 302 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 303 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 304 | #ifndef VOLUME_NAME_DOS |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 305 | #define VOLUME_NAME_DOS 0x0 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 306 | #endif |
| 307 | #ifndef VOLUME_NAME_NT |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 308 | #define VOLUME_NAME_NT 0x2 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 309 | #endif |
| 310 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 311 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 312 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 313 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 314 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 315 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 316 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 317 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 318 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 319 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 320 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 321 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 322 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 323 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 324 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 325 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 326 | #define MAXPATHLEN PATH_MAX |
| 327 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 328 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 329 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 330 | #endif /* MAXPATHLEN */ |
| 331 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 332 | #ifdef UNION_WAIT |
| 333 | /* Emulate some macros on systems that have a union instead of macros */ |
| 334 | |
| 335 | #ifndef WIFEXITED |
| 336 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 337 | #endif |
| 338 | |
| 339 | #ifndef WEXITSTATUS |
| 340 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 341 | #endif |
| 342 | |
| 343 | #ifndef WTERMSIG |
| 344 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 345 | #endif |
| 346 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 347 | #define WAIT_TYPE union wait |
| 348 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 349 | |
| 350 | #else /* !UNION_WAIT */ |
| 351 | #define WAIT_TYPE int |
| 352 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 353 | #endif /* UNION_WAIT */ |
| 354 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 355 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 356 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 357 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 358 | #define USE_CTERMID_R |
| 359 | #endif |
| 360 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 361 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 362 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 363 | #undef FSTAT |
| 364 | #undef STRUCT_STAT |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 365 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 366 | # define STAT win32_stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 367 | # define LSTAT win32_lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 368 | # define FSTAT win32_fstat |
| 369 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 370 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 371 | # define STAT stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 372 | # define LSTAT lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 373 | # define FSTAT fstat |
| 374 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 375 | #endif |
| 376 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 377 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 378 | #include <sys/mkdev.h> |
| 379 | #else |
| 380 | #if defined(MAJOR_IN_SYSMACROS) |
| 381 | #include <sys/sysmacros.h> |
| 382 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 383 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 384 | #include <sys/mkdev.h> |
| 385 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 386 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 387 | |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 388 | #define DWORD_MAX 4294967295U |
| 389 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 390 | |
| 391 | #ifdef MS_WINDOWS |
| 392 | static int |
| 393 | win32_warn_bytes_api() |
| 394 | { |
| 395 | return PyErr_WarnEx(PyExc_DeprecationWarning, |
| 396 | "The Windows bytes API has been deprecated, " |
| 397 | "use Unicode filenames instead", |
| 398 | 1); |
| 399 | } |
| 400 | #endif |
| 401 | |
| 402 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 403 | #ifndef MS_WINDOWS |
| 404 | PyObject * |
| 405 | _PyLong_FromUid(uid_t uid) |
| 406 | { |
| 407 | if (uid == (uid_t)-1) |
| 408 | return PyLong_FromLong(-1); |
| 409 | return PyLong_FromUnsignedLong(uid); |
| 410 | } |
| 411 | |
| 412 | PyObject * |
| 413 | _PyLong_FromGid(gid_t gid) |
| 414 | { |
| 415 | if (gid == (gid_t)-1) |
| 416 | return PyLong_FromLong(-1); |
| 417 | return PyLong_FromUnsignedLong(gid); |
| 418 | } |
| 419 | |
| 420 | int |
| 421 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 422 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 423 | uid_t uid; |
| 424 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 425 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 426 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 427 | unsigned long uresult; |
| 428 | |
| 429 | index = PyNumber_Index(obj); |
| 430 | if (index == NULL) { |
| 431 | PyErr_Format(PyExc_TypeError, |
| 432 | "uid should be integer, not %.200s", |
| 433 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 434 | return 0; |
| 435 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 436 | |
| 437 | /* |
| 438 | * Handling uid_t is complicated for two reasons: |
| 439 | * * Although uid_t is (always?) unsigned, it still |
| 440 | * accepts -1. |
| 441 | * * We don't know its size in advance--it may be |
| 442 | * bigger than an int, or it may be smaller than |
| 443 | * a long. |
| 444 | * |
| 445 | * So a bit of defensive programming is in order. |
| 446 | * Start with interpreting the value passed |
| 447 | * in as a signed long and see if it works. |
| 448 | */ |
| 449 | |
| 450 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 451 | |
| 452 | if (!overflow) { |
| 453 | uid = (uid_t)result; |
| 454 | |
| 455 | if (result == -1) { |
| 456 | if (PyErr_Occurred()) |
| 457 | goto fail; |
| 458 | /* It's a legitimate -1, we're done. */ |
| 459 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 460 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 461 | |
| 462 | /* Any other negative number is disallowed. */ |
| 463 | if (result < 0) |
| 464 | goto underflow; |
| 465 | |
| 466 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 467 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 468 | (long)uid != result) |
| 469 | goto underflow; |
| 470 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 471 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 472 | |
| 473 | if (overflow < 0) |
| 474 | goto underflow; |
| 475 | |
| 476 | /* |
| 477 | * Okay, the value overflowed a signed long. If it |
| 478 | * fits in an *unsigned* long, it may still be okay, |
| 479 | * as uid_t may be unsigned long on this platform. |
| 480 | */ |
| 481 | uresult = PyLong_AsUnsignedLong(index); |
| 482 | if (PyErr_Occurred()) { |
| 483 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 484 | goto overflow; |
| 485 | goto fail; |
| 486 | } |
| 487 | |
| 488 | uid = (uid_t)uresult; |
| 489 | |
| 490 | /* |
| 491 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, |
| 492 | * but this value would get interpreted as (uid_t)-1 by chown |
| 493 | * and its siblings. That's not what the user meant! So we |
| 494 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 495 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 496 | */ |
| 497 | if (uid == (uid_t)-1) |
| 498 | goto overflow; |
| 499 | |
| 500 | /* Ensure the value wasn't truncated. */ |
| 501 | if (sizeof(uid_t) < sizeof(long) && |
| 502 | (unsigned long)uid != uresult) |
| 503 | goto overflow; |
| 504 | /* fallthrough */ |
| 505 | |
| 506 | success: |
| 507 | Py_DECREF(index); |
| 508 | *(uid_t *)p = uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 509 | return 1; |
| 510 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 511 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 512 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 513 | "uid is less than minimum"); |
| 514 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 515 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 516 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 517 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 518 | "uid is greater than maximum"); |
| 519 | /* fallthrough */ |
| 520 | |
| 521 | fail: |
| 522 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | int |
| 527 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 528 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 529 | gid_t gid; |
| 530 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 531 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 532 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 533 | unsigned long uresult; |
| 534 | |
| 535 | index = PyNumber_Index(obj); |
| 536 | if (index == NULL) { |
| 537 | PyErr_Format(PyExc_TypeError, |
| 538 | "gid should be integer, not %.200s", |
| 539 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 540 | return 0; |
| 541 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 542 | |
| 543 | /* |
| 544 | * Handling gid_t is complicated for two reasons: |
| 545 | * * Although gid_t is (always?) unsigned, it still |
| 546 | * accepts -1. |
| 547 | * * We don't know its size in advance--it may be |
| 548 | * bigger than an int, or it may be smaller than |
| 549 | * a long. |
| 550 | * |
| 551 | * So a bit of defensive programming is in order. |
| 552 | * Start with interpreting the value passed |
| 553 | * in as a signed long and see if it works. |
| 554 | */ |
| 555 | |
| 556 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 557 | |
| 558 | if (!overflow) { |
| 559 | gid = (gid_t)result; |
| 560 | |
| 561 | if (result == -1) { |
| 562 | if (PyErr_Occurred()) |
| 563 | goto fail; |
| 564 | /* It's a legitimate -1, we're done. */ |
| 565 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 566 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 567 | |
| 568 | /* Any other negative number is disallowed. */ |
| 569 | if (result < 0) { |
| 570 | goto underflow; |
| 571 | } |
| 572 | |
| 573 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 574 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 575 | (long)gid != result) |
| 576 | goto underflow; |
| 577 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 578 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 579 | |
| 580 | if (overflow < 0) |
| 581 | goto underflow; |
| 582 | |
| 583 | /* |
| 584 | * Okay, the value overflowed a signed long. If it |
| 585 | * fits in an *unsigned* long, it may still be okay, |
| 586 | * as gid_t may be unsigned long on this platform. |
| 587 | */ |
| 588 | uresult = PyLong_AsUnsignedLong(index); |
| 589 | if (PyErr_Occurred()) { |
| 590 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 591 | goto overflow; |
| 592 | goto fail; |
| 593 | } |
| 594 | |
| 595 | gid = (gid_t)uresult; |
| 596 | |
| 597 | /* |
| 598 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, |
| 599 | * but this value would get interpreted as (gid_t)-1 by chown |
| 600 | * and its siblings. That's not what the user meant! So we |
| 601 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 602 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 603 | */ |
| 604 | if (gid == (gid_t)-1) |
| 605 | goto overflow; |
| 606 | |
| 607 | /* Ensure the value wasn't truncated. */ |
| 608 | if (sizeof(gid_t) < sizeof(long) && |
| 609 | (unsigned long)gid != uresult) |
| 610 | goto overflow; |
| 611 | /* fallthrough */ |
| 612 | |
| 613 | success: |
| 614 | Py_DECREF(index); |
| 615 | *(gid_t *)p = gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 616 | return 1; |
| 617 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 618 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 619 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 620 | "gid is less than minimum"); |
| 621 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 622 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 623 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 624 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 625 | "gid is greater than maximum"); |
| 626 | /* fallthrough */ |
| 627 | |
| 628 | fail: |
| 629 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 630 | return 0; |
| 631 | } |
| 632 | #endif /* MS_WINDOWS */ |
| 633 | |
| 634 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 635 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 636 | /* |
| 637 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 638 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 639 | * which doesn't play nicely with all the initializer lines in this file that |
| 640 | * look like this: |
| 641 | * int dir_fd = DEFAULT_DIR_FD; |
| 642 | */ |
| 643 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 644 | #else |
| 645 | #define DEFAULT_DIR_FD (-100) |
| 646 | #endif |
| 647 | |
| 648 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 649 | _fd_converter(PyObject *o, int *p, const char *allowed) |
| 650 | { |
| 651 | int overflow; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 652 | long long_value; |
| 653 | |
| 654 | PyObject *index = PyNumber_Index(o); |
| 655 | if (index == NULL) { |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 656 | PyErr_Format(PyExc_TypeError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 657 | "argument should be %s, not %.200s", |
| 658 | allowed, Py_TYPE(o)->tp_name); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 659 | return 0; |
| 660 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 661 | |
| 662 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
| 663 | Py_DECREF(index); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 664 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 665 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 666 | "fd is greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 667 | return 0; |
| 668 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 669 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 670 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 671 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 672 | return 0; |
| 673 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 674 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 675 | *p = (int)long_value; |
| 676 | return 1; |
| 677 | } |
| 678 | |
| 679 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 680 | dir_fd_converter(PyObject *o, void *p) |
| 681 | { |
| 682 | if (o == Py_None) { |
| 683 | *(int *)p = DEFAULT_DIR_FD; |
| 684 | return 1; |
| 685 | } |
| 686 | return _fd_converter(o, (int *)p, "integer"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | |
| 690 | |
| 691 | /* |
| 692 | * A PyArg_ParseTuple "converter" function |
| 693 | * that handles filesystem paths in the manner |
| 694 | * preferred by the os module. |
| 695 | * |
| 696 | * path_converter accepts (Unicode) strings and their |
| 697 | * subclasses, and bytes and their subclasses. What |
| 698 | * it does with the argument depends on the platform: |
| 699 | * |
| 700 | * * On Windows, if we get a (Unicode) string we |
| 701 | * extract the wchar_t * and return it; if we get |
| 702 | * bytes we extract the char * and return that. |
| 703 | * |
| 704 | * * On all other platforms, strings are encoded |
| 705 | * to bytes using PyUnicode_FSConverter, then we |
| 706 | * extract the char * from the bytes object and |
| 707 | * return that. |
| 708 | * |
| 709 | * path_converter also optionally accepts signed |
| 710 | * integers (representing open file descriptors) instead |
| 711 | * of path strings. |
| 712 | * |
| 713 | * Input fields: |
| 714 | * path.nullable |
| 715 | * If nonzero, the path is permitted to be None. |
| 716 | * path.allow_fd |
| 717 | * If nonzero, the path is permitted to be a file handle |
| 718 | * (a signed int) instead of a string. |
| 719 | * path.function_name |
| 720 | * If non-NULL, path_converter will use that as the name |
| 721 | * of the function in error messages. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 722 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 723 | * path.argument_name |
| 724 | * If non-NULL, path_converter will use that as the name |
| 725 | * of the parameter in error messages. |
| 726 | * (If path.argument_name is NULL it uses "path".) |
| 727 | * |
| 728 | * Output fields: |
| 729 | * path.wide |
| 730 | * Points to the path if it was expressed as Unicode |
| 731 | * and was not encoded. (Only used on Windows.) |
| 732 | * path.narrow |
| 733 | * Points to the path if it was expressed as bytes, |
| 734 | * or it was Unicode and was encoded to bytes. |
| 735 | * path.fd |
| 736 | * Contains a file descriptor if path.accept_fd was true |
| 737 | * and the caller provided a signed integer instead of any |
| 738 | * sort of string. |
| 739 | * |
| 740 | * WARNING: if your "path" parameter is optional, and is |
| 741 | * unspecified, path_converter will never get called. |
| 742 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 743 | * yourself! |
| 744 | * path.length |
| 745 | * The length of the path in characters, if specified as |
| 746 | * a string. |
| 747 | * path.object |
| 748 | * The original object passed in. |
| 749 | * path.cleanup |
| 750 | * For internal use only. May point to a temporary object. |
| 751 | * (Pay no attention to the man behind the curtain.) |
| 752 | * |
| 753 | * At most one of path.wide or path.narrow will be non-NULL. |
| 754 | * If path was None and path.nullable was set, |
| 755 | * or if path was an integer and path.allow_fd was set, |
| 756 | * both path.wide and path.narrow will be NULL |
| 757 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 758 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 759 | * path_converter takes care to not write to the path_t |
| 760 | * unless it's successful. However it must reset the |
| 761 | * "cleanup" field each time it's called. |
| 762 | * |
| 763 | * Use as follows: |
| 764 | * path_t path; |
| 765 | * memset(&path, 0, sizeof(path)); |
| 766 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 767 | * // ... use values from path ... |
| 768 | * path_cleanup(&path); |
| 769 | * |
| 770 | * (Note that if PyArg_Parse fails you don't need to call |
| 771 | * path_cleanup(). However it is safe to do so.) |
| 772 | */ |
| 773 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 774 | const char *function_name; |
| 775 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 776 | int nullable; |
| 777 | int allow_fd; |
| 778 | wchar_t *wide; |
| 779 | char *narrow; |
| 780 | int fd; |
| 781 | Py_ssize_t length; |
| 782 | PyObject *object; |
| 783 | PyObject *cleanup; |
| 784 | } path_t; |
| 785 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 786 | #define PATH_T_INITIALIZE(function_name, nullable, allow_fd) \ |
| 787 | {function_name, NULL, nullable, allow_fd, NULL, NULL, 0, 0, NULL, NULL} |
| 788 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 789 | static void |
| 790 | path_cleanup(path_t *path) { |
| 791 | if (path->cleanup) { |
| 792 | Py_DECREF(path->cleanup); |
| 793 | path->cleanup = NULL; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | static int |
| 798 | path_converter(PyObject *o, void *p) { |
| 799 | path_t *path = (path_t *)p; |
| 800 | PyObject *unicode, *bytes; |
| 801 | Py_ssize_t length; |
| 802 | char *narrow; |
| 803 | |
| 804 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 805 | PyErr_Format(exc, "%s%s" fmt, \ |
| 806 | path->function_name ? path->function_name : "", \ |
| 807 | path->function_name ? ": " : "", \ |
| 808 | path->argument_name ? path->argument_name : "path") |
| 809 | |
| 810 | /* Py_CLEANUP_SUPPORTED support */ |
| 811 | if (o == NULL) { |
| 812 | path_cleanup(path); |
| 813 | return 1; |
| 814 | } |
| 815 | |
| 816 | /* ensure it's always safe to call path_cleanup() */ |
| 817 | path->cleanup = NULL; |
| 818 | |
| 819 | if (o == Py_None) { |
| 820 | if (!path->nullable) { |
| 821 | FORMAT_EXCEPTION(PyExc_TypeError, |
| 822 | "can't specify None for %s argument"); |
| 823 | return 0; |
| 824 | } |
| 825 | path->wide = NULL; |
| 826 | path->narrow = NULL; |
| 827 | path->length = 0; |
| 828 | path->object = o; |
| 829 | path->fd = -1; |
| 830 | return 1; |
| 831 | } |
| 832 | |
| 833 | unicode = PyUnicode_FromObject(o); |
| 834 | if (unicode) { |
| 835 | #ifdef MS_WINDOWS |
| 836 | wchar_t *wide; |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 837 | |
| 838 | wide = PyUnicode_AsUnicodeAndSize(unicode, &length); |
| 839 | if (!wide) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 840 | Py_DECREF(unicode); |
| 841 | return 0; |
| 842 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 843 | if (length > 32767) { |
| 844 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 845 | Py_DECREF(unicode); |
| 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | path->wide = wide; |
| 850 | path->narrow = NULL; |
| 851 | path->length = length; |
| 852 | path->object = o; |
| 853 | path->fd = -1; |
| 854 | path->cleanup = unicode; |
| 855 | return Py_CLEANUP_SUPPORTED; |
| 856 | #else |
| 857 | int converted = PyUnicode_FSConverter(unicode, &bytes); |
| 858 | Py_DECREF(unicode); |
| 859 | if (!converted) |
| 860 | bytes = NULL; |
| 861 | #endif |
| 862 | } |
| 863 | else { |
| 864 | PyErr_Clear(); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 865 | if (PyObject_CheckBuffer(o)) |
| 866 | bytes = PyBytes_FromObject(o); |
| 867 | else |
| 868 | bytes = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 869 | if (!bytes) { |
| 870 | PyErr_Clear(); |
| 871 | if (path->allow_fd) { |
| 872 | int fd; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 873 | int result = _fd_converter(o, &fd, |
| 874 | "string, bytes or integer"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 875 | if (result) { |
| 876 | path->wide = NULL; |
| 877 | path->narrow = NULL; |
| 878 | path->length = 0; |
| 879 | path->object = o; |
| 880 | path->fd = fd; |
| 881 | return result; |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | if (!bytes) { |
| 888 | if (!PyErr_Occurred()) |
| 889 | FORMAT_EXCEPTION(PyExc_TypeError, "illegal type for %s parameter"); |
| 890 | return 0; |
| 891 | } |
| 892 | |
| 893 | #ifdef MS_WINDOWS |
| 894 | if (win32_warn_bytes_api()) { |
| 895 | Py_DECREF(bytes); |
| 896 | return 0; |
| 897 | } |
| 898 | #endif |
| 899 | |
| 900 | length = PyBytes_GET_SIZE(bytes); |
| 901 | #ifdef MS_WINDOWS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 902 | if (length > MAX_PATH-1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 903 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
| 904 | Py_DECREF(bytes); |
| 905 | return 0; |
| 906 | } |
| 907 | #endif |
| 908 | |
| 909 | narrow = PyBytes_AS_STRING(bytes); |
| 910 | if (length != strlen(narrow)) { |
| 911 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded NUL character in %s"); |
| 912 | Py_DECREF(bytes); |
| 913 | return 0; |
| 914 | } |
| 915 | |
| 916 | path->wide = NULL; |
| 917 | path->narrow = narrow; |
| 918 | path->length = length; |
| 919 | path->object = o; |
| 920 | path->fd = -1; |
| 921 | path->cleanup = bytes; |
| 922 | return Py_CLEANUP_SUPPORTED; |
| 923 | } |
| 924 | |
| 925 | static void |
| 926 | argument_unavailable_error(char *function_name, char *argument_name) { |
| 927 | PyErr_Format(PyExc_NotImplementedError, |
| 928 | "%s%s%s unavailable on this platform", |
| 929 | (function_name != NULL) ? function_name : "", |
| 930 | (function_name != NULL) ? ": ": "", |
| 931 | argument_name); |
| 932 | } |
| 933 | |
| 934 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 935 | dir_fd_unavailable(PyObject *o, void *p) |
| 936 | { |
| 937 | int dir_fd; |
| 938 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 939 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 940 | if (dir_fd != DEFAULT_DIR_FD) { |
| 941 | argument_unavailable_error(NULL, "dir_fd"); |
| 942 | return 0; |
| 943 | } |
| 944 | *(int *)p = dir_fd; |
| 945 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | static int |
| 949 | fd_specified(char *function_name, int fd) { |
| 950 | if (fd == -1) |
| 951 | return 0; |
| 952 | |
| 953 | argument_unavailable_error(function_name, "fd"); |
| 954 | return 1; |
| 955 | } |
| 956 | |
| 957 | static int |
| 958 | follow_symlinks_specified(char *function_name, int follow_symlinks) { |
| 959 | if (follow_symlinks) |
| 960 | return 0; |
| 961 | |
| 962 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 963 | return 1; |
| 964 | } |
| 965 | |
| 966 | static int |
| 967 | path_and_dir_fd_invalid(char *function_name, path_t *path, int dir_fd) { |
| 968 | if (!path->narrow && !path->wide && (dir_fd != DEFAULT_DIR_FD)) { |
| 969 | PyErr_Format(PyExc_ValueError, |
| 970 | "%s: can't specify dir_fd without matching path", |
| 971 | function_name); |
| 972 | return 1; |
| 973 | } |
| 974 | return 0; |
| 975 | } |
| 976 | |
| 977 | static int |
| 978 | dir_fd_and_fd_invalid(char *function_name, int dir_fd, int fd) { |
| 979 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 980 | PyErr_Format(PyExc_ValueError, |
| 981 | "%s: can't specify both dir_fd and fd", |
| 982 | function_name); |
| 983 | return 1; |
| 984 | } |
| 985 | return 0; |
| 986 | } |
| 987 | |
| 988 | static int |
| 989 | fd_and_follow_symlinks_invalid(char *function_name, int fd, |
| 990 | int follow_symlinks) { |
| 991 | if ((fd > 0) && (!follow_symlinks)) { |
| 992 | PyErr_Format(PyExc_ValueError, |
| 993 | "%s: cannot use fd and follow_symlinks together", |
| 994 | function_name); |
| 995 | return 1; |
| 996 | } |
| 997 | return 0; |
| 998 | } |
| 999 | |
| 1000 | static int |
| 1001 | dir_fd_and_follow_symlinks_invalid(char *function_name, int dir_fd, |
| 1002 | int follow_symlinks) { |
| 1003 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 1004 | PyErr_Format(PyExc_ValueError, |
| 1005 | "%s: cannot use dir_fd and follow_symlinks together", |
| 1006 | function_name); |
| 1007 | return 1; |
| 1008 | } |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1012 | /* A helper used by a number of POSIX-only functions */ |
| 1013 | #ifndef MS_WINDOWS |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 1014 | static int |
| 1015 | _parse_off_t(PyObject* arg, void* addr) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1016 | { |
| 1017 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 1018 | *((off_t*)addr) = PyLong_AsLong(arg); |
| 1019 | #else |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 1020 | *((off_t*)addr) = PyLong_AsLongLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1021 | #endif |
| 1022 | if (PyErr_Occurred()) |
| 1023 | return 0; |
| 1024 | return 1; |
| 1025 | } |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1026 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1027 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1028 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 1029 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 1030 | * valid and raise an assertion if it isn't. |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1031 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 1032 | * an assertion can be useful, but it does contradict the POSIX standard |
| 1033 | * which for write(2) states: |
| 1034 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 1035 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 1036 | * writing." |
| 1037 | * Furthermore, python allows the user to enter any old integer |
| 1038 | * as a fd and should merely raise a python exception on error. |
| 1039 | * The Microsoft CRT doesn't provide an official way to check for the |
| 1040 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1041 | * 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] | 1042 | * internal structures involved. |
| 1043 | * The structures below must be updated for each version of visual studio |
| 1044 | * according to the file internal.h in the CRT source, until MS comes |
| 1045 | * up with a less hacky way to do this. |
| 1046 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 1047 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 1048 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 1049 | /* The actual size of the structure is determined at runtime. |
| 1050 | * Only the first items must be present. |
| 1051 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1052 | typedef struct { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1053 | intptr_t osfhnd; |
| 1054 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 1055 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1056 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 1057 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1058 | #define IOINFO_L2E 5 |
| 1059 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 1060 | #define IOINFO_ARRAYS 64 |
| 1061 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 1062 | #define FOPEN 0x01 |
| 1063 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 1064 | |
| 1065 | /* This function emulates what the windows CRT does to validate file handles */ |
| 1066 | int |
| 1067 | _PyVerify_fd(int fd) |
| 1068 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1069 | const int i1 = fd >> IOINFO_L2E; |
| 1070 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 1071 | |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 1072 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1073 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1074 | /* Determine the actual size of the ioinfo structure, |
| 1075 | * as used by the CRT loaded in memory |
| 1076 | */ |
| 1077 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 1078 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 1079 | } |
| 1080 | if (sizeof_ioinfo == 0) { |
| 1081 | /* This should not happen... */ |
| 1082 | goto fail; |
| 1083 | } |
| 1084 | |
| 1085 | /* See that it isn't a special CLEAR fileno */ |
| 1086 | if (fd != _NO_CONSOLE_FILENO) { |
| 1087 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 1088 | * we check pointer validity and other info |
| 1089 | */ |
| 1090 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 1091 | /* finally, check that the file is open */ |
| 1092 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 1093 | if (info->osfile & FOPEN) { |
| 1094 | return 1; |
| 1095 | } |
| 1096 | } |
| 1097 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 1098 | fail: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1099 | errno = EBADF; |
| 1100 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 1104 | static int |
| 1105 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 1106 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1107 | if (!_PyVerify_fd(fd1)) |
| 1108 | return 0; |
| 1109 | if (fd2 == _NO_CONSOLE_FILENO) |
| 1110 | return 0; |
| 1111 | if ((unsigned)fd2 < _NHANDLE_) |
| 1112 | return 1; |
| 1113 | else |
| 1114 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1115 | } |
| 1116 | #else |
| 1117 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 1118 | #define _PyVerify_fd_dup2(A, B) (1) |
| 1119 | #endif |
| 1120 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1121 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1122 | /* The following structure was copied from |
| 1123 | http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required |
| 1124 | include doesn't seem to be present in the Windows SDK (at least as included |
| 1125 | with Visual Studio Express). */ |
| 1126 | typedef struct _REPARSE_DATA_BUFFER { |
| 1127 | ULONG ReparseTag; |
| 1128 | USHORT ReparseDataLength; |
| 1129 | USHORT Reserved; |
| 1130 | union { |
| 1131 | struct { |
| 1132 | USHORT SubstituteNameOffset; |
| 1133 | USHORT SubstituteNameLength; |
| 1134 | USHORT PrintNameOffset; |
| 1135 | USHORT PrintNameLength; |
| 1136 | ULONG Flags; |
| 1137 | WCHAR PathBuffer[1]; |
| 1138 | } SymbolicLinkReparseBuffer; |
| 1139 | |
| 1140 | struct { |
| 1141 | USHORT SubstituteNameOffset; |
| 1142 | USHORT SubstituteNameLength; |
| 1143 | USHORT PrintNameOffset; |
| 1144 | USHORT PrintNameLength; |
| 1145 | WCHAR PathBuffer[1]; |
| 1146 | } MountPointReparseBuffer; |
| 1147 | |
| 1148 | struct { |
| 1149 | UCHAR DataBuffer[1]; |
| 1150 | } GenericReparseBuffer; |
| 1151 | }; |
| 1152 | } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 1153 | |
| 1154 | #define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ |
| 1155 | GenericReparseBuffer) |
| 1156 | #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) |
| 1157 | |
| 1158 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1159 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1160 | { |
| 1161 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1162 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 1163 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1164 | |
| 1165 | if (0 == DeviceIoControl( |
| 1166 | reparse_point_handle, |
| 1167 | FSCTL_GET_REPARSE_POINT, |
| 1168 | NULL, 0, /* in buffer */ |
| 1169 | target_buffer, sizeof(target_buffer), |
| 1170 | &n_bytes_returned, |
| 1171 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1172 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1173 | |
| 1174 | if (reparse_tag) |
| 1175 | *reparse_tag = rdb->ReparseTag; |
| 1176 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1177 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1178 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1179 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1180 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1181 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1182 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1183 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1184 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1185 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1186 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1187 | */ |
| 1188 | #include <crt_externs.h> |
| 1189 | static char **environ; |
| 1190 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1191 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1192 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1193 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1194 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1195 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1196 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1197 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1198 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1199 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1200 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1201 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1202 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1203 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1204 | d = PyDict_New(); |
| 1205 | if (d == NULL) |
| 1206 | return NULL; |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1207 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1208 | if (environ == NULL) |
| 1209 | environ = *_NSGetEnviron(); |
| 1210 | #endif |
| 1211 | #ifdef MS_WINDOWS |
| 1212 | /* _wenviron must be initialized in this way if the program is started |
| 1213 | through main() instead of wmain(). */ |
| 1214 | _wgetenv(L""); |
| 1215 | if (_wenviron == NULL) |
| 1216 | return d; |
| 1217 | /* This part ignores errors */ |
| 1218 | for (e = _wenviron; *e != NULL; e++) { |
| 1219 | PyObject *k; |
| 1220 | PyObject *v; |
| 1221 | wchar_t *p = wcschr(*e, L'='); |
| 1222 | if (p == NULL) |
| 1223 | continue; |
| 1224 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1225 | if (k == NULL) { |
| 1226 | PyErr_Clear(); |
| 1227 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1228 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1229 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1230 | if (v == NULL) { |
| 1231 | PyErr_Clear(); |
| 1232 | Py_DECREF(k); |
| 1233 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1234 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1235 | if (PyDict_GetItem(d, k) == NULL) { |
| 1236 | if (PyDict_SetItem(d, k, v) != 0) |
| 1237 | PyErr_Clear(); |
| 1238 | } |
| 1239 | Py_DECREF(k); |
| 1240 | Py_DECREF(v); |
| 1241 | } |
| 1242 | #else |
| 1243 | if (environ == NULL) |
| 1244 | return d; |
| 1245 | /* This part ignores errors */ |
| 1246 | for (e = environ; *e != NULL; e++) { |
| 1247 | PyObject *k; |
| 1248 | PyObject *v; |
| 1249 | char *p = strchr(*e, '='); |
| 1250 | if (p == NULL) |
| 1251 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1252 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1253 | if (k == NULL) { |
| 1254 | PyErr_Clear(); |
| 1255 | continue; |
| 1256 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1257 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1258 | if (v == NULL) { |
| 1259 | PyErr_Clear(); |
| 1260 | Py_DECREF(k); |
| 1261 | continue; |
| 1262 | } |
| 1263 | if (PyDict_GetItem(d, k) == NULL) { |
| 1264 | if (PyDict_SetItem(d, k, v) != 0) |
| 1265 | PyErr_Clear(); |
| 1266 | } |
| 1267 | Py_DECREF(k); |
| 1268 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1269 | } |
| 1270 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1271 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1274 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1275 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1276 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1277 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1278 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1279 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1280 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1281 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1282 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1283 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1284 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1285 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1286 | /* XXX We should pass the function name along in the future. |
| 1287 | (winreg.c also wants to pass the function name.) |
| 1288 | This would however require an additional param to the |
| 1289 | Windows error object, which is non-trivial. |
| 1290 | */ |
| 1291 | errno = GetLastError(); |
| 1292 | if (filename) |
| 1293 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1294 | else |
| 1295 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1296 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1297 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1298 | static PyObject * |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1299 | win32_error_object(char* function, PyObject* filename) |
| 1300 | { |
| 1301 | /* XXX - see win32_error for comments on 'function' */ |
| 1302 | errno = GetLastError(); |
| 1303 | if (filename) |
| 1304 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1305 | PyExc_OSError, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1306 | errno, |
| 1307 | filename); |
| 1308 | else |
| 1309 | return PyErr_SetFromWindowsErr(errno); |
| 1310 | } |
| 1311 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1312 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1313 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1314 | static PyObject * |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1315 | path_error(path_t *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1316 | { |
| 1317 | #ifdef MS_WINDOWS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1318 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 1319 | 0, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1320 | #else |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1321 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1322 | #endif |
| 1323 | } |
| 1324 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1325 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1326 | /* POSIX generic methods */ |
| 1327 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1328 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1329 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 1330 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1331 | int fd; |
| 1332 | int res; |
| 1333 | fd = PyObject_AsFileDescriptor(fdobj); |
| 1334 | if (fd < 0) |
| 1335 | return NULL; |
| 1336 | if (!_PyVerify_fd(fd)) |
| 1337 | return posix_error(); |
| 1338 | Py_BEGIN_ALLOW_THREADS |
| 1339 | res = (*func)(fd); |
| 1340 | Py_END_ALLOW_THREADS |
| 1341 | if (res < 0) |
| 1342 | return posix_error(); |
| 1343 | Py_INCREF(Py_None); |
| 1344 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1345 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1346 | |
| 1347 | static PyObject * |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1348 | posix_1str(const char *func_name, PyObject *args, char *format, |
| 1349 | int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1350 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1351 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1352 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1353 | memset(&path, 0, sizeof(path)); |
| 1354 | path.function_name = func_name; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1355 | if (!PyArg_ParseTuple(args, format, |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1356 | path_converter, &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1357 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1358 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1359 | res = (*func)(path.narrow); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1360 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1361 | if (res < 0) { |
| 1362 | path_error(&path); |
| 1363 | path_cleanup(&path); |
| 1364 | return NULL; |
| 1365 | } |
| 1366 | path_cleanup(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1367 | Py_INCREF(Py_None); |
| 1368 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1371 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1372 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1373 | /* This is a reimplementation of the C library's chdir function, |
| 1374 | but one that produces Win32 errors instead of DOS error codes. |
| 1375 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1376 | it also needs to set "magic" environment variables indicating |
| 1377 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1378 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1379 | win32_chdir(LPCSTR path) |
| 1380 | { |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1381 | char new_path[MAX_PATH]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1382 | int result; |
| 1383 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1384 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1385 | if(!SetCurrentDirectoryA(path)) |
| 1386 | return FALSE; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1387 | result = GetCurrentDirectoryA(Py_ARRAY_LENGTH(new_path), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1388 | if (!result) |
| 1389 | return FALSE; |
| 1390 | /* In the ANSI API, there should not be any paths longer |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1391 | than MAX_PATH-1 (not including the final null character). */ |
| 1392 | assert(result < Py_ARRAY_LENGTH(new_path)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1393 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 1394 | strncmp(new_path, "//", 2) == 0) |
| 1395 | /* UNC path, nothing to do. */ |
| 1396 | return TRUE; |
| 1397 | env[1] = new_path[0]; |
| 1398 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | /* The Unicode version differs from the ANSI version |
| 1402 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1403 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1404 | win32_wchdir(LPCWSTR path) |
| 1405 | { |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1406 | wchar_t _new_path[MAX_PATH], *new_path = _new_path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1407 | int result; |
| 1408 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1409 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1410 | if(!SetCurrentDirectoryW(path)) |
| 1411 | return FALSE; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1412 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(new_path), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1413 | if (!result) |
| 1414 | return FALSE; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1415 | if (result > Py_ARRAY_LENGTH(new_path)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1416 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1417 | if (!new_path) { |
| 1418 | SetLastError(ERROR_OUTOFMEMORY); |
| 1419 | return FALSE; |
| 1420 | } |
| 1421 | result = GetCurrentDirectoryW(result, new_path); |
| 1422 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1423 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1424 | return FALSE; |
| 1425 | } |
| 1426 | } |
| 1427 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1428 | wcsncmp(new_path, L"//", 2) == 0) |
| 1429 | /* UNC path, nothing to do. */ |
| 1430 | return TRUE; |
| 1431 | env[1] = new_path[0]; |
| 1432 | result = SetEnvironmentVariableW(env, new_path); |
| 1433 | if (new_path != _new_path) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1434 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1435 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1436 | } |
| 1437 | #endif |
| 1438 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1439 | #ifdef MS_WINDOWS |
| 1440 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1441 | - time stamps are restricted to second resolution |
| 1442 | - file modification times suffer from forth-and-back conversions between |
| 1443 | UTC and local time |
| 1444 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1445 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1446 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1447 | |
| 1448 | struct win32_stat{ |
Brian Curtin | 87e63a2 | 2012-12-31 11:59:48 -0600 | [diff] [blame] | 1449 | unsigned long st_dev; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1450 | __int64 st_ino; |
| 1451 | unsigned short st_mode; |
| 1452 | int st_nlink; |
| 1453 | int st_uid; |
| 1454 | int st_gid; |
Brian Curtin | 87e63a2 | 2012-12-31 11:59:48 -0600 | [diff] [blame] | 1455 | unsigned long st_rdev; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1456 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1457 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1458 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1459 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1460 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1461 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1462 | int st_ctime_nsec; |
| 1463 | }; |
| 1464 | |
| 1465 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 1466 | |
| 1467 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1468 | 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] | 1469 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1470 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 1471 | /* Cannot simply cast and dereference in_ptr, |
| 1472 | since it might not be aligned properly */ |
| 1473 | __int64 in; |
| 1474 | memcpy(&in, in_ptr, sizeof(in)); |
| 1475 | *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] | 1476 | *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] | 1477 | } |
| 1478 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1479 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1480 | 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] | 1481 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1482 | /* XXX endianness */ |
| 1483 | __int64 out; |
| 1484 | out = time_in + secs_between_epochs; |
| 1485 | out = out * 10000000 + nsec_in / 100; |
| 1486 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1489 | /* Below, we *know* that ugo+r is 0444 */ |
| 1490 | #if _S_IREAD != 0400 |
| 1491 | #error Unsupported C library |
| 1492 | #endif |
| 1493 | static int |
| 1494 | attributes_to_mode(DWORD attr) |
| 1495 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1496 | int m = 0; |
| 1497 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1498 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1499 | else |
| 1500 | m |= _S_IFREG; |
| 1501 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1502 | m |= 0444; |
| 1503 | else |
| 1504 | m |= 0666; |
| 1505 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1509 | 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] | 1510 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1511 | memset(result, 0, sizeof(*result)); |
| 1512 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1513 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
Brian Curtin | 490b32a | 2012-12-26 07:03:03 -0600 | [diff] [blame] | 1514 | result->st_dev = info->dwVolumeSerialNumber; |
| 1515 | result->st_rdev = result->st_dev; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1516 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1517 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1518 | 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] | 1519 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1520 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1521 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1522 | /* first clear the S_IFMT bits */ |
Christian Heimes | 99d6135 | 2013-06-23 23:56:05 +0200 | [diff] [blame] | 1523 | result->st_mode ^= (result->st_mode & S_IFMT); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1524 | /* now set the bits that make this a symlink */ |
Christian Heimes | 99d6135 | 2013-06-23 23:56:05 +0200 | [diff] [blame] | 1525 | result->st_mode |= S_IFLNK; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1526 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1527 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1528 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1531 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1532 | 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] | 1533 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1534 | HANDLE hFindFile; |
| 1535 | WIN32_FIND_DATAA FileData; |
| 1536 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1537 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1538 | return FALSE; |
| 1539 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1540 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1541 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1542 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1543 | info->ftCreationTime = FileData.ftCreationTime; |
| 1544 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1545 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1546 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1547 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1548 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1549 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1550 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1551 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1555 | 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] | 1556 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1557 | HANDLE hFindFile; |
| 1558 | WIN32_FIND_DATAW FileData; |
| 1559 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1560 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1561 | return FALSE; |
| 1562 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1563 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1564 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1565 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1566 | info->ftCreationTime = FileData.ftCreationTime; |
| 1567 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1568 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1569 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1570 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1571 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1572 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1573 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1574 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1577 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
Antoine Pitrou | 06eecea | 2012-10-21 16:33:33 +0200 | [diff] [blame] | 1578 | static int has_GetFinalPathNameByHandle = -1; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1579 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 1580 | DWORD); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1581 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1582 | check_GetFinalPathNameByHandle() |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1583 | { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1584 | HINSTANCE hKernel32; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1585 | DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 1586 | DWORD); |
| 1587 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1588 | /* only recheck */ |
Antoine Pitrou | 06eecea | 2012-10-21 16:33:33 +0200 | [diff] [blame] | 1589 | if (-1 == has_GetFinalPathNameByHandle) |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1590 | { |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1591 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1592 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 1593 | "GetFinalPathNameByHandleA"); |
| 1594 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 1595 | "GetFinalPathNameByHandleW"); |
| 1596 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 1597 | Py_GetFinalPathNameByHandleW; |
| 1598 | } |
| 1599 | return has_GetFinalPathNameByHandle; |
| 1600 | } |
| 1601 | |
| 1602 | static BOOL |
| 1603 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1604 | { |
| 1605 | int buf_size, result_length; |
| 1606 | wchar_t *buf; |
| 1607 | |
| 1608 | /* We have a good handle to the target, use it to determine |
| 1609 | the target path name (then we'll call lstat on it). */ |
| 1610 | buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1611 | VOLUME_NAME_DOS); |
| 1612 | if(!buf_size) |
| 1613 | return FALSE; |
| 1614 | |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1615 | buf = (wchar_t *)PyMem_Malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1616 | if (!buf) { |
| 1617 | SetLastError(ERROR_OUTOFMEMORY); |
| 1618 | return FALSE; |
| 1619 | } |
| 1620 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1621 | result_length = Py_GetFinalPathNameByHandleW(hdl, |
| 1622 | buf, buf_size, VOLUME_NAME_DOS); |
| 1623 | |
| 1624 | if(!result_length) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1625 | PyMem_Free(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1626 | return FALSE; |
| 1627 | } |
| 1628 | |
| 1629 | if(!CloseHandle(hdl)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1630 | PyMem_Free(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1631 | return FALSE; |
| 1632 | } |
| 1633 | |
| 1634 | buf[result_length] = 0; |
| 1635 | |
| 1636 | *target_path = buf; |
| 1637 | return TRUE; |
| 1638 | } |
| 1639 | |
| 1640 | static int |
| 1641 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1642 | BOOL traverse); |
| 1643 | static int |
| 1644 | win32_xstat_impl(const char *path, struct win32_stat *result, |
| 1645 | BOOL traverse) |
| 1646 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1647 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1648 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1649 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1650 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1651 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1652 | const char *dot; |
| 1653 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1654 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1655 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1656 | traverse reparse point. */ |
| 1657 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1660 | hFile = CreateFileA( |
| 1661 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1662 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1663 | 0, /* share mode */ |
| 1664 | NULL, /* security attributes */ |
| 1665 | OPEN_EXISTING, |
| 1666 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1667 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1668 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1669 | the symlink path agin and not the actual final path. */ |
| 1670 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1671 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1672 | NULL); |
| 1673 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1674 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1675 | /* Either the target doesn't exist, or we don't have access to |
| 1676 | get a handle to it. If the former, we need to return an error. |
| 1677 | If the latter, we can use attributes_from_dir. */ |
| 1678 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1679 | return -1; |
| 1680 | /* Could not get attributes on open file. Fall back to |
| 1681 | reading the directory. */ |
| 1682 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1683 | /* Very strange. This should not fail now */ |
| 1684 | return -1; |
| 1685 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1686 | if (traverse) { |
| 1687 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1688 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1689 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1690 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1691 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1692 | } else { |
| 1693 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1694 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1695 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1696 | } |
| 1697 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1698 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1699 | return -1; |
| 1700 | |
| 1701 | /* Close the outer open file handle now that we're about to |
| 1702 | reopen it with different flags. */ |
| 1703 | if (!CloseHandle(hFile)) |
| 1704 | return -1; |
| 1705 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1706 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1707 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1708 | the file without the reparse handling flag set. */ |
| 1709 | hFile2 = CreateFileA( |
| 1710 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1711 | NULL, OPEN_EXISTING, |
| 1712 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1713 | NULL); |
| 1714 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1715 | return -1; |
| 1716 | |
| 1717 | if (!get_target_path(hFile2, &target_path)) |
| 1718 | return -1; |
| 1719 | |
| 1720 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1721 | PyMem_Free(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1722 | return code; |
| 1723 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1724 | } else |
| 1725 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1726 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1727 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1728 | |
| 1729 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1730 | dot = strrchr(path, '.'); |
| 1731 | if (dot) { |
| 1732 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1733 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1734 | result->st_mode |= 0111; |
| 1735 | } |
| 1736 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1740 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1741 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1742 | { |
| 1743 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1744 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1745 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1746 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1747 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1748 | const wchar_t *dot; |
| 1749 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1750 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1751 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1752 | traverse reparse point. */ |
| 1753 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1756 | hFile = CreateFileW( |
| 1757 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1758 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1759 | 0, /* share mode */ |
| 1760 | NULL, /* security attributes */ |
| 1761 | OPEN_EXISTING, |
| 1762 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1763 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1764 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1765 | the symlink path agin and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1766 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1767 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1768 | NULL); |
| 1769 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1770 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1771 | /* Either the target doesn't exist, or we don't have access to |
| 1772 | get a handle to it. If the former, we need to return an error. |
| 1773 | If the latter, we can use attributes_from_dir. */ |
| 1774 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1775 | return -1; |
| 1776 | /* Could not get attributes on open file. Fall back to |
| 1777 | reading the directory. */ |
| 1778 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1779 | /* Very strange. This should not fail now */ |
| 1780 | return -1; |
| 1781 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1782 | if (traverse) { |
| 1783 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1784 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1785 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1786 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1787 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1788 | } else { |
| 1789 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1790 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1791 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1792 | } |
| 1793 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1794 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1795 | return -1; |
| 1796 | |
| 1797 | /* Close the outer open file handle now that we're about to |
| 1798 | reopen it with different flags. */ |
| 1799 | if (!CloseHandle(hFile)) |
| 1800 | return -1; |
| 1801 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1802 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1803 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1804 | the file without the reparse handling flag set. */ |
| 1805 | hFile2 = CreateFileW( |
| 1806 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1807 | NULL, OPEN_EXISTING, |
| 1808 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1809 | NULL); |
| 1810 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1811 | return -1; |
| 1812 | |
| 1813 | if (!get_target_path(hFile2, &target_path)) |
| 1814 | return -1; |
| 1815 | |
| 1816 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1817 | PyMem_Free(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1818 | return code; |
| 1819 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1820 | } else |
| 1821 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1822 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1823 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1824 | |
| 1825 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1826 | dot = wcsrchr(path, '.'); |
| 1827 | if (dot) { |
| 1828 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1829 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1830 | result->st_mode |= 0111; |
| 1831 | } |
| 1832 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1836 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1837 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1838 | /* Protocol violation: we explicitly clear errno, instead of |
| 1839 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1840 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1841 | errno = 0; |
| 1842 | return code; |
| 1843 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1844 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1845 | static int |
| 1846 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1847 | { |
| 1848 | /* Protocol violation: we explicitly clear errno, instead of |
| 1849 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1850 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1851 | errno = 0; |
| 1852 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1853 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1854 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1855 | |
| 1856 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1857 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1858 | default does not traverse symlinks and instead returns attributes for |
| 1859 | the symlink. |
| 1860 | |
| 1861 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1862 | win32_stat will first explicitly resolve the symlink target and then will |
| 1863 | call win32_lstat on that result. |
| 1864 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1865 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1866 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1867 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1868 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1869 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1870 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1873 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1874 | 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] | 1875 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1876 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | static int |
| 1880 | win32_stat(const char* path, struct win32_stat *result) |
| 1881 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1882 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1885 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1886 | win32_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1887 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1888 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | static int |
| 1892 | win32_fstat(int file_number, struct win32_stat *result) |
| 1893 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1894 | BY_HANDLE_FILE_INFORMATION info; |
| 1895 | HANDLE h; |
| 1896 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1897 | |
Richard Oudkerk | 2240ac1 | 2012-07-06 12:05:32 +0100 | [diff] [blame] | 1898 | if (!_PyVerify_fd(file_number)) |
| 1899 | h = INVALID_HANDLE_VALUE; |
| 1900 | else |
| 1901 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1902 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1903 | /* Protocol violation: we explicitly clear errno, instead of |
| 1904 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1905 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1906 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1907 | if (h == INVALID_HANDLE_VALUE) { |
| 1908 | /* This is really a C library error (invalid file handle). |
| 1909 | We set the Win32 error to the closes one matching. */ |
| 1910 | SetLastError(ERROR_INVALID_HANDLE); |
| 1911 | return -1; |
| 1912 | } |
| 1913 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1914 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1915 | type = GetFileType(h); |
| 1916 | if (type == FILE_TYPE_UNKNOWN) { |
| 1917 | DWORD error = GetLastError(); |
| 1918 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1919 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1920 | } |
| 1921 | /* else: valid but unknown file */ |
| 1922 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1923 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1924 | if (type != FILE_TYPE_DISK) { |
| 1925 | if (type == FILE_TYPE_CHAR) |
| 1926 | result->st_mode = _S_IFCHR; |
| 1927 | else if (type == FILE_TYPE_PIPE) |
| 1928 | result->st_mode = _S_IFIFO; |
| 1929 | return 0; |
| 1930 | } |
| 1931 | |
| 1932 | if (!GetFileInformationByHandle(h, &info)) { |
| 1933 | return -1; |
| 1934 | } |
| 1935 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1936 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1937 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1938 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1939 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | #endif /* MS_WINDOWS */ |
| 1943 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1944 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1945 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1946 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1947 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1948 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1949 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1950 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1951 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1952 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1953 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1954 | |
| 1955 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1956 | {"st_mode", "protection bits"}, |
| 1957 | {"st_ino", "inode"}, |
| 1958 | {"st_dev", "device"}, |
| 1959 | {"st_nlink", "number of hard links"}, |
| 1960 | {"st_uid", "user ID of owner"}, |
| 1961 | {"st_gid", "group ID of owner"}, |
| 1962 | {"st_size", "total size, in bytes"}, |
| 1963 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1964 | {NULL, "integer time of last access"}, |
| 1965 | {NULL, "integer time of last modification"}, |
| 1966 | {NULL, "integer time of last change"}, |
| 1967 | {"st_atime", "time of last access"}, |
| 1968 | {"st_mtime", "time of last modification"}, |
| 1969 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1970 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1971 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1972 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1973 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1974 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1975 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1976 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1977 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1978 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1979 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1980 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1981 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1982 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1983 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1984 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1985 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1986 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1987 | #endif |
| 1988 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1989 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1990 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1991 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1992 | }; |
| 1993 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1994 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1995 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1996 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1997 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1998 | #endif |
| 1999 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2000 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2001 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 2002 | #else |
| 2003 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 2004 | #endif |
| 2005 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2006 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2007 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 2008 | #else |
| 2009 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 2010 | #endif |
| 2011 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2012 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 2013 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 2014 | #else |
| 2015 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 2016 | #endif |
| 2017 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2018 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 2019 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2020 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 2021 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2022 | #endif |
| 2023 | |
| 2024 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 2025 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 2026 | #else |
| 2027 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 2028 | #endif |
| 2029 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2030 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2031 | "stat_result", /* name */ |
| 2032 | stat_result__doc__, /* doc */ |
| 2033 | stat_result_fields, |
| 2034 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2035 | }; |
| 2036 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2037 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2038 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 2039 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2040 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 2041 | 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] | 2042 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2043 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2044 | |
| 2045 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2046 | {"f_bsize", }, |
| 2047 | {"f_frsize", }, |
| 2048 | {"f_blocks", }, |
| 2049 | {"f_bfree", }, |
| 2050 | {"f_bavail", }, |
| 2051 | {"f_files", }, |
| 2052 | {"f_ffree", }, |
| 2053 | {"f_favail", }, |
| 2054 | {"f_flag", }, |
| 2055 | {"f_namemax",}, |
| 2056 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2057 | }; |
| 2058 | |
| 2059 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2060 | "statvfs_result", /* name */ |
| 2061 | statvfs_result__doc__, /* doc */ |
| 2062 | statvfs_result_fields, |
| 2063 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2064 | }; |
| 2065 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2066 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 2067 | PyDoc_STRVAR(waitid_result__doc__, |
| 2068 | "waitid_result: Result from waitid.\n\n\ |
| 2069 | This object may be accessed either as a tuple of\n\ |
| 2070 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 2071 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 2072 | \n\ |
| 2073 | See os.waitid for more information."); |
| 2074 | |
| 2075 | static PyStructSequence_Field waitid_result_fields[] = { |
| 2076 | {"si_pid", }, |
| 2077 | {"si_uid", }, |
| 2078 | {"si_signo", }, |
| 2079 | {"si_status", }, |
| 2080 | {"si_code", }, |
| 2081 | {0} |
| 2082 | }; |
| 2083 | |
| 2084 | static PyStructSequence_Desc waitid_result_desc = { |
| 2085 | "waitid_result", /* name */ |
| 2086 | waitid_result__doc__, /* doc */ |
| 2087 | waitid_result_fields, |
| 2088 | 5 |
| 2089 | }; |
| 2090 | static PyTypeObject WaitidResultType; |
| 2091 | #endif |
| 2092 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2093 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2094 | static PyTypeObject StatResultType; |
| 2095 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2096 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 2097 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2098 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2099 | static newfunc structseq_new; |
| 2100 | |
| 2101 | static PyObject * |
| 2102 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2103 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2104 | PyStructSequence *result; |
| 2105 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2106 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2107 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 2108 | if (!result) |
| 2109 | return NULL; |
| 2110 | /* If we have been initialized from a tuple, |
| 2111 | st_?time might be set to None. Initialize it |
| 2112 | from the int slots. */ |
| 2113 | for (i = 7; i <= 9; i++) { |
| 2114 | if (result->ob_item[i+3] == Py_None) { |
| 2115 | Py_DECREF(Py_None); |
| 2116 | Py_INCREF(result->ob_item[i]); |
| 2117 | result->ob_item[i+3] = result->ob_item[i]; |
| 2118 | } |
| 2119 | } |
| 2120 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | |
| 2124 | |
| 2125 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 2126 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2127 | |
| 2128 | PyDoc_STRVAR(stat_float_times__doc__, |
| 2129 | "stat_float_times([newval]) -> oldval\n\n\ |
| 2130 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 2131 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 2132 | future calls return ints. \n\ |
| 2133 | If newval is omitted, return the current setting.\n"); |
| 2134 | |
| 2135 | static PyObject* |
| 2136 | stat_float_times(PyObject* self, PyObject *args) |
| 2137 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2138 | int newval = -1; |
| 2139 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 2140 | return NULL; |
Victor Stinner | 034d0aa | 2012-06-05 01:22:15 +0200 | [diff] [blame] | 2141 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2142 | "stat_float_times() is deprecated", |
| 2143 | 1)) |
| 2144 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2145 | if (newval == -1) |
| 2146 | /* Return old value */ |
| 2147 | return PyBool_FromLong(_stat_float_times); |
| 2148 | _stat_float_times = newval; |
| 2149 | Py_INCREF(Py_None); |
| 2150 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2151 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2152 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2153 | static PyObject *billion = NULL; |
| 2154 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2155 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2156 | 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] | 2157 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2158 | PyObject *s = _PyLong_FromTime_t(sec); |
| 2159 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 2160 | PyObject *s_in_ns = NULL; |
| 2161 | PyObject *ns_total = NULL; |
| 2162 | PyObject *float_s = NULL; |
| 2163 | |
| 2164 | if (!(s && ns_fractional)) |
| 2165 | goto exit; |
| 2166 | |
| 2167 | s_in_ns = PyNumber_Multiply(s, billion); |
| 2168 | if (!s_in_ns) |
| 2169 | goto exit; |
| 2170 | |
| 2171 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 2172 | if (!ns_total) |
| 2173 | goto exit; |
| 2174 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2175 | if (_stat_float_times) { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2176 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2177 | if (!float_s) |
| 2178 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2179 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2180 | else { |
| 2181 | float_s = s; |
| 2182 | Py_INCREF(float_s); |
| 2183 | } |
| 2184 | |
| 2185 | PyStructSequence_SET_ITEM(v, index, s); |
| 2186 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2187 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2188 | s = NULL; |
| 2189 | float_s = NULL; |
| 2190 | ns_total = NULL; |
| 2191 | exit: |
| 2192 | Py_XDECREF(s); |
| 2193 | Py_XDECREF(ns_fractional); |
| 2194 | Py_XDECREF(s_in_ns); |
| 2195 | Py_XDECREF(ns_total); |
| 2196 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2199 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2200 | (used by posix_stat() and posix_fstat()) */ |
| 2201 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2202 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2203 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2204 | unsigned long ansec, mnsec, cnsec; |
| 2205 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 2206 | if (v == NULL) |
| 2207 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2208 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2209 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2210 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2211 | PyStructSequence_SET_ITEM(v, 1, |
| 2212 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2213 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2214 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2215 | #endif |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2216 | #ifdef MS_WINDOWS |
| 2217 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
| 2218 | #elif defined(HAVE_LONG_LONG) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2219 | PyStructSequence_SET_ITEM(v, 2, |
| 2220 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2221 | #else |
Brian Curtin | 9cc4321 | 2013-01-01 12:31:06 -0600 | [diff] [blame] | 2222 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2223 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2224 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2225 | #if defined(MS_WINDOWS) |
| 2226 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2227 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2228 | #else |
| 2229 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2230 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2231 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2232 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2233 | PyStructSequence_SET_ITEM(v, 6, |
| 2234 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2235 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2236 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2237 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2238 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2239 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2240 | ansec = st->st_atim.tv_nsec; |
| 2241 | mnsec = st->st_mtim.tv_nsec; |
| 2242 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2243 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2244 | ansec = st->st_atimespec.tv_nsec; |
| 2245 | mnsec = st->st_mtimespec.tv_nsec; |
| 2246 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2247 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2248 | ansec = st->st_atime_nsec; |
| 2249 | mnsec = st->st_mtime_nsec; |
| 2250 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2251 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2252 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2253 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2254 | fill_time(v, 7, st->st_atime, ansec); |
| 2255 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2256 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2257 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2258 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2259 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2260 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2261 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2262 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2263 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2264 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2265 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2266 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2267 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2268 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2269 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2270 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2271 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2272 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2273 | #endif |
| 2274 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2275 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2276 | PyObject *val; |
| 2277 | unsigned long bsec,bnsec; |
| 2278 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2279 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2280 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2281 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2282 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2283 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2284 | if (_stat_float_times) { |
| 2285 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 2286 | } else { |
| 2287 | val = PyLong_FromLong((long)bsec); |
| 2288 | } |
| 2289 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2290 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2291 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2292 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2293 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2294 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2295 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2296 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2297 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2298 | if (PyErr_Occurred()) { |
| 2299 | Py_DECREF(v); |
| 2300 | return NULL; |
| 2301 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2302 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2303 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2306 | /* POSIX methods */ |
| 2307 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2308 | |
| 2309 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2310 | posix_do_stat(char *function_name, path_t *path, |
| 2311 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2312 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2313 | STRUCT_STAT st; |
| 2314 | int result; |
| 2315 | |
| 2316 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2317 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2318 | return NULL; |
| 2319 | #endif |
| 2320 | |
| 2321 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2322 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2323 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2324 | return NULL; |
| 2325 | |
| 2326 | Py_BEGIN_ALLOW_THREADS |
| 2327 | if (path->fd != -1) |
| 2328 | result = FSTAT(path->fd, &st); |
| 2329 | else |
| 2330 | #ifdef MS_WINDOWS |
| 2331 | if (path->wide) { |
| 2332 | if (follow_symlinks) |
| 2333 | result = win32_stat_w(path->wide, &st); |
| 2334 | else |
| 2335 | result = win32_lstat_w(path->wide, &st); |
| 2336 | } |
| 2337 | else |
| 2338 | #endif |
| 2339 | #if defined(HAVE_LSTAT) || defined(MS_WINDOWS) |
| 2340 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2341 | result = LSTAT(path->narrow, &st); |
| 2342 | else |
| 2343 | #endif |
| 2344 | #ifdef HAVE_FSTATAT |
| 2345 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2346 | result = fstatat(dir_fd, path->narrow, &st, |
| 2347 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2348 | else |
| 2349 | #endif |
| 2350 | result = STAT(path->narrow, &st); |
| 2351 | Py_END_ALLOW_THREADS |
| 2352 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2353 | if (result != 0) { |
| 2354 | return path_error(path); |
| 2355 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2356 | |
| 2357 | return _pystat_fromstructstat(&st); |
| 2358 | } |
| 2359 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2360 | #ifdef HAVE_FSTATAT |
| 2361 | #define OS_STAT_DIR_FD_CONVERTER dir_fd_converter |
| 2362 | #else |
| 2363 | #define OS_STAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2364 | #endif |
| 2365 | |
| 2366 | |
| 2367 | /*[python] |
| 2368 | |
| 2369 | class path_t_converter(CConverter): |
| 2370 | |
| 2371 | type = "path_t" |
| 2372 | impl_by_reference = True |
| 2373 | parse_by_reference = True |
| 2374 | |
| 2375 | converter = 'path_converter' |
| 2376 | |
| 2377 | def converter_init(self, *, allow_fd=False, nullable=False): |
| 2378 | def strify(value): |
| 2379 | return str(int(bool(value))) |
| 2380 | |
| 2381 | # right now path_t doesn't support default values. |
| 2382 | # to support a default value, you'll need to override initialize(). |
| 2383 | |
| 2384 | assert self.default is unspecified |
| 2385 | |
| 2386 | self.nullable = nullable |
| 2387 | self.allow_fd = allow_fd |
| 2388 | |
| 2389 | self.c_default = 'PATH_T_INITIALIZE("{}", {}, {})'.format( |
| 2390 | self.function.name, |
| 2391 | strify(nullable), |
| 2392 | strify(allow_fd), |
| 2393 | ) |
| 2394 | |
| 2395 | def cleanup(self): |
| 2396 | return "path_cleanup(&" + self.name + ");\n" |
| 2397 | |
| 2398 | |
| 2399 | class dir_fd_converter(CConverter): |
| 2400 | type = 'int' |
| 2401 | converter = 'OS_STAT_DIR_FD_CONVERTER' |
| 2402 | |
| 2403 | def converter_init(self): |
| 2404 | if self.default in (unspecified, None): |
| 2405 | self.c_default = 'DEFAULT_DIR_FD' |
| 2406 | |
| 2407 | |
| 2408 | [python]*/ |
| 2409 | /*[python checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ |
| 2410 | |
| 2411 | /*[clinic] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2412 | |
| 2413 | os.stat -> object(doc_default='stat_result') |
| 2414 | |
| 2415 | path : path_t(allow_fd=True) |
| 2416 | Path to be examined; can be string, bytes, or open-file-descriptor int. |
| 2417 | |
| 2418 | * |
| 2419 | |
| 2420 | dir_fd : dir_fd = None |
| 2421 | If not None, it should be a file descriptor open to a directory, |
| 2422 | and path should be a relative string; path will then be relative to |
| 2423 | that directory. |
| 2424 | |
| 2425 | follow_symlinks: bool = True |
| 2426 | If False, and the last element of the path is a symbolic link, |
| 2427 | stat will examine the symbolic link itself instead of the file |
| 2428 | the link points to. |
| 2429 | |
| 2430 | Perform a stat system call on the given path. |
| 2431 | |
| 2432 | dir_fd and follow_symlinks may not be implemented |
| 2433 | on your platform. If they are unavailable, using them will raise a |
| 2434 | NotImplementedError. |
| 2435 | |
| 2436 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2437 | an open file descriptor. |
| 2438 | |
| 2439 | [clinic]*/ |
| 2440 | |
| 2441 | PyDoc_STRVAR(os_stat__doc__, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 2442 | "stat(path, *, dir_fd=None, follow_symlinks=True)\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2443 | "Perform a stat system call on the given path.\n" |
| 2444 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2445 | " path\n" |
| 2446 | " Path to be examined; can be string, bytes, or open-file-descriptor int.\n" |
| 2447 | " dir_fd\n" |
| 2448 | " If not None, it should be a file descriptor open to a directory,\n" |
| 2449 | " and path should be a relative string; path will then be relative to\n" |
| 2450 | " that directory.\n" |
| 2451 | " follow_symlinks\n" |
| 2452 | " If False, and the last element of the path is a symbolic link,\n" |
| 2453 | " stat will examine the symbolic link itself instead of the file\n" |
| 2454 | " the link points to.\n" |
| 2455 | "\n" |
| 2456 | "dir_fd and follow_symlinks may not be implemented\n" |
| 2457 | " on your platform. If they are unavailable, using them will raise a\n" |
| 2458 | " NotImplementedError.\n" |
| 2459 | "\n" |
| 2460 | "It\'s an error to use dir_fd or follow_symlinks when specifying path as\n" |
| 2461 | " an open file descriptor."); |
| 2462 | |
| 2463 | #define OS_STAT_METHODDEF \ |
| 2464 | {"stat", (PyCFunction)os_stat, METH_VARARGS|METH_KEYWORDS, os_stat__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2465 | |
| 2466 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2467 | 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] | 2468 | |
| 2469 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2470 | os_stat(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2471 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2472 | PyObject *return_value = NULL; |
| 2473 | static char *_keywords[] = {"path", "dir_fd", "follow_symlinks", NULL}; |
| 2474 | path_t path = PATH_T_INITIALIZE("stat", 0, 1); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2475 | int dir_fd = DEFAULT_DIR_FD; |
| 2476 | int follow_symlinks = 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2477 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2478 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2479 | "O&|$O&p:stat", _keywords, |
| 2480 | path_converter, &path, OS_STAT_DIR_FD_CONVERTER, &dir_fd, &follow_symlinks)) |
| 2481 | goto exit; |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 2482 | return_value = os_stat_impl(module, &path, dir_fd, follow_symlinks); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2483 | |
| 2484 | exit: |
| 2485 | /* Cleanup for path */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2486 | path_cleanup(&path); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2487 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2488 | return return_value; |
| 2489 | } |
| 2490 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2491 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2492 | os_stat_impl(PyModuleDef *module, path_t *path, int dir_fd, int follow_symlinks) |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 2493 | /*[clinic checksum: 85a71ad602e89f8e280118da976f70cd2f9abdf1]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2494 | { |
| 2495 | return posix_do_stat("stat", path, dir_fd, follow_symlinks); |
| 2496 | } |
| 2497 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2498 | PyDoc_STRVAR(posix_lstat__doc__, |
| 2499 | "lstat(path, *, dir_fd=None) -> stat result\n\n\ |
| 2500 | Like stat(), but do not follow symbolic links.\n\ |
| 2501 | Equivalent to stat(path, follow_symlinks=False)."); |
| 2502 | |
| 2503 | static PyObject * |
| 2504 | posix_lstat(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2505 | { |
| 2506 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 2507 | path_t path; |
| 2508 | int dir_fd = DEFAULT_DIR_FD; |
| 2509 | int follow_symlinks = 0; |
| 2510 | PyObject *return_value; |
| 2511 | |
| 2512 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2513 | path.function_name = "lstat"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2514 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:lstat", keywords, |
| 2515 | path_converter, &path, |
| 2516 | #ifdef HAVE_FSTATAT |
| 2517 | dir_fd_converter, &dir_fd |
| 2518 | #else |
| 2519 | dir_fd_unavailable, &dir_fd |
| 2520 | #endif |
| 2521 | )) |
| 2522 | return NULL; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2523 | return_value = posix_do_stat("lstat", &path, dir_fd, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2524 | path_cleanup(&path); |
| 2525 | return return_value; |
| 2526 | } |
| 2527 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2528 | |
| 2529 | #ifdef HAVE_FACCESSAT |
| 2530 | #define OS_ACCESS_DIR_FD_CONVERTER dir_fd_converter |
| 2531 | #else |
| 2532 | #define OS_ACCESS_DIR_FD_CONVERTER dir_fd_unavailable |
| 2533 | #endif |
| 2534 | /*[clinic] |
| 2535 | os.access -> object(doc_default='True if granted, False otherwise') |
| 2536 | |
| 2537 | path: path_t(allow_fd=True) |
| 2538 | Path to be tested; can be string, bytes, or open-file-descriptor int. |
| 2539 | |
| 2540 | mode: int |
| 2541 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2542 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2543 | |
| 2544 | * |
| 2545 | |
| 2546 | dir_fd : dir_fd = None |
| 2547 | If not None, it should be a file descriptor open to a directory, |
| 2548 | and path should be relative; path will then be relative to that |
| 2549 | directory. |
| 2550 | |
| 2551 | effective_ids: bool = False |
| 2552 | If True, access will use the effective uid/gid instead of |
| 2553 | the real uid/gid. |
| 2554 | |
| 2555 | follow_symlinks: bool = True |
| 2556 | If False, and the last element of the path is a symbolic link, |
| 2557 | access will examine the symbolic link itself instead of the file |
| 2558 | the link points to. |
| 2559 | |
| 2560 | Use the real uid/gid to test for access to a path. |
| 2561 | |
| 2562 | {parameters} |
| 2563 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2564 | on your platform. If they are unavailable, using them will raise a |
| 2565 | NotImplementedError. |
| 2566 | |
| 2567 | Note that most operations will use the effective uid/gid, therefore this |
| 2568 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2569 | has the specified access to the path. |
| 2570 | |
| 2571 | [clinic]*/ |
| 2572 | |
| 2573 | PyDoc_STRVAR(os_access__doc__, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 2574 | "access(path, mode, *, dir_fd=None, effective_ids=False, follow_symlinks=True)\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2575 | "Use the real uid/gid to test for access to a path.\n" |
| 2576 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2577 | " path\n" |
| 2578 | " Path to be tested; can be string, bytes, or open-file-descriptor int.\n" |
| 2579 | " mode\n" |
| 2580 | " Operating-system mode bitfield. Can be F_OK to test existence,\n" |
| 2581 | " or the inclusive-OR of R_OK, W_OK, and X_OK.\n" |
| 2582 | " dir_fd\n" |
| 2583 | " If not None, it should be a file descriptor open to a directory,\n" |
| 2584 | " and path should be relative; path will then be relative to that\n" |
| 2585 | " directory.\n" |
| 2586 | " effective_ids\n" |
| 2587 | " If True, access will use the effective uid/gid instead of\n" |
| 2588 | " the real uid/gid.\n" |
| 2589 | " follow_symlinks\n" |
| 2590 | " If False, and the last element of the path is a symbolic link,\n" |
| 2591 | " access will examine the symbolic link itself instead of the file\n" |
| 2592 | " the link points to.\n" |
| 2593 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2594 | "dir_fd, effective_ids, and follow_symlinks may not be implemented\n" |
| 2595 | " on your platform. If they are unavailable, using them will raise a\n" |
| 2596 | " NotImplementedError.\n" |
| 2597 | "\n" |
| 2598 | "Note that most operations will use the effective uid/gid, therefore this\n" |
| 2599 | " routine can be used in a suid/sgid environment to test if the invoking user\n" |
| 2600 | " has the specified access to the path."); |
| 2601 | |
| 2602 | #define OS_ACCESS_METHODDEF \ |
| 2603 | {"access", (PyCFunction)os_access, METH_VARARGS|METH_KEYWORDS, os_access__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2604 | |
| 2605 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2606 | 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] | 2607 | |
| 2608 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2609 | os_access(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2610 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2611 | PyObject *return_value = NULL; |
| 2612 | static char *_keywords[] = {"path", "mode", "dir_fd", "effective_ids", "follow_symlinks", NULL}; |
| 2613 | path_t path = PATH_T_INITIALIZE("access", 0, 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2614 | int mode; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2615 | int dir_fd = DEFAULT_DIR_FD; |
| 2616 | int effective_ids = 0; |
| 2617 | int follow_symlinks = 1; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2618 | |
| 2619 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2620 | "O&i|$O&pp:access", _keywords, |
| 2621 | path_converter, &path, &mode, OS_STAT_DIR_FD_CONVERTER, &dir_fd, &effective_ids, &follow_symlinks)) |
| 2622 | goto exit; |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 2623 | 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] | 2624 | |
| 2625 | exit: |
| 2626 | /* Cleanup for path */ |
| 2627 | path_cleanup(&path); |
| 2628 | |
| 2629 | return return_value; |
| 2630 | } |
| 2631 | |
| 2632 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2633 | os_access_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, int effective_ids, int follow_symlinks) |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 2634 | /*[clinic checksum: 636e835c36562a2fc11acab75314634127fdf769]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2635 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2636 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2637 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2638 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2639 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2640 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2641 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2642 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2643 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2644 | #ifndef HAVE_FACCESSAT |
| 2645 | if (follow_symlinks_specified("access", follow_symlinks)) |
| 2646 | goto exit; |
| 2647 | |
| 2648 | if (effective_ids) { |
| 2649 | argument_unavailable_error("access", "effective_ids"); |
| 2650 | goto exit; |
| 2651 | } |
| 2652 | #endif |
| 2653 | |
| 2654 | #ifdef MS_WINDOWS |
| 2655 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | ebe83f9 | 2013-10-19 22:36:17 +0200 | [diff] [blame] | 2656 | if (path->wide != NULL) |
| 2657 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2658 | else |
Christian Heimes | ebe83f9 | 2013-10-19 22:36:17 +0200 | [diff] [blame] | 2659 | attr = GetFileAttributesA(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2660 | Py_END_ALLOW_THREADS |
| 2661 | |
| 2662 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2663 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2664 | * * we didn't get a -1, and |
| 2665 | * * write access wasn't requested, |
| 2666 | * * or the file isn't read-only, |
| 2667 | * * or it's a directory. |
| 2668 | * (Directories cannot be read-only on Windows.) |
| 2669 | */ |
| 2670 | return_value = PyBool_FromLong( |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 2671 | (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2672 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2673 | !(attr & FILE_ATTRIBUTE_READONLY) || |
| 2674 | (attr & FILE_ATTRIBUTE_DIRECTORY))); |
| 2675 | #else |
| 2676 | |
| 2677 | Py_BEGIN_ALLOW_THREADS |
| 2678 | #ifdef HAVE_FACCESSAT |
| 2679 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2680 | effective_ids || |
| 2681 | !follow_symlinks) { |
| 2682 | int flags = 0; |
| 2683 | if (!follow_symlinks) |
| 2684 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2685 | if (effective_ids) |
| 2686 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2687 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2688 | } |
| 2689 | else |
| 2690 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2691 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2692 | Py_END_ALLOW_THREADS |
| 2693 | return_value = PyBool_FromLong(!result); |
| 2694 | #endif |
| 2695 | |
| 2696 | #ifndef HAVE_FACCESSAT |
| 2697 | exit: |
| 2698 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2699 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2702 | #ifndef F_OK |
| 2703 | #define F_OK 0 |
| 2704 | #endif |
| 2705 | #ifndef R_OK |
| 2706 | #define R_OK 4 |
| 2707 | #endif |
| 2708 | #ifndef W_OK |
| 2709 | #define W_OK 2 |
| 2710 | #endif |
| 2711 | #ifndef X_OK |
| 2712 | #define X_OK 1 |
| 2713 | #endif |
| 2714 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2715 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2716 | #ifdef HAVE_TTYNAME |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2717 | |
| 2718 | /*[clinic] |
| 2719 | os.ttyname -> DecodeFSDefault |
| 2720 | |
| 2721 | fd: int |
| 2722 | Integer file descriptor handle. |
| 2723 | |
| 2724 | / |
| 2725 | |
| 2726 | Return the name of the terminal device connected to 'fd'. |
| 2727 | [clinic]*/ |
| 2728 | |
| 2729 | PyDoc_STRVAR(os_ttyname__doc__, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 2730 | "ttyname(fd)\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2731 | "Return the name of the terminal device connected to \'fd\'.\n" |
| 2732 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2733 | " fd\n" |
| 2734 | " Integer file descriptor handle."); |
| 2735 | |
| 2736 | #define OS_TTYNAME_METHODDEF \ |
| 2737 | {"ttyname", (PyCFunction)os_ttyname, METH_VARARGS, os_ttyname__doc__}, |
| 2738 | |
| 2739 | static char * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2740 | os_ttyname_impl(PyModuleDef *module, int fd); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2741 | |
| 2742 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2743 | os_ttyname(PyModuleDef *module, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2744 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2745 | PyObject *return_value = NULL; |
| 2746 | int fd; |
| 2747 | char *_return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2748 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2749 | if (!PyArg_ParseTuple(args, |
| 2750 | "i:ttyname", |
| 2751 | &fd)) |
| 2752 | goto exit; |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 2753 | _return_value = os_ttyname_impl(module, fd); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2754 | if (_return_value == NULL) |
| 2755 | goto exit; |
| 2756 | return_value = PyUnicode_DecodeFSDefault(_return_value); |
| 2757 | |
| 2758 | exit: |
| 2759 | return return_value; |
| 2760 | } |
| 2761 | |
| 2762 | static char * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2763 | os_ttyname_impl(PyModuleDef *module, int fd) |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 2764 | /*[clinic checksum: 0f368134dc0a7f21f25185e2e6bacf7675fb473a]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2765 | { |
| 2766 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2767 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2768 | #if defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2769 | /* file descriptor 0 only, the default input device (stdin) */ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2770 | if (fd == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2771 | ret = ttyname(); |
| 2772 | } |
| 2773 | else { |
| 2774 | ret = NULL; |
| 2775 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2776 | #else |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2777 | ret = ttyname(fd); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2778 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2779 | if (ret == NULL) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2780 | posix_error(); |
| 2781 | return ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2782 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2783 | #else |
| 2784 | #define OS_TTYNAME_METHODDEF |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2785 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2786 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2787 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2788 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2789 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2790 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2791 | |
| 2792 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2793 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2794 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2795 | char *ret; |
| 2796 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2797 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2798 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2799 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2800 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2801 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2802 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2803 | if (ret == NULL) |
| 2804 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2805 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2806 | } |
| 2807 | #endif |
| 2808 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2809 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2810 | "chdir(path)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2811 | Change the current working directory to the specified path.\n\ |
| 2812 | \n\ |
| 2813 | path may always be specified as a string.\n\ |
| 2814 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2815 | If this functionality is unavailable, using it raises an exception."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2816 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2817 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2818 | posix_chdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2819 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2820 | path_t path; |
| 2821 | int result; |
| 2822 | PyObject *return_value = NULL; |
| 2823 | static char *keywords[] = {"path", NULL}; |
| 2824 | |
| 2825 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2826 | path.function_name = "chdir"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2827 | #ifdef HAVE_FCHDIR |
| 2828 | path.allow_fd = 1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2829 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2830 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:chdir", keywords, |
| 2831 | path_converter, &path |
| 2832 | )) |
| 2833 | return NULL; |
| 2834 | |
| 2835 | Py_BEGIN_ALLOW_THREADS |
| 2836 | #ifdef MS_WINDOWS |
| 2837 | if (path.wide) |
| 2838 | result = win32_wchdir(path.wide); |
| 2839 | else |
| 2840 | result = win32_chdir(path.narrow); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 2841 | result = !result; /* on unix, success = 0, on windows, success = !0 */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2842 | #else |
| 2843 | #ifdef HAVE_FCHDIR |
| 2844 | if (path.fd != -1) |
| 2845 | result = fchdir(path.fd); |
| 2846 | else |
| 2847 | #endif |
| 2848 | result = chdir(path.narrow); |
| 2849 | #endif |
| 2850 | Py_END_ALLOW_THREADS |
| 2851 | |
| 2852 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2853 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2854 | goto exit; |
| 2855 | } |
| 2856 | |
| 2857 | return_value = Py_None; |
| 2858 | Py_INCREF(Py_None); |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2859 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2860 | exit: |
| 2861 | path_cleanup(&path); |
| 2862 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2865 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2866 | PyDoc_STRVAR(posix_fchdir__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2867 | "fchdir(fd)\n\n\ |
| 2868 | Change to the directory of the given file descriptor. fd must be\n\ |
| 2869 | opened on a directory, not a file. Equivalent to os.chdir(fd)."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2870 | |
| 2871 | static PyObject * |
| 2872 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 2873 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2874 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2875 | } |
| 2876 | #endif /* HAVE_FCHDIR */ |
| 2877 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2878 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2879 | PyDoc_STRVAR(posix_chmod__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2880 | "chmod(path, mode, *, dir_fd=None, follow_symlinks=True)\n\n\ |
| 2881 | Change the access permissions of a file.\n\ |
| 2882 | \n\ |
| 2883 | path may always be specified as a string.\n\ |
| 2884 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2885 | If this functionality is unavailable, using it raises an exception.\n\ |
| 2886 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2887 | and path should be relative; path will then be relative to that directory.\n\ |
| 2888 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2889 | link, chmod will modify the symbolic link itself instead of the file the\n\ |
| 2890 | link points to.\n\ |
| 2891 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2892 | an open file descriptor.\n\ |
| 2893 | dir_fd and follow_symlinks may not be implemented on your platform.\n\ |
| 2894 | If they are unavailable, using them will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2895 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2896 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2897 | posix_chmod(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2898 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2899 | path_t path; |
| 2900 | int mode; |
| 2901 | int dir_fd = DEFAULT_DIR_FD; |
| 2902 | int follow_symlinks = 1; |
| 2903 | int result; |
| 2904 | PyObject *return_value = NULL; |
| 2905 | static char *keywords[] = {"path", "mode", "dir_fd", |
| 2906 | "follow_symlinks", NULL}; |
| 2907 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2908 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2909 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2910 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2911 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2912 | #ifdef HAVE_FCHMODAT |
| 2913 | int fchmodat_nofollow_unsupported = 0; |
| 2914 | #endif |
| 2915 | |
| 2916 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2917 | path.function_name = "chmod"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2918 | #ifdef HAVE_FCHMOD |
| 2919 | path.allow_fd = 1; |
| 2920 | #endif |
| 2921 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&p:chmod", keywords, |
| 2922 | path_converter, &path, |
| 2923 | &mode, |
| 2924 | #ifdef HAVE_FCHMODAT |
| 2925 | dir_fd_converter, &dir_fd, |
| 2926 | #else |
| 2927 | dir_fd_unavailable, &dir_fd, |
| 2928 | #endif |
| 2929 | &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2930 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2931 | |
| 2932 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2933 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
| 2934 | goto exit; |
| 2935 | #endif |
| 2936 | |
| 2937 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2938 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2939 | if (path.wide) |
| 2940 | attr = GetFileAttributesW(path.wide); |
| 2941 | else |
| 2942 | attr = GetFileAttributesA(path.narrow); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 2943 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2944 | result = 0; |
| 2945 | else { |
| 2946 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2947 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2948 | else |
| 2949 | attr |= FILE_ATTRIBUTE_READONLY; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2950 | if (path.wide) |
| 2951 | result = SetFileAttributesW(path.wide, attr); |
| 2952 | else |
| 2953 | result = SetFileAttributesA(path.narrow, attr); |
| 2954 | } |
| 2955 | Py_END_ALLOW_THREADS |
| 2956 | |
| 2957 | if (!result) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 2958 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2959 | goto exit; |
| 2960 | } |
| 2961 | #else /* MS_WINDOWS */ |
| 2962 | Py_BEGIN_ALLOW_THREADS |
| 2963 | #ifdef HAVE_FCHMOD |
| 2964 | if (path.fd != -1) |
| 2965 | result = fchmod(path.fd, mode); |
| 2966 | else |
| 2967 | #endif |
| 2968 | #ifdef HAVE_LCHMOD |
| 2969 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2970 | result = lchmod(path.narrow, mode); |
| 2971 | else |
| 2972 | #endif |
| 2973 | #ifdef HAVE_FCHMODAT |
| 2974 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2975 | /* |
| 2976 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2977 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2978 | * and then says it isn't implemented yet. |
| 2979 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2980 | * |
| 2981 | * Once it is supported, os.chmod will automatically |
| 2982 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2983 | * Until then, we need to be careful what exception we raise. |
| 2984 | */ |
| 2985 | result = fchmodat(dir_fd, path.narrow, mode, |
| 2986 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2987 | /* |
| 2988 | * But wait! We can't throw the exception without allowing threads, |
| 2989 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2990 | */ |
| 2991 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2992 | result && |
| 2993 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2994 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2995 | } |
| 2996 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2997 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2998 | result = chmod(path.narrow, mode); |
| 2999 | Py_END_ALLOW_THREADS |
| 3000 | |
| 3001 | if (result) { |
| 3002 | #ifdef HAVE_FCHMODAT |
| 3003 | if (fchmodat_nofollow_unsupported) { |
| 3004 | if (dir_fd != DEFAULT_DIR_FD) |
| 3005 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 3006 | dir_fd, follow_symlinks); |
| 3007 | else |
| 3008 | follow_symlinks_specified("chmod", follow_symlinks); |
| 3009 | } |
| 3010 | else |
| 3011 | #endif |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3012 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3013 | goto exit; |
| 3014 | } |
| 3015 | #endif |
| 3016 | |
| 3017 | Py_INCREF(Py_None); |
| 3018 | return_value = Py_None; |
| 3019 | exit: |
| 3020 | path_cleanup(&path); |
| 3021 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3024 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3025 | #ifdef HAVE_FCHMOD |
| 3026 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 3027 | "fchmod(fd, mode)\n\n\ |
| 3028 | Change the access permissions of the file given by file\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3029 | descriptor fd. Equivalent to os.chmod(fd, mode)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3030 | |
| 3031 | static PyObject * |
| 3032 | posix_fchmod(PyObject *self, PyObject *args) |
| 3033 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3034 | int fd, mode, res; |
| 3035 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 3036 | return NULL; |
| 3037 | Py_BEGIN_ALLOW_THREADS |
| 3038 | res = fchmod(fd, mode); |
| 3039 | Py_END_ALLOW_THREADS |
| 3040 | if (res < 0) |
| 3041 | return posix_error(); |
| 3042 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3043 | } |
| 3044 | #endif /* HAVE_FCHMOD */ |
| 3045 | |
| 3046 | #ifdef HAVE_LCHMOD |
| 3047 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 3048 | "lchmod(path, mode)\n\n\ |
| 3049 | 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] | 3050 | affects the link itself rather than the target.\n\ |
| 3051 | Equivalent to chmod(path, mode, follow_symlinks=False)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3052 | |
| 3053 | static PyObject * |
| 3054 | posix_lchmod(PyObject *self, PyObject *args) |
| 3055 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3056 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3057 | int i; |
| 3058 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3059 | memset(&path, 0, sizeof(path)); |
| 3060 | path.function_name = "lchmod"; |
| 3061 | if (!PyArg_ParseTuple(args, "O&i:lchmod", |
| 3062 | path_converter, &path, &i)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3063 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3064 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3065 | res = lchmod(path.narrow, i); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3066 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3067 | if (res < 0) { |
| 3068 | path_error(&path); |
| 3069 | path_cleanup(&path); |
| 3070 | return NULL; |
| 3071 | } |
| 3072 | path_cleanup(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3073 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3074 | } |
| 3075 | #endif /* HAVE_LCHMOD */ |
| 3076 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3077 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3078 | #ifdef HAVE_CHFLAGS |
| 3079 | PyDoc_STRVAR(posix_chflags__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3080 | "chflags(path, flags, *, follow_symlinks=True)\n\n\ |
| 3081 | Set file flags.\n\ |
| 3082 | \n\ |
| 3083 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 3084 | link, chflags will change flags on the symbolic link itself instead of the\n\ |
| 3085 | file the link points to.\n\ |
| 3086 | follow_symlinks may not be implemented on your platform. If it is\n\ |
| 3087 | unavailable, using it will raise a NotImplementedError."); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3088 | |
| 3089 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3090 | posix_chflags(PyObject *self, PyObject *args, PyObject *kwargs) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3091 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3092 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3093 | unsigned long flags; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3094 | int follow_symlinks = 1; |
| 3095 | int result; |
Victor Stinner | 45e9039 | 2013-07-18 23:57:35 +0200 | [diff] [blame] | 3096 | PyObject *return_value = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3097 | static char *keywords[] = {"path", "flags", "follow_symlinks", NULL}; |
| 3098 | |
| 3099 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3100 | path.function_name = "chflags"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3101 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&k|$i:chflags", keywords, |
| 3102 | path_converter, &path, |
| 3103 | &flags, &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3104 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3105 | |
| 3106 | #ifndef HAVE_LCHFLAGS |
| 3107 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
| 3108 | goto exit; |
| 3109 | #endif |
| 3110 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3111 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3112 | #ifdef HAVE_LCHFLAGS |
| 3113 | if (!follow_symlinks) |
| 3114 | result = lchflags(path.narrow, flags); |
| 3115 | else |
| 3116 | #endif |
| 3117 | result = chflags(path.narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3118 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3119 | |
| 3120 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3121 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3122 | goto exit; |
| 3123 | } |
| 3124 | |
| 3125 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3126 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3127 | |
| 3128 | exit: |
| 3129 | path_cleanup(&path); |
| 3130 | return return_value; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3131 | } |
| 3132 | #endif /* HAVE_CHFLAGS */ |
| 3133 | |
| 3134 | #ifdef HAVE_LCHFLAGS |
| 3135 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 3136 | "lchflags(path, flags)\n\n\ |
| 3137 | Set file flags.\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3138 | This function will not follow symbolic links.\n\ |
| 3139 | Equivalent to chflags(path, flags, follow_symlinks=False)."); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3140 | |
| 3141 | static PyObject * |
| 3142 | posix_lchflags(PyObject *self, PyObject *args) |
| 3143 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3144 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3145 | unsigned long flags; |
| 3146 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3147 | memset(&path, 0, sizeof(path)); |
| 3148 | path.function_name = "lchflags"; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3149 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3150 | path_converter, &path, &flags)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3151 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3152 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3153 | res = lchflags(path.narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3154 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3155 | if (res < 0) { |
| 3156 | path_error(&path); |
| 3157 | path_cleanup(&path); |
| 3158 | return NULL; |
| 3159 | } |
| 3160 | path_cleanup(&path); |
| 3161 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3162 | } |
| 3163 | #endif /* HAVE_LCHFLAGS */ |
| 3164 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3165 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3166 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3167 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3168 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3169 | |
| 3170 | static PyObject * |
| 3171 | posix_chroot(PyObject *self, PyObject *args) |
| 3172 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3173 | return posix_1str("chroot", args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3174 | } |
| 3175 | #endif |
| 3176 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3177 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3178 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3179 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3180 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3181 | |
| 3182 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 3183 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3184 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 3185 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3186 | } |
| 3187 | #endif /* HAVE_FSYNC */ |
| 3188 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3189 | #ifdef HAVE_SYNC |
| 3190 | PyDoc_STRVAR(posix_sync__doc__, |
| 3191 | "sync()\n\n\ |
| 3192 | Force write of everything to disk."); |
| 3193 | |
| 3194 | static PyObject * |
| 3195 | posix_sync(PyObject *self, PyObject *noargs) |
| 3196 | { |
| 3197 | Py_BEGIN_ALLOW_THREADS |
| 3198 | sync(); |
| 3199 | Py_END_ALLOW_THREADS |
| 3200 | Py_RETURN_NONE; |
| 3201 | } |
| 3202 | #endif |
| 3203 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3204 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3205 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3206 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3207 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3208 | #endif |
| 3209 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3210 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3211 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3212 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3213 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3214 | |
| 3215 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 3216 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3217 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 3218 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3219 | } |
| 3220 | #endif /* HAVE_FDATASYNC */ |
| 3221 | |
| 3222 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3223 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3224 | PyDoc_STRVAR(posix_chown__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3225 | "chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True)\n\n\ |
| 3226 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 3227 | \n\ |
| 3228 | path may always be specified as a string.\n\ |
| 3229 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 3230 | If this functionality is unavailable, using it raises an exception.\n\ |
| 3231 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 3232 | and path should be relative; path will then be relative to that directory.\n\ |
| 3233 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 3234 | link, chown will modify the symbolic link itself instead of the file the\n\ |
| 3235 | link points to.\n\ |
| 3236 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 3237 | an open file descriptor.\n\ |
| 3238 | dir_fd and follow_symlinks may not be implemented on your platform.\n\ |
| 3239 | If they are unavailable, using them will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3240 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3241 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3242 | posix_chown(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3243 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3244 | path_t path; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3245 | uid_t uid; |
| 3246 | gid_t gid; |
| 3247 | int dir_fd = DEFAULT_DIR_FD; |
| 3248 | int follow_symlinks = 1; |
| 3249 | int result; |
| 3250 | PyObject *return_value = NULL; |
| 3251 | static char *keywords[] = {"path", "uid", "gid", "dir_fd", |
| 3252 | "follow_symlinks", NULL}; |
| 3253 | |
| 3254 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3255 | path.function_name = "chown"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3256 | #ifdef HAVE_FCHOWN |
| 3257 | path.allow_fd = 1; |
| 3258 | #endif |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3259 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&O&|$O&p:chown", keywords, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3260 | path_converter, &path, |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3261 | _Py_Uid_Converter, &uid, |
| 3262 | _Py_Gid_Converter, &gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3263 | #ifdef HAVE_FCHOWNAT |
| 3264 | dir_fd_converter, &dir_fd, |
| 3265 | #else |
| 3266 | dir_fd_unavailable, &dir_fd, |
| 3267 | #endif |
| 3268 | &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3269 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3270 | |
| 3271 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3272 | if (follow_symlinks_specified("chown", follow_symlinks)) |
| 3273 | goto exit; |
| 3274 | #endif |
| 3275 | if (dir_fd_and_fd_invalid("chown", dir_fd, path.fd) || |
| 3276 | fd_and_follow_symlinks_invalid("chown", path.fd, follow_symlinks)) |
| 3277 | goto exit; |
| 3278 | |
| 3279 | #ifdef __APPLE__ |
| 3280 | /* |
| 3281 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3282 | * (But we still have an lchown symbol because of weak-linking.) |
| 3283 | * It doesn't have fchownat either. So there's no possibility |
| 3284 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3285 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3286 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3287 | follow_symlinks_specified("chown", follow_symlinks); |
| 3288 | goto exit; |
| 3289 | } |
| 3290 | #endif |
| 3291 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3292 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3293 | #ifdef HAVE_FCHOWN |
| 3294 | if (path.fd != -1) |
| 3295 | result = fchown(path.fd, uid, gid); |
| 3296 | else |
| 3297 | #endif |
| 3298 | #ifdef HAVE_LCHOWN |
| 3299 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 3300 | result = lchown(path.narrow, uid, gid); |
| 3301 | else |
| 3302 | #endif |
| 3303 | #ifdef HAVE_FCHOWNAT |
| 3304 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
| 3305 | result = fchownat(dir_fd, path.narrow, uid, gid, |
| 3306 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3307 | else |
| 3308 | #endif |
| 3309 | result = chown(path.narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3310 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3311 | |
| 3312 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3313 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3314 | goto exit; |
| 3315 | } |
| 3316 | |
| 3317 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3318 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3319 | |
| 3320 | exit: |
| 3321 | path_cleanup(&path); |
| 3322 | return return_value; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3323 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3324 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3325 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3326 | #ifdef HAVE_FCHOWN |
| 3327 | PyDoc_STRVAR(posix_fchown__doc__, |
| 3328 | "fchown(fd, uid, gid)\n\n\ |
| 3329 | 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] | 3330 | 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] | 3331 | |
| 3332 | static PyObject * |
| 3333 | posix_fchown(PyObject *self, PyObject *args) |
| 3334 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3335 | int fd; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3336 | uid_t uid; |
| 3337 | gid_t gid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3338 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3339 | if (!PyArg_ParseTuple(args, "iO&O&:fchown", &fd, |
| 3340 | _Py_Uid_Converter, &uid, |
| 3341 | _Py_Gid_Converter, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3342 | return NULL; |
| 3343 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3344 | res = fchown(fd, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3345 | Py_END_ALLOW_THREADS |
| 3346 | if (res < 0) |
| 3347 | return posix_error(); |
| 3348 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3349 | } |
| 3350 | #endif /* HAVE_FCHOWN */ |
| 3351 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3352 | #ifdef HAVE_LCHOWN |
| 3353 | PyDoc_STRVAR(posix_lchown__doc__, |
| 3354 | "lchown(path, uid, gid)\n\n\ |
| 3355 | 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] | 3356 | This function will not follow symbolic links.\n\ |
| 3357 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False)."); |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3358 | |
| 3359 | static PyObject * |
| 3360 | posix_lchown(PyObject *self, PyObject *args) |
| 3361 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3362 | path_t path; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3363 | uid_t uid; |
| 3364 | gid_t gid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3365 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3366 | memset(&path, 0, sizeof(path)); |
| 3367 | path.function_name = "lchown"; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3368 | if (!PyArg_ParseTuple(args, "O&O&O&:lchown", |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3369 | path_converter, &path, |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3370 | _Py_Uid_Converter, &uid, |
| 3371 | _Py_Gid_Converter, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3372 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3373 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | c2d0200 | 2013-02-10 22:03:08 +0200 | [diff] [blame] | 3374 | res = lchown(path.narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3375 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3376 | if (res < 0) { |
| 3377 | path_error(&path); |
| 3378 | path_cleanup(&path); |
| 3379 | return NULL; |
| 3380 | } |
| 3381 | path_cleanup(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3382 | Py_INCREF(Py_None); |
| 3383 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3384 | } |
| 3385 | #endif /* HAVE_LCHOWN */ |
| 3386 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3387 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3388 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3389 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3390 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3391 | char buf[1026]; |
| 3392 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3393 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3394 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3395 | if (!use_bytes) { |
| 3396 | wchar_t wbuf[1026]; |
| 3397 | wchar_t *wbuf2 = wbuf; |
| 3398 | PyObject *resobj; |
| 3399 | DWORD len; |
| 3400 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3401 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3402 | /* If the buffer is large enough, len does not include the |
| 3403 | terminating \0. If the buffer is too small, len includes |
| 3404 | the space needed for the terminator. */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3405 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3406 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3407 | if (wbuf2) |
| 3408 | len = GetCurrentDirectoryW(len, wbuf2); |
| 3409 | } |
| 3410 | Py_END_ALLOW_THREADS |
| 3411 | if (!wbuf2) { |
| 3412 | PyErr_NoMemory(); |
| 3413 | return NULL; |
| 3414 | } |
| 3415 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3416 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3417 | PyMem_RawFree(wbuf2); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3418 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3419 | } |
| 3420 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3421 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3422 | PyMem_RawFree(wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3423 | return resobj; |
| 3424 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3425 | |
| 3426 | if (win32_warn_bytes_api()) |
| 3427 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3428 | #endif |
| 3429 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3430 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3431 | res = getcwd(buf, sizeof buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3432 | Py_END_ALLOW_THREADS |
| 3433 | if (res == NULL) |
| 3434 | return posix_error(); |
| 3435 | if (use_bytes) |
| 3436 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 3437 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3438 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3439 | |
| 3440 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 3441 | "getcwd() -> path\n\n\ |
| 3442 | Return a unicode string representing the current working directory."); |
| 3443 | |
| 3444 | static PyObject * |
| 3445 | posix_getcwd_unicode(PyObject *self) |
| 3446 | { |
| 3447 | return posix_getcwd(0); |
| 3448 | } |
| 3449 | |
| 3450 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 3451 | "getcwdb() -> path\n\n\ |
| 3452 | Return a bytes string representing the current working directory."); |
| 3453 | |
| 3454 | static PyObject * |
| 3455 | posix_getcwd_bytes(PyObject *self) |
| 3456 | { |
| 3457 | return posix_getcwd(1); |
| 3458 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3459 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3460 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3461 | #define HAVE_LINK 1 |
| 3462 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3463 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3464 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3465 | PyDoc_STRVAR(posix_link__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3466 | "link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True)\n\n\ |
| 3467 | Create a hard link to a file.\n\ |
| 3468 | \n\ |
| 3469 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 3470 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 3471 | should be relative; the path will then be relative to that directory.\n\ |
| 3472 | If follow_symlinks is False, and the last element of src is a symbolic\n\ |
| 3473 | link, link will create a link to the symbolic link itself instead of the\n\ |
| 3474 | file the link points to.\n\ |
| 3475 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your\n\ |
| 3476 | platform. If they are unavailable, using them will raise a\n\ |
| 3477 | NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3478 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3479 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3480 | posix_link(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3481 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3482 | path_t src, dst; |
| 3483 | int src_dir_fd = DEFAULT_DIR_FD; |
| 3484 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 3485 | int follow_symlinks = 1; |
| 3486 | PyObject *return_value = NULL; |
| 3487 | static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", |
| 3488 | "follow_symlinks", NULL}; |
| 3489 | #ifdef MS_WINDOWS |
| 3490 | BOOL result; |
| 3491 | #else |
| 3492 | int result; |
| 3493 | #endif |
| 3494 | |
| 3495 | memset(&src, 0, sizeof(src)); |
| 3496 | memset(&dst, 0, sizeof(dst)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3497 | src.function_name = "link"; |
| 3498 | dst.function_name = "link"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3499 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|O&O&p:link", keywords, |
| 3500 | path_converter, &src, |
| 3501 | path_converter, &dst, |
| 3502 | dir_fd_converter, &src_dir_fd, |
| 3503 | dir_fd_converter, &dst_dir_fd, |
| 3504 | &follow_symlinks)) |
| 3505 | return NULL; |
| 3506 | |
| 3507 | #ifndef HAVE_LINKAT |
| 3508 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3509 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
| 3510 | goto exit; |
| 3511 | } |
| 3512 | #endif |
| 3513 | |
| 3514 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 3515 | PyErr_SetString(PyExc_NotImplementedError, |
| 3516 | "link: src and dst must be the same type"); |
| 3517 | goto exit; |
| 3518 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3519 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3520 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3521 | Py_BEGIN_ALLOW_THREADS |
| 3522 | if (src.wide) |
| 3523 | result = CreateHardLinkW(dst.wide, src.wide, NULL); |
| 3524 | else |
| 3525 | result = CreateHardLinkA(dst.narrow, src.narrow, NULL); |
| 3526 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3527 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3528 | if (!result) { |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 3529 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3530 | goto exit; |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3531 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3532 | #else |
| 3533 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3534 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3535 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3536 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3537 | (!follow_symlinks)) |
| 3538 | result = linkat(src_dir_fd, src.narrow, |
| 3539 | dst_dir_fd, dst.narrow, |
| 3540 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3541 | else |
| 3542 | #endif |
| 3543 | result = link(src.narrow, dst.narrow); |
| 3544 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3545 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3546 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3547 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3548 | goto exit; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3549 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3550 | #endif |
| 3551 | |
| 3552 | return_value = Py_None; |
| 3553 | Py_INCREF(Py_None); |
| 3554 | |
| 3555 | exit: |
| 3556 | path_cleanup(&src); |
| 3557 | path_cleanup(&dst); |
| 3558 | return return_value; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3559 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3560 | #endif |
| 3561 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3562 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3563 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3564 | PyDoc_STRVAR(posix_listdir__doc__, |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3565 | "listdir(path='.') -> list_of_filenames\n\n\ |
| 3566 | 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] | 3567 | 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] | 3568 | entries '.' and '..' even if they are present in the directory.\n\ |
| 3569 | \n\ |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3570 | path can be specified as either str or bytes. If path is bytes,\n\ |
| 3571 | the filenames returned will also be bytes; in all other circumstances\n\ |
| 3572 | the filenames returned will be str.\n\ |
| 3573 | On some platforms, path may also be specified as an open file descriptor;\n\ |
| 3574 | the file descriptor must refer to a directory.\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3575 | If this functionality is unavailable, using it raises NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3576 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3577 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3578 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3579 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3580 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3581 | static char *keywords[] = {"path", NULL}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3582 | PyObject *v; |
| 3583 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3584 | BOOL result; |
| 3585 | WIN32_FIND_DATA FileData; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3586 | char namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3587 | char *bufptr = namebuf; |
| 3588 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3589 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3590 | PyObject *po = NULL; |
| 3591 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3592 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3593 | if (!path->narrow) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3594 | WIN32_FIND_DATAW wFileData; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3595 | wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3596 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3597 | if (!path->wide) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3598 | po_wchars = L"."; |
| 3599 | len = 1; |
| 3600 | } else { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3601 | po_wchars = path->wide; |
| 3602 | len = wcslen(path->wide); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3603 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3604 | /* The +5 is so we can append "\\*.*\0" */ |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3605 | wnamebuf = PyMem_Malloc((len + 5) * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3606 | if (!wnamebuf) { |
| 3607 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3608 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3609 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3610 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3611 | if (len > 0) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3612 | wchar_t wch = wnamebuf[len-1]; |
Tim Golden | 781bbeb | 2013-10-25 20:24:06 +0100 | [diff] [blame] | 3613 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3614 | wnamebuf[len++] = SEP; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3615 | wcscpy(wnamebuf + len, L"*.*"); |
| 3616 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3617 | if ((list = PyList_New(0)) == NULL) { |
| 3618 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3619 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3620 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3621 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3622 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3623 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3624 | int error = GetLastError(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3625 | if (error == ERROR_FILE_NOT_FOUND) |
| 3626 | goto exit; |
| 3627 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3628 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3629 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3630 | } |
| 3631 | do { |
| 3632 | /* Skip over . and .. */ |
| 3633 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3634 | wcscmp(wFileData.cFileName, L"..") != 0) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3635 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3636 | wcslen(wFileData.cFileName)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3637 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3638 | Py_DECREF(list); |
| 3639 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3640 | break; |
| 3641 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3642 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3643 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3644 | Py_DECREF(list); |
| 3645 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3646 | break; |
| 3647 | } |
| 3648 | Py_DECREF(v); |
| 3649 | } |
| 3650 | Py_BEGIN_ALLOW_THREADS |
| 3651 | result = FindNextFileW(hFindFile, &wFileData); |
| 3652 | Py_END_ALLOW_THREADS |
| 3653 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3654 | it got to the end of the directory. */ |
| 3655 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3656 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3657 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3658 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3659 | } |
| 3660 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3661 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3662 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3663 | } |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3664 | strcpy(namebuf, path->narrow); |
| 3665 | len = path->length; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3666 | if (len > 0) { |
| 3667 | char ch = namebuf[len-1]; |
Tim Golden | 781bbeb | 2013-10-25 20:24:06 +0100 | [diff] [blame] | 3668 | if (ch != '\\' && ch != '/' && ch != ':') |
| 3669 | namebuf[len++] = '\\'; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3670 | strcpy(namebuf + len, "*.*"); |
| 3671 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3672 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3673 | if ((list = PyList_New(0)) == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3674 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3675 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3676 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3677 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3678 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3679 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3680 | int error = GetLastError(); |
| 3681 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3682 | goto exit; |
| 3683 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3684 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3685 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3686 | } |
| 3687 | do { |
| 3688 | /* Skip over . and .. */ |
| 3689 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 3690 | strcmp(FileData.cFileName, "..") != 0) { |
| 3691 | v = PyBytes_FromString(FileData.cFileName); |
| 3692 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3693 | Py_DECREF(list); |
| 3694 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3695 | break; |
| 3696 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3697 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3698 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3699 | Py_DECREF(list); |
| 3700 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3701 | break; |
| 3702 | } |
| 3703 | Py_DECREF(v); |
| 3704 | } |
| 3705 | Py_BEGIN_ALLOW_THREADS |
| 3706 | result = FindNextFile(hFindFile, &FileData); |
| 3707 | Py_END_ALLOW_THREADS |
| 3708 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3709 | it got to the end of the directory. */ |
| 3710 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3711 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3712 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3713 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3714 | } |
| 3715 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3716 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3717 | exit: |
| 3718 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3719 | if (FindClose(hFindFile) == FALSE) { |
| 3720 | if (list != NULL) { |
| 3721 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3722 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3723 | } |
| 3724 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3725 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3726 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3727 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3728 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3729 | } /* end of _listdir_windows_no_opendir */ |
| 3730 | |
| 3731 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3732 | |
| 3733 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3734 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3735 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3736 | PyObject *v; |
| 3737 | DIR *dirp = NULL; |
| 3738 | struct dirent *ep; |
| 3739 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3740 | #ifdef HAVE_FDOPENDIR |
| 3741 | int fd = -1; |
| 3742 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3743 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3744 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3745 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3746 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3747 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3748 | fd = _Py_dup(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3749 | if (fd == -1) { |
| 3750 | list = posix_error(); |
| 3751 | goto exit; |
| 3752 | } |
| 3753 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3754 | return_str = 1; |
| 3755 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3756 | Py_BEGIN_ALLOW_THREADS |
| 3757 | dirp = fdopendir(fd); |
| 3758 | Py_END_ALLOW_THREADS |
| 3759 | } |
| 3760 | else |
| 3761 | #endif |
| 3762 | { |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3763 | char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3764 | if (path->narrow) { |
| 3765 | name = path->narrow; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3766 | /* only return bytes if they specified a bytes object */ |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3767 | return_str = !(PyBytes_Check(path->object)); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3768 | } |
| 3769 | else { |
| 3770 | name = "."; |
| 3771 | return_str = 1; |
| 3772 | } |
| 3773 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3774 | Py_BEGIN_ALLOW_THREADS |
| 3775 | dirp = opendir(name); |
| 3776 | Py_END_ALLOW_THREADS |
| 3777 | } |
| 3778 | |
| 3779 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3780 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3781 | #ifdef HAVE_FDOPENDIR |
| 3782 | if (fd != -1) { |
| 3783 | Py_BEGIN_ALLOW_THREADS |
| 3784 | close(fd); |
| 3785 | Py_END_ALLOW_THREADS |
| 3786 | } |
| 3787 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3788 | goto exit; |
| 3789 | } |
| 3790 | if ((list = PyList_New(0)) == NULL) { |
| 3791 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3792 | } |
| 3793 | for (;;) { |
| 3794 | errno = 0; |
| 3795 | Py_BEGIN_ALLOW_THREADS |
| 3796 | ep = readdir(dirp); |
| 3797 | Py_END_ALLOW_THREADS |
| 3798 | if (ep == NULL) { |
| 3799 | if (errno == 0) { |
| 3800 | break; |
| 3801 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3802 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3803 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3804 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3805 | } |
| 3806 | } |
| 3807 | if (ep->d_name[0] == '.' && |
| 3808 | (NAMLEN(ep) == 1 || |
| 3809 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3810 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3811 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3812 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3813 | else |
| 3814 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3815 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3816 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3817 | break; |
| 3818 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3819 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3820 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3821 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3822 | break; |
| 3823 | } |
| 3824 | Py_DECREF(v); |
| 3825 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3826 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3827 | exit: |
| 3828 | if (dirp != NULL) { |
| 3829 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3830 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3831 | if (fd > -1) |
| 3832 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3833 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3834 | closedir(dirp); |
| 3835 | Py_END_ALLOW_THREADS |
| 3836 | } |
| 3837 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3838 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3839 | } /* end of _posix_listdir */ |
| 3840 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3841 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3842 | static PyObject * |
| 3843 | posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs) |
| 3844 | { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3845 | path_t path; |
| 3846 | PyObject *list = NULL; |
| 3847 | static char *keywords[] = {"path", NULL}; |
| 3848 | PyObject *return_value; |
| 3849 | |
| 3850 | memset(&path, 0, sizeof(path)); |
| 3851 | path.function_name = "listdir"; |
| 3852 | path.nullable = 1; |
| 3853 | #ifdef HAVE_FDOPENDIR |
| 3854 | path.allow_fd = 1; |
| 3855 | path.fd = -1; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3856 | #endif |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3857 | |
| 3858 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:listdir", keywords, |
| 3859 | path_converter, &path)) { |
| 3860 | return NULL; |
| 3861 | } |
| 3862 | |
| 3863 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3864 | return_value = _listdir_windows_no_opendir(&path, list); |
| 3865 | #else |
| 3866 | return_value = _posix_listdir(&path, list); |
| 3867 | #endif |
| 3868 | path_cleanup(&path); |
| 3869 | return return_value; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3870 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3871 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3872 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3873 | /* A helper function for abspath on win32 */ |
| 3874 | static PyObject * |
| 3875 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 3876 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3877 | const char *path; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3878 | char outbuf[MAX_PATH]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3879 | char *temp; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3880 | PyObject *po; |
| 3881 | |
| 3882 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) |
| 3883 | { |
| 3884 | wchar_t *wpath; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3885 | wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3886 | wchar_t *wtemp; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3887 | DWORD result; |
| 3888 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3889 | |
| 3890 | wpath = PyUnicode_AsUnicode(po); |
| 3891 | if (wpath == NULL) |
| 3892 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3893 | result = GetFullPathNameW(wpath, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3894 | Py_ARRAY_LENGTH(woutbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3895 | woutbuf, &wtemp); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3896 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3897 | woutbufp = PyMem_Malloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3898 | if (!woutbufp) |
| 3899 | return PyErr_NoMemory(); |
| 3900 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 3901 | } |
| 3902 | if (result) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3903 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3904 | else |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3905 | v = win32_error_object("GetFullPathNameW", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3906 | if (woutbufp != woutbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3907 | PyMem_Free(woutbufp); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3908 | return v; |
| 3909 | } |
| 3910 | /* Drop the argument parsing error as narrow strings |
| 3911 | are also valid. */ |
| 3912 | PyErr_Clear(); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3913 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3914 | if (!PyArg_ParseTuple (args, "y:_getfullpathname", |
| 3915 | &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3916 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3917 | if (win32_warn_bytes_api()) |
| 3918 | return NULL; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3919 | if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3920 | outbuf, &temp)) { |
| 3921 | win32_error("GetFullPathName", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3922 | return NULL; |
| 3923 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3924 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 3925 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 3926 | Py_FileSystemDefaultEncoding, NULL); |
| 3927 | } |
| 3928 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3929 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3930 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3931 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3932 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3933 | /* A helper function for samepath on windows */ |
| 3934 | static PyObject * |
| 3935 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 3936 | { |
| 3937 | HANDLE hFile; |
| 3938 | int buf_size; |
| 3939 | wchar_t *target_path; |
| 3940 | int result_length; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3941 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3942 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3943 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3944 | if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po)) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3945 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3946 | path = PyUnicode_AsUnicode(po); |
| 3947 | if (path == NULL) |
| 3948 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3949 | |
| 3950 | if(!check_GetFinalPathNameByHandle()) { |
| 3951 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 3952 | NotImplementedError. */ |
| 3953 | return PyErr_Format(PyExc_NotImplementedError, |
| 3954 | "GetFinalPathNameByHandle not available on this platform"); |
| 3955 | } |
| 3956 | |
| 3957 | hFile = CreateFileW( |
| 3958 | path, |
| 3959 | 0, /* desired access */ |
| 3960 | 0, /* share mode */ |
| 3961 | NULL, /* security attributes */ |
| 3962 | OPEN_EXISTING, |
| 3963 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3964 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3965 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3966 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3967 | if(hFile == INVALID_HANDLE_VALUE) |
| 3968 | return win32_error_object("CreateFileW", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3969 | |
| 3970 | /* We have a good handle to the target, use it to determine the |
| 3971 | target path name. */ |
| 3972 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 3973 | |
| 3974 | if(!buf_size) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3975 | return win32_error_object("GetFinalPathNameByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3976 | |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3977 | target_path = (wchar_t *)PyMem_Malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3978 | if(!target_path) |
| 3979 | return PyErr_NoMemory(); |
| 3980 | |
| 3981 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 3982 | buf_size, VOLUME_NAME_DOS); |
| 3983 | if(!result_length) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3984 | return win32_error_object("GetFinalPathNamyByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3985 | |
| 3986 | if(!CloseHandle(hFile)) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3987 | return win32_error_object("CloseHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3988 | |
| 3989 | target_path[result_length] = 0; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3990 | result = PyUnicode_FromWideChar(target_path, result_length); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3991 | PyMem_Free(target_path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3992 | return result; |
| 3993 | |
| 3994 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3995 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3996 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3997 | "Return true if the pathname refers to an existing directory."); |
| 3998 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3999 | static PyObject * |
| 4000 | posix__isdir(PyObject *self, PyObject *args) |
| 4001 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 4002 | const char *path; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4003 | PyObject *po; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 4004 | DWORD attributes; |
| 4005 | |
| 4006 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4007 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 4008 | if (wpath == NULL) |
| 4009 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 4010 | |
| 4011 | attributes = GetFileAttributesW(wpath); |
| 4012 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 4013 | Py_RETURN_FALSE; |
| 4014 | goto check; |
| 4015 | } |
| 4016 | /* Drop the argument parsing error as narrow strings |
| 4017 | are also valid. */ |
| 4018 | PyErr_Clear(); |
| 4019 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 4020 | if (!PyArg_ParseTuple(args, "y:_isdir", &path)) |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 4021 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 4022 | if (win32_warn_bytes_api()) |
| 4023 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 4024 | attributes = GetFileAttributesA(path); |
| 4025 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 4026 | Py_RETURN_FALSE; |
| 4027 | |
| 4028 | check: |
| 4029 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 4030 | Py_RETURN_TRUE; |
| 4031 | else |
| 4032 | Py_RETURN_FALSE; |
| 4033 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4034 | |
| 4035 | PyDoc_STRVAR(posix__getvolumepathname__doc__, |
| 4036 | "Return volume mount point of the specified path."); |
| 4037 | |
| 4038 | /* A helper function for ismount on windows */ |
| 4039 | static PyObject * |
| 4040 | posix__getvolumepathname(PyObject *self, PyObject *args) |
| 4041 | { |
| 4042 | PyObject *po, *result; |
| 4043 | wchar_t *path, *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4044 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4045 | BOOL ret; |
| 4046 | |
| 4047 | if (!PyArg_ParseTuple(args, "U|:_getvolumepathname", &po)) |
| 4048 | return NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4049 | path = PyUnicode_AsUnicodeAndSize(po, &buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4050 | if (path == NULL) |
| 4051 | return NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4052 | buflen += 1; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4053 | |
| 4054 | /* Volume path should be shorter than entire path */ |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4055 | buflen = Py_MAX(buflen, MAX_PATH); |
| 4056 | |
| 4057 | if (buflen > DWORD_MAX) { |
| 4058 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 4059 | return NULL; |
| 4060 | } |
| 4061 | |
| 4062 | mountpath = (wchar_t *)PyMem_Malloc(buflen * sizeof(wchar_t)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4063 | if (mountpath == NULL) |
| 4064 | return PyErr_NoMemory(); |
| 4065 | |
| 4066 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | 85ba92a | 2013-11-18 10:30:42 +0100 | [diff] [blame] | 4067 | ret = GetVolumePathNameW(path, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4068 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4069 | Py_END_ALLOW_THREADS |
| 4070 | |
| 4071 | if (!ret) { |
| 4072 | result = win32_error_object("_getvolumepathname", po); |
| 4073 | goto exit; |
| 4074 | } |
| 4075 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
| 4076 | |
| 4077 | exit: |
| 4078 | PyMem_Free(mountpath); |
| 4079 | return result; |
| 4080 | } |
| 4081 | /* end of posix__getvolumepathname */ |
| 4082 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4083 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4084 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4085 | PyDoc_STRVAR(posix_mkdir__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4086 | "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 4087 | Create a directory.\n\ |
| 4088 | \n\ |
| 4089 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4090 | and path should be relative; path will then be relative to that directory.\n\ |
| 4091 | dir_fd may not be implemented on your platform.\n\ |
| 4092 | If it is unavailable, using it will raise a NotImplementedError.\n\ |
| 4093 | \n\ |
| 4094 | The mode argument is ignored on Windows."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4095 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4096 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4097 | posix_mkdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4098 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4099 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4100 | int mode = 0777; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4101 | int dir_fd = DEFAULT_DIR_FD; |
| 4102 | static char *keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 4103 | PyObject *return_value = NULL; |
| 4104 | int result; |
| 4105 | |
| 4106 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4107 | path.function_name = "mkdir"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4108 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkdir", keywords, |
| 4109 | path_converter, &path, &mode, |
| 4110 | #ifdef HAVE_MKDIRAT |
| 4111 | dir_fd_converter, &dir_fd |
| 4112 | #else |
| 4113 | dir_fd_unavailable, &dir_fd |
| 4114 | #endif |
| 4115 | )) |
| 4116 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4117 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4118 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4119 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4120 | if (path.wide) |
| 4121 | result = CreateDirectoryW(path.wide, NULL); |
| 4122 | else |
| 4123 | result = CreateDirectoryA(path.narrow, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4124 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4125 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4126 | if (!result) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4127 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4128 | goto exit; |
| 4129 | } |
| 4130 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4131 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4132 | #if HAVE_MKDIRAT |
| 4133 | if (dir_fd != DEFAULT_DIR_FD) |
| 4134 | result = mkdirat(dir_fd, path.narrow, mode); |
| 4135 | else |
| 4136 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4137 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4138 | result = mkdir(path.narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4139 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4140 | result = mkdir(path.narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4141 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4142 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4143 | if (result < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4144 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4145 | goto exit; |
| 4146 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4147 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4148 | return_value = Py_None; |
| 4149 | Py_INCREF(Py_None); |
| 4150 | exit: |
| 4151 | path_cleanup(&path); |
| 4152 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4153 | } |
| 4154 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4155 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4156 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 4157 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4158 | #include <sys/resource.h> |
| 4159 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4160 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4161 | |
| 4162 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4163 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4164 | "nice(inc) -> new_priority\n\n\ |
| 4165 | 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] | 4166 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4167 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4168 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4169 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4170 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4171 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4172 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 4173 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4174 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4175 | /* There are two flavours of 'nice': one that returns the new |
| 4176 | priority (as required by almost all standards out there) and the |
| 4177 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 4178 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4179 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4180 | If we are of the nice family that returns the new priority, we |
| 4181 | need to clear errno before the call, and check if errno is filled |
| 4182 | before calling posix_error() on a returnvalue of -1, because the |
| 4183 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4184 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4185 | errno = 0; |
| 4186 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4187 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4188 | if (value == 0) |
| 4189 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4190 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4191 | if (value == -1 && errno != 0) |
| 4192 | /* either nice() or getpriority() returned an error */ |
| 4193 | return posix_error(); |
| 4194 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4195 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4196 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4197 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4198 | |
| 4199 | #ifdef HAVE_GETPRIORITY |
| 4200 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 4201 | "getpriority(which, who) -> current_priority\n\n\ |
| 4202 | Get program scheduling priority."); |
| 4203 | |
| 4204 | static PyObject * |
| 4205 | posix_getpriority(PyObject *self, PyObject *args) |
| 4206 | { |
| 4207 | int which, who, retval; |
| 4208 | |
| 4209 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 4210 | return NULL; |
| 4211 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4212 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4213 | if (errno != 0) |
| 4214 | return posix_error(); |
| 4215 | return PyLong_FromLong((long)retval); |
| 4216 | } |
| 4217 | #endif /* HAVE_GETPRIORITY */ |
| 4218 | |
| 4219 | |
| 4220 | #ifdef HAVE_SETPRIORITY |
| 4221 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 4222 | "setpriority(which, who, prio) -> None\n\n\ |
| 4223 | Set program scheduling priority."); |
| 4224 | |
| 4225 | static PyObject * |
| 4226 | posix_setpriority(PyObject *self, PyObject *args) |
| 4227 | { |
| 4228 | int which, who, prio, retval; |
| 4229 | |
| 4230 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 4231 | return NULL; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4232 | retval = setpriority(which, who, prio); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4233 | if (retval == -1) |
| 4234 | return posix_error(); |
| 4235 | Py_RETURN_NONE; |
| 4236 | } |
| 4237 | #endif /* HAVE_SETPRIORITY */ |
| 4238 | |
| 4239 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4240 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4241 | internal_rename(PyObject *args, PyObject *kwargs, int is_replace) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4242 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4243 | char *function_name = is_replace ? "replace" : "rename"; |
| 4244 | path_t src; |
| 4245 | path_t dst; |
| 4246 | int src_dir_fd = DEFAULT_DIR_FD; |
| 4247 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 4248 | int dir_fd_specified; |
| 4249 | PyObject *return_value = NULL; |
| 4250 | char format[24]; |
| 4251 | static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL}; |
| 4252 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4253 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4254 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4255 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4256 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4257 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4258 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4259 | |
| 4260 | memset(&src, 0, sizeof(src)); |
| 4261 | memset(&dst, 0, sizeof(dst)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4262 | src.function_name = function_name; |
| 4263 | dst.function_name = function_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4264 | strcpy(format, "O&O&|$O&O&:"); |
| 4265 | strcat(format, function_name); |
| 4266 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, keywords, |
| 4267 | path_converter, &src, |
| 4268 | path_converter, &dst, |
| 4269 | dir_fd_converter, &src_dir_fd, |
| 4270 | dir_fd_converter, &dst_dir_fd)) |
| 4271 | return NULL; |
| 4272 | |
| 4273 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4274 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4275 | #ifndef HAVE_RENAMEAT |
| 4276 | if (dir_fd_specified) { |
| 4277 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
| 4278 | goto exit; |
| 4279 | } |
| 4280 | #endif |
| 4281 | |
| 4282 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 4283 | PyErr_Format(PyExc_ValueError, |
| 4284 | "%s: src and dst must be the same type", function_name); |
| 4285 | goto exit; |
| 4286 | } |
| 4287 | |
| 4288 | #ifdef MS_WINDOWS |
| 4289 | Py_BEGIN_ALLOW_THREADS |
| 4290 | if (src.wide) |
| 4291 | result = MoveFileExW(src.wide, dst.wide, flags); |
| 4292 | else |
| 4293 | result = MoveFileExA(src.narrow, dst.narrow, flags); |
| 4294 | Py_END_ALLOW_THREADS |
| 4295 | |
| 4296 | if (!result) { |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 4297 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4298 | goto exit; |
| 4299 | } |
| 4300 | |
| 4301 | #else |
| 4302 | Py_BEGIN_ALLOW_THREADS |
| 4303 | #ifdef HAVE_RENAMEAT |
| 4304 | if (dir_fd_specified) |
| 4305 | result = renameat(src_dir_fd, src.narrow, dst_dir_fd, dst.narrow); |
| 4306 | else |
| 4307 | #endif |
| 4308 | result = rename(src.narrow, dst.narrow); |
| 4309 | Py_END_ALLOW_THREADS |
| 4310 | |
| 4311 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4312 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4313 | goto exit; |
| 4314 | } |
| 4315 | #endif |
| 4316 | |
| 4317 | Py_INCREF(Py_None); |
| 4318 | return_value = Py_None; |
| 4319 | exit: |
| 4320 | path_cleanup(&src); |
| 4321 | path_cleanup(&dst); |
| 4322 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4323 | } |
| 4324 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4325 | PyDoc_STRVAR(posix_rename__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4326 | "rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\ |
| 4327 | Rename a file or directory.\n\ |
| 4328 | \n\ |
| 4329 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 4330 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 4331 | should be relative; the path will then be relative to that directory.\n\ |
| 4332 | src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\ |
| 4333 | If they are unavailable, using them will raise a NotImplementedError."); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4334 | |
| 4335 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4336 | posix_rename(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4337 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4338 | return internal_rename(args, kwargs, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4339 | } |
| 4340 | |
| 4341 | PyDoc_STRVAR(posix_replace__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4342 | "replace(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\ |
| 4343 | Rename a file or directory, overwriting the destination.\n\ |
| 4344 | \n\ |
| 4345 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 4346 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 4347 | should be relative; the path will then be relative to that directory.\n\ |
| 4348 | src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\ |
| 4349 | If they are unavailable, using them will raise a NotImplementedError."); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4350 | |
| 4351 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4352 | posix_replace(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4353 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4354 | return internal_rename(args, kwargs, 1); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4355 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4356 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4357 | PyDoc_STRVAR(posix_rmdir__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4358 | "rmdir(path, *, dir_fd=None)\n\n\ |
| 4359 | Remove a directory.\n\ |
| 4360 | \n\ |
| 4361 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4362 | and path should be relative; path will then be relative to that directory.\n\ |
| 4363 | dir_fd may not be implemented on your platform.\n\ |
| 4364 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4365 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4366 | static PyObject * |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4367 | posix_rmdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4368 | { |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4369 | path_t path; |
| 4370 | int dir_fd = DEFAULT_DIR_FD; |
| 4371 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 4372 | int result; |
| 4373 | PyObject *return_value = NULL; |
| 4374 | |
| 4375 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4376 | path.function_name = "rmdir"; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4377 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:rmdir", keywords, |
| 4378 | path_converter, &path, |
| 4379 | #ifdef HAVE_UNLINKAT |
| 4380 | dir_fd_converter, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4381 | #else |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4382 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4383 | #endif |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4384 | )) |
| 4385 | return NULL; |
| 4386 | |
| 4387 | Py_BEGIN_ALLOW_THREADS |
| 4388 | #ifdef MS_WINDOWS |
| 4389 | if (path.wide) |
| 4390 | result = RemoveDirectoryW(path.wide); |
| 4391 | else |
| 4392 | result = RemoveDirectoryA(path.narrow); |
| 4393 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4394 | #else |
| 4395 | #ifdef HAVE_UNLINKAT |
| 4396 | if (dir_fd != DEFAULT_DIR_FD) |
| 4397 | result = unlinkat(dir_fd, path.narrow, AT_REMOVEDIR); |
| 4398 | else |
| 4399 | #endif |
| 4400 | result = rmdir(path.narrow); |
| 4401 | #endif |
| 4402 | Py_END_ALLOW_THREADS |
| 4403 | |
| 4404 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4405 | return_value = path_error(&path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4406 | goto exit; |
| 4407 | } |
| 4408 | |
| 4409 | return_value = Py_None; |
| 4410 | Py_INCREF(Py_None); |
| 4411 | |
| 4412 | exit: |
| 4413 | path_cleanup(&path); |
| 4414 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4415 | } |
| 4416 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4417 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4418 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4419 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4420 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4421 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4422 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4423 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4424 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4425 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4426 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 4427 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4428 | wchar_t *command; |
| 4429 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 4430 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4431 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4432 | Py_BEGIN_ALLOW_THREADS |
| 4433 | sts = _wsystem(command); |
| 4434 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4435 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4436 | PyObject *command_obj; |
| 4437 | char *command; |
| 4438 | if (!PyArg_ParseTuple(args, "O&:system", |
| 4439 | PyUnicode_FSConverter, &command_obj)) |
| 4440 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4441 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4442 | command = PyBytes_AsString(command_obj); |
| 4443 | Py_BEGIN_ALLOW_THREADS |
| 4444 | sts = system(command); |
| 4445 | Py_END_ALLOW_THREADS |
| 4446 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4447 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4448 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4449 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4450 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4451 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4452 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4453 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4454 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4455 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4456 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4457 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4458 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4459 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4460 | int i; |
| 4461 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 4462 | return NULL; |
| 4463 | i = (int)umask(i); |
| 4464 | if (i < 0) |
| 4465 | return posix_error(); |
| 4466 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4467 | } |
| 4468 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4469 | #ifdef MS_WINDOWS |
| 4470 | |
| 4471 | /* override the default DeleteFileW behavior so that directory |
| 4472 | symlinks can be removed with this function, the same as with |
| 4473 | Unix symlinks */ |
| 4474 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4475 | { |
| 4476 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4477 | WIN32_FIND_DATAW find_data; |
| 4478 | HANDLE find_data_handle; |
| 4479 | int is_directory = 0; |
| 4480 | int is_link = 0; |
| 4481 | |
| 4482 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4483 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4484 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4485 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4486 | it is a symlink */ |
| 4487 | if(is_directory && |
| 4488 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4489 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4490 | |
| 4491 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
| 4492 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; |
| 4493 | FindClose(find_data_handle); |
| 4494 | } |
| 4495 | } |
| 4496 | } |
| 4497 | |
| 4498 | if (is_directory && is_link) |
| 4499 | return RemoveDirectoryW(lpFileName); |
| 4500 | |
| 4501 | return DeleteFileW(lpFileName); |
| 4502 | } |
| 4503 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4504 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4505 | PyDoc_STRVAR(posix_unlink__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4506 | "unlink(path, *, dir_fd=None)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4507 | Remove a file (same as remove()).\n\ |
| 4508 | \n\ |
| 4509 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4510 | and path should be relative; path will then be relative to that directory.\n\ |
| 4511 | dir_fd may not be implemented on your platform.\n\ |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4512 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4513 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4514 | PyDoc_STRVAR(posix_remove__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4515 | "remove(path, *, dir_fd=None)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4516 | Remove a file (same as unlink()).\n\ |
| 4517 | \n\ |
| 4518 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4519 | and path should be relative; path will then be relative to that directory.\n\ |
| 4520 | dir_fd may not be implemented on your platform.\n\ |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4521 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4522 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4523 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4524 | posix_unlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4525 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4526 | path_t path; |
| 4527 | int dir_fd = DEFAULT_DIR_FD; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4528 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4529 | int result; |
| 4530 | PyObject *return_value = NULL; |
| 4531 | |
| 4532 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4533 | path.function_name = "unlink"; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4534 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:unlink", keywords, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4535 | path_converter, &path, |
| 4536 | #ifdef HAVE_UNLINKAT |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4537 | dir_fd_converter, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4538 | #else |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4539 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4540 | #endif |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4541 | )) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4542 | return NULL; |
| 4543 | |
| 4544 | Py_BEGIN_ALLOW_THREADS |
| 4545 | #ifdef MS_WINDOWS |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4546 | if (path.wide) |
| 4547 | result = Py_DeleteFileW(path.wide); |
| 4548 | else |
| 4549 | result = DeleteFileA(path.narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4550 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4551 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4552 | #ifdef HAVE_UNLINKAT |
| 4553 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4554 | result = unlinkat(dir_fd, path.narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4555 | else |
| 4556 | #endif /* HAVE_UNLINKAT */ |
| 4557 | result = unlink(path.narrow); |
| 4558 | #endif |
| 4559 | Py_END_ALLOW_THREADS |
| 4560 | |
| 4561 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4562 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4563 | goto exit; |
| 4564 | } |
| 4565 | |
| 4566 | return_value = Py_None; |
| 4567 | Py_INCREF(Py_None); |
| 4568 | |
| 4569 | exit: |
| 4570 | path_cleanup(&path); |
| 4571 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4572 | } |
| 4573 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4574 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4575 | PyDoc_STRVAR(posix_uname__doc__, |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4576 | "uname() -> uname_result\n\n\ |
| 4577 | Return an object identifying the current operating system.\n\ |
| 4578 | The object behaves like a named tuple with the following fields:\n\ |
| 4579 | (sysname, nodename, release, version, machine)"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4580 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4581 | static PyStructSequence_Field uname_result_fields[] = { |
| 4582 | {"sysname", "operating system name"}, |
| 4583 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4584 | {"release", "operating system release"}, |
| 4585 | {"version", "operating system version"}, |
| 4586 | {"machine", "hardware identifier"}, |
| 4587 | {NULL} |
| 4588 | }; |
| 4589 | |
| 4590 | PyDoc_STRVAR(uname_result__doc__, |
| 4591 | "uname_result: Result from os.uname().\n\n\ |
| 4592 | This object may be accessed either as a tuple of\n\ |
| 4593 | (sysname, nodename, release, version, machine),\n\ |
| 4594 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4595 | \n\ |
| 4596 | See os.uname for more information."); |
| 4597 | |
| 4598 | static PyStructSequence_Desc uname_result_desc = { |
| 4599 | "uname_result", /* name */ |
| 4600 | uname_result__doc__, /* doc */ |
| 4601 | uname_result_fields, |
| 4602 | 5 |
| 4603 | }; |
| 4604 | |
| 4605 | static PyTypeObject UnameResultType; |
| 4606 | |
| 4607 | |
| 4608 | #ifdef HAVE_UNAME |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4609 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4610 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4611 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4612 | struct utsname u; |
| 4613 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4614 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4615 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4616 | Py_BEGIN_ALLOW_THREADS |
| 4617 | res = uname(&u); |
| 4618 | Py_END_ALLOW_THREADS |
| 4619 | if (res < 0) |
| 4620 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4621 | |
| 4622 | value = PyStructSequence_New(&UnameResultType); |
| 4623 | if (value == NULL) |
| 4624 | return NULL; |
| 4625 | |
| 4626 | #define SET(i, field) \ |
| 4627 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4628 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4629 | if (!o) { \ |
| 4630 | Py_DECREF(value); \ |
| 4631 | return NULL; \ |
| 4632 | } \ |
| 4633 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4634 | } \ |
| 4635 | |
| 4636 | SET(0, u.sysname); |
| 4637 | SET(1, u.nodename); |
| 4638 | SET(2, u.release); |
| 4639 | SET(3, u.version); |
| 4640 | SET(4, u.machine); |
| 4641 | |
| 4642 | #undef SET |
| 4643 | |
| 4644 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4645 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4646 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4647 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4648 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4649 | PyDoc_STRVAR(posix_utime__doc__, |
| 4650 | "utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True)\n\ |
| 4651 | Set the access and modified time of path.\n\ |
| 4652 | \n\ |
| 4653 | path may always be specified as a string.\n\ |
| 4654 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 4655 | If this functionality is unavailable, using it raises an exception.\n\ |
| 4656 | \n\ |
| 4657 | If times is not None, it must be a tuple (atime, mtime);\n\ |
| 4658 | atime and mtime should be expressed as float seconds since the epoch.\n\ |
| 4659 | If ns is not None, it must be a tuple (atime_ns, mtime_ns);\n\ |
| 4660 | atime_ns and mtime_ns should be expressed as integer nanoseconds\n\ |
| 4661 | since the epoch.\n\ |
| 4662 | If both times and ns are None, utime uses the current time.\n\ |
| 4663 | Specifying tuples for both times and ns is an error.\n\ |
| 4664 | \n\ |
| 4665 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4666 | and path should be relative; path will then be relative to that directory.\n\ |
| 4667 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 4668 | link, utime will modify the symbolic link itself instead of the file the\n\ |
| 4669 | link points to.\n\ |
| 4670 | It is an error to use dir_fd or follow_symlinks when specifying path\n\ |
| 4671 | as an open file descriptor.\n\ |
| 4672 | dir_fd and follow_symlinks may not be available on your platform.\n\ |
| 4673 | If they are unavailable, using them will raise a NotImplementedError."); |
| 4674 | |
| 4675 | typedef struct { |
| 4676 | int now; |
| 4677 | time_t atime_s; |
| 4678 | long atime_ns; |
| 4679 | time_t mtime_s; |
| 4680 | long mtime_ns; |
| 4681 | } utime_t; |
| 4682 | |
| 4683 | /* |
| 4684 | * these macros assume that "utime" is a pointer to a utime_t |
| 4685 | * they also intentionally leak the declaration of a pointer named "time" |
| 4686 | */ |
| 4687 | #define UTIME_TO_TIMESPEC \ |
| 4688 | struct timespec ts[2]; \ |
| 4689 | struct timespec *time; \ |
| 4690 | if (utime->now) \ |
| 4691 | time = NULL; \ |
| 4692 | else { \ |
| 4693 | ts[0].tv_sec = utime->atime_s; \ |
| 4694 | ts[0].tv_nsec = utime->atime_ns; \ |
| 4695 | ts[1].tv_sec = utime->mtime_s; \ |
| 4696 | ts[1].tv_nsec = utime->mtime_ns; \ |
| 4697 | time = ts; \ |
| 4698 | } \ |
| 4699 | |
| 4700 | #define UTIME_TO_TIMEVAL \ |
| 4701 | struct timeval tv[2]; \ |
| 4702 | struct timeval *time; \ |
| 4703 | if (utime->now) \ |
| 4704 | time = NULL; \ |
| 4705 | else { \ |
| 4706 | tv[0].tv_sec = utime->atime_s; \ |
| 4707 | tv[0].tv_usec = utime->atime_ns / 1000; \ |
| 4708 | tv[1].tv_sec = utime->mtime_s; \ |
| 4709 | tv[1].tv_usec = utime->mtime_ns / 1000; \ |
| 4710 | time = tv; \ |
| 4711 | } \ |
| 4712 | |
| 4713 | #define UTIME_TO_UTIMBUF \ |
| 4714 | struct utimbuf u[2]; \ |
| 4715 | struct utimbuf *time; \ |
| 4716 | if (utime->now) \ |
| 4717 | time = NULL; \ |
| 4718 | else { \ |
| 4719 | u.actime = utime->atime_s; \ |
| 4720 | u.modtime = utime->mtime_s; \ |
| 4721 | time = u; \ |
| 4722 | } |
| 4723 | |
| 4724 | #define UTIME_TO_TIME_T \ |
| 4725 | time_t timet[2]; \ |
| 4726 | struct timet time; \ |
| 4727 | if (utime->now) \ |
| 4728 | time = NULL; \ |
| 4729 | else { \ |
| 4730 | timet[0] = utime->atime_s; \ |
| 4731 | timet[1] = utime->mtime_s; \ |
| 4732 | time = &timet; \ |
| 4733 | } \ |
| 4734 | |
| 4735 | |
| 4736 | #define UTIME_HAVE_DIR_FD (defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)) |
| 4737 | |
| 4738 | #if UTIME_HAVE_DIR_FD |
| 4739 | |
| 4740 | static int |
| 4741 | utime_dir_fd(utime_t *utime, int dir_fd, char *path, int follow_symlinks) |
| 4742 | { |
| 4743 | #ifdef HAVE_UTIMENSAT |
| 4744 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4745 | UTIME_TO_TIMESPEC; |
| 4746 | return utimensat(dir_fd, path, time, flags); |
| 4747 | #elif defined(HAVE_FUTIMESAT) |
| 4748 | UTIME_TO_TIMEVAL; |
| 4749 | /* |
| 4750 | * follow_symlinks will never be false here; |
| 4751 | * we only allow !follow_symlinks and dir_fd together |
| 4752 | * if we have utimensat() |
| 4753 | */ |
| 4754 | assert(follow_symlinks); |
| 4755 | return futimesat(dir_fd, path, time); |
| 4756 | #endif |
| 4757 | } |
| 4758 | |
| 4759 | #endif |
| 4760 | |
| 4761 | #define UTIME_HAVE_FD (defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)) |
| 4762 | |
| 4763 | #if UTIME_HAVE_FD |
| 4764 | |
| 4765 | static int |
| 4766 | utime_fd(utime_t *utime, int fd) |
| 4767 | { |
| 4768 | #ifdef HAVE_FUTIMENS |
| 4769 | UTIME_TO_TIMESPEC; |
| 4770 | return futimens(fd, time); |
| 4771 | #else |
| 4772 | UTIME_TO_TIMEVAL; |
| 4773 | return futimes(fd, time); |
| 4774 | #endif |
| 4775 | } |
| 4776 | |
| 4777 | #endif |
| 4778 | |
| 4779 | |
| 4780 | #define UTIME_HAVE_NOFOLLOW_SYMLINKS \ |
| 4781 | (defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)) |
| 4782 | |
| 4783 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4784 | |
| 4785 | static int |
| 4786 | utime_nofollow_symlinks(utime_t *utime, char *path) |
| 4787 | { |
| 4788 | #ifdef HAVE_UTIMENSAT |
| 4789 | UTIME_TO_TIMESPEC; |
| 4790 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4791 | #else |
| 4792 | UTIME_TO_TIMEVAL; |
| 4793 | return lutimes(path, time); |
| 4794 | #endif |
| 4795 | } |
| 4796 | |
| 4797 | #endif |
| 4798 | |
| 4799 | #ifndef MS_WINDOWS |
| 4800 | |
| 4801 | static int |
| 4802 | utime_default(utime_t *utime, char *path) |
| 4803 | { |
| 4804 | #ifdef HAVE_UTIMENSAT |
| 4805 | UTIME_TO_TIMESPEC; |
| 4806 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4807 | #elif defined(HAVE_UTIMES) |
| 4808 | UTIME_TO_TIMEVAL; |
| 4809 | return utimes(path, time); |
| 4810 | #elif defined(HAVE_UTIME_H) |
| 4811 | UTIME_TO_UTIMBUF; |
| 4812 | return utime(path, time); |
| 4813 | #else |
| 4814 | UTIME_TO_TIME_T; |
| 4815 | return utime(path, time); |
| 4816 | #endif |
| 4817 | } |
| 4818 | |
| 4819 | #endif |
| 4820 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4821 | static int |
| 4822 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4823 | { |
| 4824 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4825 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4826 | divmod = PyNumber_Divmod(py_long, billion); |
| 4827 | if (!divmod) |
| 4828 | goto exit; |
| 4829 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4830 | if ((*s == -1) && PyErr_Occurred()) |
| 4831 | goto exit; |
| 4832 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4833 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4834 | goto exit; |
| 4835 | |
| 4836 | result = 1; |
| 4837 | exit: |
| 4838 | Py_XDECREF(divmod); |
| 4839 | return result; |
| 4840 | } |
| 4841 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4842 | static PyObject * |
| 4843 | posix_utime(PyObject *self, PyObject *args, PyObject *kwargs) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4844 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4845 | path_t path; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4846 | PyObject *times = NULL; |
| 4847 | PyObject *ns = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4848 | int dir_fd = DEFAULT_DIR_FD; |
| 4849 | int follow_symlinks = 1; |
| 4850 | char *keywords[] = {"path", "times", "ns", "dir_fd", |
| 4851 | "follow_symlinks", NULL}; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4852 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4853 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4854 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4855 | #ifdef MS_WINDOWS |
| 4856 | HANDLE hFile; |
| 4857 | FILETIME atime, mtime; |
| 4858 | #else |
| 4859 | int result; |
| 4860 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4861 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4862 | PyObject *return_value = NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4863 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4864 | memset(&path, 0, sizeof(path)); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4865 | path.function_name = "utime"; |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4866 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4867 | #if UTIME_HAVE_FD |
| 4868 | path.allow_fd = 1; |
| 4869 | #endif |
| 4870 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 4871 | "O&|O$OO&p:utime", keywords, |
| 4872 | path_converter, &path, |
| 4873 | ×, &ns, |
| 4874 | #if UTIME_HAVE_DIR_FD |
| 4875 | dir_fd_converter, &dir_fd, |
| 4876 | #else |
| 4877 | dir_fd_unavailable, &dir_fd, |
| 4878 | #endif |
| 4879 | &follow_symlinks |
| 4880 | )) |
| 4881 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4882 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4883 | if (times && (times != Py_None) && ns) { |
| 4884 | PyErr_SetString(PyExc_ValueError, |
| 4885 | "utime: you may specify either 'times'" |
| 4886 | " or 'ns' but not both"); |
| 4887 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4888 | } |
| 4889 | |
| 4890 | if (times && (times != Py_None)) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4891 | time_t a_sec, m_sec; |
| 4892 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4893 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4894 | PyErr_SetString(PyExc_TypeError, |
| 4895 | "utime: 'times' must be either" |
| 4896 | " a tuple of two ints or None"); |
| 4897 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4898 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4899 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4900 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4901 | &a_sec, &a_nsec) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4902 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4903 | &m_sec, &m_nsec) == -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4904 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4905 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4906 | utime.atime_s = a_sec; |
| 4907 | utime.atime_ns = a_nsec; |
| 4908 | utime.mtime_s = m_sec; |
| 4909 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4910 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4911 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4912 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4913 | PyErr_SetString(PyExc_TypeError, |
| 4914 | "utime: 'ns' must be a tuple of two ints"); |
| 4915 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4916 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4917 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4918 | 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] | 4919 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4920 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4921 | &utime.mtime_s, &utime.mtime_ns)) { |
| 4922 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4923 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4924 | } |
| 4925 | else { |
| 4926 | /* times and ns are both None/unspecified. use "now". */ |
| 4927 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4928 | } |
| 4929 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4930 | #if !UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4931 | if (follow_symlinks_specified("utime", follow_symlinks)) |
| 4932 | goto exit; |
| 4933 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4934 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4935 | if (path_and_dir_fd_invalid("utime", &path, dir_fd) || |
| 4936 | dir_fd_and_fd_invalid("utime", dir_fd, path.fd) || |
| 4937 | fd_and_follow_symlinks_invalid("utime", path.fd, follow_symlinks)) |
| 4938 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4939 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4940 | #if !defined(HAVE_UTIMENSAT) |
| 4941 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4942 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4943 | "utime: cannot use dir_fd and follow_symlinks " |
| 4944 | "together on this platform"); |
| 4945 | goto exit; |
| 4946 | } |
| 4947 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4948 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4949 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4950 | Py_BEGIN_ALLOW_THREADS |
| 4951 | if (path.wide) |
| 4952 | hFile = CreateFileW(path.wide, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4953 | NULL, OPEN_EXISTING, |
| 4954 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4955 | else |
| 4956 | hFile = CreateFileA(path.narrow, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4957 | NULL, OPEN_EXISTING, |
| 4958 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4959 | Py_END_ALLOW_THREADS |
| 4960 | if (hFile == INVALID_HANDLE_VALUE) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4961 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4962 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4963 | } |
| 4964 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4965 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 4966 | GetSystemTimeAsFileTime(&mtime); |
| 4967 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4968 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4969 | else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4970 | time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4971 | time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4972 | } |
| 4973 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4974 | /* Avoid putting the file name into the error here, |
| 4975 | as that may confuse the user into believing that |
| 4976 | something is wrong with the file, when it also |
| 4977 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4978 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4979 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4980 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4981 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4982 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4983 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4984 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4985 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 4986 | result = utime_nofollow_symlinks(&utime, path.narrow); |
| 4987 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4988 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4989 | |
| 4990 | #if UTIME_HAVE_DIR_FD |
| 4991 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
| 4992 | result = utime_dir_fd(&utime, dir_fd, path.narrow, follow_symlinks); |
| 4993 | else |
| 4994 | #endif |
| 4995 | |
| 4996 | #if UTIME_HAVE_FD |
| 4997 | if (path.fd != -1) |
| 4998 | result = utime_fd(&utime, path.fd); |
| 4999 | else |
| 5000 | #endif |
| 5001 | |
| 5002 | result = utime_default(&utime, path.narrow); |
| 5003 | |
| 5004 | Py_END_ALLOW_THREADS |
| 5005 | |
| 5006 | if (result < 0) { |
| 5007 | /* see previous comment about not putting filename in error here */ |
| 5008 | return_value = posix_error(); |
| 5009 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5010 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5011 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 5012 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5013 | |
| 5014 | Py_INCREF(Py_None); |
| 5015 | return_value = Py_None; |
| 5016 | |
| 5017 | exit: |
| 5018 | path_cleanup(&path); |
| 5019 | #ifdef MS_WINDOWS |
| 5020 | if (hFile != INVALID_HANDLE_VALUE) |
| 5021 | CloseHandle(hFile); |
| 5022 | #endif |
| 5023 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5024 | } |
| 5025 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5026 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5027 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5028 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5029 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5030 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5031 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5032 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5033 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5034 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5035 | int sts; |
| 5036 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 5037 | return NULL; |
| 5038 | _exit(sts); |
| 5039 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5040 | } |
| 5041 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5042 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 5043 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 5044 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5045 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5046 | Py_ssize_t i; |
| 5047 | for (i = 0; i < count; i++) |
| 5048 | PyMem_Free(array[i]); |
| 5049 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5050 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5051 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 5052 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5053 | int fsconvert_strdup(PyObject *o, char**out) |
| 5054 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5055 | PyObject *bytes; |
| 5056 | Py_ssize_t size; |
| 5057 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 5058 | return 0; |
| 5059 | size = PyBytes_GET_SIZE(bytes); |
| 5060 | *out = PyMem_Malloc(size+1); |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 5061 | if (!*out) { |
| 5062 | PyErr_NoMemory(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5063 | return 0; |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 5064 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5065 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 5066 | Py_DECREF(bytes); |
| 5067 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5068 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5069 | #endif |
| 5070 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5071 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5072 | static char** |
| 5073 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 5074 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5075 | char **envlist; |
| 5076 | Py_ssize_t i, pos, envc; |
| 5077 | PyObject *keys=NULL, *vals=NULL; |
| 5078 | PyObject *key, *val, *key2, *val2; |
| 5079 | char *p, *k, *v; |
| 5080 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5081 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5082 | i = PyMapping_Size(env); |
| 5083 | if (i < 0) |
| 5084 | return NULL; |
| 5085 | envlist = PyMem_NEW(char *, i + 1); |
| 5086 | if (envlist == NULL) { |
| 5087 | PyErr_NoMemory(); |
| 5088 | return NULL; |
| 5089 | } |
| 5090 | envc = 0; |
| 5091 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5092 | if (!keys) |
| 5093 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5094 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5095 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5096 | goto error; |
| 5097 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 5098 | PyErr_Format(PyExc_TypeError, |
| 5099 | "env.keys() or env.values() is not a list"); |
| 5100 | goto error; |
| 5101 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5102 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5103 | for (pos = 0; pos < i; pos++) { |
| 5104 | key = PyList_GetItem(keys, pos); |
| 5105 | val = PyList_GetItem(vals, pos); |
| 5106 | if (!key || !val) |
| 5107 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5108 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5109 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 5110 | goto error; |
| 5111 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 5112 | Py_DECREF(key2); |
| 5113 | goto error; |
| 5114 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5115 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5116 | k = PyBytes_AsString(key2); |
| 5117 | v = PyBytes_AsString(val2); |
| 5118 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5119 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5120 | p = PyMem_NEW(char, len); |
| 5121 | if (p == NULL) { |
| 5122 | PyErr_NoMemory(); |
| 5123 | Py_DECREF(key2); |
| 5124 | Py_DECREF(val2); |
| 5125 | goto error; |
| 5126 | } |
| 5127 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 5128 | envlist[envc++] = p; |
| 5129 | Py_DECREF(key2); |
| 5130 | Py_DECREF(val2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5131 | } |
| 5132 | Py_DECREF(vals); |
| 5133 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5134 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5135 | envlist[envc] = 0; |
| 5136 | *envc_ptr = envc; |
| 5137 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5138 | |
| 5139 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5140 | Py_XDECREF(keys); |
| 5141 | Py_XDECREF(vals); |
| 5142 | while (--envc >= 0) |
| 5143 | PyMem_DEL(envlist[envc]); |
| 5144 | PyMem_DEL(envlist); |
| 5145 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5146 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5147 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5148 | static char** |
| 5149 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 5150 | { |
| 5151 | int i; |
| 5152 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 5153 | if (argvlist == NULL) { |
| 5154 | PyErr_NoMemory(); |
| 5155 | return NULL; |
| 5156 | } |
| 5157 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5158 | PyObject* item = PySequence_ITEM(argv, i); |
| 5159 | if (item == NULL) |
| 5160 | goto fail; |
| 5161 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 5162 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5163 | goto fail; |
| 5164 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5165 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5166 | } |
| 5167 | argvlist[*argc] = NULL; |
| 5168 | return argvlist; |
| 5169 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5170 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5171 | free_string_array(argvlist, *argc); |
| 5172 | return NULL; |
| 5173 | } |
| 5174 | #endif |
| 5175 | |
| 5176 | #ifdef HAVE_EXECV |
| 5177 | PyDoc_STRVAR(posix_execv__doc__, |
| 5178 | "execv(path, args)\n\n\ |
| 5179 | Execute an executable path with arguments, replacing current process.\n\ |
| 5180 | \n\ |
| 5181 | path: path of executable file\n\ |
| 5182 | args: tuple or list of strings"); |
| 5183 | |
| 5184 | static PyObject * |
| 5185 | posix_execv(PyObject *self, PyObject *args) |
| 5186 | { |
| 5187 | PyObject *opath; |
| 5188 | char *path; |
| 5189 | PyObject *argv; |
| 5190 | char **argvlist; |
| 5191 | Py_ssize_t argc; |
| 5192 | |
| 5193 | /* execv has two arguments: (path, argv), where |
| 5194 | argv is a list or tuple of strings. */ |
| 5195 | |
| 5196 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 5197 | PyUnicode_FSConverter, |
| 5198 | &opath, &argv)) |
| 5199 | return NULL; |
| 5200 | path = PyBytes_AsString(opath); |
| 5201 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 5202 | PyErr_SetString(PyExc_TypeError, |
| 5203 | "execv() arg 2 must be a tuple or list"); |
| 5204 | Py_DECREF(opath); |
| 5205 | return NULL; |
| 5206 | } |
| 5207 | argc = PySequence_Size(argv); |
| 5208 | if (argc < 1) { |
| 5209 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 5210 | Py_DECREF(opath); |
| 5211 | return NULL; |
| 5212 | } |
| 5213 | |
| 5214 | argvlist = parse_arglist(argv, &argc); |
| 5215 | if (argvlist == NULL) { |
| 5216 | Py_DECREF(opath); |
| 5217 | return NULL; |
| 5218 | } |
| 5219 | |
| 5220 | execv(path, argvlist); |
| 5221 | |
| 5222 | /* If we get here it's definitely an error */ |
| 5223 | |
| 5224 | free_string_array(argvlist, argc); |
| 5225 | Py_DECREF(opath); |
| 5226 | return posix_error(); |
| 5227 | } |
| 5228 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5229 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5230 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5231 | Execute a path with arguments and environment, replacing current process.\n\ |
| 5232 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5233 | path: path of executable file\n\ |
| 5234 | args: tuple or list of arguments\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5235 | env: dictionary of strings mapping to strings\n\ |
| 5236 | \n\ |
| 5237 | On some platforms, you may specify an open file descriptor for path;\n\ |
| 5238 | execve will execute the program the file descriptor is open to.\n\ |
| 5239 | If this functionality is unavailable, using it raises NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5240 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5241 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5242 | posix_execve(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5243 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5244 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5245 | PyObject *argv, *env; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5246 | char **argvlist = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5247 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5248 | Py_ssize_t argc, envc; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5249 | static char *keywords[] = {"path", "argv", "environment", NULL}; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5250 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5251 | /* execve has three arguments: (path, argv, env), where |
| 5252 | argv is a list or tuple of strings and env is a dictionary |
| 5253 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5254 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5255 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 5256 | path.function_name = "execve"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5257 | #ifdef HAVE_FEXECVE |
| 5258 | path.allow_fd = 1; |
| 5259 | #endif |
| 5260 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&OO:execve", keywords, |
| 5261 | path_converter, &path, |
| 5262 | &argv, &env |
| 5263 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5264 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5265 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5266 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5267 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5268 | "execve: argv must be a tuple or list"); |
| 5269 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5270 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5271 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5272 | if (!PyMapping_Check(env)) { |
| 5273 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5274 | "execve: environment must be a mapping object"); |
| 5275 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5276 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5277 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5278 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5279 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5280 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5281 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5282 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5283 | envlist = parse_envlist(env, &envc); |
| 5284 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5285 | goto fail; |
| 5286 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5287 | #ifdef HAVE_FEXECVE |
| 5288 | if (path.fd > -1) |
| 5289 | fexecve(path.fd, argvlist, envlist); |
| 5290 | else |
| 5291 | #endif |
| 5292 | execve(path.narrow, argvlist, envlist); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5293 | |
| 5294 | /* If we get here it's definitely an error */ |
| 5295 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 5296 | path_error(&path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5297 | |
| 5298 | while (--envc >= 0) |
| 5299 | PyMem_DEL(envlist[envc]); |
| 5300 | PyMem_DEL(envlist); |
| 5301 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5302 | if (argvlist) |
| 5303 | free_string_array(argvlist, argc); |
| 5304 | path_cleanup(&path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5305 | return NULL; |
| 5306 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5307 | #endif /* HAVE_EXECV */ |
| 5308 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5309 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5310 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5311 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5312 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5313 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5314 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5315 | mode: mode of process creation\n\ |
| 5316 | path: path of executable file\n\ |
| 5317 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5318 | |
| 5319 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5320 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5321 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5322 | PyObject *opath; |
| 5323 | char *path; |
| 5324 | PyObject *argv; |
| 5325 | char **argvlist; |
| 5326 | int mode, i; |
| 5327 | Py_ssize_t argc; |
| 5328 | Py_intptr_t spawnval; |
| 5329 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5330 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5331 | /* spawnv has three arguments: (mode, path, argv), where |
| 5332 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5333 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5334 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 5335 | PyUnicode_FSConverter, |
| 5336 | &opath, &argv)) |
| 5337 | return NULL; |
| 5338 | path = PyBytes_AsString(opath); |
| 5339 | if (PyList_Check(argv)) { |
| 5340 | argc = PyList_Size(argv); |
| 5341 | getitem = PyList_GetItem; |
| 5342 | } |
| 5343 | else if (PyTuple_Check(argv)) { |
| 5344 | argc = PyTuple_Size(argv); |
| 5345 | getitem = PyTuple_GetItem; |
| 5346 | } |
| 5347 | else { |
| 5348 | PyErr_SetString(PyExc_TypeError, |
| 5349 | "spawnv() arg 2 must be a tuple or list"); |
| 5350 | Py_DECREF(opath); |
| 5351 | return NULL; |
| 5352 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5353 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5354 | argvlist = PyMem_NEW(char *, argc+1); |
| 5355 | if (argvlist == NULL) { |
| 5356 | Py_DECREF(opath); |
| 5357 | return PyErr_NoMemory(); |
| 5358 | } |
| 5359 | for (i = 0; i < argc; i++) { |
| 5360 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5361 | &argvlist[i])) { |
| 5362 | free_string_array(argvlist, i); |
| 5363 | PyErr_SetString( |
| 5364 | PyExc_TypeError, |
| 5365 | "spawnv() arg 2 must contain only strings"); |
| 5366 | Py_DECREF(opath); |
| 5367 | return NULL; |
| 5368 | } |
| 5369 | } |
| 5370 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5371 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5372 | if (mode == _OLD_P_OVERLAY) |
| 5373 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5374 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5375 | Py_BEGIN_ALLOW_THREADS |
| 5376 | spawnval = _spawnv(mode, path, argvlist); |
| 5377 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5378 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5379 | free_string_array(argvlist, argc); |
| 5380 | Py_DECREF(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5381 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5382 | if (spawnval == -1) |
| 5383 | return posix_error(); |
| 5384 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5385 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5386 | } |
| 5387 | |
| 5388 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5389 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5390 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5391 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5392 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5393 | mode: mode of process creation\n\ |
| 5394 | path: path of executable file\n\ |
| 5395 | args: tuple or list of arguments\n\ |
| 5396 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5397 | |
| 5398 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5399 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5400 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5401 | PyObject *opath; |
| 5402 | char *path; |
| 5403 | PyObject *argv, *env; |
| 5404 | char **argvlist; |
| 5405 | char **envlist; |
| 5406 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5407 | int mode; |
| 5408 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5409 | Py_intptr_t spawnval; |
| 5410 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 5411 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5412 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5413 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5414 | argv is a list or tuple of strings and env is a dictionary |
| 5415 | like posix.environ. */ |
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 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 5418 | PyUnicode_FSConverter, |
| 5419 | &opath, &argv, &env)) |
| 5420 | return NULL; |
| 5421 | path = PyBytes_AsString(opath); |
| 5422 | if (PyList_Check(argv)) { |
| 5423 | argc = PyList_Size(argv); |
| 5424 | getitem = PyList_GetItem; |
| 5425 | } |
| 5426 | else if (PyTuple_Check(argv)) { |
| 5427 | argc = PyTuple_Size(argv); |
| 5428 | getitem = PyTuple_GetItem; |
| 5429 | } |
| 5430 | else { |
| 5431 | PyErr_SetString(PyExc_TypeError, |
| 5432 | "spawnve() arg 2 must be a tuple or list"); |
| 5433 | goto fail_0; |
| 5434 | } |
| 5435 | if (!PyMapping_Check(env)) { |
| 5436 | PyErr_SetString(PyExc_TypeError, |
| 5437 | "spawnve() arg 3 must be a mapping object"); |
| 5438 | goto fail_0; |
| 5439 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5440 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5441 | argvlist = PyMem_NEW(char *, argc+1); |
| 5442 | if (argvlist == NULL) { |
| 5443 | PyErr_NoMemory(); |
| 5444 | goto fail_0; |
| 5445 | } |
| 5446 | for (i = 0; i < argc; i++) { |
| 5447 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5448 | &argvlist[i])) |
| 5449 | { |
| 5450 | lastarg = i; |
| 5451 | goto fail_1; |
| 5452 | } |
| 5453 | } |
| 5454 | lastarg = argc; |
| 5455 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5456 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5457 | envlist = parse_envlist(env, &envc); |
| 5458 | if (envlist == NULL) |
| 5459 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5460 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5461 | if (mode == _OLD_P_OVERLAY) |
| 5462 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5463 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5464 | Py_BEGIN_ALLOW_THREADS |
| 5465 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 5466 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5467 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5468 | if (spawnval == -1) |
| 5469 | (void) posix_error(); |
| 5470 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5471 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5472 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5473 | while (--envc >= 0) |
| 5474 | PyMem_DEL(envlist[envc]); |
| 5475 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5476 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5477 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5478 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5479 | Py_DECREF(opath); |
| 5480 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5481 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5482 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5483 | #endif /* HAVE_SPAWNV */ |
| 5484 | |
| 5485 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5486 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5487 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5488 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5489 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 5490 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5491 | 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] | 5492 | |
| 5493 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5494 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5495 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5496 | pid_t pid; |
| 5497 | int result = 0; |
| 5498 | _PyImport_AcquireLock(); |
| 5499 | pid = fork1(); |
| 5500 | if (pid == 0) { |
| 5501 | /* child: this clobbers and resets the import lock. */ |
| 5502 | PyOS_AfterFork(); |
| 5503 | } else { |
| 5504 | /* parent: release the import lock. */ |
| 5505 | result = _PyImport_ReleaseLock(); |
| 5506 | } |
| 5507 | if (pid == -1) |
| 5508 | return posix_error(); |
| 5509 | if (result < 0) { |
| 5510 | /* Don't clobber the OSError if the fork failed. */ |
| 5511 | PyErr_SetString(PyExc_RuntimeError, |
| 5512 | "not holding the import lock"); |
| 5513 | return NULL; |
| 5514 | } |
| 5515 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5516 | } |
| 5517 | #endif |
| 5518 | |
| 5519 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5520 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5521 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5522 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5523 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5524 | 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] | 5525 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5526 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5527 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5528 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5529 | pid_t pid; |
| 5530 | int result = 0; |
| 5531 | _PyImport_AcquireLock(); |
| 5532 | pid = fork(); |
| 5533 | if (pid == 0) { |
| 5534 | /* child: this clobbers and resets the import lock. */ |
| 5535 | PyOS_AfterFork(); |
| 5536 | } else { |
| 5537 | /* parent: release the import lock. */ |
| 5538 | result = _PyImport_ReleaseLock(); |
| 5539 | } |
| 5540 | if (pid == -1) |
| 5541 | return posix_error(); |
| 5542 | if (result < 0) { |
| 5543 | /* Don't clobber the OSError if the fork failed. */ |
| 5544 | PyErr_SetString(PyExc_RuntimeError, |
| 5545 | "not holding the import lock"); |
| 5546 | return NULL; |
| 5547 | } |
| 5548 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5549 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5550 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5551 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5552 | #ifdef HAVE_SCHED_H |
| 5553 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5554 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
| 5555 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5556 | PyDoc_STRVAR(posix_sched_get_priority_max__doc__, |
| 5557 | "sched_get_priority_max(policy)\n\n\ |
| 5558 | Get the maximum scheduling priority for *policy*."); |
| 5559 | |
| 5560 | static PyObject * |
| 5561 | posix_sched_get_priority_max(PyObject *self, PyObject *args) |
| 5562 | { |
| 5563 | int policy, max; |
| 5564 | |
| 5565 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy)) |
| 5566 | return NULL; |
| 5567 | max = sched_get_priority_max(policy); |
| 5568 | if (max < 0) |
| 5569 | return posix_error(); |
| 5570 | return PyLong_FromLong(max); |
| 5571 | } |
| 5572 | |
| 5573 | PyDoc_STRVAR(posix_sched_get_priority_min__doc__, |
| 5574 | "sched_get_priority_min(policy)\n\n\ |
| 5575 | Get the minimum scheduling priority for *policy*."); |
| 5576 | |
| 5577 | static PyObject * |
| 5578 | posix_sched_get_priority_min(PyObject *self, PyObject *args) |
| 5579 | { |
| 5580 | int policy, min; |
| 5581 | |
| 5582 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy)) |
| 5583 | return NULL; |
| 5584 | min = sched_get_priority_min(policy); |
| 5585 | if (min < 0) |
| 5586 | return posix_error(); |
| 5587 | return PyLong_FromLong(min); |
| 5588 | } |
| 5589 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5590 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 5591 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5592 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5593 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5594 | PyDoc_STRVAR(posix_sched_getscheduler__doc__, |
| 5595 | "sched_getscheduler(pid)\n\n\ |
| 5596 | Get the scheduling policy for the process with a PID of *pid*.\n\ |
| 5597 | Passing a PID of 0 returns the scheduling policy for the calling process."); |
| 5598 | |
| 5599 | static PyObject * |
| 5600 | posix_sched_getscheduler(PyObject *self, PyObject *args) |
| 5601 | { |
| 5602 | pid_t pid; |
| 5603 | int policy; |
| 5604 | |
| 5605 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid)) |
| 5606 | return NULL; |
| 5607 | policy = sched_getscheduler(pid); |
| 5608 | if (policy < 0) |
| 5609 | return posix_error(); |
| 5610 | return PyLong_FromLong(policy); |
| 5611 | } |
| 5612 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5613 | #endif |
| 5614 | |
| 5615 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
| 5616 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5617 | static PyObject * |
| 5618 | sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 5619 | { |
| 5620 | PyObject *res, *priority; |
Benjamin Peterson | 4e36d5a | 2011-08-02 19:56:11 -0500 | [diff] [blame] | 5621 | static char *kwlist[] = {"sched_priority", NULL}; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5622 | |
| 5623 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority)) |
| 5624 | return NULL; |
| 5625 | res = PyStructSequence_New(type); |
| 5626 | if (!res) |
| 5627 | return NULL; |
| 5628 | Py_INCREF(priority); |
| 5629 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5630 | return res; |
| 5631 | } |
| 5632 | |
| 5633 | PyDoc_STRVAR(sched_param__doc__, |
| 5634 | "sched_param(sched_priority): A scheduling parameter.\n\n\ |
| 5635 | Current has only one field: sched_priority"); |
| 5636 | |
| 5637 | static PyStructSequence_Field sched_param_fields[] = { |
| 5638 | {"sched_priority", "the scheduling priority"}, |
| 5639 | {0} |
| 5640 | }; |
| 5641 | |
| 5642 | static PyStructSequence_Desc sched_param_desc = { |
| 5643 | "sched_param", /* name */ |
| 5644 | sched_param__doc__, /* doc */ |
| 5645 | sched_param_fields, |
| 5646 | 1 |
| 5647 | }; |
| 5648 | |
| 5649 | static int |
| 5650 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 5651 | { |
| 5652 | long priority; |
| 5653 | |
| 5654 | if (Py_TYPE(param) != &SchedParamType) { |
| 5655 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 5656 | return 0; |
| 5657 | } |
| 5658 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 5659 | if (priority == -1 && PyErr_Occurred()) |
| 5660 | return 0; |
| 5661 | if (priority > INT_MAX || priority < INT_MIN) { |
| 5662 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 5663 | return 0; |
| 5664 | } |
| 5665 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 5666 | return 1; |
| 5667 | } |
| 5668 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5669 | #endif |
| 5670 | |
| 5671 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5672 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5673 | PyDoc_STRVAR(posix_sched_setscheduler__doc__, |
| 5674 | "sched_setscheduler(pid, policy, param)\n\n\ |
| 5675 | Set the scheduling policy, *policy*, for *pid*.\n\ |
| 5676 | If *pid* is 0, the calling process is changed.\n\ |
| 5677 | *param* is an instance of sched_param."); |
| 5678 | |
| 5679 | static PyObject * |
| 5680 | posix_sched_setscheduler(PyObject *self, PyObject *args) |
| 5681 | { |
| 5682 | pid_t pid; |
| 5683 | int policy; |
| 5684 | struct sched_param param; |
| 5685 | |
| 5686 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler", |
| 5687 | &pid, &policy, &convert_sched_param, ¶m)) |
| 5688 | return NULL; |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5689 | |
| 5690 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 5691 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 5692 | ** scheduling policy under Solaris/Illumos, and others. |
| 5693 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5694 | */ |
| 5695 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5696 | return posix_error(); |
| 5697 | Py_RETURN_NONE; |
| 5698 | } |
| 5699 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5700 | #endif |
| 5701 | |
| 5702 | #ifdef HAVE_SCHED_SETPARAM |
| 5703 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5704 | PyDoc_STRVAR(posix_sched_getparam__doc__, |
| 5705 | "sched_getparam(pid) -> sched_param\n\n\ |
| 5706 | Returns scheduling parameters for the process with *pid* as an instance of the\n\ |
| 5707 | sched_param class. A PID of 0 means the calling process."); |
| 5708 | |
| 5709 | static PyObject * |
| 5710 | posix_sched_getparam(PyObject *self, PyObject *args) |
| 5711 | { |
| 5712 | pid_t pid; |
| 5713 | struct sched_param param; |
| 5714 | PyObject *res, *priority; |
| 5715 | |
| 5716 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid)) |
| 5717 | return NULL; |
| 5718 | if (sched_getparam(pid, ¶m)) |
| 5719 | return posix_error(); |
| 5720 | res = PyStructSequence_New(&SchedParamType); |
| 5721 | if (!res) |
| 5722 | return NULL; |
| 5723 | priority = PyLong_FromLong(param.sched_priority); |
| 5724 | if (!priority) { |
| 5725 | Py_DECREF(res); |
| 5726 | return NULL; |
| 5727 | } |
| 5728 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5729 | return res; |
| 5730 | } |
| 5731 | |
| 5732 | PyDoc_STRVAR(posix_sched_setparam__doc__, |
| 5733 | "sched_setparam(pid, param)\n\n\ |
| 5734 | Set scheduling parameters for a process with PID *pid*.\n\ |
| 5735 | A PID of 0 means the calling process."); |
| 5736 | |
| 5737 | static PyObject * |
| 5738 | posix_sched_setparam(PyObject *self, PyObject *args) |
| 5739 | { |
| 5740 | pid_t pid; |
| 5741 | struct sched_param param; |
| 5742 | |
| 5743 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam", |
| 5744 | &pid, &convert_sched_param, ¶m)) |
| 5745 | return NULL; |
| 5746 | if (sched_setparam(pid, ¶m)) |
| 5747 | return posix_error(); |
| 5748 | Py_RETURN_NONE; |
| 5749 | } |
| 5750 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5751 | #endif |
| 5752 | |
| 5753 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
| 5754 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5755 | PyDoc_STRVAR(posix_sched_rr_get_interval__doc__, |
| 5756 | "sched_rr_get_interval(pid) -> float\n\n\ |
| 5757 | Return the round-robin quantum for the process with PID *pid* in seconds."); |
| 5758 | |
| 5759 | static PyObject * |
| 5760 | posix_sched_rr_get_interval(PyObject *self, PyObject *args) |
| 5761 | { |
| 5762 | pid_t pid; |
| 5763 | struct timespec interval; |
| 5764 | |
| 5765 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid)) |
| 5766 | return NULL; |
| 5767 | if (sched_rr_get_interval(pid, &interval)) |
| 5768 | return posix_error(); |
| 5769 | return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec); |
| 5770 | } |
| 5771 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5772 | #endif |
| 5773 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5774 | PyDoc_STRVAR(posix_sched_yield__doc__, |
| 5775 | "sched_yield()\n\n\ |
| 5776 | Voluntarily relinquish the CPU."); |
| 5777 | |
| 5778 | static PyObject * |
| 5779 | posix_sched_yield(PyObject *self, PyObject *noargs) |
| 5780 | { |
| 5781 | if (sched_yield()) |
| 5782 | return posix_error(); |
| 5783 | Py_RETURN_NONE; |
| 5784 | } |
| 5785 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5786 | #ifdef HAVE_SCHED_SETAFFINITY |
| 5787 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5788 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 5789 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5790 | |
| 5791 | PyDoc_STRVAR(posix_sched_setaffinity__doc__, |
| 5792 | "sched_setaffinity(pid, cpu_set)\n\n\ |
| 5793 | Set the affinity of the process with PID *pid* to *cpu_set*."); |
| 5794 | |
| 5795 | static PyObject * |
| 5796 | posix_sched_setaffinity(PyObject *self, PyObject *args) |
| 5797 | { |
| 5798 | pid_t pid; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5799 | int ncpus; |
| 5800 | size_t setsize; |
| 5801 | cpu_set_t *mask = NULL; |
| 5802 | PyObject *iterable, *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5803 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5804 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O:sched_setaffinity", |
| 5805 | &pid, &iterable)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5806 | return NULL; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5807 | |
| 5808 | iterator = PyObject_GetIter(iterable); |
| 5809 | if (iterator == NULL) |
| 5810 | return NULL; |
| 5811 | |
| 5812 | ncpus = NCPUS_START; |
| 5813 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5814 | mask = CPU_ALLOC(ncpus); |
| 5815 | if (mask == NULL) { |
| 5816 | PyErr_NoMemory(); |
| 5817 | goto error; |
| 5818 | } |
| 5819 | CPU_ZERO_S(setsize, mask); |
| 5820 | |
| 5821 | while ((item = PyIter_Next(iterator))) { |
| 5822 | long cpu; |
| 5823 | if (!PyLong_Check(item)) { |
| 5824 | PyErr_Format(PyExc_TypeError, |
| 5825 | "expected an iterator of ints, " |
| 5826 | "but iterator yielded %R", |
| 5827 | Py_TYPE(item)); |
| 5828 | Py_DECREF(item); |
| 5829 | goto error; |
| 5830 | } |
| 5831 | cpu = PyLong_AsLong(item); |
| 5832 | Py_DECREF(item); |
| 5833 | if (cpu < 0) { |
| 5834 | if (!PyErr_Occurred()) |
| 5835 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 5836 | goto error; |
| 5837 | } |
| 5838 | if (cpu > INT_MAX - 1) { |
| 5839 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 5840 | goto error; |
| 5841 | } |
| 5842 | if (cpu >= ncpus) { |
| 5843 | /* Grow CPU mask to fit the CPU number */ |
| 5844 | int newncpus = ncpus; |
| 5845 | cpu_set_t *newmask; |
| 5846 | size_t newsetsize; |
| 5847 | while (newncpus <= cpu) { |
| 5848 | if (newncpus > INT_MAX / 2) |
| 5849 | newncpus = cpu + 1; |
| 5850 | else |
| 5851 | newncpus = newncpus * 2; |
| 5852 | } |
| 5853 | newmask = CPU_ALLOC(newncpus); |
| 5854 | if (newmask == NULL) { |
| 5855 | PyErr_NoMemory(); |
| 5856 | goto error; |
| 5857 | } |
| 5858 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 5859 | CPU_ZERO_S(newsetsize, newmask); |
| 5860 | memcpy(newmask, mask, setsize); |
| 5861 | CPU_FREE(mask); |
| 5862 | setsize = newsetsize; |
| 5863 | mask = newmask; |
| 5864 | ncpus = newncpus; |
| 5865 | } |
| 5866 | CPU_SET_S(cpu, setsize, mask); |
| 5867 | } |
| 5868 | Py_CLEAR(iterator); |
| 5869 | |
| 5870 | if (sched_setaffinity(pid, setsize, mask)) { |
| 5871 | posix_error(); |
| 5872 | goto error; |
| 5873 | } |
| 5874 | CPU_FREE(mask); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5875 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5876 | |
| 5877 | error: |
| 5878 | if (mask) |
| 5879 | CPU_FREE(mask); |
| 5880 | Py_XDECREF(iterator); |
| 5881 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5882 | } |
| 5883 | |
| 5884 | PyDoc_STRVAR(posix_sched_getaffinity__doc__, |
| 5885 | "sched_getaffinity(pid, ncpus) -> cpu_set\n\n\ |
| 5886 | Return the affinity of the process with PID *pid*.\n\ |
| 5887 | The returned cpu_set will be of size *ncpus*."); |
| 5888 | |
| 5889 | static PyObject * |
| 5890 | posix_sched_getaffinity(PyObject *self, PyObject *args) |
| 5891 | { |
| 5892 | pid_t pid; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5893 | int cpu, ncpus, count; |
| 5894 | size_t setsize; |
| 5895 | cpu_set_t *mask = NULL; |
| 5896 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5897 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5898 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getaffinity", |
| 5899 | &pid)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5900 | return NULL; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5901 | |
| 5902 | ncpus = NCPUS_START; |
| 5903 | while (1) { |
| 5904 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5905 | mask = CPU_ALLOC(ncpus); |
| 5906 | if (mask == NULL) |
| 5907 | return PyErr_NoMemory(); |
| 5908 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 5909 | break; |
| 5910 | CPU_FREE(mask); |
| 5911 | if (errno != EINVAL) |
| 5912 | return posix_error(); |
| 5913 | if (ncpus > INT_MAX / 2) { |
| 5914 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 5915 | "a large enough CPU set"); |
| 5916 | return NULL; |
| 5917 | } |
| 5918 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5919 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5920 | |
| 5921 | res = PySet_New(NULL); |
| 5922 | if (res == NULL) |
| 5923 | goto error; |
| 5924 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 5925 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 5926 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 5927 | --count; |
| 5928 | if (cpu_num == NULL) |
| 5929 | goto error; |
| 5930 | if (PySet_Add(res, cpu_num)) { |
| 5931 | Py_DECREF(cpu_num); |
| 5932 | goto error; |
| 5933 | } |
| 5934 | Py_DECREF(cpu_num); |
| 5935 | } |
| 5936 | } |
| 5937 | CPU_FREE(mask); |
| 5938 | return res; |
| 5939 | |
| 5940 | error: |
| 5941 | if (mask) |
| 5942 | CPU_FREE(mask); |
| 5943 | Py_XDECREF(res); |
| 5944 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5945 | } |
| 5946 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5947 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5948 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5949 | #endif /* HAVE_SCHED_H */ |
| 5950 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5951 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5952 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5953 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5954 | #define DEV_PTY_FILE "/dev/ptc" |
| 5955 | #define HAVE_DEV_PTMX |
| 5956 | #else |
| 5957 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5958 | #endif |
| 5959 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5960 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5961 | #ifdef HAVE_PTY_H |
| 5962 | #include <pty.h> |
| 5963 | #else |
| 5964 | #ifdef HAVE_LIBUTIL_H |
| 5965 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5966 | #else |
| 5967 | #ifdef HAVE_UTIL_H |
| 5968 | #include <util.h> |
| 5969 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5970 | #endif /* HAVE_LIBUTIL_H */ |
| 5971 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5972 | #ifdef HAVE_STROPTS_H |
| 5973 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5974 | #endif |
| 5975 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5976 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5977 | #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] | 5978 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5979 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5980 | 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] | 5981 | |
| 5982 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5983 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5984 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5985 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5986 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5987 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5988 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5989 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5990 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5991 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5992 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5993 | #endif |
| 5994 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5995 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5996 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5997 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5998 | goto posix_error; |
| 5999 | |
| 6000 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6001 | goto error; |
| 6002 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 6003 | goto error; |
| 6004 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6005 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6006 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 6007 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6008 | goto posix_error; |
| 6009 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6010 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6011 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6012 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6013 | if (slave_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6014 | goto posix_error; |
| 6015 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6016 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6017 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6018 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6019 | goto posix_error; |
| 6020 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6021 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6022 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6023 | /* change permission of slave */ |
| 6024 | if (grantpt(master_fd) < 0) { |
| 6025 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6026 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6027 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6028 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6029 | /* unlock slave */ |
| 6030 | if (unlockpt(master_fd) < 0) { |
| 6031 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6032 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6033 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6034 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6035 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6036 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6037 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 6038 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6039 | goto posix_error; |
| 6040 | |
| 6041 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6042 | if (slave_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6043 | goto posix_error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6044 | |
| 6045 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6046 | goto posix_error; |
| 6047 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6048 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6049 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 6050 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6051 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6052 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6053 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6054 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 6055 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6056 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6057 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6058 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6059 | posix_error: |
| 6060 | posix_error(); |
Victor Stinner | b9981ba | 2013-08-28 01:51:06 +0200 | [diff] [blame] | 6061 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6062 | error: |
Victor Stinner | b9981ba | 2013-08-28 01:51:06 +0200 | [diff] [blame] | 6063 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6064 | if (master_fd != -1) |
| 6065 | close(master_fd); |
| 6066 | if (slave_fd != -1) |
| 6067 | close(slave_fd); |
| 6068 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6069 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6070 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6071 | |
| 6072 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6073 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6074 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6075 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 6076 | 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] | 6077 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6078 | |
| 6079 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6080 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6081 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6082 | int master_fd = -1, result = 0; |
| 6083 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6084 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6085 | _PyImport_AcquireLock(); |
| 6086 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 6087 | if (pid == 0) { |
| 6088 | /* child: this clobbers and resets the import lock. */ |
| 6089 | PyOS_AfterFork(); |
| 6090 | } else { |
| 6091 | /* parent: release the import lock. */ |
| 6092 | result = _PyImport_ReleaseLock(); |
| 6093 | } |
| 6094 | if (pid == -1) |
| 6095 | return posix_error(); |
| 6096 | if (result < 0) { |
| 6097 | /* Don't clobber the OSError if the fork failed. */ |
| 6098 | PyErr_SetString(PyExc_RuntimeError, |
| 6099 | "not holding the import lock"); |
| 6100 | return NULL; |
| 6101 | } |
| 6102 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6103 | } |
| 6104 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6105 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6106 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6107 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6108 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6109 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6110 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6111 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6112 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6113 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6114 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6115 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6116 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6117 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6118 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6119 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6120 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6121 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6122 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6123 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6124 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6125 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6126 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6127 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6128 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6129 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6130 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6131 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6132 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6133 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6134 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6135 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6136 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6137 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6138 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6139 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6140 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6141 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6142 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6143 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6144 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6145 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6146 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6147 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6148 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6149 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6150 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6151 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6152 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6153 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6154 | } |
| 6155 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6156 | #ifdef HAVE_GETGROUPLIST |
| 6157 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 6158 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 6159 | Returns a list of groups to which a user belongs.\n\n\ |
| 6160 | user: username to lookup\n\ |
| 6161 | group: base group id of the user"); |
| 6162 | |
| 6163 | static PyObject * |
| 6164 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 6165 | { |
| 6166 | #ifdef NGROUPS_MAX |
| 6167 | #define MAX_GROUPS NGROUPS_MAX |
| 6168 | #else |
| 6169 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6170 | #define MAX_GROUPS 64 |
| 6171 | #endif |
| 6172 | |
| 6173 | const char *user; |
| 6174 | int i, ngroups; |
| 6175 | PyObject *list; |
| 6176 | #ifdef __APPLE__ |
| 6177 | int *groups, basegid; |
| 6178 | #else |
| 6179 | gid_t *groups, basegid; |
| 6180 | #endif |
| 6181 | ngroups = MAX_GROUPS; |
| 6182 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6183 | #ifdef __APPLE__ |
| 6184 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6185 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6186 | #else |
| 6187 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 6188 | _Py_Gid_Converter, &basegid)) |
| 6189 | return NULL; |
| 6190 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6191 | |
| 6192 | #ifdef __APPLE__ |
| 6193 | groups = PyMem_Malloc(ngroups * sizeof(int)); |
| 6194 | #else |
| 6195 | groups = PyMem_Malloc(ngroups * sizeof(gid_t)); |
| 6196 | #endif |
| 6197 | if (groups == NULL) |
| 6198 | return PyErr_NoMemory(); |
| 6199 | |
| 6200 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 6201 | PyMem_Del(groups); |
| 6202 | return posix_error(); |
| 6203 | } |
| 6204 | |
| 6205 | list = PyList_New(ngroups); |
| 6206 | if (list == NULL) { |
| 6207 | PyMem_Del(groups); |
| 6208 | return NULL; |
| 6209 | } |
| 6210 | |
| 6211 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6212 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6213 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6214 | #else |
| 6215 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 6216 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6217 | if (o == NULL) { |
| 6218 | Py_DECREF(list); |
| 6219 | PyMem_Del(groups); |
| 6220 | return NULL; |
| 6221 | } |
| 6222 | PyList_SET_ITEM(list, i, o); |
| 6223 | } |
| 6224 | |
| 6225 | PyMem_Del(groups); |
| 6226 | |
| 6227 | return list; |
| 6228 | } |
| 6229 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6230 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6231 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6232 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6233 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6234 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6235 | |
| 6236 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6237 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6238 | { |
| 6239 | PyObject *result = NULL; |
| 6240 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6241 | #ifdef NGROUPS_MAX |
| 6242 | #define MAX_GROUPS NGROUPS_MAX |
| 6243 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6244 | /* 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] | 6245 | #define MAX_GROUPS 64 |
| 6246 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6247 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6248 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6249 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6250 | * This is a helper variable to store the intermediate result when |
| 6251 | * that happens. |
| 6252 | * |
| 6253 | * To keep the code readable the OSX behaviour is unconditional, |
| 6254 | * according to the POSIX spec this should be safe on all unix-y |
| 6255 | * systems. |
| 6256 | */ |
| 6257 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6258 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6259 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6260 | #ifdef __APPLE__ |
| 6261 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 6262 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 6263 | * always first call getgroups with length 0 to get the actual number |
| 6264 | * of groups. |
| 6265 | */ |
| 6266 | n = getgroups(0, NULL); |
| 6267 | if (n < 0) { |
| 6268 | return posix_error(); |
| 6269 | } else if (n <= MAX_GROUPS) { |
| 6270 | /* groups will fit in existing array */ |
| 6271 | alt_grouplist = grouplist; |
| 6272 | } else { |
| 6273 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 6274 | if (alt_grouplist == NULL) { |
| 6275 | errno = EINVAL; |
| 6276 | return posix_error(); |
| 6277 | } |
| 6278 | } |
| 6279 | |
| 6280 | n = getgroups(n, alt_grouplist); |
| 6281 | if (n == -1) { |
| 6282 | if (alt_grouplist != grouplist) { |
| 6283 | PyMem_Free(alt_grouplist); |
| 6284 | } |
| 6285 | return posix_error(); |
| 6286 | } |
| 6287 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6288 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6289 | if (n < 0) { |
| 6290 | if (errno == EINVAL) { |
| 6291 | n = getgroups(0, NULL); |
| 6292 | if (n == -1) { |
| 6293 | return posix_error(); |
| 6294 | } |
| 6295 | if (n == 0) { |
| 6296 | /* Avoid malloc(0) */ |
| 6297 | alt_grouplist = grouplist; |
| 6298 | } else { |
| 6299 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 6300 | if (alt_grouplist == NULL) { |
| 6301 | errno = EINVAL; |
| 6302 | return posix_error(); |
| 6303 | } |
| 6304 | n = getgroups(n, alt_grouplist); |
| 6305 | if (n == -1) { |
| 6306 | PyMem_Free(alt_grouplist); |
| 6307 | return posix_error(); |
| 6308 | } |
| 6309 | } |
| 6310 | } else { |
| 6311 | return posix_error(); |
| 6312 | } |
| 6313 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6314 | #endif |
| 6315 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6316 | result = PyList_New(n); |
| 6317 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6318 | int i; |
| 6319 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6320 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6321 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6322 | Py_DECREF(result); |
| 6323 | result = NULL; |
| 6324 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6325 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6326 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6327 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6328 | } |
| 6329 | |
| 6330 | if (alt_grouplist != grouplist) { |
| 6331 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6332 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6333 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6334 | return result; |
| 6335 | } |
| 6336 | #endif |
| 6337 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6338 | #ifdef HAVE_INITGROUPS |
| 6339 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 6340 | "initgroups(username, gid) -> None\n\n\ |
| 6341 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 6342 | the groups of which the specified username is a member, plus the specified\n\ |
| 6343 | group id."); |
| 6344 | |
| 6345 | static PyObject * |
| 6346 | posix_initgroups(PyObject *self, PyObject *args) |
| 6347 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6348 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6349 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6350 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6351 | #ifdef __APPLE__ |
| 6352 | int gid; |
| 6353 | #else |
| 6354 | gid_t gid; |
| 6355 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6356 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6357 | #ifdef __APPLE__ |
| 6358 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 6359 | PyUnicode_FSConverter, &oname, |
| 6360 | &gid)) |
| 6361 | #else |
| 6362 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 6363 | PyUnicode_FSConverter, &oname, |
| 6364 | _Py_Gid_Converter, &gid)) |
| 6365 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6366 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6367 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6368 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6369 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6370 | Py_DECREF(oname); |
| 6371 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6372 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6373 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6374 | Py_INCREF(Py_None); |
| 6375 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6376 | } |
| 6377 | #endif |
| 6378 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6379 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 6380 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6381 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 6382 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6383 | |
| 6384 | static PyObject * |
| 6385 | posix_getpgid(PyObject *self, PyObject *args) |
| 6386 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6387 | pid_t pid, pgid; |
| 6388 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 6389 | return NULL; |
| 6390 | pgid = getpgid(pid); |
| 6391 | if (pgid < 0) |
| 6392 | return posix_error(); |
| 6393 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6394 | } |
| 6395 | #endif /* HAVE_GETPGID */ |
| 6396 | |
| 6397 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6398 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6399 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6400 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6401 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6402 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6403 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6404 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6405 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6406 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6407 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6408 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6409 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6410 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6411 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6412 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6413 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6414 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6415 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6416 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6417 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 6418 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6419 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6420 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6421 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6422 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6423 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6424 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6425 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6426 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6427 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6428 | return posix_error(); |
| 6429 | Py_INCREF(Py_None); |
| 6430 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6431 | } |
| 6432 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6433 | #endif /* HAVE_SETPGRP */ |
| 6434 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6435 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6436 | |
| 6437 | #ifdef MS_WINDOWS |
| 6438 | #include <tlhelp32.h> |
| 6439 | |
| 6440 | static PyObject* |
| 6441 | win32_getppid() |
| 6442 | { |
| 6443 | HANDLE snapshot; |
| 6444 | pid_t mypid; |
| 6445 | PyObject* result = NULL; |
| 6446 | BOOL have_record; |
| 6447 | PROCESSENTRY32 pe; |
| 6448 | |
| 6449 | mypid = getpid(); /* This function never fails */ |
| 6450 | |
| 6451 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 6452 | if (snapshot == INVALID_HANDLE_VALUE) |
| 6453 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 6454 | |
| 6455 | pe.dwSize = sizeof(pe); |
| 6456 | have_record = Process32First(snapshot, &pe); |
| 6457 | while (have_record) { |
| 6458 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 6459 | /* We could cache the ulong value in a static variable. */ |
| 6460 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 6461 | break; |
| 6462 | } |
| 6463 | |
| 6464 | have_record = Process32Next(snapshot, &pe); |
| 6465 | } |
| 6466 | |
| 6467 | /* If our loop exits and our pid was not found (result will be NULL) |
| 6468 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 6469 | * error anyway, so let's raise it. */ |
| 6470 | if (!result) |
| 6471 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6472 | |
| 6473 | CloseHandle(snapshot); |
| 6474 | |
| 6475 | return result; |
| 6476 | } |
| 6477 | #endif /*MS_WINDOWS*/ |
| 6478 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6479 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6480 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6481 | Return the parent's process id. If the parent process has already exited,\n\ |
| 6482 | Windows machines will still return its id; others systems will return the id\n\ |
| 6483 | of the 'init' process (1)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6484 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6485 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6486 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6487 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6488 | #ifdef MS_WINDOWS |
| 6489 | return win32_getppid(); |
| 6490 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6491 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6492 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6493 | } |
| 6494 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6495 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6496 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6497 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6498 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6499 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6500 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6501 | |
| 6502 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6503 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6504 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6505 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6506 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6507 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6508 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6509 | |
| 6510 | if (GetUserNameW(user_name, &num_chars)) { |
| 6511 | /* num_chars is the number of unicode chars plus null terminator */ |
| 6512 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6513 | } |
| 6514 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6515 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6516 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6517 | char *name; |
| 6518 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6519 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6520 | errno = 0; |
| 6521 | name = getlogin(); |
| 6522 | if (name == NULL) { |
| 6523 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6524 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6525 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6526 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6527 | } |
| 6528 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6529 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6530 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6531 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6532 | return result; |
| 6533 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6534 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6535 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6536 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6537 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6538 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6539 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6540 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6541 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6542 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6543 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6544 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6545 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6546 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6547 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6548 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6549 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6550 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6551 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6552 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6553 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6554 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6555 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6556 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6557 | pid_t pid; |
| 6558 | int sig; |
| 6559 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 6560 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6561 | if (kill(pid, sig) == -1) |
| 6562 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6563 | Py_INCREF(Py_None); |
| 6564 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6565 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6566 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6567 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6568 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6569 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6570 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6571 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6572 | |
| 6573 | static PyObject * |
| 6574 | posix_killpg(PyObject *self, PyObject *args) |
| 6575 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6576 | int sig; |
| 6577 | pid_t pgid; |
| 6578 | /* XXX some man pages make the `pgid` parameter an int, others |
| 6579 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 6580 | take the same type. Moreover, pid_t is always at least as wide as |
| 6581 | int (else compilation of this module fails), which is safe. */ |
| 6582 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 6583 | return NULL; |
| 6584 | if (killpg(pgid, sig) == -1) |
| 6585 | return posix_error(); |
| 6586 | Py_INCREF(Py_None); |
| 6587 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6588 | } |
| 6589 | #endif |
| 6590 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6591 | #ifdef MS_WINDOWS |
| 6592 | PyDoc_STRVAR(win32_kill__doc__, |
| 6593 | "kill(pid, sig)\n\n\ |
| 6594 | Kill a process with a signal."); |
| 6595 | |
| 6596 | static PyObject * |
| 6597 | win32_kill(PyObject *self, PyObject *args) |
| 6598 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 6599 | PyObject *result; |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6600 | pid_t pid; |
| 6601 | DWORD sig, err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6602 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6603 | |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6604 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "k:kill", &pid, &sig)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6605 | return NULL; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6606 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6607 | /* Console processes which share a common console can be sent CTRL+C or |
| 6608 | CTRL+BREAK events, provided they handle said events. */ |
| 6609 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6610 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6611 | err = GetLastError(); |
| 6612 | PyErr_SetFromWindowsErr(err); |
| 6613 | } |
| 6614 | else |
| 6615 | Py_RETURN_NONE; |
| 6616 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6617 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6618 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 6619 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6620 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6621 | if (handle == NULL) { |
| 6622 | err = GetLastError(); |
| 6623 | return PyErr_SetFromWindowsErr(err); |
| 6624 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6625 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6626 | if (TerminateProcess(handle, sig) == 0) { |
| 6627 | err = GetLastError(); |
| 6628 | result = PyErr_SetFromWindowsErr(err); |
| 6629 | } else { |
| 6630 | Py_INCREF(Py_None); |
| 6631 | result = Py_None; |
| 6632 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6633 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6634 | CloseHandle(handle); |
| 6635 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6636 | } |
| 6637 | #endif /* MS_WINDOWS */ |
| 6638 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6639 | #ifdef HAVE_PLOCK |
| 6640 | |
| 6641 | #ifdef HAVE_SYS_LOCK_H |
| 6642 | #include <sys/lock.h> |
| 6643 | #endif |
| 6644 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6645 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6646 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6647 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6648 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6649 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6650 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6651 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6652 | int op; |
| 6653 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 6654 | return NULL; |
| 6655 | if (plock(op) == -1) |
| 6656 | return posix_error(); |
| 6657 | Py_INCREF(Py_None); |
| 6658 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6659 | } |
| 6660 | #endif |
| 6661 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6662 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6663 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6664 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6665 | Set the current process's user id."); |
| 6666 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6667 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6668 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6669 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6670 | uid_t uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6671 | if (!PyArg_ParseTuple(args, "O&:setuid", _Py_Uid_Converter, &uid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6672 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6673 | if (setuid(uid) < 0) |
| 6674 | return posix_error(); |
| 6675 | Py_INCREF(Py_None); |
| 6676 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6677 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6678 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6679 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6680 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6681 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6682 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6683 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6684 | Set the current process's effective user id."); |
| 6685 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6686 | static PyObject * |
| 6687 | posix_seteuid (PyObject *self, PyObject *args) |
| 6688 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6689 | uid_t euid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6690 | if (!PyArg_ParseTuple(args, "O&:seteuid", _Py_Uid_Converter, &euid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6691 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6692 | if (seteuid(euid) < 0) { |
| 6693 | return posix_error(); |
| 6694 | } else { |
| 6695 | Py_INCREF(Py_None); |
| 6696 | return Py_None; |
| 6697 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6698 | } |
| 6699 | #endif /* HAVE_SETEUID */ |
| 6700 | |
| 6701 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6702 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6703 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6704 | Set the current process's effective group id."); |
| 6705 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6706 | static PyObject * |
| 6707 | posix_setegid (PyObject *self, PyObject *args) |
| 6708 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6709 | gid_t egid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6710 | if (!PyArg_ParseTuple(args, "O&:setegid", _Py_Gid_Converter, &egid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6711 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6712 | if (setegid(egid) < 0) { |
| 6713 | return posix_error(); |
| 6714 | } else { |
| 6715 | Py_INCREF(Py_None); |
| 6716 | return Py_None; |
| 6717 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6718 | } |
| 6719 | #endif /* HAVE_SETEGID */ |
| 6720 | |
| 6721 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6722 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6723 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6724 | Set the current process's real and effective user ids."); |
| 6725 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6726 | static PyObject * |
| 6727 | posix_setreuid (PyObject *self, PyObject *args) |
| 6728 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6729 | uid_t ruid, euid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6730 | if (!PyArg_ParseTuple(args, "O&O&:setreuid", |
| 6731 | _Py_Uid_Converter, &ruid, |
| 6732 | _Py_Uid_Converter, &euid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6733 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6734 | if (setreuid(ruid, euid) < 0) { |
| 6735 | return posix_error(); |
| 6736 | } else { |
| 6737 | Py_INCREF(Py_None); |
| 6738 | return Py_None; |
| 6739 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6740 | } |
| 6741 | #endif /* HAVE_SETREUID */ |
| 6742 | |
| 6743 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6744 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6745 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6746 | Set the current process's real and effective group ids."); |
| 6747 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6748 | static PyObject * |
| 6749 | posix_setregid (PyObject *self, PyObject *args) |
| 6750 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6751 | gid_t rgid, egid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6752 | if (!PyArg_ParseTuple(args, "O&O&:setregid", |
| 6753 | _Py_Gid_Converter, &rgid, |
| 6754 | _Py_Gid_Converter, &egid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6755 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6756 | if (setregid(rgid, egid) < 0) { |
| 6757 | return posix_error(); |
| 6758 | } else { |
| 6759 | Py_INCREF(Py_None); |
| 6760 | return Py_None; |
| 6761 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6762 | } |
| 6763 | #endif /* HAVE_SETREGID */ |
| 6764 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6765 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6766 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6767 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6768 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6769 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6770 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6771 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6772 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6773 | gid_t gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6774 | if (!PyArg_ParseTuple(args, "O&:setgid", _Py_Gid_Converter, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6775 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6776 | if (setgid(gid) < 0) |
| 6777 | return posix_error(); |
| 6778 | Py_INCREF(Py_None); |
| 6779 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6780 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6781 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6782 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6783 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6784 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6785 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6786 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6787 | |
| 6788 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6789 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6790 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6791 | int i, len; |
| 6792 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6793 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6794 | if (!PySequence_Check(groups)) { |
| 6795 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6796 | return NULL; |
| 6797 | } |
| 6798 | len = PySequence_Size(groups); |
| 6799 | if (len > MAX_GROUPS) { |
| 6800 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6801 | return NULL; |
| 6802 | } |
| 6803 | for(i = 0; i < len; i++) { |
| 6804 | PyObject *elem; |
| 6805 | elem = PySequence_GetItem(groups, i); |
| 6806 | if (!elem) |
| 6807 | return NULL; |
| 6808 | if (!PyLong_Check(elem)) { |
| 6809 | PyErr_SetString(PyExc_TypeError, |
| 6810 | "groups must be integers"); |
| 6811 | Py_DECREF(elem); |
| 6812 | return NULL; |
| 6813 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6814 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6815 | Py_DECREF(elem); |
| 6816 | return NULL; |
| 6817 | } |
| 6818 | } |
| 6819 | Py_DECREF(elem); |
| 6820 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6821 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6822 | if (setgroups(len, grouplist) < 0) |
| 6823 | return posix_error(); |
| 6824 | Py_INCREF(Py_None); |
| 6825 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6826 | } |
| 6827 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6828 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6829 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6830 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6831 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6832 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6833 | PyObject *result; |
| 6834 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6835 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6836 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6837 | if (pid == -1) |
| 6838 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6839 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6840 | if (struct_rusage == NULL) { |
| 6841 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6842 | if (m == NULL) |
| 6843 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6844 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6845 | Py_DECREF(m); |
| 6846 | if (struct_rusage == NULL) |
| 6847 | return NULL; |
| 6848 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6849 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6850 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6851 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6852 | if (!result) |
| 6853 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6854 | |
| 6855 | #ifndef doubletime |
| 6856 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6857 | #endif |
| 6858 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6859 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6860 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6861 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6862 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6863 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6864 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6865 | SET_INT(result, 2, ru->ru_maxrss); |
| 6866 | SET_INT(result, 3, ru->ru_ixrss); |
| 6867 | SET_INT(result, 4, ru->ru_idrss); |
| 6868 | SET_INT(result, 5, ru->ru_isrss); |
| 6869 | SET_INT(result, 6, ru->ru_minflt); |
| 6870 | SET_INT(result, 7, ru->ru_majflt); |
| 6871 | SET_INT(result, 8, ru->ru_nswap); |
| 6872 | SET_INT(result, 9, ru->ru_inblock); |
| 6873 | SET_INT(result, 10, ru->ru_oublock); |
| 6874 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6875 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6876 | SET_INT(result, 13, ru->ru_nsignals); |
| 6877 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6878 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6879 | #undef SET_INT |
| 6880 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6881 | if (PyErr_Occurred()) { |
| 6882 | Py_DECREF(result); |
| 6883 | return NULL; |
| 6884 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6885 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6886 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6887 | } |
| 6888 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6889 | |
| 6890 | #ifdef HAVE_WAIT3 |
| 6891 | PyDoc_STRVAR(posix_wait3__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6892 | "wait3(options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6893 | Wait for completion of a child process."); |
| 6894 | |
| 6895 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6896 | posix_wait3(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6897 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6898 | pid_t pid; |
| 6899 | int options; |
| 6900 | struct rusage ru; |
| 6901 | WAIT_TYPE status; |
| 6902 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6903 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6904 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6905 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6906 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6907 | Py_BEGIN_ALLOW_THREADS |
| 6908 | pid = wait3(&status, options, &ru); |
| 6909 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6910 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6911 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6912 | } |
| 6913 | #endif /* HAVE_WAIT3 */ |
| 6914 | |
| 6915 | #ifdef HAVE_WAIT4 |
| 6916 | PyDoc_STRVAR(posix_wait4__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6917 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6918 | Wait for completion of a given child process."); |
| 6919 | |
| 6920 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6921 | posix_wait4(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6922 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6923 | pid_t pid; |
| 6924 | int options; |
| 6925 | struct rusage ru; |
| 6926 | WAIT_TYPE status; |
| 6927 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6928 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6929 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6930 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6931 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6932 | Py_BEGIN_ALLOW_THREADS |
| 6933 | pid = wait4(pid, &status, options, &ru); |
| 6934 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6935 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6936 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6937 | } |
| 6938 | #endif /* HAVE_WAIT4 */ |
| 6939 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6940 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 6941 | PyDoc_STRVAR(posix_waitid__doc__, |
| 6942 | "waitid(idtype, id, options) -> waitid_result\n\n\ |
| 6943 | Wait for the completion of one or more child processes.\n\n\ |
| 6944 | idtype can be P_PID, P_PGID or P_ALL.\n\ |
| 6945 | id specifies the pid to wait on.\n\ |
| 6946 | options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\ |
| 6947 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\ |
| 6948 | Returns either waitid_result or None if WNOHANG is specified and there are\n\ |
| 6949 | no children in a waitable state."); |
| 6950 | |
| 6951 | static PyObject * |
| 6952 | posix_waitid(PyObject *self, PyObject *args) |
| 6953 | { |
| 6954 | PyObject *result; |
| 6955 | idtype_t idtype; |
| 6956 | id_t id; |
| 6957 | int options, res; |
| 6958 | siginfo_t si; |
| 6959 | si.si_pid = 0; |
| 6960 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options)) |
| 6961 | return NULL; |
| 6962 | Py_BEGIN_ALLOW_THREADS |
| 6963 | res = waitid(idtype, id, &si, options); |
| 6964 | Py_END_ALLOW_THREADS |
| 6965 | if (res == -1) |
| 6966 | return posix_error(); |
| 6967 | |
| 6968 | if (si.si_pid == 0) |
| 6969 | Py_RETURN_NONE; |
| 6970 | |
| 6971 | result = PyStructSequence_New(&WaitidResultType); |
| 6972 | if (!result) |
| 6973 | return NULL; |
| 6974 | |
| 6975 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6976 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6977 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6978 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6979 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6980 | if (PyErr_Occurred()) { |
| 6981 | Py_DECREF(result); |
| 6982 | return NULL; |
| 6983 | } |
| 6984 | |
| 6985 | return result; |
| 6986 | } |
| 6987 | #endif |
| 6988 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6989 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6990 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6991 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6992 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6993 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6994 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6995 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6996 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6997 | pid_t pid; |
| 6998 | int options; |
| 6999 | WAIT_TYPE status; |
| 7000 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7001 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7002 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 7003 | return NULL; |
| 7004 | Py_BEGIN_ALLOW_THREADS |
| 7005 | pid = waitpid(pid, &status, 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 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7011 | } |
| 7012 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7013 | #elif defined(HAVE_CWAIT) |
| 7014 | |
| 7015 | /* 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] | 7016 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7017 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7018 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7019 | |
| 7020 | static PyObject * |
| 7021 | posix_waitpid(PyObject *self, PyObject *args) |
| 7022 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7023 | Py_intptr_t pid; |
| 7024 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7025 | |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7026 | if (!PyArg_ParseTuple(args, _Py_PARSE_INTPTR "i:waitpid", &pid, &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7027 | return NULL; |
| 7028 | Py_BEGIN_ALLOW_THREADS |
| 7029 | pid = _cwait(&status, pid, options); |
| 7030 | Py_END_ALLOW_THREADS |
| 7031 | if (pid == -1) |
| 7032 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7033 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7034 | /* 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] | 7035 | return Py_BuildValue(_Py_PARSE_INTPTR "i", pid, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7036 | } |
| 7037 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7038 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7039 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7040 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7041 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7042 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7043 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7044 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7045 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7046 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7047 | pid_t pid; |
| 7048 | WAIT_TYPE status; |
| 7049 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7050 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7051 | Py_BEGIN_ALLOW_THREADS |
| 7052 | pid = wait(&status); |
| 7053 | Py_END_ALLOW_THREADS |
| 7054 | if (pid == -1) |
| 7055 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7056 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7057 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7058 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7059 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7060 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7061 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7062 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
| 7063 | PyDoc_STRVAR(readlink__doc__, |
| 7064 | "readlink(path, *, dir_fd=None) -> path\n\n\ |
| 7065 | Return a string representing the path to which the symbolic link points.\n\ |
| 7066 | \n\ |
| 7067 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7068 | and path should be relative; path will then be relative to that directory.\n\ |
| 7069 | dir_fd may not be implemented on your platform.\n\ |
| 7070 | If it is unavailable, using it will raise a NotImplementedError."); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7071 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7072 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7073 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7074 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7075 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7076 | posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7077 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7078 | path_t path; |
| 7079 | int dir_fd = DEFAULT_DIR_FD; |
| 7080 | char buffer[MAXPATHLEN]; |
| 7081 | ssize_t length; |
| 7082 | PyObject *return_value = NULL; |
| 7083 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7084 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7085 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7086 | path.function_name = "readlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7087 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords, |
| 7088 | path_converter, &path, |
| 7089 | #ifdef HAVE_READLINKAT |
| 7090 | dir_fd_converter, &dir_fd |
| 7091 | #else |
| 7092 | dir_fd_unavailable, &dir_fd |
| 7093 | #endif |
| 7094 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7095 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7096 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7097 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7098 | #ifdef HAVE_READLINKAT |
| 7099 | if (dir_fd != DEFAULT_DIR_FD) |
| 7100 | length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer)); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 7101 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7102 | #endif |
| 7103 | length = readlink(path.narrow, buffer, sizeof(buffer)); |
| 7104 | Py_END_ALLOW_THREADS |
| 7105 | |
| 7106 | if (length < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7107 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7108 | goto exit; |
| 7109 | } |
| 7110 | |
| 7111 | if (PyUnicode_Check(path.object)) |
| 7112 | return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 7113 | else |
| 7114 | return_value = PyBytes_FromStringAndSize(buffer, length); |
| 7115 | exit: |
| 7116 | path_cleanup(&path); |
| 7117 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7118 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7119 | |
| 7120 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7121 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7122 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7123 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7124 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7125 | PyDoc_STRVAR(posix_symlink__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7126 | "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 7127 | Create a symbolic link pointing to src named dst.\n\n\ |
| 7128 | target_is_directory is required on Windows if the target is to be\n\ |
| 7129 | interpreted as a directory. (On Windows, symlink requires\n\ |
| 7130 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.)\n\ |
| 7131 | target_is_directory is ignored on non-Windows platforms.\n\ |
| 7132 | \n\ |
| 7133 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7134 | and path should be relative; path will then be relative to that directory.\n\ |
| 7135 | dir_fd may not be implemented on your platform.\n\ |
| 7136 | If it is unavailable, using it will raise a NotImplementedError."); |
| 7137 | |
| 7138 | #if defined(MS_WINDOWS) |
| 7139 | |
| 7140 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 7141 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD) = NULL; |
| 7142 | static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPSTR, LPSTR, DWORD) = NULL; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7143 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7144 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7145 | check_CreateSymbolicLink(void) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7146 | { |
| 7147 | HINSTANCE hKernel32; |
| 7148 | /* only recheck */ |
| 7149 | if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA) |
| 7150 | return 1; |
| 7151 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 7152 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 7153 | "CreateSymbolicLinkW"); |
| 7154 | *(FARPROC*)&Py_CreateSymbolicLinkA = GetProcAddress(hKernel32, |
| 7155 | "CreateSymbolicLinkA"); |
| 7156 | return (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA); |
| 7157 | } |
| 7158 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7159 | /* Remove the last portion of the path */ |
| 7160 | static void |
| 7161 | _dirnameW(WCHAR *path) |
| 7162 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7163 | WCHAR *ptr; |
| 7164 | |
| 7165 | /* walk the path from the end until a backslash is encountered */ |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7166 | for(ptr = path + wcslen(path); ptr != path; ptr--) { |
Victor Stinner | 072318b | 2013-06-05 02:07:46 +0200 | [diff] [blame] | 7167 | if (*ptr == L'\\' || *ptr == L'/') |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7168 | break; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7169 | } |
| 7170 | *ptr = 0; |
| 7171 | } |
| 7172 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7173 | /* Remove the last portion of the path */ |
| 7174 | static void |
| 7175 | _dirnameA(char *path) |
| 7176 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7177 | char *ptr; |
| 7178 | |
| 7179 | /* walk the path from the end until a backslash is encountered */ |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7180 | for(ptr = path + strlen(path); ptr != path; ptr--) { |
| 7181 | if (*ptr == '\\' || *ptr == '/') |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7182 | break; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7183 | } |
| 7184 | *ptr = 0; |
| 7185 | } |
| 7186 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7187 | /* Is this path absolute? */ |
| 7188 | static int |
| 7189 | _is_absW(const WCHAR *path) |
| 7190 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7191 | return path[0] == L'\\' || path[0] == L'/' || path[1] == L':'; |
| 7192 | |
| 7193 | } |
| 7194 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7195 | /* Is this path absolute? */ |
| 7196 | static int |
| 7197 | _is_absA(const char *path) |
| 7198 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7199 | return path[0] == '\\' || path[0] == '/' || path[1] == ':'; |
| 7200 | |
| 7201 | } |
| 7202 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7203 | /* join root and rest with a backslash */ |
| 7204 | static void |
| 7205 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 7206 | { |
Victor Stinner | e7e7eba | 2013-06-05 00:35:54 +0200 | [diff] [blame] | 7207 | size_t root_len; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7208 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7209 | if (_is_absW(rest)) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7210 | wcscpy(dest_path, rest); |
| 7211 | return; |
| 7212 | } |
| 7213 | |
| 7214 | root_len = wcslen(root); |
| 7215 | |
| 7216 | wcscpy(dest_path, root); |
| 7217 | if(root_len) { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7218 | dest_path[root_len] = L'\\'; |
| 7219 | root_len++; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7220 | } |
| 7221 | wcscpy(dest_path+root_len, rest); |
| 7222 | } |
| 7223 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7224 | /* join root and rest with a backslash */ |
| 7225 | static void |
| 7226 | _joinA(char *dest_path, const char *root, const char *rest) |
| 7227 | { |
Victor Stinner | e7e7eba | 2013-06-05 00:35:54 +0200 | [diff] [blame] | 7228 | size_t root_len; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7229 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7230 | if (_is_absA(rest)) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7231 | strcpy(dest_path, rest); |
| 7232 | return; |
| 7233 | } |
| 7234 | |
| 7235 | root_len = strlen(root); |
| 7236 | |
| 7237 | strcpy(dest_path, root); |
| 7238 | if(root_len) { |
| 7239 | dest_path[root_len] = '\\'; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7240 | root_len++; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7241 | } |
| 7242 | strcpy(dest_path+root_len, rest); |
| 7243 | } |
| 7244 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7245 | /* Return True if the path at src relative to dest is a directory */ |
| 7246 | static int |
| 7247 | _check_dirW(WCHAR *src, WCHAR *dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7248 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7249 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7250 | WCHAR dest_parent[MAX_PATH]; |
| 7251 | WCHAR src_resolved[MAX_PATH] = L""; |
| 7252 | |
| 7253 | /* dest_parent = os.path.dirname(dest) */ |
| 7254 | wcscpy(dest_parent, dest); |
| 7255 | _dirnameW(dest_parent); |
| 7256 | /* src_resolved = os.path.join(dest_parent, src) */ |
| 7257 | _joinW(src_resolved, dest_parent, src); |
| 7258 | return ( |
| 7259 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 7260 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7261 | ); |
| 7262 | } |
| 7263 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7264 | /* Return True if the path at src relative to dest is a directory */ |
| 7265 | static int |
| 7266 | _check_dirA(char *src, char *dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7267 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7268 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7269 | char dest_parent[MAX_PATH]; |
| 7270 | char src_resolved[MAX_PATH] = ""; |
| 7271 | |
| 7272 | /* dest_parent = os.path.dirname(dest) */ |
| 7273 | strcpy(dest_parent, dest); |
Victor Stinner | 5a43676 | 2013-06-05 00:37:12 +0200 | [diff] [blame] | 7274 | _dirnameA(dest_parent); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7275 | /* src_resolved = os.path.join(dest_parent, src) */ |
Victor Stinner | 5a43676 | 2013-06-05 00:37:12 +0200 | [diff] [blame] | 7276 | _joinA(src_resolved, dest_parent, src); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7277 | return ( |
| 7278 | GetFileAttributesExA(src_resolved, GetFileExInfoStandard, &src_info) |
| 7279 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7280 | ); |
| 7281 | } |
| 7282 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7283 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7284 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7285 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7286 | posix_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7287 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7288 | path_t src; |
| 7289 | path_t dst; |
| 7290 | int dir_fd = DEFAULT_DIR_FD; |
| 7291 | int target_is_directory = 0; |
| 7292 | static char *keywords[] = {"src", "dst", "target_is_directory", |
| 7293 | "dir_fd", NULL}; |
| 7294 | PyObject *return_value; |
| 7295 | #ifdef MS_WINDOWS |
| 7296 | DWORD result; |
| 7297 | #else |
| 7298 | int result; |
| 7299 | #endif |
| 7300 | |
| 7301 | memset(&src, 0, sizeof(src)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7302 | src.function_name = "symlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7303 | src.argument_name = "src"; |
| 7304 | memset(&dst, 0, sizeof(dst)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7305 | dst.function_name = "symlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7306 | dst.argument_name = "dst"; |
| 7307 | |
| 7308 | #ifdef MS_WINDOWS |
| 7309 | if (!check_CreateSymbolicLink()) { |
| 7310 | PyErr_SetString(PyExc_NotImplementedError, |
| 7311 | "CreateSymbolicLink functions not found"); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7312 | return NULL; |
| 7313 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7314 | if (!win32_can_symlink) { |
| 7315 | PyErr_SetString(PyExc_OSError, "symbolic link privilege not held"); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7316 | return NULL; |
| 7317 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7318 | #endif |
| 7319 | |
| 7320 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|i$O&:symlink", |
| 7321 | keywords, |
| 7322 | path_converter, &src, |
| 7323 | path_converter, &dst, |
| 7324 | &target_is_directory, |
| 7325 | #ifdef HAVE_SYMLINKAT |
| 7326 | dir_fd_converter, &dir_fd |
| 7327 | #else |
| 7328 | dir_fd_unavailable, &dir_fd |
| 7329 | #endif |
| 7330 | )) |
| 7331 | return NULL; |
| 7332 | |
| 7333 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 7334 | PyErr_SetString(PyExc_ValueError, |
| 7335 | "symlink: src and dst must be the same type"); |
| 7336 | return_value = NULL; |
| 7337 | goto exit; |
| 7338 | } |
| 7339 | |
| 7340 | #ifdef MS_WINDOWS |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7341 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7342 | Py_BEGIN_ALLOW_THREADS |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7343 | if (dst.wide) { |
| 7344 | /* if src is a directory, ensure target_is_directory==1 */ |
| 7345 | target_is_directory |= _check_dirW(src.wide, dst.wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7346 | result = Py_CreateSymbolicLinkW(dst.wide, src.wide, |
| 7347 | target_is_directory); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7348 | } |
| 7349 | else { |
| 7350 | /* if src is a directory, ensure target_is_directory==1 */ |
| 7351 | target_is_directory |= _check_dirA(src.narrow, dst.narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7352 | result = Py_CreateSymbolicLinkA(dst.narrow, src.narrow, |
| 7353 | target_is_directory); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7354 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7355 | Py_END_ALLOW_THREADS |
| 7356 | |
| 7357 | if (!result) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 7358 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7359 | goto exit; |
| 7360 | } |
| 7361 | |
| 7362 | #else |
| 7363 | |
| 7364 | Py_BEGIN_ALLOW_THREADS |
| 7365 | #if HAVE_SYMLINKAT |
| 7366 | if (dir_fd != DEFAULT_DIR_FD) |
| 7367 | result = symlinkat(src.narrow, dir_fd, dst.narrow); |
| 7368 | else |
| 7369 | #endif |
| 7370 | result = symlink(src.narrow, dst.narrow); |
| 7371 | Py_END_ALLOW_THREADS |
| 7372 | |
| 7373 | if (result) { |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 7374 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7375 | goto exit; |
| 7376 | } |
| 7377 | #endif |
| 7378 | |
| 7379 | return_value = Py_None; |
| 7380 | Py_INCREF(Py_None); |
| 7381 | goto exit; /* silence "unused label" warning */ |
| 7382 | exit: |
| 7383 | path_cleanup(&src); |
| 7384 | path_cleanup(&dst); |
| 7385 | return return_value; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7386 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7387 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7388 | #endif /* HAVE_SYMLINK */ |
| 7389 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7390 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7391 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 7392 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7393 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7394 | win_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7395 | { |
| 7396 | wchar_t *path; |
| 7397 | DWORD n_bytes_returned; |
| 7398 | DWORD io_result; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7399 | PyObject *po, *result; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7400 | int dir_fd; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7401 | HANDLE reparse_point_handle; |
| 7402 | |
| 7403 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 7404 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 7405 | wchar_t *print_name; |
| 7406 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7407 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 7408 | |
| 7409 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords, |
| 7410 | &po, |
| 7411 | dir_fd_unavailable, &dir_fd |
| 7412 | )) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7413 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7414 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7415 | path = PyUnicode_AsUnicode(po); |
| 7416 | if (path == NULL) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7417 | return NULL; |
| 7418 | |
| 7419 | /* First get a handle to the reparse point */ |
| 7420 | Py_BEGIN_ALLOW_THREADS |
| 7421 | reparse_point_handle = CreateFileW( |
| 7422 | path, |
| 7423 | 0, |
| 7424 | 0, |
| 7425 | 0, |
| 7426 | OPEN_EXISTING, |
| 7427 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 7428 | 0); |
| 7429 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7430 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7431 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7432 | return win32_error_object("readlink", po); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7433 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7434 | Py_BEGIN_ALLOW_THREADS |
| 7435 | /* New call DeviceIoControl to read the reparse point */ |
| 7436 | io_result = DeviceIoControl( |
| 7437 | reparse_point_handle, |
| 7438 | FSCTL_GET_REPARSE_POINT, |
| 7439 | 0, 0, /* in buffer */ |
| 7440 | target_buffer, sizeof(target_buffer), |
| 7441 | &n_bytes_returned, |
| 7442 | 0 /* we're not using OVERLAPPED_IO */ |
| 7443 | ); |
| 7444 | CloseHandle(reparse_point_handle); |
| 7445 | Py_END_ALLOW_THREADS |
| 7446 | |
| 7447 | if (io_result==0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7448 | return win32_error_object("readlink", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7449 | |
| 7450 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 7451 | { |
| 7452 | PyErr_SetString(PyExc_ValueError, |
| 7453 | "not a symbolic link"); |
| 7454 | return NULL; |
| 7455 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 7456 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7457 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 7458 | |
| 7459 | result = PyUnicode_FromWideChar(print_name, |
| 7460 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7461 | return result; |
| 7462 | } |
| 7463 | |
| 7464 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 7465 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7466 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7467 | static PyStructSequence_Field times_result_fields[] = { |
| 7468 | {"user", "user time"}, |
| 7469 | {"system", "system time"}, |
| 7470 | {"children_user", "user time of children"}, |
| 7471 | {"children_system", "system time of children"}, |
| 7472 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 7473 | {NULL} |
| 7474 | }; |
| 7475 | |
| 7476 | PyDoc_STRVAR(times_result__doc__, |
| 7477 | "times_result: Result from os.times().\n\n\ |
| 7478 | This object may be accessed either as a tuple of\n\ |
| 7479 | (user, system, children_user, children_system, elapsed),\n\ |
| 7480 | or via the attributes user, system, children_user, children_system,\n\ |
| 7481 | and elapsed.\n\ |
| 7482 | \n\ |
| 7483 | See os.times for more information."); |
| 7484 | |
| 7485 | static PyStructSequence_Desc times_result_desc = { |
| 7486 | "times_result", /* name */ |
| 7487 | times_result__doc__, /* doc */ |
| 7488 | times_result_fields, |
| 7489 | 5 |
| 7490 | }; |
| 7491 | |
| 7492 | static PyTypeObject TimesResultType; |
| 7493 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7494 | #ifdef MS_WINDOWS |
| 7495 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 7496 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7497 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7498 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7499 | |
| 7500 | static PyObject * |
| 7501 | build_times_result(double user, double system, |
| 7502 | double children_user, double children_system, |
| 7503 | double elapsed) |
| 7504 | { |
| 7505 | PyObject *value = PyStructSequence_New(&TimesResultType); |
| 7506 | if (value == NULL) |
| 7507 | return NULL; |
| 7508 | |
| 7509 | #define SET(i, field) \ |
| 7510 | { \ |
| 7511 | PyObject *o = PyFloat_FromDouble(field); \ |
| 7512 | if (!o) { \ |
| 7513 | Py_DECREF(value); \ |
| 7514 | return NULL; \ |
| 7515 | } \ |
| 7516 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 7517 | } \ |
| 7518 | |
| 7519 | SET(0, user); |
| 7520 | SET(1, system); |
| 7521 | SET(2, children_user); |
| 7522 | SET(3, children_system); |
| 7523 | SET(4, elapsed); |
| 7524 | |
| 7525 | #undef SET |
| 7526 | |
| 7527 | return value; |
| 7528 | } |
| 7529 | |
| 7530 | PyDoc_STRVAR(posix_times__doc__, |
| 7531 | "times() -> times_result\n\n\ |
| 7532 | Return an object containing floating point numbers indicating process\n\ |
| 7533 | times. The object behaves like a named tuple with these fields:\n\ |
| 7534 | (utime, stime, cutime, cstime, elapsed_time)"); |
| 7535 | |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 7536 | #if defined(MS_WINDOWS) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7537 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7538 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7539 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7540 | FILETIME create, exit, kernel, user; |
| 7541 | HANDLE hProc; |
| 7542 | hProc = GetCurrentProcess(); |
| 7543 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 7544 | /* The fields of a FILETIME structure are the hi and lo part |
| 7545 | of a 64-bit value expressed in 100 nanosecond units. |
| 7546 | 1e7 is one second in such units; 1e-7 the inverse. |
| 7547 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 7548 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7549 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7550 | (double)(user.dwHighDateTime*429.4967296 + |
| 7551 | user.dwLowDateTime*1e-7), |
| 7552 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 7553 | kernel.dwLowDateTime*1e-7), |
| 7554 | (double)0, |
| 7555 | (double)0, |
| 7556 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7557 | } |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 7558 | #else /* Not Windows */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7559 | #define NEED_TICKS_PER_SECOND |
| 7560 | static long ticks_per_second = -1; |
| 7561 | static PyObject * |
| 7562 | posix_times(PyObject *self, PyObject *noargs) |
| 7563 | { |
| 7564 | struct tms t; |
| 7565 | clock_t c; |
| 7566 | errno = 0; |
| 7567 | c = times(&t); |
| 7568 | if (c == (clock_t) -1) |
| 7569 | return posix_error(); |
| 7570 | return build_times_result( |
| 7571 | (double)t.tms_utime / ticks_per_second, |
| 7572 | (double)t.tms_stime / ticks_per_second, |
| 7573 | (double)t.tms_cutime / ticks_per_second, |
| 7574 | (double)t.tms_cstime / ticks_per_second, |
| 7575 | (double)c / ticks_per_second); |
| 7576 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7577 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7578 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7579 | #endif /* HAVE_TIMES */ |
| 7580 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7581 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7582 | #ifdef HAVE_GETSID |
| 7583 | PyDoc_STRVAR(posix_getsid__doc__, |
| 7584 | "getsid(pid) -> sid\n\n\ |
| 7585 | Call the system call getsid()."); |
| 7586 | |
| 7587 | static PyObject * |
| 7588 | posix_getsid(PyObject *self, PyObject *args) |
| 7589 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7590 | pid_t pid; |
| 7591 | int sid; |
| 7592 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 7593 | return NULL; |
| 7594 | sid = getsid(pid); |
| 7595 | if (sid < 0) |
| 7596 | return posix_error(); |
| 7597 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7598 | } |
| 7599 | #endif /* HAVE_GETSID */ |
| 7600 | |
| 7601 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7602 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7603 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7604 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7605 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7606 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7607 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7608 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7609 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7610 | if (setsid() < 0) |
| 7611 | return posix_error(); |
| 7612 | Py_INCREF(Py_None); |
| 7613 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7614 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7615 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7616 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7617 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7618 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7619 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7620 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7621 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7622 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7623 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7624 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7625 | pid_t pid; |
| 7626 | int pgrp; |
| 7627 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 7628 | return NULL; |
| 7629 | if (setpgid(pid, pgrp) < 0) |
| 7630 | return posix_error(); |
| 7631 | Py_INCREF(Py_None); |
| 7632 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7633 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7634 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7635 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7636 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7637 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7638 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7639 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7640 | 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] | 7641 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7642 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7643 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7644 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7645 | int fd; |
| 7646 | pid_t pgid; |
| 7647 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 7648 | return NULL; |
| 7649 | pgid = tcgetpgrp(fd); |
| 7650 | if (pgid < 0) |
| 7651 | return posix_error(); |
| 7652 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7653 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7654 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7655 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7656 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7657 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7658 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7659 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7660 | 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] | 7661 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7662 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7663 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7664 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7665 | int fd; |
| 7666 | pid_t pgid; |
| 7667 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 7668 | return NULL; |
| 7669 | if (tcsetpgrp(fd, pgid) < 0) |
| 7670 | return posix_error(); |
| 7671 | Py_INCREF(Py_None); |
| 7672 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7673 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7674 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7675 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7676 | /* Functions acting on file descriptors */ |
| 7677 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7678 | #ifdef O_CLOEXEC |
| 7679 | extern int _Py_open_cloexec_works; |
| 7680 | #endif |
| 7681 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7682 | PyDoc_STRVAR(posix_open__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7683 | "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 7684 | Open a file for low level IO. Returns a file handle (integer).\n\ |
| 7685 | \n\ |
| 7686 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7687 | and path should be relative; path will then be relative to that directory.\n\ |
| 7688 | dir_fd may not be implemented on your platform.\n\ |
| 7689 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7690 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7691 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7692 | posix_open(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7693 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7694 | path_t path; |
| 7695 | int flags; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7696 | int mode = 0777; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7697 | int dir_fd = DEFAULT_DIR_FD; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7698 | int fd; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7699 | PyObject *return_value = NULL; |
| 7700 | static char *keywords[] = {"path", "flags", "mode", "dir_fd", NULL}; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7701 | #ifdef O_CLOEXEC |
| 7702 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 7703 | #elif !defined(MS_WINDOWS) |
| 7704 | int *atomic_flag_works = NULL; |
| 7705 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7706 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7707 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7708 | path.function_name = "open"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7709 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|i$O&:open", keywords, |
| 7710 | path_converter, &path, |
| 7711 | &flags, &mode, |
| 7712 | #ifdef HAVE_OPENAT |
| 7713 | dir_fd_converter, &dir_fd |
| 7714 | #else |
| 7715 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7716 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7717 | )) |
| 7718 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7719 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7720 | #ifdef MS_WINDOWS |
| 7721 | flags |= O_NOINHERIT; |
| 7722 | #elif defined(O_CLOEXEC) |
| 7723 | flags |= O_CLOEXEC; |
| 7724 | #endif |
| 7725 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7726 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7727 | #ifdef MS_WINDOWS |
| 7728 | if (path.wide) |
| 7729 | fd = _wopen(path.wide, flags, mode); |
| 7730 | else |
| 7731 | #endif |
| 7732 | #ifdef HAVE_OPENAT |
| 7733 | if (dir_fd != DEFAULT_DIR_FD) |
| 7734 | fd = openat(dir_fd, path.narrow, flags, mode); |
| 7735 | else |
| 7736 | #endif |
| 7737 | fd = open(path.narrow, flags, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7738 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7739 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7740 | if (fd == -1) { |
Victor Stinner | 4e7d2d4 | 2012-11-05 01:20:58 +0100 | [diff] [blame] | 7741 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path.object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7742 | goto exit; |
| 7743 | } |
| 7744 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7745 | #ifndef MS_WINDOWS |
| 7746 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 7747 | close(fd); |
| 7748 | goto exit; |
| 7749 | } |
| 7750 | #endif |
| 7751 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7752 | return_value = PyLong_FromLong((long)fd); |
| 7753 | |
| 7754 | exit: |
| 7755 | path_cleanup(&path); |
| 7756 | return return_value; |
| 7757 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7758 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7759 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7760 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7761 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7762 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7763 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7764 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7765 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7766 | int fd, res; |
| 7767 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 7768 | return NULL; |
| 7769 | if (!_PyVerify_fd(fd)) |
| 7770 | return posix_error(); |
| 7771 | Py_BEGIN_ALLOW_THREADS |
| 7772 | res = close(fd); |
| 7773 | Py_END_ALLOW_THREADS |
| 7774 | if (res < 0) |
| 7775 | return posix_error(); |
| 7776 | Py_INCREF(Py_None); |
| 7777 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7778 | } |
| 7779 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7780 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7781 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7782 | "closerange(fd_low, fd_high)\n\n\ |
| 7783 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 7784 | |
| 7785 | static PyObject * |
| 7786 | posix_closerange(PyObject *self, PyObject *args) |
| 7787 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7788 | int fd_from, fd_to, i; |
| 7789 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 7790 | return NULL; |
| 7791 | Py_BEGIN_ALLOW_THREADS |
| 7792 | for (i = fd_from; i < fd_to; i++) |
| 7793 | if (_PyVerify_fd(i)) |
| 7794 | close(i); |
| 7795 | Py_END_ALLOW_THREADS |
| 7796 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7797 | } |
| 7798 | |
| 7799 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7800 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7801 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7802 | Return a duplicate of a 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 * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7805 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7806 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7807 | int fd; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7808 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7809 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 7810 | return NULL; |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 7811 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7812 | fd = _Py_dup(fd); |
| 7813 | if (fd == -1) |
| 7814 | return NULL; |
| 7815 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7816 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7817 | } |
| 7818 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7819 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7820 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 7821 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7822 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7823 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7824 | static PyObject * |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7825 | posix_dup2(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7826 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7827 | static char *keywords[] = {"fd", "fd2", "inheritable", NULL}; |
| 7828 | int fd, fd2; |
| 7829 | int inheritable = 1; |
| 7830 | int res; |
| 7831 | #if defined(HAVE_DUP3) && \ |
| 7832 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 7833 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
| 7834 | int dup3_works = -1; |
| 7835 | #endif |
| 7836 | |
| 7837 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|i:dup2", keywords, |
| 7838 | &fd, &fd2, &inheritable)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7839 | return NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7840 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7841 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 7842 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7843 | |
| 7844 | #ifdef MS_WINDOWS |
| 7845 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7846 | res = dup2(fd, fd2); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7847 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7848 | if (res < 0) |
| 7849 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7850 | |
| 7851 | /* Character files like console cannot be make non-inheritable */ |
| 7852 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7853 | close(fd2); |
| 7854 | return NULL; |
| 7855 | } |
| 7856 | |
| 7857 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 7858 | Py_BEGIN_ALLOW_THREADS |
| 7859 | if (!inheritable) |
| 7860 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 7861 | else |
| 7862 | res = dup2(fd, fd2); |
| 7863 | Py_END_ALLOW_THREADS |
| 7864 | if (res < 0) |
| 7865 | return posix_error(); |
| 7866 | |
| 7867 | #else |
| 7868 | |
| 7869 | #ifdef HAVE_DUP3 |
| 7870 | if (!inheritable && dup3_works != 0) { |
| 7871 | Py_BEGIN_ALLOW_THREADS |
| 7872 | res = dup3(fd, fd2, O_CLOEXEC); |
| 7873 | Py_END_ALLOW_THREADS |
| 7874 | if (res < 0) { |
| 7875 | if (dup3_works == -1) |
| 7876 | dup3_works = (errno != ENOSYS); |
| 7877 | if (dup3_works) |
| 7878 | return posix_error(); |
| 7879 | } |
| 7880 | } |
| 7881 | |
| 7882 | if (inheritable || dup3_works == 0) |
| 7883 | { |
| 7884 | #endif |
| 7885 | Py_BEGIN_ALLOW_THREADS |
| 7886 | res = dup2(fd, fd2); |
| 7887 | Py_END_ALLOW_THREADS |
| 7888 | if (res < 0) |
| 7889 | return posix_error(); |
| 7890 | |
| 7891 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7892 | close(fd2); |
| 7893 | return NULL; |
| 7894 | } |
| 7895 | #ifdef HAVE_DUP3 |
| 7896 | } |
| 7897 | #endif |
| 7898 | |
| 7899 | #endif |
| 7900 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7901 | Py_INCREF(Py_None); |
| 7902 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7903 | } |
| 7904 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7905 | #ifdef HAVE_LOCKF |
| 7906 | PyDoc_STRVAR(posix_lockf__doc__, |
| 7907 | "lockf(fd, cmd, len)\n\n\ |
| 7908 | Apply, test or remove a POSIX lock on an open file descriptor.\n\n\ |
| 7909 | fd is an open file descriptor.\n\ |
| 7910 | cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\ |
| 7911 | F_TEST.\n\ |
| 7912 | len specifies the section of the file to lock."); |
| 7913 | |
| 7914 | static PyObject * |
| 7915 | posix_lockf(PyObject *self, PyObject *args) |
| 7916 | { |
| 7917 | int fd, cmd, res; |
| 7918 | off_t len; |
| 7919 | if (!PyArg_ParseTuple(args, "iiO&:lockf", |
| 7920 | &fd, &cmd, _parse_off_t, &len)) |
| 7921 | return NULL; |
| 7922 | |
| 7923 | Py_BEGIN_ALLOW_THREADS |
| 7924 | res = lockf(fd, cmd, len); |
| 7925 | Py_END_ALLOW_THREADS |
| 7926 | |
| 7927 | if (res < 0) |
| 7928 | return posix_error(); |
| 7929 | |
| 7930 | Py_RETURN_NONE; |
| 7931 | } |
| 7932 | #endif |
| 7933 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7934 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7935 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7936 | "lseek(fd, pos, how) -> newpos\n\n\ |
Victor Stinner | e83f899 | 2011-12-17 23:15:09 +0100 | [diff] [blame] | 7937 | Set the current position of a file descriptor.\n\ |
| 7938 | Return the new cursor position in bytes, starting from the beginning."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7939 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7940 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7941 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7942 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7943 | int fd, how; |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 7944 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7945 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7946 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7947 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7948 | #endif |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7949 | PyObject *posobj; |
| 7950 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7951 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7952 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7953 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 7954 | switch (how) { |
| 7955 | case 0: how = SEEK_SET; break; |
| 7956 | case 1: how = SEEK_CUR; break; |
| 7957 | case 2: how = SEEK_END; break; |
| 7958 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7959 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7960 | |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7961 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7962 | pos = PyLong_AsLong(posobj); |
| 7963 | #else |
| 7964 | pos = PyLong_AsLongLong(posobj); |
| 7965 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7966 | if (PyErr_Occurred()) |
| 7967 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7968 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7969 | if (!_PyVerify_fd(fd)) |
| 7970 | return posix_error(); |
| 7971 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 7972 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7973 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7974 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7975 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7976 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7977 | Py_END_ALLOW_THREADS |
| 7978 | if (res < 0) |
| 7979 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7980 | |
| 7981 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7982 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7983 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7984 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7985 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7986 | } |
| 7987 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7988 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7989 | PyDoc_STRVAR(posix_read__doc__, |
Benjamin Peterson | 3b08a29 | 2013-05-24 14:35:57 -0700 | [diff] [blame] | 7990 | "read(fd, buffersize) -> bytes\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7991 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7992 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7993 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7994 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7995 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7996 | int fd, size; |
| 7997 | Py_ssize_t n; |
| 7998 | PyObject *buffer; |
| 7999 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 8000 | return NULL; |
| 8001 | if (size < 0) { |
| 8002 | errno = EINVAL; |
| 8003 | return posix_error(); |
| 8004 | } |
| 8005 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 8006 | if (buffer == NULL) |
| 8007 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8008 | if (!_PyVerify_fd(fd)) { |
| 8009 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8010 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8011 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8012 | Py_BEGIN_ALLOW_THREADS |
| 8013 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 8014 | Py_END_ALLOW_THREADS |
| 8015 | if (n < 0) { |
| 8016 | Py_DECREF(buffer); |
| 8017 | return posix_error(); |
| 8018 | } |
| 8019 | if (n != size) |
| 8020 | _PyBytes_Resize(&buffer, n); |
| 8021 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8022 | } |
| 8023 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8024 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 8025 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8026 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8027 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 8028 | { |
| 8029 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8030 | Py_ssize_t blen, total = 0; |
| 8031 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8032 | *iov = PyMem_New(struct iovec, cnt); |
| 8033 | if (*iov == NULL) { |
| 8034 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8035 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8036 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8037 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8038 | *buf = PyMem_New(Py_buffer, cnt); |
| 8039 | if (*buf == NULL) { |
| 8040 | PyMem_Del(*iov); |
| 8041 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8042 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8043 | } |
| 8044 | |
| 8045 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8046 | PyObject *item = PySequence_GetItem(seq, i); |
| 8047 | if (item == NULL) |
| 8048 | goto fail; |
| 8049 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 8050 | Py_DECREF(item); |
| 8051 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8052 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8053 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8054 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8055 | blen = (*buf)[i].len; |
| 8056 | (*iov)[i].iov_len = blen; |
| 8057 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8058 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8059 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8060 | |
| 8061 | fail: |
| 8062 | PyMem_Del(*iov); |
| 8063 | for (j = 0; j < i; j++) { |
| 8064 | PyBuffer_Release(&(*buf)[j]); |
| 8065 | } |
| 8066 | PyMem_Del(*buf); |
| 8067 | return 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8068 | } |
| 8069 | |
| 8070 | static void |
| 8071 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 8072 | { |
| 8073 | int i; |
| 8074 | PyMem_Del(iov); |
| 8075 | for (i = 0; i < cnt; i++) { |
| 8076 | PyBuffer_Release(&buf[i]); |
| 8077 | } |
| 8078 | PyMem_Del(buf); |
| 8079 | } |
| 8080 | #endif |
| 8081 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8082 | #ifdef HAVE_READV |
| 8083 | PyDoc_STRVAR(posix_readv__doc__, |
| 8084 | "readv(fd, buffers) -> bytesread\n\n\ |
| 8085 | Read from a file descriptor into a number of writable buffers. buffers\n\ |
| 8086 | is an arbitrary sequence of writable buffers.\n\ |
| 8087 | Returns the total number of bytes read."); |
| 8088 | |
| 8089 | static PyObject * |
| 8090 | posix_readv(PyObject *self, PyObject *args) |
| 8091 | { |
| 8092 | int fd, cnt; |
| 8093 | Py_ssize_t n; |
| 8094 | PyObject *seq; |
| 8095 | struct iovec *iov; |
| 8096 | Py_buffer *buf; |
| 8097 | |
| 8098 | if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq)) |
| 8099 | return NULL; |
| 8100 | if (!PySequence_Check(seq)) { |
| 8101 | PyErr_SetString(PyExc_TypeError, |
| 8102 | "readv() arg 2 must be a sequence"); |
| 8103 | return NULL; |
| 8104 | } |
| 8105 | cnt = PySequence_Size(seq); |
| 8106 | |
| 8107 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE)) |
| 8108 | return NULL; |
| 8109 | |
| 8110 | Py_BEGIN_ALLOW_THREADS |
| 8111 | n = readv(fd, iov, cnt); |
| 8112 | Py_END_ALLOW_THREADS |
| 8113 | |
| 8114 | iov_cleanup(iov, buf, cnt); |
| 8115 | return PyLong_FromSsize_t(n); |
| 8116 | } |
| 8117 | #endif |
| 8118 | |
| 8119 | #ifdef HAVE_PREAD |
| 8120 | PyDoc_STRVAR(posix_pread__doc__, |
| 8121 | "pread(fd, buffersize, offset) -> string\n\n\ |
| 8122 | Read from a file descriptor, fd, at a position of offset. It will read up\n\ |
| 8123 | to buffersize number of bytes. The file offset remains unchanged."); |
| 8124 | |
| 8125 | static PyObject * |
| 8126 | posix_pread(PyObject *self, PyObject *args) |
| 8127 | { |
| 8128 | int fd, size; |
| 8129 | off_t offset; |
| 8130 | Py_ssize_t n; |
| 8131 | PyObject *buffer; |
| 8132 | if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset)) |
| 8133 | return NULL; |
| 8134 | |
| 8135 | if (size < 0) { |
| 8136 | errno = EINVAL; |
| 8137 | return posix_error(); |
| 8138 | } |
| 8139 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 8140 | if (buffer == NULL) |
| 8141 | return NULL; |
| 8142 | if (!_PyVerify_fd(fd)) { |
| 8143 | Py_DECREF(buffer); |
| 8144 | return posix_error(); |
| 8145 | } |
| 8146 | Py_BEGIN_ALLOW_THREADS |
| 8147 | n = pread(fd, PyBytes_AS_STRING(buffer), size, offset); |
| 8148 | Py_END_ALLOW_THREADS |
| 8149 | if (n < 0) { |
| 8150 | Py_DECREF(buffer); |
| 8151 | return posix_error(); |
| 8152 | } |
| 8153 | if (n != size) |
| 8154 | _PyBytes_Resize(&buffer, n); |
| 8155 | return buffer; |
| 8156 | } |
| 8157 | #endif |
| 8158 | |
| 8159 | PyDoc_STRVAR(posix_write__doc__, |
Benjamin Peterson | 3b08a29 | 2013-05-24 14:35:57 -0700 | [diff] [blame] | 8160 | "write(fd, data) -> byteswritten\n\n\ |
| 8161 | Write bytes to a file descriptor."); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8162 | |
| 8163 | static PyObject * |
| 8164 | posix_write(PyObject *self, PyObject *args) |
| 8165 | { |
| 8166 | Py_buffer pbuf; |
| 8167 | int fd; |
| 8168 | Py_ssize_t size, len; |
| 8169 | |
| 8170 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 8171 | return NULL; |
| 8172 | if (!_PyVerify_fd(fd)) { |
| 8173 | PyBuffer_Release(&pbuf); |
| 8174 | return posix_error(); |
| 8175 | } |
| 8176 | len = pbuf.len; |
| 8177 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 8178 | #ifdef MS_WINDOWS |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8179 | if (len > INT_MAX) |
| 8180 | len = INT_MAX; |
| 8181 | size = write(fd, pbuf.buf, (int)len); |
| 8182 | #else |
| 8183 | size = write(fd, pbuf.buf, len); |
| 8184 | #endif |
| 8185 | Py_END_ALLOW_THREADS |
| 8186 | PyBuffer_Release(&pbuf); |
| 8187 | if (size < 0) |
| 8188 | return posix_error(); |
| 8189 | return PyLong_FromSsize_t(size); |
| 8190 | } |
| 8191 | |
| 8192 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8193 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 8194 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 8195 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 8196 | -> byteswritten\n\ |
| 8197 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 8198 | |
| 8199 | static PyObject * |
| 8200 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 8201 | { |
| 8202 | int in, out; |
| 8203 | Py_ssize_t ret; |
| 8204 | off_t offset; |
| 8205 | |
| 8206 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 8207 | #ifndef __APPLE__ |
| 8208 | Py_ssize_t len; |
| 8209 | #endif |
| 8210 | PyObject *headers = NULL, *trailers = NULL; |
| 8211 | Py_buffer *hbuf, *tbuf; |
| 8212 | off_t sbytes; |
| 8213 | struct sf_hdtr sf; |
| 8214 | int flags = 0; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 8215 | static char *keywords[] = {"out", "in", |
| 8216 | "offset", "count", |
| 8217 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8218 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 8219 | sf.headers = NULL; |
| 8220 | sf.trailers = NULL; |
| 8221 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8222 | #ifdef __APPLE__ |
| 8223 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 8224 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8225 | #else |
| 8226 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 8227 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8228 | #endif |
| 8229 | &headers, &trailers, &flags)) |
| 8230 | return NULL; |
| 8231 | if (headers != NULL) { |
| 8232 | if (!PySequence_Check(headers)) { |
| 8233 | PyErr_SetString(PyExc_TypeError, |
| 8234 | "sendfile() headers must be a sequence or None"); |
| 8235 | return NULL; |
| 8236 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8237 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8238 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8239 | if (sf.hdr_cnt > 0 && |
| 8240 | !(i = iov_setup(&(sf.headers), &hbuf, |
| 8241 | headers, sf.hdr_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8242 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8243 | #ifdef __APPLE__ |
| 8244 | sbytes += i; |
| 8245 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8246 | } |
| 8247 | } |
| 8248 | if (trailers != NULL) { |
| 8249 | if (!PySequence_Check(trailers)) { |
| 8250 | PyErr_SetString(PyExc_TypeError, |
| 8251 | "sendfile() trailers must be a sequence or None"); |
| 8252 | return NULL; |
| 8253 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8254 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8255 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8256 | if (sf.trl_cnt > 0 && |
| 8257 | !(i = iov_setup(&(sf.trailers), &tbuf, |
| 8258 | trailers, sf.trl_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8259 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8260 | #ifdef __APPLE__ |
| 8261 | sbytes += i; |
| 8262 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8263 | } |
| 8264 | } |
| 8265 | |
| 8266 | Py_BEGIN_ALLOW_THREADS |
| 8267 | #ifdef __APPLE__ |
| 8268 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 8269 | #else |
| 8270 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 8271 | #endif |
| 8272 | Py_END_ALLOW_THREADS |
| 8273 | |
| 8274 | if (sf.headers != NULL) |
| 8275 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 8276 | if (sf.trailers != NULL) |
| 8277 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 8278 | |
| 8279 | if (ret < 0) { |
| 8280 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 8281 | if (sbytes != 0) { |
| 8282 | // some data has been sent |
| 8283 | goto done; |
| 8284 | } |
| 8285 | else { |
| 8286 | // no data has been sent; upper application is supposed |
| 8287 | // to retry on EAGAIN or EBUSY |
| 8288 | return posix_error(); |
| 8289 | } |
| 8290 | } |
| 8291 | return posix_error(); |
| 8292 | } |
| 8293 | goto done; |
| 8294 | |
| 8295 | done: |
| 8296 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 8297 | return Py_BuildValue("l", sbytes); |
| 8298 | #else |
| 8299 | return Py_BuildValue("L", sbytes); |
| 8300 | #endif |
| 8301 | |
| 8302 | #else |
| 8303 | Py_ssize_t count; |
| 8304 | PyObject *offobj; |
| 8305 | static char *keywords[] = {"out", "in", |
| 8306 | "offset", "count", NULL}; |
| 8307 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 8308 | keywords, &out, &in, &offobj, &count)) |
| 8309 | return NULL; |
| 8310 | #ifdef linux |
| 8311 | if (offobj == Py_None) { |
| 8312 | Py_BEGIN_ALLOW_THREADS |
| 8313 | ret = sendfile(out, in, NULL, count); |
| 8314 | Py_END_ALLOW_THREADS |
| 8315 | if (ret < 0) |
| 8316 | return posix_error(); |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 8317 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8318 | } |
| 8319 | #endif |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 8320 | if (!_parse_off_t(offobj, &offset)) |
| 8321 | return NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8322 | Py_BEGIN_ALLOW_THREADS |
| 8323 | ret = sendfile(out, in, &offset, count); |
| 8324 | Py_END_ALLOW_THREADS |
| 8325 | if (ret < 0) |
| 8326 | return posix_error(); |
| 8327 | return Py_BuildValue("n", ret); |
| 8328 | #endif |
| 8329 | } |
| 8330 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8331 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8332 | PyDoc_STRVAR(posix_fstat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8333 | "fstat(fd) -> stat result\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8334 | Like stat(), but for an open file descriptor.\n\ |
| 8335 | Equivalent to stat(fd=fd)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8336 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8337 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8338 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8339 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8340 | int fd; |
| 8341 | STRUCT_STAT st; |
| 8342 | int res; |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8343 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8344 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 8345 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8346 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 8347 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 8348 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8349 | Py_BEGIN_ALLOW_THREADS |
| 8350 | res = FSTAT(fd, &st); |
| 8351 | Py_END_ALLOW_THREADS |
| 8352 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8353 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8354 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8355 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8356 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8357 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8358 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8359 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8360 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8361 | } |
| 8362 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8363 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8364 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8365 | 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] | 8366 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8367 | |
| 8368 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 8369 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8370 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8371 | int fd; |
| 8372 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 8373 | return NULL; |
| 8374 | if (!_PyVerify_fd(fd)) |
| 8375 | return PyBool_FromLong(0); |
| 8376 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8377 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8378 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8379 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8380 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8381 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8382 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8383 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8384 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8385 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8386 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8387 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8388 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8389 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8390 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8391 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8392 | #else |
| 8393 | int res; |
| 8394 | #endif |
| 8395 | |
| 8396 | #ifdef MS_WINDOWS |
| 8397 | attr.nLength = sizeof(attr); |
| 8398 | attr.lpSecurityDescriptor = NULL; |
| 8399 | attr.bInheritHandle = FALSE; |
| 8400 | |
| 8401 | Py_BEGIN_ALLOW_THREADS |
| 8402 | ok = CreatePipe(&read, &write, &attr, 0); |
| 8403 | if (ok) { |
| 8404 | fds[0] = _open_osfhandle((Py_intptr_t)read, _O_RDONLY); |
| 8405 | fds[1] = _open_osfhandle((Py_intptr_t)write, _O_WRONLY); |
| 8406 | if (fds[0] == -1 || fds[1] == -1) { |
| 8407 | CloseHandle(read); |
| 8408 | CloseHandle(write); |
| 8409 | ok = 0; |
| 8410 | } |
| 8411 | } |
| 8412 | Py_END_ALLOW_THREADS |
| 8413 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8414 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8415 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8416 | #else |
| 8417 | |
| 8418 | #ifdef HAVE_PIPE2 |
| 8419 | Py_BEGIN_ALLOW_THREADS |
| 8420 | res = pipe2(fds, O_CLOEXEC); |
| 8421 | Py_END_ALLOW_THREADS |
| 8422 | |
| 8423 | if (res != 0 && errno == ENOSYS) |
| 8424 | { |
| 8425 | #endif |
| 8426 | Py_BEGIN_ALLOW_THREADS |
| 8427 | res = pipe(fds); |
| 8428 | Py_END_ALLOW_THREADS |
| 8429 | |
| 8430 | if (res == 0) { |
| 8431 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 8432 | close(fds[0]); |
| 8433 | close(fds[1]); |
| 8434 | return NULL; |
| 8435 | } |
| 8436 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 8437 | close(fds[0]); |
| 8438 | close(fds[1]); |
| 8439 | return NULL; |
| 8440 | } |
| 8441 | } |
| 8442 | #ifdef HAVE_PIPE2 |
| 8443 | } |
| 8444 | #endif |
| 8445 | |
| 8446 | if (res != 0) |
| 8447 | return PyErr_SetFromErrno(PyExc_OSError); |
| 8448 | #endif /* !MS_WINDOWS */ |
| 8449 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8450 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8451 | #endif /* HAVE_PIPE */ |
| 8452 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8453 | #ifdef HAVE_PIPE2 |
| 8454 | PyDoc_STRVAR(posix_pipe2__doc__, |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8455 | "pipe2(flags) -> (read_end, write_end)\n\n\ |
| 8456 | Create a pipe with flags set atomically.\n\ |
| 8457 | flags can be constructed by ORing together one or more of these values:\n\ |
| 8458 | O_NONBLOCK, O_CLOEXEC.\n\ |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8459 | "); |
| 8460 | |
| 8461 | static PyObject * |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8462 | posix_pipe2(PyObject *self, PyObject *arg) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8463 | { |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8464 | int flags; |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8465 | int fds[2]; |
| 8466 | int res; |
| 8467 | |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 8468 | flags = _PyLong_AsInt(arg); |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8469 | if (flags == -1 && PyErr_Occurred()) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8470 | return NULL; |
| 8471 | |
| 8472 | res = pipe2(fds, flags); |
| 8473 | if (res != 0) |
| 8474 | return posix_error(); |
| 8475 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 8476 | } |
| 8477 | #endif /* HAVE_PIPE2 */ |
| 8478 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8479 | #ifdef HAVE_WRITEV |
| 8480 | PyDoc_STRVAR(posix_writev__doc__, |
| 8481 | "writev(fd, buffers) -> byteswritten\n\n\ |
| 8482 | Write the contents of buffers to a file descriptor, where buffers is an\n\ |
| 8483 | arbitrary sequence of buffers.\n\ |
| 8484 | Returns the total bytes written."); |
| 8485 | |
| 8486 | static PyObject * |
| 8487 | posix_writev(PyObject *self, PyObject *args) |
| 8488 | { |
| 8489 | int fd, cnt; |
| 8490 | Py_ssize_t res; |
| 8491 | PyObject *seq; |
| 8492 | struct iovec *iov; |
| 8493 | Py_buffer *buf; |
| 8494 | if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq)) |
| 8495 | return NULL; |
| 8496 | if (!PySequence_Check(seq)) { |
| 8497 | PyErr_SetString(PyExc_TypeError, |
| 8498 | "writev() arg 2 must be a sequence"); |
| 8499 | return NULL; |
| 8500 | } |
| 8501 | cnt = PySequence_Size(seq); |
| 8502 | |
| 8503 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) { |
| 8504 | return NULL; |
| 8505 | } |
| 8506 | |
| 8507 | Py_BEGIN_ALLOW_THREADS |
| 8508 | res = writev(fd, iov, cnt); |
| 8509 | Py_END_ALLOW_THREADS |
| 8510 | |
| 8511 | iov_cleanup(iov, buf, cnt); |
| 8512 | return PyLong_FromSsize_t(res); |
| 8513 | } |
| 8514 | #endif |
| 8515 | |
| 8516 | #ifdef HAVE_PWRITE |
| 8517 | PyDoc_STRVAR(posix_pwrite__doc__, |
| 8518 | "pwrite(fd, string, offset) -> byteswritten\n\n\ |
| 8519 | Write string to a file descriptor, fd, from offset, leaving the file\n\ |
| 8520 | offset unchanged."); |
| 8521 | |
| 8522 | static PyObject * |
| 8523 | posix_pwrite(PyObject *self, PyObject *args) |
| 8524 | { |
| 8525 | Py_buffer pbuf; |
| 8526 | int fd; |
| 8527 | off_t offset; |
| 8528 | Py_ssize_t size; |
| 8529 | |
| 8530 | if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset)) |
| 8531 | return NULL; |
| 8532 | |
| 8533 | if (!_PyVerify_fd(fd)) { |
| 8534 | PyBuffer_Release(&pbuf); |
| 8535 | return posix_error(); |
| 8536 | } |
| 8537 | Py_BEGIN_ALLOW_THREADS |
| 8538 | size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset); |
| 8539 | Py_END_ALLOW_THREADS |
| 8540 | PyBuffer_Release(&pbuf); |
| 8541 | if (size < 0) |
| 8542 | return posix_error(); |
| 8543 | return PyLong_FromSsize_t(size); |
| 8544 | } |
| 8545 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8546 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8547 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8548 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8549 | "mkfifo(path, mode=0o666, *, dir_fd=None)\n\n\ |
| 8550 | Create a FIFO (a POSIX named pipe).\n\ |
| 8551 | \n\ |
| 8552 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 8553 | and path should be relative; path will then be relative to that directory.\n\ |
| 8554 | dir_fd may not be implemented on your platform.\n\ |
| 8555 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8556 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8557 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8558 | posix_mkfifo(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8559 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8560 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8561 | int mode = 0666; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8562 | int dir_fd = DEFAULT_DIR_FD; |
| 8563 | int result; |
| 8564 | PyObject *return_value = NULL; |
| 8565 | static char *keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 8566 | |
| 8567 | memset(&path, 0, sizeof(path)); |
| 8568 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkfifo", keywords, |
| 8569 | path_converter, &path, |
| 8570 | &mode, |
| 8571 | #ifdef HAVE_MKFIFOAT |
| 8572 | dir_fd_converter, &dir_fd |
| 8573 | #else |
| 8574 | dir_fd_unavailable, &dir_fd |
| 8575 | #endif |
| 8576 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8577 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8578 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8579 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8580 | #ifdef HAVE_MKFIFOAT |
| 8581 | if (dir_fd != DEFAULT_DIR_FD) |
| 8582 | result = mkfifoat(dir_fd, path.narrow, mode); |
| 8583 | else |
| 8584 | #endif |
| 8585 | result = mkfifo(path.narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8586 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8587 | |
| 8588 | if (result < 0) { |
| 8589 | return_value = posix_error(); |
| 8590 | goto exit; |
| 8591 | } |
| 8592 | |
| 8593 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8594 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8595 | |
| 8596 | exit: |
| 8597 | path_cleanup(&path); |
| 8598 | return return_value; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8599 | } |
| 8600 | #endif |
| 8601 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 8602 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8603 | PyDoc_STRVAR(posix_mknod__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8604 | "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] | 8605 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 8606 | named filename. mode specifies both the permissions to use and the\n\ |
| 8607 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 8608 | 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] | 8609 | device defines the newly created device special file (probably using\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8610 | os.makedev()), otherwise it is ignored.\n\ |
| 8611 | \n\ |
| 8612 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 8613 | and path should be relative; path will then be relative to that directory.\n\ |
| 8614 | dir_fd may not be implemented on your platform.\n\ |
| 8615 | If it is unavailable, using it will raise a NotImplementedError."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8616 | |
| 8617 | |
| 8618 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8619 | posix_mknod(PyObject *self, PyObject *args, PyObject *kwargs) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8620 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8621 | path_t path; |
| 8622 | int mode = 0666; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8623 | int device = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8624 | int dir_fd = DEFAULT_DIR_FD; |
| 8625 | int result; |
| 8626 | PyObject *return_value = NULL; |
| 8627 | static char *keywords[] = {"path", "mode", "device", "dir_fd", NULL}; |
| 8628 | |
| 8629 | memset(&path, 0, sizeof(path)); |
| 8630 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|ii$O&:mknod", keywords, |
| 8631 | path_converter, &path, |
| 8632 | &mode, &device, |
| 8633 | #ifdef HAVE_MKNODAT |
| 8634 | dir_fd_converter, &dir_fd |
| 8635 | #else |
| 8636 | dir_fd_unavailable, &dir_fd |
| 8637 | #endif |
| 8638 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8639 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8640 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8641 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8642 | #ifdef HAVE_MKNODAT |
| 8643 | if (dir_fd != DEFAULT_DIR_FD) |
| 8644 | result = mknodat(dir_fd, path.narrow, mode, device); |
| 8645 | else |
| 8646 | #endif |
| 8647 | result = mknod(path.narrow, mode, device); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8648 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8649 | |
| 8650 | if (result < 0) { |
| 8651 | return_value = posix_error(); |
| 8652 | goto exit; |
| 8653 | } |
| 8654 | |
| 8655 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8656 | Py_INCREF(Py_None); |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 8657 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8658 | exit: |
| 8659 | path_cleanup(&path); |
| 8660 | return return_value; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8661 | } |
| 8662 | #endif |
| 8663 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8664 | #ifdef HAVE_DEVICE_MACROS |
| 8665 | PyDoc_STRVAR(posix_major__doc__, |
| 8666 | "major(device) -> major number\n\ |
| 8667 | Extracts a device major number from a raw device number."); |
| 8668 | |
| 8669 | static PyObject * |
| 8670 | posix_major(PyObject *self, PyObject *args) |
| 8671 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8672 | int device; |
| 8673 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 8674 | return NULL; |
| 8675 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8676 | } |
| 8677 | |
| 8678 | PyDoc_STRVAR(posix_minor__doc__, |
| 8679 | "minor(device) -> minor number\n\ |
| 8680 | Extracts a device minor number from a raw device number."); |
| 8681 | |
| 8682 | static PyObject * |
| 8683 | posix_minor(PyObject *self, PyObject *args) |
| 8684 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8685 | int device; |
| 8686 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 8687 | return NULL; |
| 8688 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8689 | } |
| 8690 | |
| 8691 | PyDoc_STRVAR(posix_makedev__doc__, |
| 8692 | "makedev(major, minor) -> device number\n\ |
| 8693 | Composes a raw device number from the major and minor device numbers."); |
| 8694 | |
| 8695 | static PyObject * |
| 8696 | posix_makedev(PyObject *self, PyObject *args) |
| 8697 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8698 | int major, minor; |
| 8699 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 8700 | return NULL; |
| 8701 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8702 | } |
| 8703 | #endif /* device macros */ |
| 8704 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8705 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8706 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8707 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8708 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8709 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8710 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8711 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8712 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8713 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8714 | int fd; |
| 8715 | off_t length; |
| 8716 | int res; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8717 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8718 | if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8719 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8720 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8721 | Py_BEGIN_ALLOW_THREADS |
| 8722 | res = ftruncate(fd, length); |
| 8723 | Py_END_ALLOW_THREADS |
| 8724 | if (res < 0) |
| 8725 | return posix_error(); |
| 8726 | Py_INCREF(Py_None); |
| 8727 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8728 | } |
| 8729 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8730 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8731 | #ifdef HAVE_TRUNCATE |
| 8732 | PyDoc_STRVAR(posix_truncate__doc__, |
| 8733 | "truncate(path, length)\n\n\ |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8734 | Truncate the file given by path to length bytes.\n\ |
| 8735 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 8736 | If this functionality is unavailable, using it raises an exception."); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8737 | |
| 8738 | static PyObject * |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8739 | posix_truncate(PyObject *self, PyObject *args, PyObject *kwargs) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8740 | { |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8741 | path_t path; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8742 | off_t length; |
| 8743 | int res; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8744 | PyObject *result = NULL; |
| 8745 | static char *keywords[] = {"path", "length", NULL}; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8746 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8747 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8748 | path.function_name = "truncate"; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8749 | #ifdef HAVE_FTRUNCATE |
| 8750 | path.allow_fd = 1; |
| 8751 | #endif |
| 8752 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:truncate", keywords, |
| 8753 | path_converter, &path, |
| 8754 | _parse_off_t, &length)) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8755 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8756 | |
| 8757 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8758 | #ifdef HAVE_FTRUNCATE |
| 8759 | if (path.fd != -1) |
| 8760 | res = ftruncate(path.fd, length); |
| 8761 | else |
| 8762 | #endif |
| 8763 | res = truncate(path.narrow, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8764 | Py_END_ALLOW_THREADS |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8765 | if (res < 0) |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8766 | result = path_error(&path); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8767 | else { |
| 8768 | Py_INCREF(Py_None); |
| 8769 | result = Py_None; |
| 8770 | } |
| 8771 | path_cleanup(&path); |
| 8772 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8773 | } |
| 8774 | #endif |
| 8775 | |
| 8776 | #ifdef HAVE_POSIX_FALLOCATE |
| 8777 | PyDoc_STRVAR(posix_posix_fallocate__doc__, |
| 8778 | "posix_fallocate(fd, offset, len)\n\n\ |
| 8779 | Ensures that enough disk space is allocated for the file specified by fd\n\ |
| 8780 | starting from offset and continuing for len bytes."); |
| 8781 | |
| 8782 | static PyObject * |
| 8783 | posix_posix_fallocate(PyObject *self, PyObject *args) |
| 8784 | { |
| 8785 | off_t len, offset; |
| 8786 | int res, fd; |
| 8787 | |
| 8788 | if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate", |
| 8789 | &fd, _parse_off_t, &offset, _parse_off_t, &len)) |
| 8790 | return NULL; |
| 8791 | |
| 8792 | Py_BEGIN_ALLOW_THREADS |
| 8793 | res = posix_fallocate(fd, offset, len); |
| 8794 | Py_END_ALLOW_THREADS |
| 8795 | if (res != 0) { |
| 8796 | errno = res; |
| 8797 | return posix_error(); |
| 8798 | } |
| 8799 | Py_RETURN_NONE; |
| 8800 | } |
| 8801 | #endif |
| 8802 | |
| 8803 | #ifdef HAVE_POSIX_FADVISE |
| 8804 | PyDoc_STRVAR(posix_posix_fadvise__doc__, |
| 8805 | "posix_fadvise(fd, offset, len, advice)\n\n\ |
| 8806 | Announces an intention to access data in a specific pattern thus allowing\n\ |
| 8807 | the kernel to make optimizations.\n\ |
| 8808 | The advice applies to the region of the file specified by fd starting at\n\ |
| 8809 | offset and continuing for len bytes.\n\ |
| 8810 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\ |
| 8811 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\ |
| 8812 | POSIX_FADV_DONTNEED."); |
| 8813 | |
| 8814 | static PyObject * |
| 8815 | posix_posix_fadvise(PyObject *self, PyObject *args) |
| 8816 | { |
| 8817 | off_t len, offset; |
| 8818 | int res, fd, advice; |
| 8819 | |
| 8820 | if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise", |
| 8821 | &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice)) |
| 8822 | return NULL; |
| 8823 | |
| 8824 | Py_BEGIN_ALLOW_THREADS |
| 8825 | res = posix_fadvise(fd, offset, len, advice); |
| 8826 | Py_END_ALLOW_THREADS |
| 8827 | if (res != 0) { |
| 8828 | errno = res; |
| 8829 | return posix_error(); |
| 8830 | } |
| 8831 | Py_RETURN_NONE; |
| 8832 | } |
| 8833 | #endif |
| 8834 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8835 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8836 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8837 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8838 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8839 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8840 | /* Save putenv() parameters as values here, so we can collect them when they |
| 8841 | * get re-set with another call for the same key. */ |
| 8842 | static PyObject *posix_putenv_garbage; |
| 8843 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8844 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8845 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8846 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8847 | PyObject *newstr = NULL; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8848 | #ifdef MS_WINDOWS |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8849 | PyObject *os1, *os2; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8850 | wchar_t *newenv; |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8851 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8852 | if (!PyArg_ParseTuple(args, |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8853 | "UU:putenv", |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8854 | &os1, &os2)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8855 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8856 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8857 | newstr = PyUnicode_FromFormat("%U=%U", os1, os2); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8858 | if (newstr == NULL) { |
| 8859 | PyErr_NoMemory(); |
| 8860 | goto error; |
| 8861 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8862 | if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) { |
| 8863 | PyErr_Format(PyExc_ValueError, |
| 8864 | "the environment variable is longer than %u characters", |
| 8865 | _MAX_ENV); |
| 8866 | goto error; |
| 8867 | } |
| 8868 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8869 | newenv = PyUnicode_AsUnicode(newstr); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 8870 | if (newenv == NULL) |
| 8871 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8872 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8873 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8874 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8875 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8876 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8877 | PyObject *os1, *os2; |
| 8878 | char *s1, *s2; |
| 8879 | char *newenv; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8880 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8881 | if (!PyArg_ParseTuple(args, |
| 8882 | "O&O&:putenv", |
| 8883 | PyUnicode_FSConverter, &os1, |
| 8884 | PyUnicode_FSConverter, &os2)) |
| 8885 | return NULL; |
| 8886 | s1 = PyBytes_AsString(os1); |
| 8887 | s2 = PyBytes_AsString(os2); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8888 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8889 | newstr = PyBytes_FromFormat("%s=%s", s1, s2); |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8890 | if (newstr == NULL) { |
| 8891 | PyErr_NoMemory(); |
| 8892 | goto error; |
| 8893 | } |
| 8894 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8895 | newenv = PyBytes_AS_STRING(newstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8896 | if (putenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8897 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8898 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8899 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8900 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8901 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8902 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 8903 | * this will cause previous value to be collected. This has to |
| 8904 | * happen after the real putenv() call because the old value |
| 8905 | * was still accessible until then. */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8906 | if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8907 | /* really not much we can do; just leak */ |
| 8908 | PyErr_Clear(); |
| 8909 | } |
| 8910 | else { |
| 8911 | Py_DECREF(newstr); |
| 8912 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8913 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8914 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8915 | Py_DECREF(os1); |
| 8916 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8917 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8918 | Py_RETURN_NONE; |
| 8919 | |
| 8920 | error: |
| 8921 | #ifndef MS_WINDOWS |
| 8922 | Py_DECREF(os1); |
| 8923 | Py_DECREF(os2); |
| 8924 | #endif |
| 8925 | Py_XDECREF(newstr); |
| 8926 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8927 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8928 | #endif /* putenv */ |
| 8929 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8930 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8931 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8932 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8933 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8934 | |
| 8935 | static PyObject * |
| 8936 | posix_unsetenv(PyObject *self, PyObject *args) |
| 8937 | { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8938 | PyObject *name; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8939 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8940 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8941 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8942 | |
| 8943 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8944 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8945 | PyUnicode_FSConverter, &name)) |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8946 | return NULL; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8947 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8948 | #ifdef HAVE_BROKEN_UNSETENV |
| 8949 | unsetenv(PyBytes_AS_STRING(name)); |
| 8950 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8951 | err = unsetenv(PyBytes_AS_STRING(name)); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8952 | if (err) { |
Benjamin Peterson | e8eb0e8 | 2011-11-22 23:14:47 -0600 | [diff] [blame] | 8953 | Py_DECREF(name); |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8954 | return posix_error(); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8955 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8956 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8957 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8958 | /* Remove the key from posix_putenv_garbage; |
| 8959 | * this will cause it to be collected. This has to |
| 8960 | * happen after the real unsetenv() call because the |
| 8961 | * old value was still accessible until then. |
| 8962 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8963 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8964 | /* really not much we can do; just leak */ |
| 8965 | PyErr_Clear(); |
| 8966 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8967 | Py_DECREF(name); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8968 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8969 | } |
| 8970 | #endif /* unsetenv */ |
| 8971 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8972 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8973 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8974 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8975 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 8976 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8977 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8978 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8979 | int code; |
| 8980 | char *message; |
| 8981 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 8982 | return NULL; |
| 8983 | message = strerror(code); |
| 8984 | if (message == NULL) { |
| 8985 | PyErr_SetString(PyExc_ValueError, |
| 8986 | "strerror() argument out of range"); |
| 8987 | return NULL; |
| 8988 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 8989 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8990 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8991 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8992 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8993 | #ifdef HAVE_SYS_WAIT_H |
| 8994 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8995 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8996 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8997 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8998 | 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] | 8999 | |
| 9000 | static PyObject * |
| 9001 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 9002 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9003 | WAIT_TYPE status; |
| 9004 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9005 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9006 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 9007 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9008 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9009 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9010 | } |
| 9011 | #endif /* WCOREDUMP */ |
| 9012 | |
| 9013 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9014 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9015 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9016 | 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] | 9017 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9018 | |
| 9019 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 9020 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9021 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9022 | WAIT_TYPE status; |
| 9023 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9024 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9025 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 9026 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9027 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9028 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9029 | } |
| 9030 | #endif /* WIFCONTINUED */ |
| 9031 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9032 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9033 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9034 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9035 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9036 | |
| 9037 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9038 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9039 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9040 | WAIT_TYPE status; |
| 9041 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9042 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9043 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 9044 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9045 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9046 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9047 | } |
| 9048 | #endif /* WIFSTOPPED */ |
| 9049 | |
| 9050 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9051 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9052 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9053 | 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] | 9054 | |
| 9055 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9056 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9057 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9058 | WAIT_TYPE status; |
| 9059 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9060 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9061 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 9062 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9063 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9064 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9065 | } |
| 9066 | #endif /* WIFSIGNALED */ |
| 9067 | |
| 9068 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9069 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9070 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 9071 | 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] | 9072 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9073 | |
| 9074 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9075 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9076 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9077 | WAIT_TYPE status; |
| 9078 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9079 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9080 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 9081 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9082 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9083 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9084 | } |
| 9085 | #endif /* WIFEXITED */ |
| 9086 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 9087 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9088 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9089 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9090 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9091 | |
| 9092 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9093 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9094 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9095 | WAIT_TYPE status; |
| 9096 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9097 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9098 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 9099 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9100 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9101 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9102 | } |
| 9103 | #endif /* WEXITSTATUS */ |
| 9104 | |
| 9105 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9106 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9107 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 9108 | 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] | 9109 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9110 | |
| 9111 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9112 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9113 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9114 | WAIT_TYPE status; |
| 9115 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9116 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9117 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 9118 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9119 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9120 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9121 | } |
| 9122 | #endif /* WTERMSIG */ |
| 9123 | |
| 9124 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9125 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9126 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9127 | Return the signal that stopped the process that provided\n\ |
| 9128 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9129 | |
| 9130 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9131 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9132 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9133 | WAIT_TYPE status; |
| 9134 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9135 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9136 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 9137 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9138 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9139 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9140 | } |
| 9141 | #endif /* WSTOPSIG */ |
| 9142 | |
| 9143 | #endif /* HAVE_SYS_WAIT_H */ |
| 9144 | |
| 9145 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9146 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 9147 | #ifdef _SCO_DS |
| 9148 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 9149 | needed definitions in sys/statvfs.h */ |
| 9150 | #define _SVID3 |
| 9151 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9152 | #include <sys/statvfs.h> |
| 9153 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9154 | static PyObject* |
| 9155 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9156 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 9157 | if (v == NULL) |
| 9158 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9159 | |
| 9160 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9161 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9162 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9163 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 9164 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 9165 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 9166 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 9167 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 9168 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 9169 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9170 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9171 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9172 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9173 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9174 | PyStructSequence_SET_ITEM(v, 2, |
| 9175 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 9176 | PyStructSequence_SET_ITEM(v, 3, |
| 9177 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 9178 | PyStructSequence_SET_ITEM(v, 4, |
| 9179 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 9180 | PyStructSequence_SET_ITEM(v, 5, |
| 9181 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 9182 | PyStructSequence_SET_ITEM(v, 6, |
| 9183 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 9184 | PyStructSequence_SET_ITEM(v, 7, |
| 9185 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 9186 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9187 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9188 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 9189 | if (PyErr_Occurred()) { |
| 9190 | Py_DECREF(v); |
| 9191 | return NULL; |
| 9192 | } |
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 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9195 | } |
| 9196 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9197 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9198 | "fstatvfs(fd) -> statvfs result\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9199 | Perform an fstatvfs system call on the given fd.\n\ |
| 9200 | Equivalent to statvfs(fd)."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9201 | |
| 9202 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9203 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9204 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9205 | int fd, res; |
| 9206 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9207 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9208 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 9209 | return NULL; |
| 9210 | Py_BEGIN_ALLOW_THREADS |
| 9211 | res = fstatvfs(fd, &st); |
| 9212 | Py_END_ALLOW_THREADS |
| 9213 | if (res != 0) |
| 9214 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9215 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9216 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9217 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9218 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9219 | |
| 9220 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9221 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9222 | #include <sys/statvfs.h> |
| 9223 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9224 | PyDoc_STRVAR(posix_statvfs__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9225 | "statvfs(path)\n\n\ |
| 9226 | Perform a statvfs system call on the given path.\n\ |
| 9227 | \n\ |
| 9228 | path may always be specified as a string.\n\ |
| 9229 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 9230 | If this functionality is unavailable, using it raises an exception."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9231 | |
| 9232 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9233 | posix_statvfs(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9234 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9235 | static char *keywords[] = {"path", NULL}; |
| 9236 | path_t path; |
| 9237 | int result; |
| 9238 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9239 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9240 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9241 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9242 | path.function_name = "statvfs"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9243 | #ifdef HAVE_FSTATVFS |
| 9244 | path.allow_fd = 1; |
| 9245 | #endif |
| 9246 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:statvfs", keywords, |
| 9247 | path_converter, &path |
| 9248 | )) |
| 9249 | return NULL; |
| 9250 | |
| 9251 | Py_BEGIN_ALLOW_THREADS |
| 9252 | #ifdef HAVE_FSTATVFS |
| 9253 | if (path.fd != -1) { |
| 9254 | #ifdef __APPLE__ |
| 9255 | /* handle weak-linking on Mac OS X 10.3 */ |
| 9256 | if (fstatvfs == NULL) { |
| 9257 | fd_specified("statvfs", path.fd); |
| 9258 | goto exit; |
| 9259 | } |
| 9260 | #endif |
| 9261 | result = fstatvfs(path.fd, &st); |
| 9262 | } |
| 9263 | else |
| 9264 | #endif |
| 9265 | result = statvfs(path.narrow, &st); |
| 9266 | Py_END_ALLOW_THREADS |
| 9267 | |
| 9268 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9269 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9270 | goto exit; |
| 9271 | } |
| 9272 | |
| 9273 | return_value = _pystatvfs_fromstructstatvfs(st); |
| 9274 | |
| 9275 | exit: |
| 9276 | path_cleanup(&path); |
| 9277 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9278 | } |
| 9279 | #endif /* HAVE_STATVFS */ |
| 9280 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9281 | #ifdef MS_WINDOWS |
| 9282 | PyDoc_STRVAR(win32__getdiskusage__doc__, |
| 9283 | "_getdiskusage(path) -> (total, free)\n\n\ |
| 9284 | Return disk usage statistics about the given path as (total, free) tuple."); |
| 9285 | |
| 9286 | static PyObject * |
| 9287 | win32__getdiskusage(PyObject *self, PyObject *args) |
| 9288 | { |
| 9289 | BOOL retval; |
| 9290 | ULARGE_INTEGER _, total, free; |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 9291 | const wchar_t *path; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9292 | |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 9293 | if (! PyArg_ParseTuple(args, "u", &path)) |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9294 | return NULL; |
| 9295 | |
| 9296 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 9297 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9298 | Py_END_ALLOW_THREADS |
| 9299 | if (retval == 0) |
| 9300 | return PyErr_SetFromWindowsErr(0); |
| 9301 | |
| 9302 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 9303 | } |
| 9304 | #endif |
| 9305 | |
| 9306 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9307 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 9308 | * It maps strings representing configuration variable names to |
| 9309 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 9310 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9311 | * rarely-used constants. There are three separate tables that use |
| 9312 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9313 | * |
| 9314 | * This code is always included, even if none of the interfaces that |
| 9315 | * need it are included. The #if hackery needed to avoid it would be |
| 9316 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9317 | */ |
| 9318 | struct constdef { |
| 9319 | char *name; |
| 9320 | long value; |
| 9321 | }; |
| 9322 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9323 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9324 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 9325 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9326 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9327 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9328 | *valuep = PyLong_AS_LONG(arg); |
| 9329 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9330 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 9331 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9332 | /* look up the value in the table using a binary search */ |
| 9333 | size_t lo = 0; |
| 9334 | size_t mid; |
| 9335 | size_t hi = tablesize; |
| 9336 | int cmp; |
| 9337 | const char *confname; |
| 9338 | if (!PyUnicode_Check(arg)) { |
| 9339 | PyErr_SetString(PyExc_TypeError, |
| 9340 | "configuration names must be strings or integers"); |
| 9341 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9342 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9343 | confname = _PyUnicode_AsString(arg); |
| 9344 | if (confname == NULL) |
| 9345 | return 0; |
| 9346 | while (lo < hi) { |
| 9347 | mid = (lo + hi) / 2; |
| 9348 | cmp = strcmp(confname, table[mid].name); |
| 9349 | if (cmp < 0) |
| 9350 | hi = mid; |
| 9351 | else if (cmp > 0) |
| 9352 | lo = mid + 1; |
| 9353 | else { |
| 9354 | *valuep = table[mid].value; |
| 9355 | return 1; |
| 9356 | } |
| 9357 | } |
| 9358 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 9359 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9360 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9361 | } |
| 9362 | |
| 9363 | |
| 9364 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 9365 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9366 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9367 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9368 | #endif |
| 9369 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9370 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9371 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9372 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9373 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9374 | #endif |
| 9375 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9376 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9377 | #endif |
| 9378 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9379 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9380 | #endif |
| 9381 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9382 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9383 | #endif |
| 9384 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9385 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9386 | #endif |
| 9387 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9388 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9389 | #endif |
| 9390 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9391 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9392 | #endif |
| 9393 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9394 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9395 | #endif |
| 9396 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9397 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9398 | #endif |
| 9399 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9400 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9401 | #endif |
| 9402 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9403 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9404 | #endif |
| 9405 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9406 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9407 | #endif |
| 9408 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9409 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9410 | #endif |
| 9411 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9412 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9413 | #endif |
| 9414 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9415 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9416 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 9417 | #ifdef _PC_ACL_ENABLED |
| 9418 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 9419 | #endif |
| 9420 | #ifdef _PC_MIN_HOLE_SIZE |
| 9421 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 9422 | #endif |
| 9423 | #ifdef _PC_ALLOC_SIZE_MIN |
| 9424 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 9425 | #endif |
| 9426 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 9427 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 9428 | #endif |
| 9429 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 9430 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 9431 | #endif |
| 9432 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 9433 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 9434 | #endif |
| 9435 | #ifdef _PC_REC_XFER_ALIGN |
| 9436 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 9437 | #endif |
| 9438 | #ifdef _PC_SYMLINK_MAX |
| 9439 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 9440 | #endif |
| 9441 | #ifdef _PC_XATTR_ENABLED |
| 9442 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 9443 | #endif |
| 9444 | #ifdef _PC_XATTR_EXISTS |
| 9445 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 9446 | #endif |
| 9447 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 9448 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 9449 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9450 | }; |
| 9451 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9452 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9453 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9454 | { |
| 9455 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 9456 | sizeof(posix_constants_pathconf) |
| 9457 | / sizeof(struct constdef)); |
| 9458 | } |
| 9459 | #endif |
| 9460 | |
| 9461 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9462 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9463 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9464 | 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] | 9465 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9466 | |
| 9467 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9468 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9469 | { |
| 9470 | PyObject *result = NULL; |
| 9471 | int name, fd; |
| 9472 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9473 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 9474 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9475 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9476 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9477 | errno = 0; |
| 9478 | limit = fpathconf(fd, name); |
| 9479 | if (limit == -1 && errno != 0) |
| 9480 | posix_error(); |
| 9481 | else |
| 9482 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9483 | } |
| 9484 | return result; |
| 9485 | } |
| 9486 | #endif |
| 9487 | |
| 9488 | |
| 9489 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9490 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9491 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9492 | Return the configuration limit name for the file or directory path.\n\ |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9493 | If there is no limit, return -1.\n\ |
| 9494 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 9495 | If this functionality is unavailable, using it raises an exception."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9496 | |
| 9497 | static PyObject * |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9498 | posix_pathconf(PyObject *self, PyObject *args, PyObject *kwargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9499 | { |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9500 | path_t path; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9501 | PyObject *result = NULL; |
| 9502 | int name; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9503 | static char *keywords[] = {"path", "name", NULL}; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9504 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9505 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9506 | path.function_name = "pathconf"; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9507 | #ifdef HAVE_FPATHCONF |
| 9508 | path.allow_fd = 1; |
| 9509 | #endif |
| 9510 | if (PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:pathconf", keywords, |
| 9511 | path_converter, &path, |
| 9512 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9513 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9514 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9515 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9516 | #ifdef HAVE_FPATHCONF |
| 9517 | if (path.fd != -1) |
| 9518 | limit = fpathconf(path.fd, name); |
| 9519 | else |
| 9520 | #endif |
| 9521 | limit = pathconf(path.narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9522 | if (limit == -1 && errno != 0) { |
| 9523 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 9524 | /* could be a path or name problem */ |
| 9525 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9526 | else |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9527 | result = path_error(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9528 | } |
| 9529 | else |
| 9530 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9531 | } |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9532 | path_cleanup(&path); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9533 | return result; |
| 9534 | } |
| 9535 | #endif |
| 9536 | |
| 9537 | #ifdef HAVE_CONFSTR |
| 9538 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9539 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9540 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9541 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9542 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9543 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9544 | #endif |
| 9545 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9546 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9547 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9548 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9549 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9550 | #endif |
| 9551 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9552 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9553 | #endif |
| 9554 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9555 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9556 | #endif |
| 9557 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9558 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9559 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9560 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9561 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9562 | #endif |
| 9563 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9564 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9565 | #endif |
| 9566 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9567 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9568 | #endif |
| 9569 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9570 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9571 | #endif |
| 9572 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9573 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9574 | #endif |
| 9575 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9576 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9577 | #endif |
| 9578 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9579 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9580 | #endif |
| 9581 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9582 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9583 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9584 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9585 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9586 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9587 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9588 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9589 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9590 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9591 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9592 | #endif |
| 9593 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9594 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9595 | #endif |
| 9596 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9597 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9598 | #endif |
| 9599 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9600 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9601 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9602 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9603 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9604 | #endif |
| 9605 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9606 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9607 | #endif |
| 9608 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9609 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9610 | #endif |
| 9611 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9612 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9613 | #endif |
| 9614 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9615 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9616 | #endif |
| 9617 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9618 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9619 | #endif |
| 9620 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9621 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9622 | #endif |
| 9623 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9624 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9625 | #endif |
| 9626 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9627 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9628 | #endif |
| 9629 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9630 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9631 | #endif |
| 9632 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9633 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9634 | #endif |
| 9635 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9636 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9637 | #endif |
| 9638 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9639 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9640 | #endif |
| 9641 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9642 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9643 | #endif |
| 9644 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9645 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9646 | #endif |
| 9647 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9648 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9649 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9650 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9651 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9652 | #endif |
| 9653 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9654 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9655 | #endif |
| 9656 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9657 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9658 | #endif |
| 9659 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9660 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9661 | #endif |
| 9662 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9663 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9664 | #endif |
| 9665 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9666 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9667 | #endif |
| 9668 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9669 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9670 | #endif |
| 9671 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9672 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9673 | #endif |
| 9674 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9675 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9676 | #endif |
| 9677 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9678 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9679 | #endif |
| 9680 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9681 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9682 | #endif |
| 9683 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9684 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9685 | #endif |
| 9686 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9687 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9688 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9689 | }; |
| 9690 | |
| 9691 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9692 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9693 | { |
| 9694 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 9695 | sizeof(posix_constants_confstr) |
| 9696 | / sizeof(struct constdef)); |
| 9697 | } |
| 9698 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9699 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9700 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9701 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9702 | |
| 9703 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9704 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9705 | { |
| 9706 | PyObject *result = NULL; |
| 9707 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9708 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9709 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9710 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9711 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 9712 | return NULL; |
| 9713 | |
| 9714 | errno = 0; |
| 9715 | len = confstr(name, buffer, sizeof(buffer)); |
| 9716 | if (len == 0) { |
| 9717 | if (errno) { |
| 9718 | posix_error(); |
| 9719 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9720 | } |
| 9721 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9722 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9723 | } |
| 9724 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9725 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9726 | if (len >= sizeof(buffer)) { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9727 | char *buf = PyMem_Malloc(len); |
| 9728 | if (buf == NULL) |
| 9729 | return PyErr_NoMemory(); |
| 9730 | confstr(name, buf, len); |
| 9731 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 9732 | PyMem_Free(buf); |
| 9733 | } |
| 9734 | else |
| 9735 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9736 | return result; |
| 9737 | } |
| 9738 | #endif |
| 9739 | |
| 9740 | |
| 9741 | #ifdef HAVE_SYSCONF |
| 9742 | static struct constdef posix_constants_sysconf[] = { |
| 9743 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9744 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9745 | #endif |
| 9746 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9747 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9748 | #endif |
| 9749 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9750 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9751 | #endif |
| 9752 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9753 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9754 | #endif |
| 9755 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9756 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9757 | #endif |
| 9758 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9759 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9760 | #endif |
| 9761 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9762 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9763 | #endif |
| 9764 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9765 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9766 | #endif |
| 9767 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9768 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9769 | #endif |
| 9770 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9771 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9772 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9773 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9774 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9775 | #endif |
| 9776 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9777 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9778 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9779 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9780 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9781 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9782 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9783 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9784 | #endif |
| 9785 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9786 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9787 | #endif |
| 9788 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9789 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9790 | #endif |
| 9791 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9792 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9793 | #endif |
| 9794 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9795 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9796 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9797 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9798 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9799 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9800 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9801 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9802 | #endif |
| 9803 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9804 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9805 | #endif |
| 9806 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9807 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9808 | #endif |
| 9809 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9810 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9811 | #endif |
| 9812 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9813 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9814 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9815 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9816 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9817 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9818 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9819 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9820 | #endif |
| 9821 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9822 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9823 | #endif |
| 9824 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9825 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9826 | #endif |
| 9827 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9828 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9829 | #endif |
| 9830 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9831 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9832 | #endif |
| 9833 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9834 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9835 | #endif |
| 9836 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9837 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9838 | #endif |
| 9839 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9840 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9841 | #endif |
| 9842 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9843 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9844 | #endif |
| 9845 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9846 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9847 | #endif |
| 9848 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9849 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9850 | #endif |
| 9851 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9852 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9853 | #endif |
| 9854 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9855 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9856 | #endif |
| 9857 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9858 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9859 | #endif |
| 9860 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9861 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9862 | #endif |
| 9863 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9864 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9865 | #endif |
| 9866 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9867 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9868 | #endif |
| 9869 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9870 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9871 | #endif |
| 9872 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9873 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9874 | #endif |
| 9875 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9876 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9877 | #endif |
| 9878 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9879 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9880 | #endif |
| 9881 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9882 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9883 | #endif |
| 9884 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9885 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9886 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9887 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9888 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9889 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9890 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9891 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9892 | #endif |
| 9893 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9894 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9895 | #endif |
| 9896 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9897 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9898 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9899 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9900 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9901 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9902 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9903 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9904 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9905 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9906 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9907 | #endif |
| 9908 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9909 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9910 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9911 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9912 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9913 | #endif |
| 9914 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9915 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9916 | #endif |
| 9917 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9918 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9919 | #endif |
| 9920 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9921 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9922 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9923 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9924 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9925 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9926 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9927 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9928 | #endif |
| 9929 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9930 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9931 | #endif |
| 9932 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9933 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9934 | #endif |
| 9935 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9936 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9937 | #endif |
| 9938 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9939 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9940 | #endif |
| 9941 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9942 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9943 | #endif |
| 9944 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9945 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9946 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9947 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9948 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9949 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9950 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9951 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9952 | #endif |
| 9953 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9954 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9955 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9956 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9957 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9958 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9959 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9960 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9961 | #endif |
| 9962 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9963 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9964 | #endif |
| 9965 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9966 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9967 | #endif |
| 9968 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9969 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9970 | #endif |
| 9971 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9972 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9973 | #endif |
| 9974 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9975 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9976 | #endif |
| 9977 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9978 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9979 | #endif |
| 9980 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9981 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9982 | #endif |
| 9983 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9984 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9985 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9986 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9987 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9988 | #endif |
| 9989 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9990 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9991 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9992 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9993 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9994 | #endif |
| 9995 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9996 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9997 | #endif |
| 9998 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9999 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10000 | #endif |
| 10001 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10002 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10003 | #endif |
| 10004 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10005 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10006 | #endif |
| 10007 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10008 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10009 | #endif |
| 10010 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10011 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10012 | #endif |
| 10013 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10014 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10015 | #endif |
| 10016 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10017 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10018 | #endif |
| 10019 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10020 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10021 | #endif |
| 10022 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10023 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10024 | #endif |
| 10025 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10026 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10027 | #endif |
| 10028 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10029 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10030 | #endif |
| 10031 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10032 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10033 | #endif |
| 10034 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10035 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10036 | #endif |
| 10037 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10038 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10039 | #endif |
| 10040 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10041 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10042 | #endif |
| 10043 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10044 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10045 | #endif |
| 10046 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10047 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10048 | #endif |
| 10049 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10050 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10051 | #endif |
| 10052 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10053 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10054 | #endif |
| 10055 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10056 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10057 | #endif |
| 10058 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10059 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10060 | #endif |
| 10061 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10062 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10063 | #endif |
| 10064 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10065 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10066 | #endif |
| 10067 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10068 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10069 | #endif |
| 10070 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10071 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10072 | #endif |
| 10073 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10074 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10075 | #endif |
| 10076 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10077 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10078 | #endif |
| 10079 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10080 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10081 | #endif |
| 10082 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10083 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10084 | #endif |
| 10085 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10086 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10087 | #endif |
| 10088 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10089 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10090 | #endif |
| 10091 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10092 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10093 | #endif |
| 10094 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10095 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10096 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10097 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10098 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10099 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10100 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10101 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10102 | #endif |
| 10103 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10104 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10105 | #endif |
| 10106 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10107 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10108 | #endif |
| 10109 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10110 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10111 | #endif |
| 10112 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10113 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10114 | #endif |
| 10115 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10116 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10117 | #endif |
| 10118 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10119 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10120 | #endif |
| 10121 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10122 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10123 | #endif |
| 10124 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10125 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10126 | #endif |
| 10127 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10128 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10129 | #endif |
| 10130 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10131 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10132 | #endif |
| 10133 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10134 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10135 | #endif |
| 10136 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10137 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10138 | #endif |
| 10139 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10140 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10141 | #endif |
| 10142 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10143 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10144 | #endif |
| 10145 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10146 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10147 | #endif |
| 10148 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10149 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10150 | #endif |
| 10151 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10152 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10153 | #endif |
| 10154 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10155 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10156 | #endif |
| 10157 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10158 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10159 | #endif |
| 10160 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10161 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10162 | #endif |
| 10163 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10164 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10165 | #endif |
| 10166 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10167 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10168 | #endif |
| 10169 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10170 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10171 | #endif |
| 10172 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10173 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10174 | #endif |
| 10175 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10176 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10177 | #endif |
| 10178 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10179 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10180 | #endif |
| 10181 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10182 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10183 | #endif |
| 10184 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10185 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10186 | #endif |
| 10187 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10188 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10189 | #endif |
| 10190 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10191 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10192 | #endif |
| 10193 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10194 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10195 | #endif |
| 10196 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10197 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10198 | #endif |
| 10199 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10200 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10201 | #endif |
| 10202 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10203 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10204 | #endif |
| 10205 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10206 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10207 | #endif |
| 10208 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10209 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10210 | #endif |
| 10211 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10212 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10213 | #endif |
| 10214 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10215 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10216 | #endif |
| 10217 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10218 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10219 | #endif |
| 10220 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10221 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10222 | #endif |
| 10223 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10224 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10225 | #endif |
| 10226 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10227 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10228 | #endif |
| 10229 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10230 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10231 | #endif |
| 10232 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10233 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10234 | #endif |
| 10235 | }; |
| 10236 | |
| 10237 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10238 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10239 | { |
| 10240 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 10241 | sizeof(posix_constants_sysconf) |
| 10242 | / sizeof(struct constdef)); |
| 10243 | } |
| 10244 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10245 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 10246 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10247 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10248 | |
| 10249 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10250 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10251 | { |
| 10252 | PyObject *result = NULL; |
| 10253 | int name; |
| 10254 | |
| 10255 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
Victor Stinner | 6fdd7b8 | 2013-05-16 22:26:29 +0200 | [diff] [blame] | 10256 | long value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10257 | |
| 10258 | errno = 0; |
| 10259 | value = sysconf(name); |
| 10260 | if (value == -1 && errno != 0) |
| 10261 | posix_error(); |
| 10262 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10263 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10264 | } |
| 10265 | return result; |
| 10266 | } |
| 10267 | #endif |
| 10268 | |
| 10269 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10270 | /* This code is used to ensure that the tables of configuration value names |
| 10271 | * are in sorted order as required by conv_confname(), and also to build the |
| 10272 | * the exported dictionaries that are used to publish information about the |
| 10273 | * names available on the host platform. |
| 10274 | * |
| 10275 | * Sorting the table at runtime ensures that the table is properly ordered |
| 10276 | * when used, even for platforms we're not able to test on. It also makes |
| 10277 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10278 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10279 | |
| 10280 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10281 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10282 | { |
| 10283 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10284 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10285 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10286 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10287 | |
| 10288 | return strcmp(c1->name, c2->name); |
| 10289 | } |
| 10290 | |
| 10291 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10292 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10293 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10294 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10295 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10296 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10297 | |
| 10298 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 10299 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10300 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10301 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10302 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10303 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10304 | PyObject *o = PyLong_FromLong(table[i].value); |
| 10305 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 10306 | Py_XDECREF(o); |
| 10307 | Py_DECREF(d); |
| 10308 | return -1; |
| 10309 | } |
| 10310 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10311 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10312 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10313 | } |
| 10314 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10315 | /* Return -1 on failure, 0 on success. */ |
| 10316 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10317 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10318 | { |
| 10319 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10320 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10321 | sizeof(posix_constants_pathconf) |
| 10322 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10323 | "pathconf_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 |
| 10326 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10327 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10328 | sizeof(posix_constants_confstr) |
| 10329 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10330 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10331 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10332 | #endif |
| 10333 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10334 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10335 | sizeof(posix_constants_sysconf) |
| 10336 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10337 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10338 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10339 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10340 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10341 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10342 | |
| 10343 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10344 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 10345 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10346 | 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] | 10347 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10348 | |
| 10349 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 10350 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10351 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10352 | abort(); |
| 10353 | /*NOTREACHED*/ |
| 10354 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 10355 | return NULL; |
| 10356 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10357 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10358 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10359 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 10360 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 10361 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10362 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 10363 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 10364 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 10365 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 10366 | application (if any) its extension is associated.\n\ |
| 10367 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 10368 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10369 | \n\ |
| 10370 | startfile returns as soon as the associated application is launched.\n\ |
| 10371 | There is no option to wait for the application to close, and no way\n\ |
| 10372 | to retrieve the application's exit status.\n\ |
| 10373 | \n\ |
| 10374 | The filepath is relative to the current directory. If you want to use\n\ |
| 10375 | 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] | 10376 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10377 | |
| 10378 | static PyObject * |
| 10379 | win32_startfile(PyObject *self, PyObject *args) |
| 10380 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10381 | PyObject *ofilepath; |
| 10382 | char *filepath; |
| 10383 | char *operation = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10384 | wchar_t *wpath, *woperation; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10385 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 10386 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10387 | PyObject *unipath, *uoperation = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10388 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 10389 | &unipath, &operation)) { |
| 10390 | PyErr_Clear(); |
| 10391 | goto normal; |
| 10392 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10393 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10394 | if (operation) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10395 | uoperation = PyUnicode_DecodeASCII(operation, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10396 | strlen(operation), NULL); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10397 | if (!uoperation) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10398 | PyErr_Clear(); |
| 10399 | operation = NULL; |
| 10400 | goto normal; |
| 10401 | } |
| 10402 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10403 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10404 | wpath = PyUnicode_AsUnicode(unipath); |
| 10405 | if (wpath == NULL) |
| 10406 | goto normal; |
| 10407 | if (uoperation) { |
| 10408 | woperation = PyUnicode_AsUnicode(uoperation); |
| 10409 | if (woperation == NULL) |
| 10410 | goto normal; |
| 10411 | } |
| 10412 | else |
| 10413 | woperation = NULL; |
| 10414 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10415 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10416 | rc = ShellExecuteW((HWND)0, woperation, wpath, |
| 10417 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10418 | Py_END_ALLOW_THREADS |
| 10419 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10420 | Py_XDECREF(uoperation); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10421 | if (rc <= (HINSTANCE)32) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10422 | win32_error_object("startfile", unipath); |
| 10423 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10424 | } |
| 10425 | Py_INCREF(Py_None); |
| 10426 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10427 | |
| 10428 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10429 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 10430 | PyUnicode_FSConverter, &ofilepath, |
| 10431 | &operation)) |
| 10432 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 10433 | if (win32_warn_bytes_api()) { |
| 10434 | Py_DECREF(ofilepath); |
| 10435 | return NULL; |
| 10436 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10437 | filepath = PyBytes_AsString(ofilepath); |
| 10438 | Py_BEGIN_ALLOW_THREADS |
| 10439 | rc = ShellExecute((HWND)0, operation, filepath, |
| 10440 | NULL, NULL, SW_SHOWNORMAL); |
| 10441 | Py_END_ALLOW_THREADS |
| 10442 | if (rc <= (HINSTANCE)32) { |
| 10443 | PyObject *errval = win32_error("startfile", filepath); |
| 10444 | Py_DECREF(ofilepath); |
| 10445 | return errval; |
| 10446 | } |
| 10447 | Py_DECREF(ofilepath); |
| 10448 | Py_INCREF(Py_None); |
| 10449 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10450 | } |
| 10451 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10452 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10453 | #ifdef HAVE_GETLOADAVG |
| 10454 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 10455 | "getloadavg() -> (float, float, float)\n\n\ |
| 10456 | Return the number of processes in the system run queue averaged over\n\ |
| 10457 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 10458 | was unobtainable"); |
| 10459 | |
| 10460 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 10461 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10462 | { |
| 10463 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10464 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10465 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 10466 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10467 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10468 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10469 | } |
| 10470 | #endif |
| 10471 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 10472 | PyDoc_STRVAR(device_encoding__doc__, |
| 10473 | "device_encoding(fd) -> str\n\n\ |
| 10474 | Return a string describing the encoding of the device\n\ |
| 10475 | if the output is a terminal; else return None."); |
| 10476 | |
| 10477 | static PyObject * |
| 10478 | device_encoding(PyObject *self, PyObject *args) |
| 10479 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10480 | int fd; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10481 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10482 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 10483 | return NULL; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10484 | |
| 10485 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 10486 | } |
| 10487 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10488 | #ifdef HAVE_SETRESUID |
| 10489 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 10490 | "setresuid(ruid, euid, suid)\n\n\ |
| 10491 | Set the current process's real, effective, and saved user ids."); |
| 10492 | |
| 10493 | static PyObject* |
| 10494 | posix_setresuid (PyObject *self, PyObject *args) |
| 10495 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10496 | /* We assume uid_t is no larger than a long. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10497 | uid_t ruid, euid, suid; |
| 10498 | if (!PyArg_ParseTuple(args, "O&O&O&:setresuid", |
| 10499 | _Py_Uid_Converter, &ruid, |
| 10500 | _Py_Uid_Converter, &euid, |
| 10501 | _Py_Uid_Converter, &suid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10502 | return NULL; |
| 10503 | if (setresuid(ruid, euid, suid) < 0) |
| 10504 | return posix_error(); |
| 10505 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10506 | } |
| 10507 | #endif |
| 10508 | |
| 10509 | #ifdef HAVE_SETRESGID |
| 10510 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 10511 | "setresgid(rgid, egid, sgid)\n\n\ |
| 10512 | Set the current process's real, effective, and saved group ids."); |
| 10513 | |
| 10514 | static PyObject* |
| 10515 | posix_setresgid (PyObject *self, PyObject *args) |
| 10516 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10517 | gid_t rgid, egid, sgid; |
| 10518 | if (!PyArg_ParseTuple(args, "O&O&O&:setresgid", |
| 10519 | _Py_Gid_Converter, &rgid, |
| 10520 | _Py_Gid_Converter, &egid, |
| 10521 | _Py_Gid_Converter, &sgid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10522 | return NULL; |
| 10523 | if (setresgid(rgid, egid, sgid) < 0) |
| 10524 | return posix_error(); |
| 10525 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10526 | } |
| 10527 | #endif |
| 10528 | |
| 10529 | #ifdef HAVE_GETRESUID |
| 10530 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 10531 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 10532 | Get tuple of the current process's real, effective, and saved user ids."); |
| 10533 | |
| 10534 | static PyObject* |
| 10535 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 10536 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10537 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10538 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 10539 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10540 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 10541 | _PyLong_FromUid(euid), |
| 10542 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10543 | } |
| 10544 | #endif |
| 10545 | |
| 10546 | #ifdef HAVE_GETRESGID |
| 10547 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 10548 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 10549 | 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] | 10550 | |
| 10551 | static PyObject* |
| 10552 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 10553 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10554 | uid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10555 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 10556 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10557 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 10558 | _PyLong_FromGid(egid), |
| 10559 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10560 | } |
| 10561 | #endif |
| 10562 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10563 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10564 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10565 | PyDoc_STRVAR(posix_getxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10566 | "getxattr(path, attribute, *, follow_symlinks=True) -> value\n\n\ |
| 10567 | Return the value of extended attribute attribute on path.\n\ |
| 10568 | \n\ |
| 10569 | path may be either a string or an open file descriptor.\n\ |
| 10570 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10571 | link, getxattr will examine the symbolic link itself instead of the file\n\ |
| 10572 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10573 | |
| 10574 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10575 | posix_getxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10576 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10577 | path_t path; |
| 10578 | path_t attribute; |
| 10579 | int follow_symlinks = 1; |
| 10580 | PyObject *buffer = NULL; |
| 10581 | int i; |
| 10582 | static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
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 | memset(&path, 0, sizeof(path)); |
| 10585 | memset(&attribute, 0, sizeof(attribute)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10586 | path.function_name = "getxattr"; |
| 10587 | attribute.function_name = "getxattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10588 | path.allow_fd = 1; |
| 10589 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:getxattr", keywords, |
| 10590 | path_converter, &path, |
| 10591 | path_converter, &attribute, |
| 10592 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10593 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10594 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10595 | if (fd_and_follow_symlinks_invalid("getxattr", path.fd, follow_symlinks)) |
| 10596 | goto exit; |
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 | for (i = 0; ; i++) { |
| 10599 | void *ptr; |
| 10600 | ssize_t result; |
| 10601 | static Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
| 10602 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10603 | if (!buffer_size) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10604 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10605 | goto exit; |
| 10606 | } |
| 10607 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 10608 | if (!buffer) |
| 10609 | goto exit; |
| 10610 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10611 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10612 | Py_BEGIN_ALLOW_THREADS; |
| 10613 | if (path.fd >= 0) |
| 10614 | result = fgetxattr(path.fd, attribute.narrow, ptr, buffer_size); |
| 10615 | else if (follow_symlinks) |
| 10616 | result = getxattr(path.narrow, attribute.narrow, ptr, buffer_size); |
| 10617 | else |
| 10618 | result = lgetxattr(path.narrow, attribute.narrow, ptr, buffer_size); |
| 10619 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10620 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10621 | if (result < 0) { |
| 10622 | Py_DECREF(buffer); |
| 10623 | buffer = NULL; |
| 10624 | if (errno == ERANGE) |
| 10625 | continue; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10626 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10627 | goto exit; |
| 10628 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10629 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10630 | if (result != buffer_size) { |
| 10631 | /* Can only shrink. */ |
| 10632 | _PyBytes_Resize(&buffer, result); |
| 10633 | } |
| 10634 | break; |
| 10635 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10636 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10637 | exit: |
| 10638 | path_cleanup(&path); |
| 10639 | path_cleanup(&attribute); |
| 10640 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10641 | } |
| 10642 | |
| 10643 | PyDoc_STRVAR(posix_setxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10644 | "setxattr(path, attribute, value, flags=0, *, follow_symlinks=True)\n\n\ |
| 10645 | Set extended attribute attribute on path to value.\n\ |
| 10646 | path may be either a string or an open file descriptor.\n\ |
| 10647 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10648 | link, setxattr will modify the symbolic link itself instead of the file\n\ |
| 10649 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10650 | |
| 10651 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10652 | posix_setxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10653 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10654 | path_t path; |
| 10655 | path_t attribute; |
| 10656 | Py_buffer value; |
| 10657 | int flags = 0; |
| 10658 | int follow_symlinks = 1; |
| 10659 | int result; |
| 10660 | PyObject *return_value = NULL; |
| 10661 | static char *keywords[] = {"path", "attribute", "value", |
| 10662 | "flags", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10663 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10664 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10665 | path.function_name = "setxattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10666 | path.allow_fd = 1; |
| 10667 | memset(&attribute, 0, sizeof(attribute)); |
| 10668 | memset(&value, 0, sizeof(value)); |
| 10669 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&y*|i$p:setxattr", |
| 10670 | keywords, |
| 10671 | path_converter, &path, |
| 10672 | path_converter, &attribute, |
| 10673 | &value, &flags, |
| 10674 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10675 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10676 | |
| 10677 | if (fd_and_follow_symlinks_invalid("setxattr", path.fd, follow_symlinks)) |
| 10678 | goto exit; |
| 10679 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10680 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10681 | if (path.fd > -1) |
| 10682 | result = fsetxattr(path.fd, attribute.narrow, |
| 10683 | value.buf, value.len, flags); |
| 10684 | else if (follow_symlinks) |
| 10685 | result = setxattr(path.narrow, attribute.narrow, |
| 10686 | value.buf, value.len, flags); |
| 10687 | else |
| 10688 | result = lsetxattr(path.narrow, attribute.narrow, |
| 10689 | value.buf, value.len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10690 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10691 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10692 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10693 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10694 | goto exit; |
| 10695 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10696 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10697 | return_value = Py_None; |
| 10698 | Py_INCREF(return_value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10699 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10700 | exit: |
| 10701 | path_cleanup(&path); |
| 10702 | path_cleanup(&attribute); |
| 10703 | PyBuffer_Release(&value); |
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 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10706 | } |
| 10707 | |
| 10708 | PyDoc_STRVAR(posix_removexattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10709 | "removexattr(path, attribute, *, follow_symlinks=True)\n\n\ |
| 10710 | Remove extended attribute attribute on path.\n\ |
| 10711 | path may be either a string or an open file descriptor.\n\ |
| 10712 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10713 | link, removexattr will modify the symbolic link itself instead of the file\n\ |
| 10714 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10715 | |
| 10716 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10717 | posix_removexattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10718 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10719 | path_t path; |
| 10720 | path_t attribute; |
| 10721 | int follow_symlinks = 1; |
| 10722 | int result; |
| 10723 | PyObject *return_value = NULL; |
| 10724 | static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10725 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10726 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10727 | path.function_name = "removexattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10728 | memset(&attribute, 0, sizeof(attribute)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10729 | attribute.function_name = "removexattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10730 | path.allow_fd = 1; |
| 10731 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:removexattr", |
| 10732 | keywords, |
| 10733 | path_converter, &path, |
| 10734 | path_converter, &attribute, |
| 10735 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10736 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10737 | |
| 10738 | if (fd_and_follow_symlinks_invalid("removexattr", path.fd, follow_symlinks)) |
| 10739 | goto exit; |
| 10740 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10741 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10742 | if (path.fd > -1) |
| 10743 | result = fremovexattr(path.fd, attribute.narrow); |
| 10744 | else if (follow_symlinks) |
| 10745 | result = removexattr(path.narrow, attribute.narrow); |
| 10746 | else |
| 10747 | result = lremovexattr(path.narrow, attribute.narrow); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10748 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10749 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10750 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10751 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10752 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10753 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10754 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10755 | return_value = Py_None; |
| 10756 | Py_INCREF(return_value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10757 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10758 | exit: |
| 10759 | path_cleanup(&path); |
| 10760 | path_cleanup(&attribute); |
| 10761 | |
| 10762 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10763 | } |
| 10764 | |
| 10765 | PyDoc_STRVAR(posix_listxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10766 | "listxattr(path='.', *, follow_symlinks=True)\n\n\ |
| 10767 | Return a list of extended attributes on path.\n\ |
| 10768 | \n\ |
| 10769 | path may be either None, a string, or an open file descriptor.\n\ |
| 10770 | if path is None, listxattr will examine the current directory.\n\ |
| 10771 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10772 | link, listxattr will examine the symbolic link itself instead of the file\n\ |
| 10773 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10774 | |
| 10775 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10776 | posix_listxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10777 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10778 | path_t path; |
| 10779 | int follow_symlinks = 1; |
| 10780 | Py_ssize_t i; |
| 10781 | PyObject *result = NULL; |
| 10782 | char *buffer = NULL; |
| 10783 | char *name; |
| 10784 | static char *keywords[] = {"path", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10785 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10786 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10787 | path.function_name = "listxattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10788 | path.allow_fd = 1; |
| 10789 | path.fd = -1; |
| 10790 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&$p:listxattr", keywords, |
| 10791 | path_converter, &path, |
| 10792 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10793 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10794 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10795 | if (fd_and_follow_symlinks_invalid("listxattr", path.fd, follow_symlinks)) |
| 10796 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10797 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10798 | name = path.narrow ? path.narrow : "."; |
| 10799 | for (i = 0; ; i++) { |
| 10800 | char *start, *trace, *end; |
| 10801 | ssize_t length; |
| 10802 | static Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
| 10803 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10804 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 10805 | /* ERANGE */ |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10806 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10807 | break; |
| 10808 | } |
| 10809 | buffer = PyMem_MALLOC(buffer_size); |
| 10810 | if (!buffer) { |
| 10811 | PyErr_NoMemory(); |
| 10812 | break; |
| 10813 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10814 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10815 | Py_BEGIN_ALLOW_THREADS; |
| 10816 | if (path.fd > -1) |
| 10817 | length = flistxattr(path.fd, buffer, buffer_size); |
| 10818 | else if (follow_symlinks) |
| 10819 | length = listxattr(name, buffer, buffer_size); |
| 10820 | else |
| 10821 | length = llistxattr(name, buffer, buffer_size); |
| 10822 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10823 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10824 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 10825 | if (errno == ERANGE) { |
| 10826 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 10827 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10828 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 10829 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10830 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10831 | break; |
| 10832 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10833 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10834 | result = PyList_New(0); |
| 10835 | if (!result) { |
| 10836 | goto exit; |
| 10837 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10838 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10839 | end = buffer + length; |
| 10840 | for (trace = start = buffer; trace != end; trace++) { |
| 10841 | if (!*trace) { |
| 10842 | int error; |
| 10843 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 10844 | trace - start); |
| 10845 | if (!attribute) { |
| 10846 | Py_DECREF(result); |
| 10847 | result = NULL; |
| 10848 | goto exit; |
| 10849 | } |
| 10850 | error = PyList_Append(result, attribute); |
| 10851 | Py_DECREF(attribute); |
| 10852 | if (error) { |
| 10853 | Py_DECREF(result); |
| 10854 | result = NULL; |
| 10855 | goto exit; |
| 10856 | } |
| 10857 | start = trace + 1; |
| 10858 | } |
| 10859 | } |
| 10860 | break; |
| 10861 | } |
| 10862 | exit: |
| 10863 | path_cleanup(&path); |
| 10864 | if (buffer) |
| 10865 | PyMem_FREE(buffer); |
| 10866 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10867 | } |
| 10868 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10869 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10870 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10871 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10872 | PyDoc_STRVAR(posix_urandom__doc__, |
| 10873 | "urandom(n) -> str\n\n\ |
| 10874 | Return n random bytes suitable for cryptographic use."); |
| 10875 | |
| 10876 | static PyObject * |
| 10877 | posix_urandom(PyObject *self, PyObject *args) |
| 10878 | { |
| 10879 | Py_ssize_t size; |
| 10880 | PyObject *result; |
| 10881 | int ret; |
| 10882 | |
| 10883 | /* Read arguments */ |
| 10884 | if (!PyArg_ParseTuple(args, "n:urandom", &size)) |
| 10885 | return NULL; |
| 10886 | if (size < 0) |
| 10887 | return PyErr_Format(PyExc_ValueError, |
| 10888 | "negative argument not allowed"); |
| 10889 | result = PyBytes_FromStringAndSize(NULL, size); |
| 10890 | if (result == NULL) |
| 10891 | return NULL; |
| 10892 | |
| 10893 | ret = _PyOS_URandom(PyBytes_AS_STRING(result), |
| 10894 | PyBytes_GET_SIZE(result)); |
| 10895 | if (ret == -1) { |
| 10896 | Py_DECREF(result); |
| 10897 | return NULL; |
| 10898 | } |
| 10899 | return result; |
| 10900 | } |
| 10901 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10902 | /* Terminal size querying */ |
| 10903 | |
| 10904 | static PyTypeObject TerminalSizeType; |
| 10905 | |
| 10906 | PyDoc_STRVAR(TerminalSize_docstring, |
| 10907 | "A tuple of (columns, lines) for holding terminal window size"); |
| 10908 | |
| 10909 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 10910 | {"columns", "width of the terminal window in characters"}, |
| 10911 | {"lines", "height of the terminal window in characters"}, |
| 10912 | {NULL, NULL} |
| 10913 | }; |
| 10914 | |
| 10915 | static PyStructSequence_Desc TerminalSize_desc = { |
| 10916 | "os.terminal_size", |
| 10917 | TerminalSize_docstring, |
| 10918 | TerminalSize_fields, |
| 10919 | 2, |
| 10920 | }; |
| 10921 | |
| 10922 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 10923 | PyDoc_STRVAR(termsize__doc__, |
| 10924 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 10925 | "\n" \ |
| 10926 | "The optional argument fd (default standard output) specifies\n" \ |
| 10927 | "which file descriptor should be queried.\n" \ |
| 10928 | "\n" \ |
| 10929 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 10930 | "is thrown.\n" \ |
| 10931 | "\n" \ |
| 10932 | "This function will only be defined if an implementation is\n" \ |
| 10933 | "available for this system.\n" \ |
| 10934 | "\n" \ |
| 10935 | "shutil.get_terminal_size is the high-level function which should \n" \ |
| 10936 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 10937 | |
| 10938 | static PyObject* |
| 10939 | get_terminal_size(PyObject *self, PyObject *args) |
| 10940 | { |
| 10941 | int columns, lines; |
| 10942 | PyObject *termsize; |
| 10943 | |
| 10944 | int fd = fileno(stdout); |
| 10945 | /* Under some conditions stdout may not be connected and |
| 10946 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 10947 | * GUI apps don't have valid standard streams by default. |
| 10948 | * |
| 10949 | * If this happens, and the optional fd argument is not present, |
| 10950 | * the ioctl below will fail returning EBADF. This is what we want. |
| 10951 | */ |
| 10952 | |
| 10953 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 10954 | return NULL; |
| 10955 | |
| 10956 | #ifdef TERMSIZE_USE_IOCTL |
| 10957 | { |
| 10958 | struct winsize w; |
| 10959 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 10960 | return PyErr_SetFromErrno(PyExc_OSError); |
| 10961 | columns = w.ws_col; |
| 10962 | lines = w.ws_row; |
| 10963 | } |
| 10964 | #endif /* TERMSIZE_USE_IOCTL */ |
| 10965 | |
| 10966 | #ifdef TERMSIZE_USE_CONIO |
| 10967 | { |
| 10968 | DWORD nhandle; |
| 10969 | HANDLE handle; |
| 10970 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 10971 | switch (fd) { |
| 10972 | case 0: nhandle = STD_INPUT_HANDLE; |
| 10973 | break; |
| 10974 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 10975 | break; |
| 10976 | case 2: nhandle = STD_ERROR_HANDLE; |
| 10977 | break; |
| 10978 | default: |
| 10979 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 10980 | } |
| 10981 | handle = GetStdHandle(nhandle); |
| 10982 | if (handle == NULL) |
| 10983 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 10984 | if (handle == INVALID_HANDLE_VALUE) |
| 10985 | return PyErr_SetFromWindowsErr(0); |
| 10986 | |
| 10987 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 10988 | return PyErr_SetFromWindowsErr(0); |
| 10989 | |
| 10990 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 10991 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 10992 | } |
| 10993 | #endif /* TERMSIZE_USE_CONIO */ |
| 10994 | |
| 10995 | termsize = PyStructSequence_New(&TerminalSizeType); |
| 10996 | if (termsize == NULL) |
| 10997 | return NULL; |
| 10998 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 10999 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 11000 | if (PyErr_Occurred()) { |
| 11001 | Py_DECREF(termsize); |
| 11002 | return NULL; |
| 11003 | } |
| 11004 | return termsize; |
| 11005 | } |
| 11006 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 11007 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11008 | PyDoc_STRVAR(posix_cpu_count__doc__, |
| 11009 | "cpu_count() -> integer\n\n\ |
| 11010 | Return the number of CPUs in the system, or None if this value cannot be\n\ |
| 11011 | established."); |
| 11012 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11013 | static PyObject * |
| 11014 | posix_cpu_count(PyObject *self) |
| 11015 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11016 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11017 | #ifdef MS_WINDOWS |
| 11018 | SYSTEM_INFO sysinfo; |
| 11019 | GetSystemInfo(&sysinfo); |
| 11020 | ncpu = sysinfo.dwNumberOfProcessors; |
| 11021 | #elif defined(__hpux) |
| 11022 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 11023 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 11024 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11025 | #elif defined(__DragonFly__) || \ |
| 11026 | defined(__OpenBSD__) || \ |
| 11027 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11028 | defined(__NetBSD__) || \ |
| 11029 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 11030 | int mib[2]; |
| 11031 | size_t len = sizeof(ncpu); |
| 11032 | mib[0] = CTL_HW; |
| 11033 | mib[1] = HW_NCPU; |
| 11034 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 11035 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11036 | #endif |
| 11037 | if (ncpu >= 1) |
| 11038 | return PyLong_FromLong(ncpu); |
| 11039 | else |
| 11040 | Py_RETURN_NONE; |
| 11041 | } |
| 11042 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11043 | PyDoc_STRVAR(get_inheritable__doc__, |
| 11044 | "get_inheritable(fd) -> bool\n" \ |
| 11045 | "\n" \ |
| 11046 | "Get the close-on-exe flag of the specified file descriptor."); |
| 11047 | |
| 11048 | static PyObject* |
| 11049 | posix_get_inheritable(PyObject *self, PyObject *args) |
| 11050 | { |
| 11051 | int fd; |
| 11052 | int inheritable; |
| 11053 | |
| 11054 | if (!PyArg_ParseTuple(args, "i:get_inheritable", &fd)) |
| 11055 | return NULL; |
| 11056 | |
| 11057 | if (!_PyVerify_fd(fd)) |
| 11058 | return posix_error(); |
| 11059 | |
| 11060 | inheritable = _Py_get_inheritable(fd); |
| 11061 | if (inheritable < 0) |
| 11062 | return NULL; |
| 11063 | return PyBool_FromLong(inheritable); |
| 11064 | } |
| 11065 | |
| 11066 | PyDoc_STRVAR(set_inheritable__doc__, |
| 11067 | "set_inheritable(fd, inheritable)\n" \ |
| 11068 | "\n" \ |
| 11069 | "Set the inheritable flag of the specified file descriptor."); |
| 11070 | |
| 11071 | static PyObject* |
| 11072 | posix_set_inheritable(PyObject *self, PyObject *args) |
| 11073 | { |
| 11074 | int fd, inheritable; |
| 11075 | |
| 11076 | if (!PyArg_ParseTuple(args, "ii:set_inheritable", &fd, &inheritable)) |
| 11077 | return NULL; |
| 11078 | |
| 11079 | if (!_PyVerify_fd(fd)) |
| 11080 | return posix_error(); |
| 11081 | |
| 11082 | if (_Py_set_inheritable(fd, inheritable, NULL) < 0) |
| 11083 | return NULL; |
| 11084 | Py_RETURN_NONE; |
| 11085 | } |
| 11086 | |
| 11087 | |
| 11088 | #ifdef MS_WINDOWS |
| 11089 | PyDoc_STRVAR(get_handle_inheritable__doc__, |
| 11090 | "get_handle_inheritable(fd) -> bool\n" \ |
| 11091 | "\n" \ |
| 11092 | "Get the close-on-exe flag of the specified file descriptor."); |
| 11093 | |
| 11094 | static PyObject* |
| 11095 | posix_get_handle_inheritable(PyObject *self, PyObject *args) |
| 11096 | { |
| 11097 | Py_intptr_t handle; |
| 11098 | DWORD flags; |
| 11099 | |
| 11100 | if (!PyArg_ParseTuple(args, _Py_PARSE_INTPTR ":get_handle_inheritable", &handle)) |
| 11101 | return NULL; |
| 11102 | |
| 11103 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 11104 | PyErr_SetFromWindowsErr(0); |
| 11105 | return NULL; |
| 11106 | } |
| 11107 | |
| 11108 | return PyBool_FromLong(flags & HANDLE_FLAG_INHERIT); |
| 11109 | } |
| 11110 | |
| 11111 | PyDoc_STRVAR(set_handle_inheritable__doc__, |
| 11112 | "set_handle_inheritable(fd, inheritable)\n" \ |
| 11113 | "\n" \ |
| 11114 | "Set the inheritable flag of the specified handle."); |
| 11115 | |
| 11116 | static PyObject* |
| 11117 | posix_set_handle_inheritable(PyObject *self, PyObject *args) |
| 11118 | { |
| 11119 | int inheritable = 1; |
| 11120 | Py_intptr_t handle; |
| 11121 | DWORD flags; |
| 11122 | |
| 11123 | if (!PyArg_ParseTuple(args, _Py_PARSE_INTPTR "i:set_handle_inheritable", |
| 11124 | &handle, &inheritable)) |
| 11125 | return NULL; |
| 11126 | |
| 11127 | if (inheritable) |
| 11128 | flags = HANDLE_FLAG_INHERIT; |
| 11129 | else |
| 11130 | flags = 0; |
| 11131 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 11132 | PyErr_SetFromWindowsErr(0); |
| 11133 | return NULL; |
| 11134 | } |
| 11135 | Py_RETURN_NONE; |
| 11136 | } |
| 11137 | #endif /* MS_WINDOWS */ |
| 11138 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11139 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 11140 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11141 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 11142 | |
| 11143 | OS_STAT_METHODDEF |
| 11144 | OS_ACCESS_METHODDEF |
| 11145 | OS_TTYNAME_METHODDEF |
| 11146 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11147 | {"chdir", (PyCFunction)posix_chdir, |
| 11148 | METH_VARARGS | METH_KEYWORDS, |
| 11149 | posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 11150 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11151 | {"chflags", (PyCFunction)posix_chflags, |
| 11152 | METH_VARARGS | METH_KEYWORDS, |
| 11153 | posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 11154 | #endif /* HAVE_CHFLAGS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11155 | {"chmod", (PyCFunction)posix_chmod, |
| 11156 | METH_VARARGS | METH_KEYWORDS, |
| 11157 | posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 11158 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11159 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 11160 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11161 | #ifdef HAVE_CHOWN |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11162 | {"chown", (PyCFunction)posix_chown, |
| 11163 | METH_VARARGS | METH_KEYWORDS, |
| 11164 | posix_chown__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 11165 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 11166 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11167 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 11168 | #endif /* HAVE_LCHMOD */ |
| 11169 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11170 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 11171 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 11172 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11173 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 11174 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 11175 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11176 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 11177 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 11178 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11179 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 11180 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11181 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11182 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11183 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11184 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 11185 | METH_NOARGS, posix_getcwd__doc__}, |
| 11186 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 11187 | METH_NOARGS, posix_getcwdb__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11188 | #if defined(HAVE_LINK) || defined(MS_WINDOWS) |
| 11189 | {"link", (PyCFunction)posix_link, |
| 11190 | METH_VARARGS | METH_KEYWORDS, |
| 11191 | posix_link__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11192 | #endif /* HAVE_LINK */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11193 | {"listdir", (PyCFunction)posix_listdir, |
| 11194 | METH_VARARGS | METH_KEYWORDS, |
| 11195 | posix_listdir__doc__}, |
| 11196 | {"lstat", (PyCFunction)posix_lstat, |
| 11197 | METH_VARARGS | METH_KEYWORDS, |
| 11198 | posix_lstat__doc__}, |
| 11199 | {"mkdir", (PyCFunction)posix_mkdir, |
| 11200 | METH_VARARGS | METH_KEYWORDS, |
| 11201 | posix_mkdir__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 11202 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11203 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 11204 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11205 | #ifdef HAVE_GETPRIORITY |
| 11206 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 11207 | #endif /* HAVE_GETPRIORITY */ |
| 11208 | #ifdef HAVE_SETPRIORITY |
| 11209 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 11210 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 11211 | #ifdef HAVE_READLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11212 | {"readlink", (PyCFunction)posix_readlink, |
| 11213 | METH_VARARGS | METH_KEYWORDS, |
| 11214 | readlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 11215 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 11216 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11217 | {"readlink", (PyCFunction)win_readlink, |
| 11218 | METH_VARARGS | METH_KEYWORDS, |
| 11219 | readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 11220 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11221 | {"rename", (PyCFunction)posix_rename, |
| 11222 | METH_VARARGS | METH_KEYWORDS, |
| 11223 | posix_rename__doc__}, |
| 11224 | {"replace", (PyCFunction)posix_replace, |
| 11225 | METH_VARARGS | METH_KEYWORDS, |
| 11226 | posix_replace__doc__}, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 11227 | {"rmdir", (PyCFunction)posix_rmdir, |
| 11228 | METH_VARARGS | METH_KEYWORDS, |
| 11229 | posix_rmdir__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11230 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11231 | #if defined(HAVE_SYMLINK) |
| 11232 | {"symlink", (PyCFunction)posix_symlink, |
| 11233 | METH_VARARGS | METH_KEYWORDS, |
| 11234 | posix_symlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 11235 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 11236 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11237 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11238 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11239 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11240 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11241 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11242 | #endif /* HAVE_UNAME */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11243 | {"unlink", (PyCFunction)posix_unlink, |
| 11244 | METH_VARARGS | METH_KEYWORDS, |
| 11245 | posix_unlink__doc__}, |
| 11246 | {"remove", (PyCFunction)posix_unlink, |
| 11247 | METH_VARARGS | METH_KEYWORDS, |
| 11248 | posix_remove__doc__}, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 11249 | {"utime", (PyCFunction)posix_utime, |
| 11250 | METH_VARARGS | METH_KEYWORDS, posix_utime__doc__}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 11251 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11252 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 11253 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11254 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 11255 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11256 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11257 | {"execve", (PyCFunction)posix_execve, |
| 11258 | METH_VARARGS | METH_KEYWORDS, |
| 11259 | posix_execve__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 11260 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 11261 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11262 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 11263 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 11264 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 11265 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11266 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 11267 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11268 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11269 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11270 | #endif /* HAVE_FORK */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11271 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 11272 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11273 | {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__}, |
| 11274 | {"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] | 11275 | #endif |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11276 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11277 | {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11278 | #endif |
| 11279 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11280 | {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11281 | #endif |
| 11282 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11283 | {"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] | 11284 | #endif |
| 11285 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11286 | {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11287 | #endif |
| 11288 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11289 | {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11290 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11291 | {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__}, |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11292 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11293 | {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__}, |
| 11294 | {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__}, |
| 11295 | #endif |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 11296 | #endif /* HAVE_SCHED_H */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 11297 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11298 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 11299 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 11300 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11301 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 11302 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 11303 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11304 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 11305 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11306 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11307 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 11308 | #endif /* HAVE_GETEUID */ |
| 11309 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11310 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11311 | #endif /* HAVE_GETGID */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 11312 | #ifdef HAVE_GETGROUPLIST |
| 11313 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 11314 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11315 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11316 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11317 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11318 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11319 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11320 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11321 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11322 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11323 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11324 | #endif /* HAVE_GETPPID */ |
| 11325 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11326 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11327 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 11328 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11329 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 11330 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11331 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11332 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11333 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 11334 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11335 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 11336 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 11337 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11338 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 11339 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 11340 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11341 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 11342 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 11343 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11344 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11345 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11346 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11347 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11348 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11349 | #endif /* HAVE_SETEUID */ |
| 11350 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11351 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11352 | #endif /* HAVE_SETEGID */ |
| 11353 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11354 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11355 | #endif /* HAVE_SETREUID */ |
| 11356 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11357 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11358 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11359 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11360 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11361 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 11362 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11363 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 11364 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 11365 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11366 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 11367 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 11368 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11369 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 11370 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11371 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11372 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11373 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11374 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11375 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11376 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11377 | #ifdef HAVE_WAIT3 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 11378 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11379 | #endif /* HAVE_WAIT3 */ |
| 11380 | #ifdef HAVE_WAIT4 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 11381 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11382 | #endif /* HAVE_WAIT4 */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11383 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11384 | {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__}, |
| 11385 | #endif |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 11386 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11387 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11388 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 11389 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11390 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 11391 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11392 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11393 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11394 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11395 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11396 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11397 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11398 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11399 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11400 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11401 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11402 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11403 | #endif /* HAVE_TCSETPGRP */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11404 | {"open", (PyCFunction)posix_open,\ |
| 11405 | METH_VARARGS | METH_KEYWORDS, |
| 11406 | posix_open__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11407 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 11408 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 11409 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 11410 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11411 | {"dup2", (PyCFunction)posix_dup2, |
| 11412 | METH_VARARGS | METH_KEYWORDS, posix_dup2__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11413 | #ifdef HAVE_LOCKF |
| 11414 | {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__}, |
| 11415 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11416 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 11417 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11418 | #ifdef HAVE_READV |
| 11419 | {"readv", posix_readv, METH_VARARGS, posix_readv__doc__}, |
| 11420 | #endif |
| 11421 | #ifdef HAVE_PREAD |
| 11422 | {"pread", posix_pread, METH_VARARGS, posix_pread__doc__}, |
| 11423 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11424 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11425 | #ifdef HAVE_WRITEV |
| 11426 | {"writev", posix_writev, METH_VARARGS, posix_writev__doc__}, |
| 11427 | #endif |
| 11428 | #ifdef HAVE_PWRITE |
| 11429 | {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__}, |
| 11430 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11431 | #ifdef HAVE_SENDFILE |
| 11432 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 11433 | posix_sendfile__doc__}, |
| 11434 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 11435 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11436 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11437 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11438 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11439 | #endif |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 11440 | #ifdef HAVE_PIPE2 |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 11441 | {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__}, |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 11442 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11443 | #ifdef HAVE_MKFIFO |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11444 | {"mkfifo", (PyCFunction)posix_mkfifo, |
| 11445 | METH_VARARGS | METH_KEYWORDS, |
| 11446 | posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11447 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 11448 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11449 | {"mknod", (PyCFunction)posix_mknod, |
| 11450 | METH_VARARGS | METH_KEYWORDS, |
| 11451 | posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 11452 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 11453 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11454 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 11455 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 11456 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 11457 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11458 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11459 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11460 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11461 | #ifdef HAVE_TRUNCATE |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11462 | {"truncate", (PyCFunction)posix_truncate, |
| 11463 | METH_VARARGS | METH_KEYWORDS, |
| 11464 | posix_truncate__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11465 | #endif |
| 11466 | #ifdef HAVE_POSIX_FALLOCATE |
| 11467 | {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__}, |
| 11468 | #endif |
| 11469 | #ifdef HAVE_POSIX_FADVISE |
| 11470 | {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__}, |
| 11471 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 11472 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11473 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 11474 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 11475 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11476 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 11477 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11478 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11479 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11480 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11481 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11482 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11483 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11484 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11485 | #ifdef HAVE_SYNC |
| 11486 | {"sync", posix_sync, METH_NOARGS, posix_sync__doc__}, |
| 11487 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11488 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11489 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11490 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11491 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11492 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11493 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11494 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 11495 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11496 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 11497 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11498 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11499 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11500 | #endif /* WIFSTOPPED */ |
| 11501 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11502 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11503 | #endif /* WIFSIGNALED */ |
| 11504 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11505 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11506 | #endif /* WIFEXITED */ |
| 11507 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11508 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11509 | #endif /* WEXITSTATUS */ |
| 11510 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11511 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11512 | #endif /* WTERMSIG */ |
| 11513 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11514 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11515 | #endif /* WSTOPSIG */ |
| 11516 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11517 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11518 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11519 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11520 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11521 | {"statvfs", (PyCFunction)posix_statvfs, |
| 11522 | METH_VARARGS | METH_KEYWORDS, |
| 11523 | posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11524 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11525 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11526 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11527 | #endif |
| 11528 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11529 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11530 | #endif |
| 11531 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11532 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11533 | #endif |
| 11534 | #ifdef HAVE_PATHCONF |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11535 | {"pathconf", (PyCFunction)posix_pathconf, |
| 11536 | METH_VARARGS | METH_KEYWORDS, |
| 11537 | posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11538 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11539 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11540 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11541 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 11542 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 11543 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 11544 | {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__}, |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 11545 | {"_getvolumepathname", posix__getvolumepathname, METH_VARARGS, posix__getvolumepathname__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 11546 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11547 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11548 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11549 | #endif |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 11550 | {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11551 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11552 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11553 | #endif |
| 11554 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11555 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11556 | #endif |
| 11557 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11558 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11559 | #endif |
| 11560 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11561 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11562 | #endif |
| 11563 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11564 | #ifdef USE_XATTRS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11565 | {"setxattr", (PyCFunction)posix_setxattr, |
| 11566 | METH_VARARGS | METH_KEYWORDS, |
| 11567 | posix_setxattr__doc__}, |
| 11568 | {"getxattr", (PyCFunction)posix_getxattr, |
| 11569 | METH_VARARGS | METH_KEYWORDS, |
| 11570 | posix_getxattr__doc__}, |
| 11571 | {"removexattr", (PyCFunction)posix_removexattr, |
| 11572 | METH_VARARGS | METH_KEYWORDS, |
| 11573 | posix_removexattr__doc__}, |
| 11574 | {"listxattr", (PyCFunction)posix_listxattr, |
| 11575 | METH_VARARGS | METH_KEYWORDS, |
| 11576 | posix_listxattr__doc__}, |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11577 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11578 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 11579 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 11580 | #endif |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11581 | {"cpu_count", (PyCFunction)posix_cpu_count, |
| 11582 | METH_NOARGS, posix_cpu_count__doc__}, |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11583 | {"get_inheritable", posix_get_inheritable, METH_VARARGS, get_inheritable__doc__}, |
| 11584 | {"set_inheritable", posix_set_inheritable, METH_VARARGS, set_inheritable__doc__}, |
| 11585 | #ifdef MS_WINDOWS |
| 11586 | {"get_handle_inheritable", posix_get_handle_inheritable, |
| 11587 | METH_VARARGS, get_handle_inheritable__doc__}, |
| 11588 | {"set_handle_inheritable", posix_set_handle_inheritable, |
| 11589 | METH_VARARGS, set_handle_inheritable__doc__}, |
| 11590 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11591 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11592 | }; |
| 11593 | |
| 11594 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11595 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11596 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11597 | enable_symlink() |
| 11598 | { |
| 11599 | HANDLE tok; |
| 11600 | TOKEN_PRIVILEGES tok_priv; |
| 11601 | LUID luid; |
| 11602 | int meth_idx = 0; |
| 11603 | |
| 11604 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11605 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11606 | |
| 11607 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11608 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11609 | |
| 11610 | tok_priv.PrivilegeCount = 1; |
| 11611 | tok_priv.Privileges[0].Luid = luid; |
| 11612 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 11613 | |
| 11614 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 11615 | sizeof(TOKEN_PRIVILEGES), |
| 11616 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11617 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11618 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11619 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 11620 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11621 | } |
| 11622 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 11623 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11624 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11625 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11626 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11627 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11628 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11629 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11630 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11631 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11632 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11633 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11634 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11635 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11636 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11637 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11638 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11639 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11640 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11641 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11642 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11643 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11644 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11645 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11646 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11647 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11648 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11649 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11650 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11651 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11652 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11653 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11654 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11655 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11656 | #endif |
| 11657 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11658 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11659 | #endif |
| 11660 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11661 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11662 | #endif |
| 11663 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11664 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11665 | #endif |
| 11666 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11667 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11668 | #endif |
| 11669 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11670 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11671 | #endif |
| 11672 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11673 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11674 | #endif |
| 11675 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11676 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11677 | #endif |
| 11678 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11679 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11680 | #endif |
| 11681 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11682 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11683 | #endif |
| 11684 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11685 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11686 | #endif |
| 11687 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11688 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11689 | #endif |
| 11690 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11691 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11692 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11693 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11694 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11695 | #endif |
| 11696 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11697 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11698 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11699 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11700 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11701 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11702 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11703 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11704 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11705 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11706 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11707 | #endif |
| 11708 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11709 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11710 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11711 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11712 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11713 | #endif |
| 11714 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11715 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11716 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 11717 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11718 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 11719 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11720 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11721 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11722 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 11723 | #ifdef O_TMPFILE |
| 11724 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 11725 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11726 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11727 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11728 | #endif |
| 11729 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11730 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11731 | #endif |
| 11732 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11733 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11734 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 11735 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11736 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 11737 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11738 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11739 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11740 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11741 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11742 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 11743 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11744 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 11745 | #endif |
| 11746 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11747 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 11748 | #endif |
| 11749 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11750 | /* MS Windows */ |
| 11751 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11752 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11753 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11754 | #endif |
| 11755 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11756 | /* Optimize for short life (keep in memory). */ |
| 11757 | /* 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] | 11758 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11759 | #endif |
| 11760 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11761 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11762 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11763 | #endif |
| 11764 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11765 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11766 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11767 | #endif |
| 11768 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11769 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11770 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11771 | #endif |
| 11772 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11773 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11774 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11775 | /* Send a SIGIO signal whenever input or output |
| 11776 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11777 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11778 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11779 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11780 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11781 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11782 | #endif |
| 11783 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11784 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11785 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11786 | #endif |
| 11787 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11788 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11789 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11790 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11791 | #ifdef O_NOLINKS |
| 11792 | /* 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] | 11793 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11794 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11795 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11796 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11797 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11798 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11799 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11800 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11801 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11802 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11803 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11804 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11805 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11806 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11807 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11808 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11809 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11810 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11811 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11812 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11813 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11814 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11815 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11816 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11817 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11818 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11819 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11820 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11821 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11822 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11823 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11824 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11825 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11826 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11827 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11828 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11829 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11830 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11831 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11832 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11833 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11834 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11835 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11836 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11837 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11838 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11839 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11840 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11841 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11842 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11843 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11844 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11845 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11846 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11847 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11848 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11849 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11850 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11851 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11852 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11853 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11854 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11855 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11856 | #endif /* ST_RDONLY */ |
| 11857 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11858 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11859 | #endif /* ST_NOSUID */ |
| 11860 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11861 | /* FreeBSD sendfile() constants */ |
| 11862 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11863 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11864 | #endif |
| 11865 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11866 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11867 | #endif |
| 11868 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11869 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11870 | #endif |
| 11871 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11872 | /* constants for posix_fadvise */ |
| 11873 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11874 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11875 | #endif |
| 11876 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11877 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11878 | #endif |
| 11879 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11880 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11881 | #endif |
| 11882 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11883 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11884 | #endif |
| 11885 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11886 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11887 | #endif |
| 11888 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11889 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11890 | #endif |
| 11891 | |
| 11892 | /* constants for waitid */ |
| 11893 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11894 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 11895 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 11896 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11897 | #endif |
| 11898 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11899 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11900 | #endif |
| 11901 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11902 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11903 | #endif |
| 11904 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11905 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11906 | #endif |
| 11907 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11908 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11909 | #endif |
| 11910 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11911 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11912 | #endif |
| 11913 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11914 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11915 | #endif |
| 11916 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11917 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11918 | #endif |
| 11919 | |
| 11920 | /* constants for lockf */ |
| 11921 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11922 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11923 | #endif |
| 11924 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11925 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11926 | #endif |
| 11927 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11928 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11929 | #endif |
| 11930 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11931 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11932 | #endif |
| 11933 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11934 | #ifdef HAVE_SPAWNV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11935 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 11936 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
| 11937 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
| 11938 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
| 11939 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11940 | #endif |
| 11941 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11942 | #ifdef HAVE_SCHED_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11943 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
| 11944 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
| 11945 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11946 | #ifdef SCHED_SPORADIC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11947 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11948 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11949 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11950 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11951 | #endif |
| 11952 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11953 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11954 | #endif |
| 11955 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11956 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11957 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11958 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11959 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11960 | #endif |
| 11961 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11962 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11963 | #endif |
| 11964 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11965 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11966 | #endif |
| 11967 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11968 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11969 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11970 | #endif |
| 11971 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11972 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11973 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 11974 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 11975 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11976 | #endif |
| 11977 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11978 | #ifdef RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11979 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11980 | #endif |
| 11981 | #ifdef RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11982 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11983 | #endif |
| 11984 | #ifdef RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11985 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11986 | #endif |
| 11987 | #ifdef RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11988 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11989 | #endif |
| 11990 | #ifdef RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11991 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11992 | #endif |
| 11993 | #ifdef RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11994 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11995 | #endif |
| 11996 | #ifdef RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11997 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11998 | #endif |
| 11999 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12000 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12001 | } |
| 12002 | |
| 12003 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12004 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12005 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 12006 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 12007 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 12008 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12009 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 12010 | #define MODNAME "posix" |
| 12011 | #endif |
| 12012 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12013 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12014 | PyModuleDef_HEAD_INIT, |
| 12015 | MODNAME, |
| 12016 | posix__doc__, |
| 12017 | -1, |
| 12018 | posix_methods, |
| 12019 | NULL, |
| 12020 | NULL, |
| 12021 | NULL, |
| 12022 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12023 | }; |
| 12024 | |
| 12025 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12026 | static char *have_functions[] = { |
| 12027 | |
| 12028 | #ifdef HAVE_FACCESSAT |
| 12029 | "HAVE_FACCESSAT", |
| 12030 | #endif |
| 12031 | |
| 12032 | #ifdef HAVE_FCHDIR |
| 12033 | "HAVE_FCHDIR", |
| 12034 | #endif |
| 12035 | |
| 12036 | #ifdef HAVE_FCHMOD |
| 12037 | "HAVE_FCHMOD", |
| 12038 | #endif |
| 12039 | |
| 12040 | #ifdef HAVE_FCHMODAT |
| 12041 | "HAVE_FCHMODAT", |
| 12042 | #endif |
| 12043 | |
| 12044 | #ifdef HAVE_FCHOWN |
| 12045 | "HAVE_FCHOWN", |
| 12046 | #endif |
| 12047 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 12048 | #ifdef HAVE_FCHOWNAT |
| 12049 | "HAVE_FCHOWNAT", |
| 12050 | #endif |
| 12051 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12052 | #ifdef HAVE_FEXECVE |
| 12053 | "HAVE_FEXECVE", |
| 12054 | #endif |
| 12055 | |
| 12056 | #ifdef HAVE_FDOPENDIR |
| 12057 | "HAVE_FDOPENDIR", |
| 12058 | #endif |
| 12059 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12060 | #ifdef HAVE_FPATHCONF |
| 12061 | "HAVE_FPATHCONF", |
| 12062 | #endif |
| 12063 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12064 | #ifdef HAVE_FSTATAT |
| 12065 | "HAVE_FSTATAT", |
| 12066 | #endif |
| 12067 | |
| 12068 | #ifdef HAVE_FSTATVFS |
| 12069 | "HAVE_FSTATVFS", |
| 12070 | #endif |
| 12071 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12072 | #ifdef HAVE_FTRUNCATE |
| 12073 | "HAVE_FTRUNCATE", |
| 12074 | #endif |
| 12075 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12076 | #ifdef HAVE_FUTIMENS |
| 12077 | "HAVE_FUTIMENS", |
| 12078 | #endif |
| 12079 | |
| 12080 | #ifdef HAVE_FUTIMES |
| 12081 | "HAVE_FUTIMES", |
| 12082 | #endif |
| 12083 | |
| 12084 | #ifdef HAVE_FUTIMESAT |
| 12085 | "HAVE_FUTIMESAT", |
| 12086 | #endif |
| 12087 | |
| 12088 | #ifdef HAVE_LINKAT |
| 12089 | "HAVE_LINKAT", |
| 12090 | #endif |
| 12091 | |
| 12092 | #ifdef HAVE_LCHFLAGS |
| 12093 | "HAVE_LCHFLAGS", |
| 12094 | #endif |
| 12095 | |
| 12096 | #ifdef HAVE_LCHMOD |
| 12097 | "HAVE_LCHMOD", |
| 12098 | #endif |
| 12099 | |
| 12100 | #ifdef HAVE_LCHOWN |
| 12101 | "HAVE_LCHOWN", |
| 12102 | #endif |
| 12103 | |
| 12104 | #ifdef HAVE_LSTAT |
| 12105 | "HAVE_LSTAT", |
| 12106 | #endif |
| 12107 | |
| 12108 | #ifdef HAVE_LUTIMES |
| 12109 | "HAVE_LUTIMES", |
| 12110 | #endif |
| 12111 | |
| 12112 | #ifdef HAVE_MKDIRAT |
| 12113 | "HAVE_MKDIRAT", |
| 12114 | #endif |
| 12115 | |
| 12116 | #ifdef HAVE_MKFIFOAT |
| 12117 | "HAVE_MKFIFOAT", |
| 12118 | #endif |
| 12119 | |
| 12120 | #ifdef HAVE_MKNODAT |
| 12121 | "HAVE_MKNODAT", |
| 12122 | #endif |
| 12123 | |
| 12124 | #ifdef HAVE_OPENAT |
| 12125 | "HAVE_OPENAT", |
| 12126 | #endif |
| 12127 | |
| 12128 | #ifdef HAVE_READLINKAT |
| 12129 | "HAVE_READLINKAT", |
| 12130 | #endif |
| 12131 | |
| 12132 | #ifdef HAVE_RENAMEAT |
| 12133 | "HAVE_RENAMEAT", |
| 12134 | #endif |
| 12135 | |
| 12136 | #ifdef HAVE_SYMLINKAT |
| 12137 | "HAVE_SYMLINKAT", |
| 12138 | #endif |
| 12139 | |
| 12140 | #ifdef HAVE_UNLINKAT |
| 12141 | "HAVE_UNLINKAT", |
| 12142 | #endif |
| 12143 | |
| 12144 | #ifdef HAVE_UTIMENSAT |
| 12145 | "HAVE_UTIMENSAT", |
| 12146 | #endif |
| 12147 | |
| 12148 | #ifdef MS_WINDOWS |
| 12149 | "MS_WINDOWS", |
| 12150 | #endif |
| 12151 | |
| 12152 | NULL |
| 12153 | }; |
| 12154 | |
| 12155 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 12156 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 12157 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12158 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12159 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12160 | PyObject *list; |
| 12161 | char **trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12162 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12163 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12164 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12165 | #endif |
| 12166 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12167 | m = PyModule_Create(&posixmodule); |
| 12168 | if (m == NULL) |
| 12169 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12170 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12171 | /* Initialize environ dictionary */ |
| 12172 | v = convertenviron(); |
| 12173 | Py_XINCREF(v); |
| 12174 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 12175 | return NULL; |
| 12176 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12177 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12178 | if (all_ins(m)) |
| 12179 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12180 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12181 | if (setup_confname_tables(m)) |
| 12182 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12183 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12184 | Py_INCREF(PyExc_OSError); |
| 12185 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 12186 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12187 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12188 | if (posix_putenv_garbage == NULL) |
| 12189 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12190 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 12191 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12192 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12193 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 12194 | waitid_result_desc.name = MODNAME ".waitid_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12195 | if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0) |
| 12196 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12197 | #endif |
| 12198 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 12199 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12200 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 12201 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 12202 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12203 | if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0) |
| 12204 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12205 | structseq_new = StatResultType.tp_new; |
| 12206 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12207 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 12208 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12209 | if (PyStructSequence_InitType2(&StatVFSResultType, |
| 12210 | &statvfs_result_desc) < 0) |
| 12211 | return NULL; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12212 | #ifdef NEED_TICKS_PER_SECOND |
| 12213 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12214 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12215 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12216 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12217 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12218 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12219 | # endif |
| 12220 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12221 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 12222 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12223 | sched_param_desc.name = MODNAME ".sched_param"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12224 | if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0) |
| 12225 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12226 | SchedParamType.tp_new = sched_param_new; |
| 12227 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12228 | |
| 12229 | /* initialize TerminalSize_info */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12230 | if (PyStructSequence_InitType2(&TerminalSizeType, |
| 12231 | &TerminalSize_desc) < 0) |
| 12232 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12233 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12234 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 12235 | Py_INCREF((PyObject*) &WaitidResultType); |
| 12236 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 12237 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12238 | Py_INCREF((PyObject*) &StatResultType); |
| 12239 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 12240 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 12241 | PyModule_AddObject(m, "statvfs_result", |
| 12242 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 12243 | |
| 12244 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12245 | Py_INCREF(&SchedParamType); |
| 12246 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 12247 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12248 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 12249 | times_result_desc.name = MODNAME ".times_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12250 | if (PyStructSequence_InitType2(&TimesResultType, ×_result_desc) < 0) |
| 12251 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 12252 | PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType); |
| 12253 | |
| 12254 | uname_result_desc.name = MODNAME ".uname_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12255 | if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0) |
| 12256 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 12257 | PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType); |
| 12258 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12259 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12260 | /* |
| 12261 | * Step 2 of weak-linking support on Mac OS X. |
| 12262 | * |
| 12263 | * The code below removes functions that are not available on the |
| 12264 | * currently active platform. |
| 12265 | * |
| 12266 | * 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] | 12267 | * 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] | 12268 | * OSX 10.4. |
| 12269 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12270 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12271 | if (fstatvfs == NULL) { |
| 12272 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 12273 | return NULL; |
| 12274 | } |
| 12275 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12276 | #endif /* HAVE_FSTATVFS */ |
| 12277 | |
| 12278 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12279 | if (statvfs == NULL) { |
| 12280 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 12281 | return NULL; |
| 12282 | } |
| 12283 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12284 | #endif /* HAVE_STATVFS */ |
| 12285 | |
| 12286 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12287 | if (lchown == NULL) { |
| 12288 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 12289 | return NULL; |
| 12290 | } |
| 12291 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12292 | #endif /* HAVE_LCHOWN */ |
| 12293 | |
| 12294 | |
| 12295 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12296 | |
Antoine Pitrou | 9d20e0e | 2012-09-12 18:01:36 +0200 | [diff] [blame] | 12297 | Py_INCREF(&TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12298 | PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); |
| 12299 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 12300 | billion = PyLong_FromLong(1000000000); |
| 12301 | if (!billion) |
| 12302 | return NULL; |
| 12303 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12304 | /* suppress "function not used" warnings */ |
| 12305 | { |
| 12306 | int ignored; |
| 12307 | fd_specified("", -1); |
| 12308 | follow_symlinks_specified("", 1); |
| 12309 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 12310 | dir_fd_converter(Py_None, &ignored); |
| 12311 | dir_fd_unavailable(Py_None, &ignored); |
| 12312 | } |
| 12313 | |
| 12314 | /* |
| 12315 | * provide list of locally available functions |
| 12316 | * so os.py can populate support_* lists |
| 12317 | */ |
| 12318 | list = PyList_New(0); |
| 12319 | if (!list) |
| 12320 | return NULL; |
| 12321 | for (trace = have_functions; *trace; trace++) { |
| 12322 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 12323 | if (!unicode) |
| 12324 | return NULL; |
| 12325 | if (PyList_Append(list, unicode)) |
| 12326 | return NULL; |
| 12327 | Py_DECREF(unicode); |
| 12328 | } |
| 12329 | PyModule_AddObject(m, "_have_functions", list); |
| 12330 | |
| 12331 | initialized = 1; |
| 12332 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12333 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12334 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12335 | |
| 12336 | #ifdef __cplusplus |
| 12337 | } |
| 12338 | #endif |