Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win. In that case the |
| 5 | module actually calls itself 'nt', not 'posix', and a few |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Victor Stinner | f427a14 | 2014-10-22 12:33:23 +0200 | [diff] [blame] | 9 | test macro, e.g. '_MSC_VER'. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 11 | |
| 12 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13 | #ifdef __APPLE__ |
| 14 | /* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15 | * Step 1 of support for weak-linking a number of symbols existing on |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 16 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 17 | * at the end of this file for more information. |
| 18 | */ |
| 19 | # pragma weak lchown |
| 20 | # pragma weak statvfs |
| 21 | # pragma weak fstatvfs |
| 22 | |
| 23 | #endif /* __APPLE__ */ |
| 24 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 25 | #define PY_SSIZE_T_CLEAN |
| 26 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 27 | #include "Python.h" |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 28 | #include "structmember.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 29 | #ifndef MS_WINDOWS |
| 30 | #include "posixmodule.h" |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 31 | #else |
| 32 | #include "winreparse.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 33 | #endif |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 34 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 39 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 40 | "This module provides access to operating system functionality that is\n\ |
| 41 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 42 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 43 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 45 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 46 | #ifdef HAVE_SYS_UIO_H |
| 47 | #include <sys/uio.h> |
| 48 | #endif |
| 49 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 50 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 51 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 52 | #endif /* HAVE_SYS_TYPES_H */ |
| 53 | |
| 54 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 55 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 56 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 58 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 59 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 60 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 61 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 62 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 63 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 64 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 66 | #ifdef HAVE_FCNTL_H |
| 67 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 68 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 70 | #ifdef HAVE_GRP_H |
| 71 | #include <grp.h> |
| 72 | #endif |
| 73 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 74 | #ifdef HAVE_SYSEXITS_H |
| 75 | #include <sysexits.h> |
| 76 | #endif /* HAVE_SYSEXITS_H */ |
| 77 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 78 | #ifdef HAVE_SYS_LOADAVG_H |
| 79 | #include <sys/loadavg.h> |
| 80 | #endif |
| 81 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 82 | #ifdef HAVE_LANGINFO_H |
| 83 | #include <langinfo.h> |
| 84 | #endif |
| 85 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 86 | #ifdef HAVE_SYS_SENDFILE_H |
| 87 | #include <sys/sendfile.h> |
| 88 | #endif |
| 89 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 90 | #ifdef HAVE_SCHED_H |
| 91 | #include <sched.h> |
| 92 | #endif |
| 93 | |
Benjamin Peterson | 2dbda07 | 2012-03-16 10:12:55 -0500 | [diff] [blame] | 94 | #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) |
Benjamin Peterson | 7b51b8d | 2012-03-14 22:28:25 -0500 | [diff] [blame] | 95 | #undef HAVE_SCHED_SETAFFINITY |
| 96 | #endif |
| 97 | |
doko@ubuntu.com | 4a173bc | 2014-04-17 19:47:16 +0200 | [diff] [blame] | 98 | #if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__) |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 99 | #define USE_XATTRS |
| 100 | #endif |
| 101 | |
| 102 | #ifdef USE_XATTRS |
Benjamin Peterson | b77fe17 | 2011-09-13 17:20:47 -0400 | [diff] [blame] | 103 | #include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 104 | #endif |
| 105 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 106 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 107 | #ifdef HAVE_SYS_SOCKET_H |
| 108 | #include <sys/socket.h> |
| 109 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 110 | #endif |
| 111 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 112 | #ifdef HAVE_DLFCN_H |
| 113 | #include <dlfcn.h> |
| 114 | #endif |
| 115 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 116 | #ifdef __hpux |
| 117 | #include <sys/mpctl.h> |
| 118 | #endif |
| 119 | |
| 120 | #if defined(__DragonFly__) || \ |
| 121 | defined(__OpenBSD__) || \ |
| 122 | defined(__FreeBSD__) || \ |
| 123 | defined(__NetBSD__) || \ |
| 124 | defined(__APPLE__) |
| 125 | #include <sys/sysctl.h> |
| 126 | #endif |
| 127 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 128 | #if defined(MS_WINDOWS) |
| 129 | # define TERMSIZE_USE_CONIO |
| 130 | #elif defined(HAVE_SYS_IOCTL_H) |
| 131 | # include <sys/ioctl.h> |
| 132 | # if defined(HAVE_TERMIOS_H) |
| 133 | # include <termios.h> |
| 134 | # endif |
| 135 | # if defined(TIOCGWINSZ) |
| 136 | # define TERMSIZE_USE_IOCTL |
| 137 | # endif |
| 138 | #endif /* MS_WINDOWS */ |
| 139 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 140 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 141 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 142 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 143 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 144 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 145 | #include <process.h> |
| 146 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 147 | #ifdef _MSC_VER /* Microsoft compiler */ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 148 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 149 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 150 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 151 | #define HAVE_EXECV 1 |
| 152 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 153 | #define HAVE_SYSTEM 1 |
| 154 | #define HAVE_CWAIT 1 |
| 155 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 156 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 157 | #else |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 158 | /* Unix functions that the configure script doesn't check for */ |
| 159 | #define HAVE_EXECV 1 |
| 160 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 161 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 162 | #define HAVE_FORK1 1 |
| 163 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 164 | #define HAVE_GETEGID 1 |
| 165 | #define HAVE_GETEUID 1 |
| 166 | #define HAVE_GETGID 1 |
| 167 | #define HAVE_GETPPID 1 |
| 168 | #define HAVE_GETUID 1 |
| 169 | #define HAVE_KILL 1 |
| 170 | #define HAVE_OPENDIR 1 |
| 171 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 172 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 173 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 174 | #define HAVE_TTYNAME 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 175 | #endif /* _MSC_VER */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 176 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 177 | |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 178 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 179 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 180 | # one of the few times we lie about this name! |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 181 | module os |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 182 | [clinic start generated code]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 183 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/ |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 184 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 185 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 186 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 187 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 188 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 189 | (default) */ |
| 190 | extern char *ctermid_r(char *); |
| 191 | #endif |
| 192 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 193 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 194 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 195 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 196 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 197 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 198 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 199 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 200 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 201 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 202 | #endif |
| 203 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 204 | extern int chdir(char *); |
| 205 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 206 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 207 | extern int chdir(const char *); |
| 208 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 209 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 210 | extern int chmod(const char *, mode_t); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 211 | /*#ifdef HAVE_FCHMOD |
| 212 | extern int fchmod(int, mode_t); |
| 213 | #endif*/ |
| 214 | /*#ifdef HAVE_LCHMOD |
| 215 | extern int lchmod(const char *, mode_t); |
| 216 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 217 | extern int chown(const char *, uid_t, gid_t); |
| 218 | extern char *getcwd(char *, int); |
| 219 | extern char *strerror(int); |
| 220 | extern int link(const char *, const char *); |
| 221 | extern int rename(const char *, const char *); |
| 222 | extern int stat(const char *, struct stat *); |
| 223 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 224 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 225 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 226 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 227 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 228 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 229 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 230 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 231 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 232 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 233 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 234 | #ifdef HAVE_UTIME_H |
| 235 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 236 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 237 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 238 | #ifdef HAVE_SYS_UTIME_H |
| 239 | #include <sys/utime.h> |
| 240 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 241 | #endif /* HAVE_SYS_UTIME_H */ |
| 242 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 243 | #ifdef HAVE_SYS_TIMES_H |
| 244 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 245 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 246 | |
| 247 | #ifdef HAVE_SYS_PARAM_H |
| 248 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 249 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 250 | |
| 251 | #ifdef HAVE_SYS_UTSNAME_H |
| 252 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 253 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 255 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 256 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 257 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 258 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 259 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 260 | #include <direct.h> |
| 261 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 262 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 263 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 264 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 265 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 266 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 267 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 268 | #endif |
| 269 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 270 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 271 | #endif |
| 272 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 273 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 274 | #endif |
| 275 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 276 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 277 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 278 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 279 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 280 | #endif |
| 281 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 282 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 283 | #endif |
| 284 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 285 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 286 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 287 | #ifndef VOLUME_NAME_DOS |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 288 | #define VOLUME_NAME_DOS 0x0 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 289 | #endif |
| 290 | #ifndef VOLUME_NAME_NT |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 291 | #define VOLUME_NAME_NT 0x2 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 292 | #endif |
| 293 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 294 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 295 | #endif |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 296 | #ifndef IO_REPARSE_TAG_MOUNT_POINT |
| 297 | #define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L) |
| 298 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 299 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 300 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 301 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 302 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 303 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 304 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 305 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 306 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 307 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 308 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 309 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 310 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 311 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 312 | #define MAXPATHLEN PATH_MAX |
| 313 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 314 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 315 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 316 | #endif /* MAXPATHLEN */ |
| 317 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 318 | #ifdef UNION_WAIT |
| 319 | /* Emulate some macros on systems that have a union instead of macros */ |
| 320 | |
| 321 | #ifndef WIFEXITED |
| 322 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 323 | #endif |
| 324 | |
| 325 | #ifndef WEXITSTATUS |
| 326 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 327 | #endif |
| 328 | |
| 329 | #ifndef WTERMSIG |
| 330 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 331 | #endif |
| 332 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 333 | #define WAIT_TYPE union wait |
| 334 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 335 | |
| 336 | #else /* !UNION_WAIT */ |
| 337 | #define WAIT_TYPE int |
| 338 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 339 | #endif /* UNION_WAIT */ |
| 340 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 341 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 342 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 343 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 344 | #define USE_CTERMID_R |
| 345 | #endif |
| 346 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 347 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 348 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 349 | #undef FSTAT |
| 350 | #undef STRUCT_STAT |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 351 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 352 | # define STAT win32_stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 353 | # define LSTAT win32_lstat |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 354 | # define FSTAT _Py_fstat_noraise |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 355 | # define STRUCT_STAT struct _Py_stat_struct |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 356 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 357 | # define STAT stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 358 | # define LSTAT lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 359 | # define FSTAT fstat |
| 360 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 361 | #endif |
| 362 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 363 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 364 | #include <sys/mkdev.h> |
| 365 | #else |
| 366 | #if defined(MAJOR_IN_SYSMACROS) |
| 367 | #include <sys/sysmacros.h> |
| 368 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 369 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 370 | #include <sys/mkdev.h> |
| 371 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 372 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 373 | |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 374 | #define DWORD_MAX 4294967295U |
| 375 | |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 376 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 377 | #define INITFUNC PyInit_nt |
| 378 | #define MODNAME "nt" |
| 379 | #else |
| 380 | #define INITFUNC PyInit_posix |
| 381 | #define MODNAME "posix" |
| 382 | #endif |
| 383 | |
| 384 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 385 | /* defined in fileutils.c */ |
| 386 | PyAPI_FUNC(void) _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *); |
| 387 | PyAPI_FUNC(void) _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *, |
| 388 | ULONG, struct _Py_stat_struct *); |
| 389 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 390 | |
| 391 | #ifdef MS_WINDOWS |
| 392 | static int |
| 393 | win32_warn_bytes_api() |
| 394 | { |
| 395 | return PyErr_WarnEx(PyExc_DeprecationWarning, |
| 396 | "The Windows bytes API has been deprecated, " |
| 397 | "use Unicode filenames instead", |
| 398 | 1); |
| 399 | } |
| 400 | #endif |
| 401 | |
| 402 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 403 | #ifndef MS_WINDOWS |
| 404 | PyObject * |
| 405 | _PyLong_FromUid(uid_t uid) |
| 406 | { |
| 407 | if (uid == (uid_t)-1) |
| 408 | return PyLong_FromLong(-1); |
| 409 | return PyLong_FromUnsignedLong(uid); |
| 410 | } |
| 411 | |
| 412 | PyObject * |
| 413 | _PyLong_FromGid(gid_t gid) |
| 414 | { |
| 415 | if (gid == (gid_t)-1) |
| 416 | return PyLong_FromLong(-1); |
| 417 | return PyLong_FromUnsignedLong(gid); |
| 418 | } |
| 419 | |
| 420 | int |
| 421 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 422 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 423 | uid_t uid; |
| 424 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 425 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 426 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 427 | unsigned long uresult; |
| 428 | |
| 429 | index = PyNumber_Index(obj); |
| 430 | if (index == NULL) { |
| 431 | PyErr_Format(PyExc_TypeError, |
| 432 | "uid should be integer, not %.200s", |
| 433 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 434 | return 0; |
| 435 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 436 | |
| 437 | /* |
| 438 | * Handling uid_t is complicated for two reasons: |
| 439 | * * Although uid_t is (always?) unsigned, it still |
| 440 | * accepts -1. |
| 441 | * * We don't know its size in advance--it may be |
| 442 | * bigger than an int, or it may be smaller than |
| 443 | * a long. |
| 444 | * |
| 445 | * So a bit of defensive programming is in order. |
| 446 | * Start with interpreting the value passed |
| 447 | * in as a signed long and see if it works. |
| 448 | */ |
| 449 | |
| 450 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 451 | |
| 452 | if (!overflow) { |
| 453 | uid = (uid_t)result; |
| 454 | |
| 455 | if (result == -1) { |
| 456 | if (PyErr_Occurred()) |
| 457 | goto fail; |
| 458 | /* It's a legitimate -1, we're done. */ |
| 459 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 460 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 461 | |
| 462 | /* Any other negative number is disallowed. */ |
| 463 | if (result < 0) |
| 464 | goto underflow; |
| 465 | |
| 466 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 467 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 468 | (long)uid != result) |
| 469 | goto underflow; |
| 470 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 471 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 472 | |
| 473 | if (overflow < 0) |
| 474 | goto underflow; |
| 475 | |
| 476 | /* |
| 477 | * Okay, the value overflowed a signed long. If it |
| 478 | * fits in an *unsigned* long, it may still be okay, |
| 479 | * as uid_t may be unsigned long on this platform. |
| 480 | */ |
| 481 | uresult = PyLong_AsUnsignedLong(index); |
| 482 | if (PyErr_Occurred()) { |
| 483 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 484 | goto overflow; |
| 485 | goto fail; |
| 486 | } |
| 487 | |
| 488 | uid = (uid_t)uresult; |
| 489 | |
| 490 | /* |
| 491 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, |
| 492 | * but this value would get interpreted as (uid_t)-1 by chown |
| 493 | * and its siblings. That's not what the user meant! So we |
| 494 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 495 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 496 | */ |
| 497 | if (uid == (uid_t)-1) |
| 498 | goto overflow; |
| 499 | |
| 500 | /* Ensure the value wasn't truncated. */ |
| 501 | if (sizeof(uid_t) < sizeof(long) && |
| 502 | (unsigned long)uid != uresult) |
| 503 | goto overflow; |
| 504 | /* fallthrough */ |
| 505 | |
| 506 | success: |
| 507 | Py_DECREF(index); |
| 508 | *(uid_t *)p = uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 509 | return 1; |
| 510 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 511 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 512 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 513 | "uid is less than minimum"); |
| 514 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 515 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 516 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 517 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 518 | "uid is greater than maximum"); |
| 519 | /* fallthrough */ |
| 520 | |
| 521 | fail: |
| 522 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | int |
| 527 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 528 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 529 | gid_t gid; |
| 530 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 531 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 532 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 533 | unsigned long uresult; |
| 534 | |
| 535 | index = PyNumber_Index(obj); |
| 536 | if (index == NULL) { |
| 537 | PyErr_Format(PyExc_TypeError, |
| 538 | "gid should be integer, not %.200s", |
| 539 | Py_TYPE(obj)->tp_name); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 540 | return 0; |
| 541 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 542 | |
| 543 | /* |
| 544 | * Handling gid_t is complicated for two reasons: |
| 545 | * * Although gid_t is (always?) unsigned, it still |
| 546 | * accepts -1. |
| 547 | * * We don't know its size in advance--it may be |
| 548 | * bigger than an int, or it may be smaller than |
| 549 | * a long. |
| 550 | * |
| 551 | * So a bit of defensive programming is in order. |
| 552 | * Start with interpreting the value passed |
| 553 | * in as a signed long and see if it works. |
| 554 | */ |
| 555 | |
| 556 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 557 | |
| 558 | if (!overflow) { |
| 559 | gid = (gid_t)result; |
| 560 | |
| 561 | if (result == -1) { |
| 562 | if (PyErr_Occurred()) |
| 563 | goto fail; |
| 564 | /* It's a legitimate -1, we're done. */ |
| 565 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 566 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 567 | |
| 568 | /* Any other negative number is disallowed. */ |
| 569 | if (result < 0) { |
| 570 | goto underflow; |
| 571 | } |
| 572 | |
| 573 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 574 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 575 | (long)gid != result) |
| 576 | goto underflow; |
| 577 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 578 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 579 | |
| 580 | if (overflow < 0) |
| 581 | goto underflow; |
| 582 | |
| 583 | /* |
| 584 | * Okay, the value overflowed a signed long. If it |
| 585 | * fits in an *unsigned* long, it may still be okay, |
| 586 | * as gid_t may be unsigned long on this platform. |
| 587 | */ |
| 588 | uresult = PyLong_AsUnsignedLong(index); |
| 589 | if (PyErr_Occurred()) { |
| 590 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 591 | goto overflow; |
| 592 | goto fail; |
| 593 | } |
| 594 | |
| 595 | gid = (gid_t)uresult; |
| 596 | |
| 597 | /* |
| 598 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, |
| 599 | * but this value would get interpreted as (gid_t)-1 by chown |
| 600 | * and its siblings. That's not what the user meant! So we |
| 601 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 602 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 603 | */ |
| 604 | if (gid == (gid_t)-1) |
| 605 | goto overflow; |
| 606 | |
| 607 | /* Ensure the value wasn't truncated. */ |
| 608 | if (sizeof(gid_t) < sizeof(long) && |
| 609 | (unsigned long)gid != uresult) |
| 610 | goto overflow; |
| 611 | /* fallthrough */ |
| 612 | |
| 613 | success: |
| 614 | Py_DECREF(index); |
| 615 | *(gid_t *)p = gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 616 | return 1; |
| 617 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 618 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 619 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 620 | "gid is less than minimum"); |
| 621 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 622 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 623 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 624 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 625 | "gid is greater than maximum"); |
| 626 | /* fallthrough */ |
| 627 | |
| 628 | fail: |
| 629 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 630 | return 0; |
| 631 | } |
| 632 | #endif /* MS_WINDOWS */ |
| 633 | |
| 634 | |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 635 | #ifdef HAVE_LONG_LONG |
| 636 | # define _PyLong_FromDev PyLong_FromLongLong |
| 637 | #else |
| 638 | # define _PyLong_FromDev PyLong_FromLong |
| 639 | #endif |
| 640 | |
| 641 | |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 642 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 643 | static int |
| 644 | _Py_Dev_Converter(PyObject *obj, void *p) |
| 645 | { |
| 646 | #ifdef HAVE_LONG_LONG |
| 647 | *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj); |
| 648 | #else |
| 649 | *((dev_t *)p) = PyLong_AsUnsignedLong(obj); |
| 650 | #endif |
| 651 | if (PyErr_Occurred()) |
| 652 | return 0; |
| 653 | return 1; |
| 654 | } |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 655 | #endif /* HAVE_MKNOD && HAVE_MAKEDEV */ |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 656 | |
| 657 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 658 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 659 | /* |
| 660 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 661 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 662 | * which doesn't play nicely with all the initializer lines in this file that |
| 663 | * look like this: |
| 664 | * int dir_fd = DEFAULT_DIR_FD; |
| 665 | */ |
| 666 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 667 | #else |
| 668 | #define DEFAULT_DIR_FD (-100) |
| 669 | #endif |
| 670 | |
| 671 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 672 | _fd_converter(PyObject *o, int *p, const char *allowed) |
| 673 | { |
| 674 | int overflow; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 675 | long long_value; |
| 676 | |
| 677 | PyObject *index = PyNumber_Index(o); |
| 678 | if (index == NULL) { |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 679 | PyErr_Format(PyExc_TypeError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 680 | "argument should be %s, not %.200s", |
| 681 | allowed, Py_TYPE(o)->tp_name); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 682 | return 0; |
| 683 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 684 | |
| 685 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
| 686 | Py_DECREF(index); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 687 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 688 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 689 | "fd is greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 690 | return 0; |
| 691 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 692 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 693 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 694 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 695 | return 0; |
| 696 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 697 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 698 | *p = (int)long_value; |
| 699 | return 1; |
| 700 | } |
| 701 | |
| 702 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 703 | dir_fd_converter(PyObject *o, void *p) |
| 704 | { |
| 705 | if (o == Py_None) { |
| 706 | *(int *)p = DEFAULT_DIR_FD; |
| 707 | return 1; |
| 708 | } |
| 709 | return _fd_converter(o, (int *)p, "integer"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 713 | /* |
| 714 | * A PyArg_ParseTuple "converter" function |
| 715 | * that handles filesystem paths in the manner |
| 716 | * preferred by the os module. |
| 717 | * |
| 718 | * path_converter accepts (Unicode) strings and their |
| 719 | * subclasses, and bytes and their subclasses. What |
| 720 | * it does with the argument depends on the platform: |
| 721 | * |
| 722 | * * On Windows, if we get a (Unicode) string we |
| 723 | * extract the wchar_t * and return it; if we get |
| 724 | * bytes we extract the char * and return that. |
| 725 | * |
| 726 | * * On all other platforms, strings are encoded |
| 727 | * to bytes using PyUnicode_FSConverter, then we |
| 728 | * extract the char * from the bytes object and |
| 729 | * return that. |
| 730 | * |
| 731 | * path_converter also optionally accepts signed |
| 732 | * integers (representing open file descriptors) instead |
| 733 | * of path strings. |
| 734 | * |
| 735 | * Input fields: |
| 736 | * path.nullable |
| 737 | * If nonzero, the path is permitted to be None. |
| 738 | * path.allow_fd |
| 739 | * If nonzero, the path is permitted to be a file handle |
| 740 | * (a signed int) instead of a string. |
| 741 | * path.function_name |
| 742 | * If non-NULL, path_converter will use that as the name |
| 743 | * of the function in error messages. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 744 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 745 | * path.argument_name |
| 746 | * If non-NULL, path_converter will use that as the name |
| 747 | * of the parameter in error messages. |
| 748 | * (If path.argument_name is NULL it uses "path".) |
| 749 | * |
| 750 | * Output fields: |
| 751 | * path.wide |
| 752 | * Points to the path if it was expressed as Unicode |
| 753 | * and was not encoded. (Only used on Windows.) |
| 754 | * path.narrow |
| 755 | * Points to the path if it was expressed as bytes, |
| 756 | * or it was Unicode and was encoded to bytes. |
| 757 | * path.fd |
| 758 | * Contains a file descriptor if path.accept_fd was true |
| 759 | * and the caller provided a signed integer instead of any |
| 760 | * sort of string. |
| 761 | * |
| 762 | * WARNING: if your "path" parameter is optional, and is |
| 763 | * unspecified, path_converter will never get called. |
| 764 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 765 | * yourself! |
| 766 | * path.length |
| 767 | * The length of the path in characters, if specified as |
| 768 | * a string. |
| 769 | * path.object |
| 770 | * The original object passed in. |
| 771 | * path.cleanup |
| 772 | * For internal use only. May point to a temporary object. |
| 773 | * (Pay no attention to the man behind the curtain.) |
| 774 | * |
| 775 | * At most one of path.wide or path.narrow will be non-NULL. |
| 776 | * If path was None and path.nullable was set, |
| 777 | * or if path was an integer and path.allow_fd was set, |
| 778 | * both path.wide and path.narrow will be NULL |
| 779 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 780 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 781 | * path_converter takes care to not write to the path_t |
| 782 | * unless it's successful. However it must reset the |
| 783 | * "cleanup" field each time it's called. |
| 784 | * |
| 785 | * Use as follows: |
| 786 | * path_t path; |
| 787 | * memset(&path, 0, sizeof(path)); |
| 788 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 789 | * // ... use values from path ... |
| 790 | * path_cleanup(&path); |
| 791 | * |
| 792 | * (Note that if PyArg_Parse fails you don't need to call |
| 793 | * path_cleanup(). However it is safe to do so.) |
| 794 | */ |
| 795 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 796 | const char *function_name; |
| 797 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 798 | int nullable; |
| 799 | int allow_fd; |
| 800 | wchar_t *wide; |
| 801 | char *narrow; |
| 802 | int fd; |
| 803 | Py_ssize_t length; |
| 804 | PyObject *object; |
| 805 | PyObject *cleanup; |
| 806 | } path_t; |
| 807 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 808 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 809 | {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL} |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 810 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 811 | static void |
| 812 | path_cleanup(path_t *path) { |
| 813 | if (path->cleanup) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 814 | Py_CLEAR(path->cleanup); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | |
| 818 | static int |
| 819 | path_converter(PyObject *o, void *p) { |
| 820 | path_t *path = (path_t *)p; |
| 821 | PyObject *unicode, *bytes; |
| 822 | Py_ssize_t length; |
| 823 | char *narrow; |
| 824 | |
| 825 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 826 | PyErr_Format(exc, "%s%s" fmt, \ |
| 827 | path->function_name ? path->function_name : "", \ |
| 828 | path->function_name ? ": " : "", \ |
| 829 | path->argument_name ? path->argument_name : "path") |
| 830 | |
| 831 | /* Py_CLEANUP_SUPPORTED support */ |
| 832 | if (o == NULL) { |
| 833 | path_cleanup(path); |
| 834 | return 1; |
| 835 | } |
| 836 | |
| 837 | /* ensure it's always safe to call path_cleanup() */ |
| 838 | path->cleanup = NULL; |
| 839 | |
| 840 | if (o == Py_None) { |
| 841 | if (!path->nullable) { |
| 842 | FORMAT_EXCEPTION(PyExc_TypeError, |
| 843 | "can't specify None for %s argument"); |
| 844 | return 0; |
| 845 | } |
| 846 | path->wide = NULL; |
| 847 | path->narrow = NULL; |
| 848 | path->length = 0; |
| 849 | path->object = o; |
| 850 | path->fd = -1; |
| 851 | return 1; |
| 852 | } |
| 853 | |
| 854 | unicode = PyUnicode_FromObject(o); |
| 855 | if (unicode) { |
| 856 | #ifdef MS_WINDOWS |
| 857 | wchar_t *wide; |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 858 | |
| 859 | wide = PyUnicode_AsUnicodeAndSize(unicode, &length); |
| 860 | if (!wide) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 861 | Py_DECREF(unicode); |
| 862 | return 0; |
| 863 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 864 | if (length > 32767) { |
| 865 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 866 | Py_DECREF(unicode); |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | path->wide = wide; |
| 871 | path->narrow = NULL; |
| 872 | path->length = length; |
| 873 | path->object = o; |
| 874 | path->fd = -1; |
| 875 | path->cleanup = unicode; |
| 876 | return Py_CLEANUP_SUPPORTED; |
| 877 | #else |
| 878 | int converted = PyUnicode_FSConverter(unicode, &bytes); |
| 879 | Py_DECREF(unicode); |
| 880 | if (!converted) |
| 881 | bytes = NULL; |
| 882 | #endif |
| 883 | } |
| 884 | else { |
| 885 | PyErr_Clear(); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 886 | if (PyObject_CheckBuffer(o)) |
| 887 | bytes = PyBytes_FromObject(o); |
| 888 | else |
| 889 | bytes = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 890 | if (!bytes) { |
| 891 | PyErr_Clear(); |
| 892 | if (path->allow_fd) { |
| 893 | int fd; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 894 | int result = _fd_converter(o, &fd, |
| 895 | "string, bytes or integer"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 896 | if (result) { |
| 897 | path->wide = NULL; |
| 898 | path->narrow = NULL; |
| 899 | path->length = 0; |
| 900 | path->object = o; |
| 901 | path->fd = fd; |
| 902 | return result; |
| 903 | } |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | if (!bytes) { |
| 909 | if (!PyErr_Occurred()) |
| 910 | FORMAT_EXCEPTION(PyExc_TypeError, "illegal type for %s parameter"); |
| 911 | return 0; |
| 912 | } |
| 913 | |
| 914 | #ifdef MS_WINDOWS |
| 915 | if (win32_warn_bytes_api()) { |
| 916 | Py_DECREF(bytes); |
| 917 | return 0; |
| 918 | } |
| 919 | #endif |
| 920 | |
| 921 | length = PyBytes_GET_SIZE(bytes); |
| 922 | #ifdef MS_WINDOWS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 923 | if (length > MAX_PATH-1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 924 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
| 925 | Py_DECREF(bytes); |
| 926 | return 0; |
| 927 | } |
| 928 | #endif |
| 929 | |
| 930 | narrow = PyBytes_AS_STRING(bytes); |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 931 | if ((size_t)length != strlen(narrow)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 932 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 933 | Py_DECREF(bytes); |
| 934 | return 0; |
| 935 | } |
| 936 | |
| 937 | path->wide = NULL; |
| 938 | path->narrow = narrow; |
| 939 | path->length = length; |
| 940 | path->object = o; |
| 941 | path->fd = -1; |
| 942 | path->cleanup = bytes; |
| 943 | return Py_CLEANUP_SUPPORTED; |
| 944 | } |
| 945 | |
| 946 | static void |
| 947 | argument_unavailable_error(char *function_name, char *argument_name) { |
| 948 | PyErr_Format(PyExc_NotImplementedError, |
| 949 | "%s%s%s unavailable on this platform", |
| 950 | (function_name != NULL) ? function_name : "", |
| 951 | (function_name != NULL) ? ": ": "", |
| 952 | argument_name); |
| 953 | } |
| 954 | |
| 955 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 956 | dir_fd_unavailable(PyObject *o, void *p) |
| 957 | { |
| 958 | int dir_fd; |
| 959 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 960 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 961 | if (dir_fd != DEFAULT_DIR_FD) { |
| 962 | argument_unavailable_error(NULL, "dir_fd"); |
| 963 | return 0; |
| 964 | } |
| 965 | *(int *)p = dir_fd; |
| 966 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | static int |
| 970 | fd_specified(char *function_name, int fd) { |
| 971 | if (fd == -1) |
| 972 | return 0; |
| 973 | |
| 974 | argument_unavailable_error(function_name, "fd"); |
| 975 | return 1; |
| 976 | } |
| 977 | |
| 978 | static int |
| 979 | follow_symlinks_specified(char *function_name, int follow_symlinks) { |
| 980 | if (follow_symlinks) |
| 981 | return 0; |
| 982 | |
| 983 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 984 | return 1; |
| 985 | } |
| 986 | |
| 987 | static int |
| 988 | path_and_dir_fd_invalid(char *function_name, path_t *path, int dir_fd) { |
| 989 | if (!path->narrow && !path->wide && (dir_fd != DEFAULT_DIR_FD)) { |
| 990 | PyErr_Format(PyExc_ValueError, |
| 991 | "%s: can't specify dir_fd without matching path", |
| 992 | function_name); |
| 993 | return 1; |
| 994 | } |
| 995 | return 0; |
| 996 | } |
| 997 | |
| 998 | static int |
| 999 | dir_fd_and_fd_invalid(char *function_name, int dir_fd, int fd) { |
| 1000 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 1001 | PyErr_Format(PyExc_ValueError, |
| 1002 | "%s: can't specify both dir_fd and fd", |
| 1003 | function_name); |
| 1004 | return 1; |
| 1005 | } |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | static int |
| 1010 | fd_and_follow_symlinks_invalid(char *function_name, int fd, |
| 1011 | int follow_symlinks) { |
| 1012 | if ((fd > 0) && (!follow_symlinks)) { |
| 1013 | PyErr_Format(PyExc_ValueError, |
| 1014 | "%s: cannot use fd and follow_symlinks together", |
| 1015 | function_name); |
| 1016 | return 1; |
| 1017 | } |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
| 1021 | static int |
| 1022 | dir_fd_and_follow_symlinks_invalid(char *function_name, int dir_fd, |
| 1023 | int follow_symlinks) { |
| 1024 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 1025 | PyErr_Format(PyExc_ValueError, |
| 1026 | "%s: cannot use dir_fd and follow_symlinks together", |
| 1027 | function_name); |
| 1028 | return 1; |
| 1029 | } |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1033 | #ifdef MS_WINDOWS |
| 1034 | typedef PY_LONG_LONG Py_off_t; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1035 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1036 | typedef off_t Py_off_t; |
| 1037 | #endif |
| 1038 | |
| 1039 | static int |
| 1040 | Py_off_t_converter(PyObject *arg, void *addr) |
| 1041 | { |
| 1042 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1043 | *((Py_off_t *)addr) = PyLong_AsLongLong(arg); |
| 1044 | #else |
| 1045 | *((Py_off_t *)addr) = PyLong_AsLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1046 | #endif |
| 1047 | if (PyErr_Occurred()) |
| 1048 | return 0; |
| 1049 | return 1; |
| 1050 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1051 | |
| 1052 | static PyObject * |
| 1053 | PyLong_FromPy_off_t(Py_off_t offset) |
| 1054 | { |
| 1055 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1056 | return PyLong_FromLongLong(offset); |
| 1057 | #else |
| 1058 | return PyLong_FromLong(offset); |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1059 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1060 | } |
| 1061 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1062 | |
Steve Dower | d81431f | 2015-03-06 14:47:02 -0800 | [diff] [blame] | 1063 | #if defined _MSC_VER && _MSC_VER >= 1400 && _MSC_VER < 1900 |
| 1064 | /* Legacy implementation of _PyVerify_fd_dup2 while transitioning to |
| 1065 | * MSVC 14.0. This should eventually be removed. (issue23524) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1066 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1067 | #define IOINFO_L2E 5 |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1068 | #define IOINFO_ARRAYS 64 |
Steve Dower | 65e4cb1 | 2014-11-22 12:54:57 -0800 | [diff] [blame] | 1069 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1070 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1071 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 1072 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1073 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 1074 | static int |
| 1075 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 1076 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1077 | if (!_PyVerify_fd(fd1)) |
| 1078 | return 0; |
| 1079 | if (fd2 == _NO_CONSOLE_FILENO) |
| 1080 | return 0; |
| 1081 | if ((unsigned)fd2 < _NHANDLE_) |
| 1082 | return 1; |
| 1083 | else |
| 1084 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1085 | } |
| 1086 | #else |
Steve Dower | d81431f | 2015-03-06 14:47:02 -0800 | [diff] [blame] | 1087 | #define _PyVerify_fd_dup2(fd1, fd2) (_PyVerify_fd(fd1) && (fd2) >= 0) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1088 | #endif |
| 1089 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1090 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1091 | |
| 1092 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1093 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1094 | { |
| 1095 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1096 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 1097 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1098 | |
| 1099 | if (0 == DeviceIoControl( |
| 1100 | reparse_point_handle, |
| 1101 | FSCTL_GET_REPARSE_POINT, |
| 1102 | NULL, 0, /* in buffer */ |
| 1103 | target_buffer, sizeof(target_buffer), |
| 1104 | &n_bytes_returned, |
| 1105 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1106 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1107 | |
| 1108 | if (reparse_tag) |
| 1109 | *reparse_tag = rdb->ReparseTag; |
| 1110 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1111 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1112 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1113 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1114 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1115 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1116 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1117 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1118 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1119 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1120 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1121 | */ |
| 1122 | #include <crt_externs.h> |
| 1123 | static char **environ; |
| 1124 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1125 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1126 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1127 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1128 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1129 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1130 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1131 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1132 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1133 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1134 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1135 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1136 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1137 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1138 | d = PyDict_New(); |
| 1139 | if (d == NULL) |
| 1140 | return NULL; |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1141 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1142 | if (environ == NULL) |
| 1143 | environ = *_NSGetEnviron(); |
| 1144 | #endif |
| 1145 | #ifdef MS_WINDOWS |
| 1146 | /* _wenviron must be initialized in this way if the program is started |
| 1147 | through main() instead of wmain(). */ |
| 1148 | _wgetenv(L""); |
| 1149 | if (_wenviron == NULL) |
| 1150 | return d; |
| 1151 | /* This part ignores errors */ |
| 1152 | for (e = _wenviron; *e != NULL; e++) { |
| 1153 | PyObject *k; |
| 1154 | PyObject *v; |
| 1155 | wchar_t *p = wcschr(*e, L'='); |
| 1156 | if (p == NULL) |
| 1157 | continue; |
| 1158 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1159 | if (k == NULL) { |
| 1160 | PyErr_Clear(); |
| 1161 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1162 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1163 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1164 | if (v == NULL) { |
| 1165 | PyErr_Clear(); |
| 1166 | Py_DECREF(k); |
| 1167 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1168 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1169 | if (PyDict_GetItem(d, k) == NULL) { |
| 1170 | if (PyDict_SetItem(d, k, v) != 0) |
| 1171 | PyErr_Clear(); |
| 1172 | } |
| 1173 | Py_DECREF(k); |
| 1174 | Py_DECREF(v); |
| 1175 | } |
| 1176 | #else |
| 1177 | if (environ == NULL) |
| 1178 | return d; |
| 1179 | /* This part ignores errors */ |
| 1180 | for (e = environ; *e != NULL; e++) { |
| 1181 | PyObject *k; |
| 1182 | PyObject *v; |
| 1183 | char *p = strchr(*e, '='); |
| 1184 | if (p == NULL) |
| 1185 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1186 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1187 | if (k == NULL) { |
| 1188 | PyErr_Clear(); |
| 1189 | continue; |
| 1190 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1191 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1192 | if (v == NULL) { |
| 1193 | PyErr_Clear(); |
| 1194 | Py_DECREF(k); |
| 1195 | continue; |
| 1196 | } |
| 1197 | if (PyDict_GetItem(d, k) == NULL) { |
| 1198 | if (PyDict_SetItem(d, k, v) != 0) |
| 1199 | PyErr_Clear(); |
| 1200 | } |
| 1201 | Py_DECREF(k); |
| 1202 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1203 | } |
| 1204 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1205 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1208 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1209 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1210 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1211 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1212 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1213 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1214 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1215 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1216 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1217 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1218 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1219 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1220 | /* XXX We should pass the function name along in the future. |
| 1221 | (winreg.c also wants to pass the function name.) |
| 1222 | This would however require an additional param to the |
| 1223 | Windows error object, which is non-trivial. |
| 1224 | */ |
| 1225 | errno = GetLastError(); |
| 1226 | if (filename) |
| 1227 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1228 | else |
| 1229 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1230 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1231 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1232 | static PyObject * |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1233 | win32_error_object(char* function, PyObject* filename) |
| 1234 | { |
| 1235 | /* XXX - see win32_error for comments on 'function' */ |
| 1236 | errno = GetLastError(); |
| 1237 | if (filename) |
| 1238 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1239 | PyExc_OSError, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1240 | errno, |
| 1241 | filename); |
| 1242 | else |
| 1243 | return PyErr_SetFromWindowsErr(errno); |
| 1244 | } |
| 1245 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1246 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1247 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1248 | static PyObject * |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1249 | path_error(path_t *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1250 | { |
| 1251 | #ifdef MS_WINDOWS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1252 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 1253 | 0, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1254 | #else |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1255 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1256 | #endif |
| 1257 | } |
| 1258 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1259 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1260 | static PyObject * |
| 1261 | path_error2(path_t *path, path_t *path2) |
| 1262 | { |
| 1263 | #ifdef MS_WINDOWS |
| 1264 | return PyErr_SetExcFromWindowsErrWithFilenameObjects(PyExc_OSError, |
| 1265 | 0, path->object, path2->object); |
| 1266 | #else |
| 1267 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, |
| 1268 | path->object, path2->object); |
| 1269 | #endif |
| 1270 | } |
| 1271 | |
| 1272 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1273 | /* POSIX generic methods */ |
| 1274 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1275 | static int |
| 1276 | fildes_converter(PyObject *o, void *p) |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1277 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1278 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1279 | int *pointer = (int *)p; |
| 1280 | fd = PyObject_AsFileDescriptor(o); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1281 | if (fd < 0) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1282 | return 0; |
| 1283 | if (!_PyVerify_fd(fd)) { |
| 1284 | posix_error(); |
| 1285 | return 0; |
| 1286 | } |
| 1287 | *pointer = fd; |
| 1288 | return 1; |
| 1289 | } |
| 1290 | |
| 1291 | static PyObject * |
| 1292 | posix_fildes_fd(int fd, int (*func)(int)) |
| 1293 | { |
| 1294 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1295 | int async_err = 0; |
| 1296 | |
| 1297 | do { |
| 1298 | Py_BEGIN_ALLOW_THREADS |
| 1299 | res = (*func)(fd); |
| 1300 | Py_END_ALLOW_THREADS |
| 1301 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 1302 | if (res != 0) |
| 1303 | return (!async_err) ? posix_error() : NULL; |
| 1304 | Py_RETURN_NONE; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1305 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1306 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1307 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1308 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1309 | /* This is a reimplementation of the C library's chdir function, |
| 1310 | but one that produces Win32 errors instead of DOS error codes. |
| 1311 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1312 | it also needs to set "magic" environment variables indicating |
| 1313 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1314 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1315 | win32_chdir(LPCSTR path) |
| 1316 | { |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1317 | char new_path[MAX_PATH]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1318 | int result; |
| 1319 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1320 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1321 | if(!SetCurrentDirectoryA(path)) |
| 1322 | return FALSE; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1323 | result = GetCurrentDirectoryA(Py_ARRAY_LENGTH(new_path), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1324 | if (!result) |
| 1325 | return FALSE; |
| 1326 | /* In the ANSI API, there should not be any paths longer |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1327 | than MAX_PATH-1 (not including the final null character). */ |
| 1328 | assert(result < Py_ARRAY_LENGTH(new_path)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1329 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 1330 | strncmp(new_path, "//", 2) == 0) |
| 1331 | /* UNC path, nothing to do. */ |
| 1332 | return TRUE; |
| 1333 | env[1] = new_path[0]; |
| 1334 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | /* The Unicode version differs from the ANSI version |
| 1338 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1339 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1340 | win32_wchdir(LPCWSTR path) |
| 1341 | { |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1342 | wchar_t _new_path[MAX_PATH], *new_path = _new_path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1343 | int result; |
| 1344 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1345 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1346 | if(!SetCurrentDirectoryW(path)) |
| 1347 | return FALSE; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1348 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(new_path), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1349 | if (!result) |
| 1350 | return FALSE; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 1351 | if (result > Py_ARRAY_LENGTH(new_path)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1352 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1353 | if (!new_path) { |
| 1354 | SetLastError(ERROR_OUTOFMEMORY); |
| 1355 | return FALSE; |
| 1356 | } |
| 1357 | result = GetCurrentDirectoryW(result, new_path); |
| 1358 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1359 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1360 | return FALSE; |
| 1361 | } |
| 1362 | } |
| 1363 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1364 | wcsncmp(new_path, L"//", 2) == 0) |
| 1365 | /* UNC path, nothing to do. */ |
| 1366 | return TRUE; |
| 1367 | env[1] = new_path[0]; |
| 1368 | result = SetEnvironmentVariableW(env, new_path); |
| 1369 | if (new_path != _new_path) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1370 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1371 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1372 | } |
| 1373 | #endif |
| 1374 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1375 | #ifdef MS_WINDOWS |
| 1376 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1377 | - time stamps are restricted to second resolution |
| 1378 | - file modification times suffer from forth-and-back conversions between |
| 1379 | UTC and local time |
| 1380 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1381 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1382 | #define HAVE_STAT_NSEC 1 |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1383 | #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1384 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1385 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1386 | 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] | 1387 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1388 | HANDLE hFindFile; |
| 1389 | WIN32_FIND_DATAA FileData; |
| 1390 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1391 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1392 | return FALSE; |
| 1393 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1394 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1395 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1396 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1397 | info->ftCreationTime = FileData.ftCreationTime; |
| 1398 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1399 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1400 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1401 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1402 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1403 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1404 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1405 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1408 | static void |
| 1409 | find_data_to_file_info_w(WIN32_FIND_DATAW *pFileData, |
| 1410 | BY_HANDLE_FILE_INFORMATION *info, |
| 1411 | ULONG *reparse_tag) |
| 1412 | { |
| 1413 | memset(info, 0, sizeof(*info)); |
| 1414 | info->dwFileAttributes = pFileData->dwFileAttributes; |
| 1415 | info->ftCreationTime = pFileData->ftCreationTime; |
| 1416 | info->ftLastAccessTime = pFileData->ftLastAccessTime; |
| 1417 | info->ftLastWriteTime = pFileData->ftLastWriteTime; |
| 1418 | info->nFileSizeHigh = pFileData->nFileSizeHigh; |
| 1419 | info->nFileSizeLow = pFileData->nFileSizeLow; |
| 1420 | /* info->nNumberOfLinks = 1; */ |
| 1421 | if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1422 | *reparse_tag = pFileData->dwReserved0; |
| 1423 | else |
| 1424 | *reparse_tag = 0; |
| 1425 | } |
| 1426 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1427 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1428 | 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] | 1429 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1430 | HANDLE hFindFile; |
| 1431 | WIN32_FIND_DATAW FileData; |
| 1432 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1433 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1434 | return FALSE; |
| 1435 | FindClose(hFindFile); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1436 | find_data_to_file_info_w(&FileData, info, reparse_tag); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1437 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1440 | static BOOL |
| 1441 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1442 | { |
| 1443 | int buf_size, result_length; |
| 1444 | wchar_t *buf; |
| 1445 | |
| 1446 | /* We have a good handle to the target, use it to determine |
| 1447 | the target path name (then we'll call lstat on it). */ |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1448 | buf_size = GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1449 | VOLUME_NAME_DOS); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1450 | if(!buf_size) |
| 1451 | return FALSE; |
| 1452 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 1453 | buf = PyMem_New(wchar_t, buf_size+1); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1454 | if (!buf) { |
| 1455 | SetLastError(ERROR_OUTOFMEMORY); |
| 1456 | return FALSE; |
| 1457 | } |
| 1458 | |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 1459 | result_length = GetFinalPathNameByHandleW(hdl, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1460 | buf, buf_size, VOLUME_NAME_DOS); |
| 1461 | |
| 1462 | if(!result_length) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1463 | PyMem_Free(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1464 | return FALSE; |
| 1465 | } |
| 1466 | |
| 1467 | if(!CloseHandle(hdl)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1468 | PyMem_Free(buf); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1469 | return FALSE; |
| 1470 | } |
| 1471 | |
| 1472 | buf[result_length] = 0; |
| 1473 | |
| 1474 | *target_path = buf; |
| 1475 | return TRUE; |
| 1476 | } |
| 1477 | |
| 1478 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1479 | win32_xstat_impl_w(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1480 | BOOL traverse); |
| 1481 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1482 | win32_xstat_impl(const char *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1483 | BOOL traverse) |
| 1484 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1485 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1486 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1487 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1488 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1489 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1490 | const char *dot; |
| 1491 | |
| 1492 | hFile = CreateFileA( |
| 1493 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1494 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1495 | 0, /* share mode */ |
| 1496 | NULL, /* security attributes */ |
| 1497 | OPEN_EXISTING, |
| 1498 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1499 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1500 | Because of this, calls like GetFinalPathNameByHandle will return |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 1501 | the symlink path again and not the actual final path. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1502 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1503 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1504 | NULL); |
| 1505 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1506 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1507 | /* Either the target doesn't exist, or we don't have access to |
| 1508 | get a handle to it. If the former, we need to return an error. |
| 1509 | If the latter, we can use attributes_from_dir. */ |
| 1510 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1511 | return -1; |
| 1512 | /* Could not get attributes on open file. Fall back to |
| 1513 | reading the directory. */ |
| 1514 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1515 | /* Very strange. This should not fail now */ |
| 1516 | return -1; |
| 1517 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1518 | if (traverse) { |
| 1519 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1520 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1521 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1522 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1523 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1524 | } else { |
| 1525 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1526 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1527 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1528 | } |
| 1529 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1530 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1531 | return -1; |
| 1532 | |
| 1533 | /* Close the outer open file handle now that we're about to |
| 1534 | reopen it with different flags. */ |
| 1535 | if (!CloseHandle(hFile)) |
| 1536 | return -1; |
| 1537 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1538 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1539 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1540 | the file without the reparse handling flag set. */ |
| 1541 | hFile2 = CreateFileA( |
| 1542 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1543 | NULL, OPEN_EXISTING, |
| 1544 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1545 | NULL); |
| 1546 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1547 | return -1; |
| 1548 | |
| 1549 | if (!get_target_path(hFile2, &target_path)) |
| 1550 | return -1; |
| 1551 | |
| 1552 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1553 | PyMem_Free(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1554 | return code; |
| 1555 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1556 | } else |
| 1557 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1558 | } |
Steve Dower | a2af1a5 | 2015-02-21 10:04:10 -0800 | [diff] [blame] | 1559 | _Py_attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1560 | |
| 1561 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1562 | dot = strrchr(path, '.'); |
| 1563 | if (dot) { |
| 1564 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1565 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1566 | result->st_mode |= 0111; |
| 1567 | } |
| 1568 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1572 | win32_xstat_impl_w(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1573 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1574 | { |
| 1575 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1576 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1577 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1578 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1579 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1580 | const wchar_t *dot; |
| 1581 | |
| 1582 | hFile = CreateFileW( |
| 1583 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1584 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1585 | 0, /* share mode */ |
| 1586 | NULL, /* security attributes */ |
| 1587 | OPEN_EXISTING, |
| 1588 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1589 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1590 | Because of this, calls like GetFinalPathNameByHandle will return |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 1591 | the symlink path again and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1592 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1593 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1594 | NULL); |
| 1595 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1596 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1597 | /* Either the target doesn't exist, or we don't have access to |
| 1598 | get a handle to it. If the former, we need to return an error. |
| 1599 | If the latter, we can use attributes_from_dir. */ |
| 1600 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1601 | return -1; |
| 1602 | /* Could not get attributes on open file. Fall back to |
| 1603 | reading the directory. */ |
| 1604 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1605 | /* Very strange. This should not fail now */ |
| 1606 | return -1; |
| 1607 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1608 | if (traverse) { |
| 1609 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1610 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1611 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1612 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1613 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1614 | } else { |
| 1615 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1616 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1617 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1618 | } |
| 1619 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1620 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1621 | return -1; |
| 1622 | |
| 1623 | /* Close the outer open file handle now that we're about to |
| 1624 | reopen it with different flags. */ |
| 1625 | if (!CloseHandle(hFile)) |
| 1626 | return -1; |
| 1627 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1628 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1629 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1630 | the file without the reparse handling flag set. */ |
| 1631 | hFile2 = CreateFileW( |
| 1632 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1633 | NULL, OPEN_EXISTING, |
| 1634 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1635 | NULL); |
| 1636 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1637 | return -1; |
| 1638 | |
| 1639 | if (!get_target_path(hFile2, &target_path)) |
| 1640 | return -1; |
| 1641 | |
| 1642 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1643 | PyMem_Free(target_path); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1644 | return code; |
| 1645 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1646 | } else |
| 1647 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1648 | } |
Steve Dower | a2af1a5 | 2015-02-21 10:04:10 -0800 | [diff] [blame] | 1649 | _Py_attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1650 | |
| 1651 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1652 | dot = wcsrchr(path, '.'); |
| 1653 | if (dot) { |
| 1654 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1655 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1656 | result->st_mode |= 0111; |
| 1657 | } |
| 1658 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1662 | win32_xstat(const char *path, struct _Py_stat_struct *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1663 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1664 | /* Protocol violation: we explicitly clear errno, instead of |
| 1665 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1666 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1667 | errno = 0; |
| 1668 | return code; |
| 1669 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1670 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1671 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1672 | win32_xstat_w(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1673 | { |
| 1674 | /* Protocol violation: we explicitly clear errno, instead of |
| 1675 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1676 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1677 | errno = 0; |
| 1678 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1679 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1680 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1681 | |
| 1682 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1683 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1684 | default does not traverse symlinks and instead returns attributes for |
| 1685 | the symlink. |
| 1686 | |
| 1687 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1688 | win32_stat will first explicitly resolve the symlink target and then will |
| 1689 | call win32_lstat on that result. |
| 1690 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1691 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1692 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1693 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1694 | win32_lstat(const char* path, struct _Py_stat_struct *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1695 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1696 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1699 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1700 | win32_lstat_w(const wchar_t* path, struct _Py_stat_struct *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1701 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1702 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1706 | win32_stat(const char* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1707 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1708 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1711 | static int |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1712 | win32_stat_w(const wchar_t* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1713 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1714 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1717 | #endif /* MS_WINDOWS */ |
| 1718 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1719 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1720 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1721 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1722 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1723 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1724 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1725 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1726 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1727 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1728 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1729 | |
| 1730 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1731 | {"st_mode", "protection bits"}, |
| 1732 | {"st_ino", "inode"}, |
| 1733 | {"st_dev", "device"}, |
| 1734 | {"st_nlink", "number of hard links"}, |
| 1735 | {"st_uid", "user ID of owner"}, |
| 1736 | {"st_gid", "group ID of owner"}, |
| 1737 | {"st_size", "total size, in bytes"}, |
| 1738 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1739 | {NULL, "integer time of last access"}, |
| 1740 | {NULL, "integer time of last modification"}, |
| 1741 | {NULL, "integer time of last change"}, |
| 1742 | {"st_atime", "time of last access"}, |
| 1743 | {"st_mtime", "time of last modification"}, |
| 1744 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1745 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1746 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1747 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1748 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1749 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1750 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1751 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1752 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1753 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1754 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1755 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1756 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1757 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1758 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1759 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1760 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1761 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1762 | #endif |
| 1763 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1764 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1765 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1766 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1767 | {"st_file_attributes", "Windows file attribute bits"}, |
| 1768 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1769 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1770 | }; |
| 1771 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1772 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1773 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1774 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1775 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1776 | #endif |
| 1777 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1778 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1779 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1780 | #else |
| 1781 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1782 | #endif |
| 1783 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1784 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1785 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1786 | #else |
| 1787 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1788 | #endif |
| 1789 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1790 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1791 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1792 | #else |
| 1793 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1794 | #endif |
| 1795 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1796 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1797 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1798 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1799 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1800 | #endif |
| 1801 | |
| 1802 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1803 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1804 | #else |
| 1805 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1806 | #endif |
| 1807 | |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1808 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1809 | #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1) |
| 1810 | #else |
| 1811 | #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX |
| 1812 | #endif |
| 1813 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1814 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1815 | "stat_result", /* name */ |
| 1816 | stat_result__doc__, /* doc */ |
| 1817 | stat_result_fields, |
| 1818 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1819 | }; |
| 1820 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1821 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1822 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1823 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1824 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1825 | 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] | 1826 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1827 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1828 | |
| 1829 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1830 | {"f_bsize", }, |
| 1831 | {"f_frsize", }, |
| 1832 | {"f_blocks", }, |
| 1833 | {"f_bfree", }, |
| 1834 | {"f_bavail", }, |
| 1835 | {"f_files", }, |
| 1836 | {"f_ffree", }, |
| 1837 | {"f_favail", }, |
| 1838 | {"f_flag", }, |
| 1839 | {"f_namemax",}, |
| 1840 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1841 | }; |
| 1842 | |
| 1843 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1844 | "statvfs_result", /* name */ |
| 1845 | statvfs_result__doc__, /* doc */ |
| 1846 | statvfs_result_fields, |
| 1847 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1848 | }; |
| 1849 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1850 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1851 | PyDoc_STRVAR(waitid_result__doc__, |
| 1852 | "waitid_result: Result from waitid.\n\n\ |
| 1853 | This object may be accessed either as a tuple of\n\ |
| 1854 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1855 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1856 | \n\ |
| 1857 | See os.waitid for more information."); |
| 1858 | |
| 1859 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1860 | {"si_pid", }, |
| 1861 | {"si_uid", }, |
| 1862 | {"si_signo", }, |
| 1863 | {"si_status", }, |
| 1864 | {"si_code", }, |
| 1865 | {0} |
| 1866 | }; |
| 1867 | |
| 1868 | static PyStructSequence_Desc waitid_result_desc = { |
| 1869 | "waitid_result", /* name */ |
| 1870 | waitid_result__doc__, /* doc */ |
| 1871 | waitid_result_fields, |
| 1872 | 5 |
| 1873 | }; |
| 1874 | static PyTypeObject WaitidResultType; |
| 1875 | #endif |
| 1876 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1877 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1878 | static PyTypeObject StatResultType; |
| 1879 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1880 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 1881 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1882 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1883 | static newfunc structseq_new; |
| 1884 | |
| 1885 | static PyObject * |
| 1886 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1887 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1888 | PyStructSequence *result; |
| 1889 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1890 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1891 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1892 | if (!result) |
| 1893 | return NULL; |
| 1894 | /* If we have been initialized from a tuple, |
| 1895 | st_?time might be set to None. Initialize it |
| 1896 | from the int slots. */ |
| 1897 | for (i = 7; i <= 9; i++) { |
| 1898 | if (result->ob_item[i+3] == Py_None) { |
| 1899 | Py_DECREF(Py_None); |
| 1900 | Py_INCREF(result->ob_item[i]); |
| 1901 | result->ob_item[i+3] = result->ob_item[i]; |
| 1902 | } |
| 1903 | } |
| 1904 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | |
| 1908 | |
| 1909 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1910 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1911 | |
| 1912 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1913 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1914 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1915 | \n\ |
| 1916 | If value is True, future calls to stat() return floats; if it is False,\n\ |
| 1917 | future calls return ints.\n\ |
| 1918 | If value is omitted, return the current setting.\n"); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1919 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1920 | /* AC 3.5: the public default value should be None, not ready for that yet */ |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1921 | static PyObject* |
| 1922 | stat_float_times(PyObject* self, PyObject *args) |
| 1923 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1924 | int newval = -1; |
| 1925 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1926 | return NULL; |
Victor Stinner | 034d0aa | 2012-06-05 01:22:15 +0200 | [diff] [blame] | 1927 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 1928 | "stat_float_times() is deprecated", |
| 1929 | 1)) |
| 1930 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1931 | if (newval == -1) |
| 1932 | /* Return old value */ |
| 1933 | return PyBool_FromLong(_stat_float_times); |
| 1934 | _stat_float_times = newval; |
| 1935 | Py_INCREF(Py_None); |
| 1936 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1937 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1938 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1939 | static PyObject *billion = NULL; |
| 1940 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1941 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1942 | 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] | 1943 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1944 | PyObject *s = _PyLong_FromTime_t(sec); |
| 1945 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 1946 | PyObject *s_in_ns = NULL; |
| 1947 | PyObject *ns_total = NULL; |
| 1948 | PyObject *float_s = NULL; |
| 1949 | |
| 1950 | if (!(s && ns_fractional)) |
| 1951 | goto exit; |
| 1952 | |
| 1953 | s_in_ns = PyNumber_Multiply(s, billion); |
| 1954 | if (!s_in_ns) |
| 1955 | goto exit; |
| 1956 | |
| 1957 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 1958 | if (!ns_total) |
| 1959 | goto exit; |
| 1960 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1961 | if (_stat_float_times) { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1962 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1963 | if (!float_s) |
| 1964 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1965 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1966 | else { |
| 1967 | float_s = s; |
| 1968 | Py_INCREF(float_s); |
| 1969 | } |
| 1970 | |
| 1971 | PyStructSequence_SET_ITEM(v, index, s); |
| 1972 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 1973 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 1974 | s = NULL; |
| 1975 | float_s = NULL; |
| 1976 | ns_total = NULL; |
| 1977 | exit: |
| 1978 | Py_XDECREF(s); |
| 1979 | Py_XDECREF(ns_fractional); |
| 1980 | Py_XDECREF(s_in_ns); |
| 1981 | Py_XDECREF(ns_total); |
| 1982 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1985 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1986 | (used by posix_stat() and posix_fstat()) */ |
| 1987 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1988 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1989 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1990 | unsigned long ansec, mnsec, cnsec; |
| 1991 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 1992 | if (v == NULL) |
| 1993 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1994 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1995 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1996 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1997 | PyStructSequence_SET_ITEM(v, 1, |
| 1998 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1999 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2000 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2001 | #endif |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2002 | #ifdef MS_WINDOWS |
| 2003 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2004 | #else |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 2005 | PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2006 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2007 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2008 | #if defined(MS_WINDOWS) |
| 2009 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2010 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2011 | #else |
| 2012 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2013 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2014 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2015 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2016 | PyStructSequence_SET_ITEM(v, 6, |
| 2017 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2018 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2019 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2020 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2021 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2022 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2023 | ansec = st->st_atim.tv_nsec; |
| 2024 | mnsec = st->st_mtim.tv_nsec; |
| 2025 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2026 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2027 | ansec = st->st_atimespec.tv_nsec; |
| 2028 | mnsec = st->st_mtimespec.tv_nsec; |
| 2029 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2030 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2031 | ansec = st->st_atime_nsec; |
| 2032 | mnsec = st->st_mtime_nsec; |
| 2033 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2034 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2035 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2036 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2037 | fill_time(v, 7, st->st_atime, ansec); |
| 2038 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2039 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2040 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2041 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2042 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2043 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2044 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2045 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2046 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2047 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2048 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2049 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2050 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2051 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2052 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2053 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2054 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2055 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2056 | #endif |
| 2057 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2058 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2059 | PyObject *val; |
| 2060 | unsigned long bsec,bnsec; |
| 2061 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2062 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2063 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2064 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2065 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2066 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2067 | if (_stat_float_times) { |
| 2068 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 2069 | } else { |
| 2070 | val = PyLong_FromLong((long)bsec); |
| 2071 | } |
| 2072 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2073 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2074 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2075 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2076 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2077 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2078 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2079 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2080 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2081 | PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX, |
| 2082 | PyLong_FromUnsignedLong(st->st_file_attributes)); |
| 2083 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2084 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2085 | if (PyErr_Occurred()) { |
| 2086 | Py_DECREF(v); |
| 2087 | return NULL; |
| 2088 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2089 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2090 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2093 | /* POSIX methods */ |
| 2094 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2095 | |
| 2096 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2097 | posix_do_stat(char *function_name, path_t *path, |
| 2098 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2099 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2100 | STRUCT_STAT st; |
| 2101 | int result; |
| 2102 | |
| 2103 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2104 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2105 | return NULL; |
| 2106 | #endif |
| 2107 | |
| 2108 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2109 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2110 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2111 | return NULL; |
| 2112 | |
| 2113 | Py_BEGIN_ALLOW_THREADS |
| 2114 | if (path->fd != -1) |
| 2115 | result = FSTAT(path->fd, &st); |
| 2116 | else |
| 2117 | #ifdef MS_WINDOWS |
| 2118 | if (path->wide) { |
| 2119 | if (follow_symlinks) |
| 2120 | result = win32_stat_w(path->wide, &st); |
| 2121 | else |
| 2122 | result = win32_lstat_w(path->wide, &st); |
| 2123 | } |
| 2124 | else |
| 2125 | #endif |
| 2126 | #if defined(HAVE_LSTAT) || defined(MS_WINDOWS) |
| 2127 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2128 | result = LSTAT(path->narrow, &st); |
| 2129 | else |
| 2130 | #endif |
| 2131 | #ifdef HAVE_FSTATAT |
| 2132 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2133 | result = fstatat(dir_fd, path->narrow, &st, |
| 2134 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2135 | else |
| 2136 | #endif |
| 2137 | result = STAT(path->narrow, &st); |
| 2138 | Py_END_ALLOW_THREADS |
| 2139 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2140 | if (result != 0) { |
| 2141 | return path_error(path); |
| 2142 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2143 | |
| 2144 | return _pystat_fromstructstat(&st); |
| 2145 | } |
| 2146 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2147 | /*[python input] |
| 2148 | |
| 2149 | for s in """ |
| 2150 | |
| 2151 | FACCESSAT |
| 2152 | FCHMODAT |
| 2153 | FCHOWNAT |
| 2154 | FSTATAT |
| 2155 | LINKAT |
| 2156 | MKDIRAT |
| 2157 | MKFIFOAT |
| 2158 | MKNODAT |
| 2159 | OPENAT |
| 2160 | READLINKAT |
| 2161 | SYMLINKAT |
| 2162 | UNLINKAT |
| 2163 | |
| 2164 | """.strip().split(): |
| 2165 | s = s.strip() |
| 2166 | print(""" |
| 2167 | #ifdef HAVE_{s} |
| 2168 | #define {s}_DIR_FD_CONVERTER dir_fd_converter |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2169 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2170 | #define {s}_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2171 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2172 | """.rstrip().format(s=s)) |
| 2173 | |
| 2174 | for s in """ |
| 2175 | |
| 2176 | FCHDIR |
| 2177 | FCHMOD |
| 2178 | FCHOWN |
| 2179 | FDOPENDIR |
| 2180 | FEXECVE |
| 2181 | FPATHCONF |
| 2182 | FSTATVFS |
| 2183 | FTRUNCATE |
| 2184 | |
| 2185 | """.strip().split(): |
| 2186 | s = s.strip() |
| 2187 | print(""" |
| 2188 | #ifdef HAVE_{s} |
| 2189 | #define PATH_HAVE_{s} 1 |
| 2190 | #else |
| 2191 | #define PATH_HAVE_{s} 0 |
| 2192 | #endif |
| 2193 | |
| 2194 | """.rstrip().format(s=s)) |
| 2195 | [python start generated code]*/ |
| 2196 | |
| 2197 | #ifdef HAVE_FACCESSAT |
| 2198 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter |
| 2199 | #else |
| 2200 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2201 | #endif |
| 2202 | |
| 2203 | #ifdef HAVE_FCHMODAT |
| 2204 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2205 | #else |
| 2206 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2207 | #endif |
| 2208 | |
| 2209 | #ifdef HAVE_FCHOWNAT |
| 2210 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter |
| 2211 | #else |
| 2212 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2213 | #endif |
| 2214 | |
| 2215 | #ifdef HAVE_FSTATAT |
| 2216 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter |
| 2217 | #else |
| 2218 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2219 | #endif |
| 2220 | |
| 2221 | #ifdef HAVE_LINKAT |
| 2222 | #define LINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2223 | #else |
| 2224 | #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2225 | #endif |
| 2226 | |
| 2227 | #ifdef HAVE_MKDIRAT |
| 2228 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter |
| 2229 | #else |
| 2230 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2231 | #endif |
| 2232 | |
| 2233 | #ifdef HAVE_MKFIFOAT |
| 2234 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter |
| 2235 | #else |
| 2236 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2237 | #endif |
| 2238 | |
| 2239 | #ifdef HAVE_MKNODAT |
| 2240 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2241 | #else |
| 2242 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2243 | #endif |
| 2244 | |
| 2245 | #ifdef HAVE_OPENAT |
| 2246 | #define OPENAT_DIR_FD_CONVERTER dir_fd_converter |
| 2247 | #else |
| 2248 | #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2249 | #endif |
| 2250 | |
| 2251 | #ifdef HAVE_READLINKAT |
| 2252 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2253 | #else |
| 2254 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2255 | #endif |
| 2256 | |
| 2257 | #ifdef HAVE_SYMLINKAT |
| 2258 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2259 | #else |
| 2260 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2261 | #endif |
| 2262 | |
| 2263 | #ifdef HAVE_UNLINKAT |
| 2264 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2265 | #else |
| 2266 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2267 | #endif |
| 2268 | |
| 2269 | #ifdef HAVE_FCHDIR |
| 2270 | #define PATH_HAVE_FCHDIR 1 |
| 2271 | #else |
| 2272 | #define PATH_HAVE_FCHDIR 0 |
| 2273 | #endif |
| 2274 | |
| 2275 | #ifdef HAVE_FCHMOD |
| 2276 | #define PATH_HAVE_FCHMOD 1 |
| 2277 | #else |
| 2278 | #define PATH_HAVE_FCHMOD 0 |
| 2279 | #endif |
| 2280 | |
| 2281 | #ifdef HAVE_FCHOWN |
| 2282 | #define PATH_HAVE_FCHOWN 1 |
| 2283 | #else |
| 2284 | #define PATH_HAVE_FCHOWN 0 |
| 2285 | #endif |
| 2286 | |
| 2287 | #ifdef HAVE_FDOPENDIR |
| 2288 | #define PATH_HAVE_FDOPENDIR 1 |
| 2289 | #else |
| 2290 | #define PATH_HAVE_FDOPENDIR 0 |
| 2291 | #endif |
| 2292 | |
| 2293 | #ifdef HAVE_FEXECVE |
| 2294 | #define PATH_HAVE_FEXECVE 1 |
| 2295 | #else |
| 2296 | #define PATH_HAVE_FEXECVE 0 |
| 2297 | #endif |
| 2298 | |
| 2299 | #ifdef HAVE_FPATHCONF |
| 2300 | #define PATH_HAVE_FPATHCONF 1 |
| 2301 | #else |
| 2302 | #define PATH_HAVE_FPATHCONF 0 |
| 2303 | #endif |
| 2304 | |
| 2305 | #ifdef HAVE_FSTATVFS |
| 2306 | #define PATH_HAVE_FSTATVFS 1 |
| 2307 | #else |
| 2308 | #define PATH_HAVE_FSTATVFS 0 |
| 2309 | #endif |
| 2310 | |
| 2311 | #ifdef HAVE_FTRUNCATE |
| 2312 | #define PATH_HAVE_FTRUNCATE 1 |
| 2313 | #else |
| 2314 | #define PATH_HAVE_FTRUNCATE 0 |
| 2315 | #endif |
| 2316 | /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2317 | |
| 2318 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2319 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2320 | |
| 2321 | class path_t_converter(CConverter): |
| 2322 | |
| 2323 | type = "path_t" |
| 2324 | impl_by_reference = True |
| 2325 | parse_by_reference = True |
| 2326 | |
| 2327 | converter = 'path_converter' |
| 2328 | |
| 2329 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2330 | # right now path_t doesn't support default values. |
| 2331 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2332 | if self.default not in (unspecified, None): |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2333 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2334 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2335 | if self.c_default not in (None, 'Py_None'): |
| 2336 | raise RuntimeError("Can't specify a c_default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2337 | |
| 2338 | self.nullable = nullable |
| 2339 | self.allow_fd = allow_fd |
| 2340 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2341 | def pre_render(self): |
| 2342 | def strify(value): |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2343 | if isinstance(value, str): |
| 2344 | return value |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2345 | return str(int(bool(value))) |
| 2346 | |
| 2347 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2348 | self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format( |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2349 | self.function.name, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2350 | self.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2351 | strify(self.nullable), |
| 2352 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2353 | ) |
| 2354 | |
| 2355 | def cleanup(self): |
| 2356 | return "path_cleanup(&" + self.name + ");\n" |
| 2357 | |
| 2358 | |
| 2359 | class dir_fd_converter(CConverter): |
| 2360 | type = 'int' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2361 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2362 | def converter_init(self, requires=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2363 | if self.default in (unspecified, None): |
| 2364 | self.c_default = 'DEFAULT_DIR_FD' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2365 | if isinstance(requires, str): |
| 2366 | self.converter = requires.upper() + '_DIR_FD_CONVERTER' |
| 2367 | else: |
| 2368 | self.converter = 'dir_fd_converter' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2369 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2370 | class fildes_converter(CConverter): |
| 2371 | type = 'int' |
| 2372 | converter = 'fildes_converter' |
| 2373 | |
| 2374 | class uid_t_converter(CConverter): |
| 2375 | type = "uid_t" |
| 2376 | converter = '_Py_Uid_Converter' |
| 2377 | |
| 2378 | class gid_t_converter(CConverter): |
| 2379 | type = "gid_t" |
| 2380 | converter = '_Py_Gid_Converter' |
| 2381 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2382 | class dev_t_converter(CConverter): |
| 2383 | type = 'dev_t' |
| 2384 | converter = '_Py_Dev_Converter' |
| 2385 | |
| 2386 | class dev_t_return_converter(unsigned_long_return_converter): |
| 2387 | type = 'dev_t' |
| 2388 | conversion_fn = '_PyLong_FromDev' |
| 2389 | unsigned_cast = '(dev_t)' |
| 2390 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2391 | class FSConverter_converter(CConverter): |
| 2392 | type = 'PyObject *' |
| 2393 | converter = 'PyUnicode_FSConverter' |
| 2394 | def converter_init(self): |
| 2395 | if self.default is not unspecified: |
| 2396 | fail("FSConverter_converter does not support default values") |
| 2397 | self.c_default = 'NULL' |
| 2398 | |
| 2399 | def cleanup(self): |
| 2400 | return "Py_XDECREF(" + self.name + ");\n" |
| 2401 | |
| 2402 | class pid_t_converter(CConverter): |
| 2403 | type = 'pid_t' |
| 2404 | format_unit = '" _Py_PARSE_PID "' |
| 2405 | |
| 2406 | class idtype_t_converter(int_converter): |
| 2407 | type = 'idtype_t' |
| 2408 | |
| 2409 | class id_t_converter(CConverter): |
| 2410 | type = 'id_t' |
| 2411 | format_unit = '" _Py_PARSE_PID "' |
| 2412 | |
| 2413 | class Py_intptr_t_converter(CConverter): |
| 2414 | type = 'Py_intptr_t' |
| 2415 | format_unit = '" _Py_PARSE_INTPTR "' |
| 2416 | |
| 2417 | class Py_off_t_converter(CConverter): |
| 2418 | type = 'Py_off_t' |
| 2419 | converter = 'Py_off_t_converter' |
| 2420 | |
| 2421 | class Py_off_t_return_converter(long_return_converter): |
| 2422 | type = 'Py_off_t' |
| 2423 | conversion_fn = 'PyLong_FromPy_off_t' |
| 2424 | |
| 2425 | class path_confname_converter(CConverter): |
| 2426 | type="int" |
| 2427 | converter="conv_path_confname" |
| 2428 | |
| 2429 | class confstr_confname_converter(path_confname_converter): |
| 2430 | converter='conv_confstr_confname' |
| 2431 | |
| 2432 | class sysconf_confname_converter(path_confname_converter): |
| 2433 | converter="conv_sysconf_confname" |
| 2434 | |
| 2435 | class sched_param_converter(CConverter): |
| 2436 | type = 'struct sched_param' |
| 2437 | converter = 'convert_sched_param' |
| 2438 | impl_by_reference = True; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2439 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2440 | [python start generated code]*/ |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2441 | /*[python end generated code: output=da39a3ee5e6b4b0d input=affe68316f160401]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2442 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2443 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2444 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2445 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2446 | |
| 2447 | path : path_t(allow_fd=True) |
| 2448 | Path to be examined; can be string, bytes, or open-file-descriptor int. |
| 2449 | |
| 2450 | * |
| 2451 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2452 | dir_fd : dir_fd(requires='fstatat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2453 | If not None, it should be a file descriptor open to a directory, |
| 2454 | and path should be a relative string; path will then be relative to |
| 2455 | that directory. |
| 2456 | |
| 2457 | follow_symlinks: bool = True |
| 2458 | If False, and the last element of the path is a symbolic link, |
| 2459 | stat will examine the symbolic link itself instead of the file |
| 2460 | the link points to. |
| 2461 | |
| 2462 | Perform a stat system call on the given path. |
| 2463 | |
| 2464 | dir_fd and follow_symlinks may not be implemented |
| 2465 | on your platform. If they are unavailable, using them will raise a |
| 2466 | NotImplementedError. |
| 2467 | |
| 2468 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2469 | an open file descriptor. |
| 2470 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2471 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2472 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2473 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2474 | os_stat_impl(PyModuleDef *module, path_t *path, int dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2475 | /*[clinic end generated code: output=708c225f94fcfc8e input=099d356c306fa24a]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2476 | { |
| 2477 | return posix_do_stat("stat", path, dir_fd, follow_symlinks); |
| 2478 | } |
| 2479 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2480 | |
| 2481 | /*[clinic input] |
| 2482 | os.lstat |
| 2483 | |
| 2484 | path : path_t |
| 2485 | |
| 2486 | * |
| 2487 | |
| 2488 | dir_fd : dir_fd(requires='fstatat') = None |
| 2489 | |
| 2490 | Perform a stat system call on the given path, without following symbolic links. |
| 2491 | |
| 2492 | Like stat(), but do not follow symbolic links. |
| 2493 | Equivalent to stat(path, follow_symlinks=False). |
| 2494 | [clinic start generated code]*/ |
| 2495 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2496 | static PyObject * |
| 2497 | os_lstat_impl(PyModuleDef *module, path_t *path, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2498 | /*[clinic end generated code: output=7a748e333fcb39bd input=0b7474765927b925]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2499 | { |
| 2500 | int follow_symlinks = 0; |
| 2501 | return posix_do_stat("lstat", path, dir_fd, follow_symlinks); |
| 2502 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2503 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2504 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2505 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2506 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2507 | |
| 2508 | path: path_t(allow_fd=True) |
| 2509 | Path to be tested; can be string, bytes, or open-file-descriptor int. |
| 2510 | |
| 2511 | mode: int |
| 2512 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2513 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2514 | |
| 2515 | * |
| 2516 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2517 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2518 | If not None, it should be a file descriptor open to a directory, |
| 2519 | and path should be relative; path will then be relative to that |
| 2520 | directory. |
| 2521 | |
| 2522 | effective_ids: bool = False |
| 2523 | If True, access will use the effective uid/gid instead of |
| 2524 | the real uid/gid. |
| 2525 | |
| 2526 | follow_symlinks: bool = True |
| 2527 | If False, and the last element of the path is a symbolic link, |
| 2528 | access will examine the symbolic link itself instead of the file |
| 2529 | the link points to. |
| 2530 | |
| 2531 | Use the real uid/gid to test for access to a path. |
| 2532 | |
| 2533 | {parameters} |
| 2534 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2535 | on your platform. If they are unavailable, using them will raise a |
| 2536 | NotImplementedError. |
| 2537 | |
| 2538 | Note that most operations will use the effective uid/gid, therefore this |
| 2539 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2540 | has the specified access to the path. |
| 2541 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2542 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2543 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2544 | static int |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2545 | os_access_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, int effective_ids, int follow_symlinks) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2546 | /*[clinic end generated code: output=f9e734db3d88b767 input=b75a756797af45ec]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2547 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2548 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2549 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2550 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2551 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2552 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2553 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2554 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2555 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2556 | #ifndef HAVE_FACCESSAT |
| 2557 | if (follow_symlinks_specified("access", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2558 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2559 | |
| 2560 | if (effective_ids) { |
| 2561 | argument_unavailable_error("access", "effective_ids"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2562 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2563 | } |
| 2564 | #endif |
| 2565 | |
| 2566 | #ifdef MS_WINDOWS |
| 2567 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | ebe83f9 | 2013-10-19 22:36:17 +0200 | [diff] [blame] | 2568 | if (path->wide != NULL) |
| 2569 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2570 | else |
Christian Heimes | ebe83f9 | 2013-10-19 22:36:17 +0200 | [diff] [blame] | 2571 | attr = GetFileAttributesA(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2572 | Py_END_ALLOW_THREADS |
| 2573 | |
| 2574 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2575 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2576 | * * we didn't get a -1, and |
| 2577 | * * write access wasn't requested, |
| 2578 | * * or the file isn't read-only, |
| 2579 | * * or it's a directory. |
| 2580 | * (Directories cannot be read-only on Windows.) |
| 2581 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2582 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2583 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2584 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2585 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2586 | #else |
| 2587 | |
| 2588 | Py_BEGIN_ALLOW_THREADS |
| 2589 | #ifdef HAVE_FACCESSAT |
| 2590 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2591 | effective_ids || |
| 2592 | !follow_symlinks) { |
| 2593 | int flags = 0; |
| 2594 | if (!follow_symlinks) |
| 2595 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2596 | if (effective_ids) |
| 2597 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2598 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2599 | } |
| 2600 | else |
| 2601 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2602 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2603 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2604 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2605 | #endif |
| 2606 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2607 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2610 | #ifndef F_OK |
| 2611 | #define F_OK 0 |
| 2612 | #endif |
| 2613 | #ifndef R_OK |
| 2614 | #define R_OK 4 |
| 2615 | #endif |
| 2616 | #ifndef W_OK |
| 2617 | #define W_OK 2 |
| 2618 | #endif |
| 2619 | #ifndef X_OK |
| 2620 | #define X_OK 1 |
| 2621 | #endif |
| 2622 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2623 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2624 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2625 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2626 | os.ttyname -> DecodeFSDefault |
| 2627 | |
| 2628 | fd: int |
| 2629 | Integer file descriptor handle. |
| 2630 | |
| 2631 | / |
| 2632 | |
| 2633 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2634 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2635 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2636 | static char * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2637 | os_ttyname_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2638 | /*[clinic end generated code: output=03ad3d5ccaef75c3 input=5f72ca83e76b3b45]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2639 | { |
| 2640 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2641 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2642 | ret = ttyname(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2643 | if (ret == NULL) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2644 | posix_error(); |
| 2645 | return ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2646 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2647 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2648 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2649 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2650 | /*[clinic input] |
| 2651 | os.ctermid |
| 2652 | |
| 2653 | Return the name of the controlling terminal for this process. |
| 2654 | [clinic start generated code]*/ |
| 2655 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2656 | static PyObject * |
| 2657 | os_ctermid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2658 | /*[clinic end generated code: output=1b73788201e0aebd input=3b87fdd52556382d]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2659 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2660 | char *ret; |
| 2661 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2662 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2663 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2664 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2665 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2666 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2667 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2668 | if (ret == NULL) |
| 2669 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2670 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2671 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2672 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2673 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2674 | |
| 2675 | /*[clinic input] |
| 2676 | os.chdir |
| 2677 | |
| 2678 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') |
| 2679 | |
| 2680 | Change the current working directory to the specified path. |
| 2681 | |
| 2682 | path may always be specified as a string. |
| 2683 | On some platforms, path may also be specified as an open file descriptor. |
| 2684 | If this functionality is unavailable, using it raises an exception. |
| 2685 | [clinic start generated code]*/ |
| 2686 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2687 | static PyObject * |
| 2688 | os_chdir_impl(PyModuleDef *module, path_t *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2689 | /*[clinic end generated code: output=7358e3a20fb5aa93 input=1a4a15b4d12cb15d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2690 | { |
| 2691 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2692 | |
| 2693 | Py_BEGIN_ALLOW_THREADS |
| 2694 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2695 | if (path->wide) |
| 2696 | result = win32_wchdir(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2697 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2698 | result = win32_chdir(path->narrow); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 2699 | result = !result; /* on unix, success = 0, on windows, success = !0 */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2700 | #else |
| 2701 | #ifdef HAVE_FCHDIR |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2702 | if (path->fd != -1) |
| 2703 | result = fchdir(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2704 | else |
| 2705 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2706 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2707 | #endif |
| 2708 | Py_END_ALLOW_THREADS |
| 2709 | |
| 2710 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2711 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2712 | } |
| 2713 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2714 | Py_RETURN_NONE; |
| 2715 | } |
| 2716 | |
| 2717 | |
| 2718 | #ifdef HAVE_FCHDIR |
| 2719 | /*[clinic input] |
| 2720 | os.fchdir |
| 2721 | |
| 2722 | fd: fildes |
| 2723 | |
| 2724 | Change to the directory of the given file descriptor. |
| 2725 | |
| 2726 | fd must be opened on a directory, not a file. |
| 2727 | Equivalent to os.chdir(fd). |
| 2728 | |
| 2729 | [clinic start generated code]*/ |
| 2730 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2731 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2732 | os_fchdir_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2733 | /*[clinic end generated code: output=361d30df6b2d3418 input=18e816479a2fa985]*/ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2734 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2735 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2736 | } |
| 2737 | #endif /* HAVE_FCHDIR */ |
| 2738 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2739 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2740 | /*[clinic input] |
| 2741 | os.chmod |
| 2742 | |
| 2743 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
| 2744 | Path to be modified. May always be specified as a str or bytes. |
| 2745 | On some platforms, path may also be specified as an open file descriptor. |
| 2746 | If this functionality is unavailable, using it raises an exception. |
| 2747 | |
| 2748 | mode: int |
| 2749 | Operating-system mode bitfield. |
| 2750 | |
| 2751 | * |
| 2752 | |
| 2753 | dir_fd : dir_fd(requires='fchmodat') = None |
| 2754 | If not None, it should be a file descriptor open to a directory, |
| 2755 | and path should be relative; path will then be relative to that |
| 2756 | directory. |
| 2757 | |
| 2758 | follow_symlinks: bool = True |
| 2759 | If False, and the last element of the path is a symbolic link, |
| 2760 | chmod will modify the symbolic link itself instead of the file |
| 2761 | the link points to. |
| 2762 | |
| 2763 | Change the access permissions of a file. |
| 2764 | |
| 2765 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 2766 | an open file descriptor. |
| 2767 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 2768 | If they are unavailable, using them will raise a NotImplementedError. |
| 2769 | |
| 2770 | [clinic start generated code]*/ |
| 2771 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2772 | static PyObject * |
| 2773 | os_chmod_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2774 | /*[clinic end generated code: output=96063c976f23106a input=7f1618e5e15cc196]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2775 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2776 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2777 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2778 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2779 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2780 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2781 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2782 | #ifdef HAVE_FCHMODAT |
| 2783 | int fchmodat_nofollow_unsupported = 0; |
| 2784 | #endif |
| 2785 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2786 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2787 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2788 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2789 | #endif |
| 2790 | |
| 2791 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2792 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2793 | if (path->wide) |
| 2794 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2795 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2796 | attr = GetFileAttributesA(path->narrow); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 2797 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2798 | result = 0; |
| 2799 | else { |
| 2800 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2801 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2802 | else |
| 2803 | attr |= FILE_ATTRIBUTE_READONLY; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2804 | if (path->wide) |
| 2805 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2806 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2807 | result = SetFileAttributesA(path->narrow, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2808 | } |
| 2809 | Py_END_ALLOW_THREADS |
| 2810 | |
| 2811 | if (!result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2812 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2813 | } |
| 2814 | #else /* MS_WINDOWS */ |
| 2815 | Py_BEGIN_ALLOW_THREADS |
| 2816 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2817 | if (path->fd != -1) |
| 2818 | result = fchmod(path->fd, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2819 | else |
| 2820 | #endif |
| 2821 | #ifdef HAVE_LCHMOD |
| 2822 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2823 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2824 | else |
| 2825 | #endif |
| 2826 | #ifdef HAVE_FCHMODAT |
| 2827 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2828 | /* |
| 2829 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2830 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2831 | * and then says it isn't implemented yet. |
| 2832 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2833 | * |
| 2834 | * Once it is supported, os.chmod will automatically |
| 2835 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2836 | * Until then, we need to be careful what exception we raise. |
| 2837 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2838 | result = fchmodat(dir_fd, path->narrow, mode, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2839 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2840 | /* |
| 2841 | * But wait! We can't throw the exception without allowing threads, |
| 2842 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2843 | */ |
| 2844 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2845 | result && |
| 2846 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2847 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2848 | } |
| 2849 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2850 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2851 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2852 | Py_END_ALLOW_THREADS |
| 2853 | |
| 2854 | if (result) { |
| 2855 | #ifdef HAVE_FCHMODAT |
| 2856 | if (fchmodat_nofollow_unsupported) { |
| 2857 | if (dir_fd != DEFAULT_DIR_FD) |
| 2858 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 2859 | dir_fd, follow_symlinks); |
| 2860 | else |
| 2861 | follow_symlinks_specified("chmod", follow_symlinks); |
| 2862 | } |
| 2863 | else |
| 2864 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2865 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2866 | } |
| 2867 | #endif |
| 2868 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2869 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2870 | } |
| 2871 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2872 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2873 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2874 | /*[clinic input] |
| 2875 | os.fchmod |
| 2876 | |
| 2877 | fd: int |
| 2878 | mode: int |
| 2879 | |
| 2880 | Change the access permissions of the file given by file descriptor fd. |
| 2881 | |
| 2882 | Equivalent to os.chmod(fd, mode). |
| 2883 | [clinic start generated code]*/ |
| 2884 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2885 | static PyObject * |
| 2886 | os_fchmod_impl(PyModuleDef *module, int fd, int mode) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2887 | /*[clinic end generated code: output=2ee31ca226d1ed33 input=8ab11975ca01ee5b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2888 | { |
| 2889 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 2890 | int async_err = 0; |
| 2891 | |
| 2892 | do { |
| 2893 | Py_BEGIN_ALLOW_THREADS |
| 2894 | res = fchmod(fd, mode); |
| 2895 | Py_END_ALLOW_THREADS |
| 2896 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 2897 | if (res != 0) |
| 2898 | return (!async_err) ? posix_error() : NULL; |
| 2899 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2900 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2901 | } |
| 2902 | #endif /* HAVE_FCHMOD */ |
| 2903 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2904 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2905 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2906 | /*[clinic input] |
| 2907 | os.lchmod |
| 2908 | |
| 2909 | path: path_t |
| 2910 | mode: int |
| 2911 | |
| 2912 | Change the access permissions of a file, without following symbolic links. |
| 2913 | |
| 2914 | If path is a symlink, this affects the link itself rather than the target. |
| 2915 | Equivalent to chmod(path, mode, follow_symlinks=False)." |
| 2916 | [clinic start generated code]*/ |
| 2917 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2918 | static PyObject * |
| 2919 | os_lchmod_impl(PyModuleDef *module, path_t *path, int mode) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2920 | /*[clinic end generated code: output=7c0cc46588d89e46 input=90c5663c7465d24f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2921 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2922 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2923 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 2924 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2925 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2926 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2927 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2928 | return NULL; |
| 2929 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2930 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2931 | } |
| 2932 | #endif /* HAVE_LCHMOD */ |
| 2933 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2934 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2935 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2936 | /*[clinic input] |
| 2937 | os.chflags |
| 2938 | |
| 2939 | path: path_t |
| 2940 | flags: unsigned_long(bitwise=True) |
| 2941 | follow_symlinks: bool=True |
| 2942 | |
| 2943 | Set file flags. |
| 2944 | |
| 2945 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 2946 | link, chflags will change flags on the symbolic link itself instead of the |
| 2947 | file the link points to. |
| 2948 | follow_symlinks may not be implemented on your platform. If it is |
| 2949 | unavailable, using it will raise a NotImplementedError. |
| 2950 | |
| 2951 | [clinic start generated code]*/ |
| 2952 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2953 | static PyObject * |
| 2954 | os_chflags_impl(PyModuleDef *module, path_t *path, unsigned long flags, int follow_symlinks) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2955 | /*[clinic end generated code: output=9e5f9417afc20c4b input=0327e29feb876236]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2956 | { |
| 2957 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2958 | |
| 2959 | #ifndef HAVE_LCHFLAGS |
| 2960 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2961 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2962 | #endif |
| 2963 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2964 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2965 | #ifdef HAVE_LCHFLAGS |
| 2966 | if (!follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2967 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2968 | else |
| 2969 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2970 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2971 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2972 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2973 | if (result) |
| 2974 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2975 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2976 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2977 | } |
| 2978 | #endif /* HAVE_CHFLAGS */ |
| 2979 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2980 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2981 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2982 | /*[clinic input] |
| 2983 | os.lchflags |
| 2984 | |
| 2985 | path: path_t |
| 2986 | flags: unsigned_long(bitwise=True) |
| 2987 | |
| 2988 | Set file flags. |
| 2989 | |
| 2990 | This function will not follow symbolic links. |
| 2991 | Equivalent to chflags(path, flags, follow_symlinks=False). |
| 2992 | [clinic start generated code]*/ |
| 2993 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2994 | static PyObject * |
| 2995 | os_lchflags_impl(PyModuleDef *module, path_t *path, unsigned long flags) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2996 | /*[clinic end generated code: output=6741322fb949661b input=f9f82ea8b585ca9d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2997 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2998 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2999 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3000 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3001 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3002 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3003 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3004 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3005 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3006 | } |
| 3007 | #endif /* HAVE_LCHFLAGS */ |
| 3008 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3009 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3010 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3011 | /*[clinic input] |
| 3012 | os.chroot |
| 3013 | path: path_t |
| 3014 | |
| 3015 | Change root directory to path. |
| 3016 | |
| 3017 | [clinic start generated code]*/ |
| 3018 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3019 | static PyObject * |
| 3020 | os_chroot_impl(PyModuleDef *module, path_t *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3021 | /*[clinic end generated code: output=b6dbfabe74ecaa9d input=14822965652c3dc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3022 | { |
| 3023 | int res; |
| 3024 | Py_BEGIN_ALLOW_THREADS |
| 3025 | res = chroot(path->narrow); |
| 3026 | Py_END_ALLOW_THREADS |
| 3027 | if (res < 0) |
| 3028 | return path_error(path); |
| 3029 | Py_RETURN_NONE; |
| 3030 | } |
| 3031 | #endif /* HAVE_CHROOT */ |
| 3032 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3033 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3034 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3035 | /*[clinic input] |
| 3036 | os.fsync |
| 3037 | |
| 3038 | fd: fildes |
| 3039 | |
| 3040 | Force write of fd to disk. |
| 3041 | [clinic start generated code]*/ |
| 3042 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3043 | static PyObject * |
| 3044 | os_fsync_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3045 | /*[clinic end generated code: output=83a350851064aea7 input=21c3645c056967f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3046 | { |
| 3047 | return posix_fildes_fd(fd, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3048 | } |
| 3049 | #endif /* HAVE_FSYNC */ |
| 3050 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3051 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3052 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3053 | /*[clinic input] |
| 3054 | os.sync |
| 3055 | |
| 3056 | Force write of everything to disk. |
| 3057 | [clinic start generated code]*/ |
| 3058 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3059 | static PyObject * |
| 3060 | os_sync_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3061 | /*[clinic end generated code: output=ba524f656c201c40 input=84749fe5e9b404ff]*/ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3062 | { |
| 3063 | Py_BEGIN_ALLOW_THREADS |
| 3064 | sync(); |
| 3065 | Py_END_ALLOW_THREADS |
| 3066 | Py_RETURN_NONE; |
| 3067 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3068 | #endif /* HAVE_SYNC */ |
| 3069 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3070 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3071 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3072 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3073 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3074 | #endif |
| 3075 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3076 | /*[clinic input] |
| 3077 | os.fdatasync |
| 3078 | |
| 3079 | fd: fildes |
| 3080 | |
| 3081 | Force write of fd to disk without forcing update of metadata. |
| 3082 | [clinic start generated code]*/ |
| 3083 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3084 | static PyObject * |
| 3085 | os_fdatasync_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3086 | /*[clinic end generated code: output=e0f04a3aff515b75 input=bc74791ee54dd291]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3087 | { |
| 3088 | return posix_fildes_fd(fd, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3089 | } |
| 3090 | #endif /* HAVE_FDATASYNC */ |
| 3091 | |
| 3092 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3093 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3094 | /*[clinic input] |
| 3095 | os.chown |
| 3096 | |
| 3097 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') |
| 3098 | Path to be examined; can be string, bytes, or open-file-descriptor int. |
| 3099 | |
| 3100 | uid: uid_t |
| 3101 | |
| 3102 | gid: gid_t |
| 3103 | |
| 3104 | * |
| 3105 | |
| 3106 | dir_fd : dir_fd(requires='fchownat') = None |
| 3107 | If not None, it should be a file descriptor open to a directory, |
| 3108 | and path should be relative; path will then be relative to that |
| 3109 | directory. |
| 3110 | |
| 3111 | follow_symlinks: bool = True |
| 3112 | If False, and the last element of the path is a symbolic link, |
| 3113 | stat will examine the symbolic link itself instead of the file |
| 3114 | the link points to. |
| 3115 | |
| 3116 | Change the owner and group id of path to the numeric uid and gid.\ |
| 3117 | |
| 3118 | path may always be specified as a string. |
| 3119 | On some platforms, path may also be specified as an open file descriptor. |
| 3120 | If this functionality is unavailable, using it raises an exception. |
| 3121 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3122 | and path should be relative; path will then be relative to that directory. |
| 3123 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3124 | link, chown will modify the symbolic link itself instead of the file the |
| 3125 | link points to. |
| 3126 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3127 | an open file descriptor. |
| 3128 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3129 | If they are unavailable, using them will raise a NotImplementedError. |
| 3130 | |
| 3131 | [clinic start generated code]*/ |
| 3132 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3133 | static PyObject * |
| 3134 | os_chown_impl(PyModuleDef *module, path_t *path, uid_t uid, gid_t gid, int dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3135 | /*[clinic end generated code: output=59a8db91897fb46c input=a61cc35574814d5d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3136 | { |
| 3137 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3138 | |
| 3139 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3140 | if (follow_symlinks_specified("chown", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3141 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3142 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3143 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
| 3144 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) |
| 3145 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3146 | |
| 3147 | #ifdef __APPLE__ |
| 3148 | /* |
| 3149 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3150 | * (But we still have an lchown symbol because of weak-linking.) |
| 3151 | * It doesn't have fchownat either. So there's no possibility |
| 3152 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3153 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3154 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3155 | follow_symlinks_specified("chown", follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3156 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3157 | } |
| 3158 | #endif |
| 3159 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3160 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3161 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3162 | if (path->fd != -1) |
| 3163 | result = fchown(path->fd, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3164 | else |
| 3165 | #endif |
| 3166 | #ifdef HAVE_LCHOWN |
| 3167 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3168 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3169 | else |
| 3170 | #endif |
| 3171 | #ifdef HAVE_FCHOWNAT |
| 3172 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3173 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3174 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3175 | else |
| 3176 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3177 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3178 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3179 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3180 | if (result) |
| 3181 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3182 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3183 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3184 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3185 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3186 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3187 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3188 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3189 | /*[clinic input] |
| 3190 | os.fchown |
| 3191 | |
| 3192 | fd: int |
| 3193 | uid: uid_t |
| 3194 | gid: gid_t |
| 3195 | |
| 3196 | Change the owner and group id of the file specified by file descriptor. |
| 3197 | |
| 3198 | Equivalent to os.chown(fd, uid, gid). |
| 3199 | |
| 3200 | [clinic start generated code]*/ |
| 3201 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3202 | static PyObject * |
| 3203 | os_fchown_impl(PyModuleDef *module, int fd, uid_t uid, gid_t gid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3204 | /*[clinic end generated code: output=7545abf8f6086d76 input=3af544ba1b13a0d7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3205 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3206 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3207 | int async_err = 0; |
| 3208 | |
| 3209 | do { |
| 3210 | Py_BEGIN_ALLOW_THREADS |
| 3211 | res = fchown(fd, uid, gid); |
| 3212 | Py_END_ALLOW_THREADS |
| 3213 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3214 | if (res != 0) |
| 3215 | return (!async_err) ? posix_error() : NULL; |
| 3216 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3217 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3218 | } |
| 3219 | #endif /* HAVE_FCHOWN */ |
| 3220 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3221 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3222 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3223 | /*[clinic input] |
| 3224 | os.lchown |
| 3225 | |
| 3226 | path : path_t |
| 3227 | uid: uid_t |
| 3228 | gid: gid_t |
| 3229 | |
| 3230 | Change the owner and group id of path to the numeric uid and gid. |
| 3231 | |
| 3232 | This function will not follow symbolic links. |
| 3233 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). |
| 3234 | [clinic start generated code]*/ |
| 3235 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3236 | static PyObject * |
| 3237 | os_lchown_impl(PyModuleDef *module, path_t *path, uid_t uid, gid_t gid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3238 | /*[clinic end generated code: output=bb0d2da1579ac275 input=b1c6014d563a7161]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3239 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3240 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3241 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3242 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3243 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3244 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3245 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3246 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3247 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3248 | } |
| 3249 | #endif /* HAVE_LCHOWN */ |
| 3250 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3251 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3252 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3253 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3254 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3255 | char buf[1026]; |
| 3256 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3257 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3258 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3259 | if (!use_bytes) { |
| 3260 | wchar_t wbuf[1026]; |
| 3261 | wchar_t *wbuf2 = wbuf; |
| 3262 | PyObject *resobj; |
| 3263 | DWORD len; |
| 3264 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3265 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3266 | /* If the buffer is large enough, len does not include the |
| 3267 | terminating \0. If the buffer is too small, len includes |
| 3268 | the space needed for the terminator. */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3269 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3270 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3271 | if (wbuf2) |
| 3272 | len = GetCurrentDirectoryW(len, wbuf2); |
| 3273 | } |
| 3274 | Py_END_ALLOW_THREADS |
| 3275 | if (!wbuf2) { |
| 3276 | PyErr_NoMemory(); |
| 3277 | return NULL; |
| 3278 | } |
| 3279 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3280 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3281 | PyMem_RawFree(wbuf2); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3282 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3283 | } |
| 3284 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3285 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3286 | PyMem_RawFree(wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3287 | return resobj; |
| 3288 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3289 | |
| 3290 | if (win32_warn_bytes_api()) |
| 3291 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3292 | #endif |
| 3293 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3294 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3295 | res = getcwd(buf, sizeof buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3296 | Py_END_ALLOW_THREADS |
| 3297 | if (res == NULL) |
| 3298 | return posix_error(); |
| 3299 | if (use_bytes) |
| 3300 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 3301 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3302 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3303 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3304 | |
| 3305 | /*[clinic input] |
| 3306 | os.getcwd |
| 3307 | |
| 3308 | Return a unicode string representing the current working directory. |
| 3309 | [clinic start generated code]*/ |
| 3310 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3311 | static PyObject * |
| 3312 | os_getcwd_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3313 | /*[clinic end generated code: output=efe3a8c0121525ea input=f069211bb70e3d39]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3314 | { |
| 3315 | return posix_getcwd(0); |
| 3316 | } |
| 3317 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3318 | |
| 3319 | /*[clinic input] |
| 3320 | os.getcwdb |
| 3321 | |
| 3322 | Return a bytes string representing the current working directory. |
| 3323 | [clinic start generated code]*/ |
| 3324 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3325 | static PyObject * |
| 3326 | os_getcwdb_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3327 | /*[clinic end generated code: output=7fce42ee4b2a296a input=f6f6a378dad3d9cb]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3328 | { |
| 3329 | return posix_getcwd(1); |
| 3330 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3331 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3332 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3333 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3334 | #define HAVE_LINK 1 |
| 3335 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3336 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3337 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3338 | /*[clinic input] |
| 3339 | |
| 3340 | os.link |
| 3341 | |
| 3342 | src : path_t |
| 3343 | dst : path_t |
| 3344 | * |
| 3345 | src_dir_fd : dir_fd = None |
| 3346 | dst_dir_fd : dir_fd = None |
| 3347 | follow_symlinks: bool = True |
| 3348 | |
| 3349 | Create a hard link to a file. |
| 3350 | |
| 3351 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 3352 | descriptor open to a directory, and the respective path string (src or dst) |
| 3353 | should be relative; the path will then be relative to that directory. |
| 3354 | If follow_symlinks is False, and the last element of src is a symbolic |
| 3355 | link, link will create a link to the symbolic link itself instead of the |
| 3356 | file the link points to. |
| 3357 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 3358 | platform. If they are unavailable, using them will raise a |
| 3359 | NotImplementedError. |
| 3360 | [clinic start generated code]*/ |
| 3361 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3362 | static PyObject * |
| 3363 | os_link_impl(PyModuleDef *module, path_t *src, path_t *dst, int src_dir_fd, int dst_dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3364 | /*[clinic end generated code: output=c0a9ded8111d2a79 input=b0095ebbcbaa7e04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3365 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3366 | #ifdef MS_WINDOWS |
| 3367 | BOOL result; |
| 3368 | #else |
| 3369 | int result; |
| 3370 | #endif |
| 3371 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3372 | #ifndef HAVE_LINKAT |
| 3373 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3374 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3375 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3376 | } |
| 3377 | #endif |
| 3378 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3379 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3380 | PyErr_SetString(PyExc_NotImplementedError, |
| 3381 | "link: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3382 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3383 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3384 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3385 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3386 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3387 | if (src->wide) |
| 3388 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3389 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3390 | result = CreateHardLinkA(dst->narrow, src->narrow, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3391 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3392 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3393 | if (!result) |
| 3394 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3395 | #else |
| 3396 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3397 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3398 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3399 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3400 | (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3401 | result = linkat(src_dir_fd, src->narrow, |
| 3402 | dst_dir_fd, dst->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3403 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3404 | else |
| 3405 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3406 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3407 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3408 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3409 | if (result) |
| 3410 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3411 | #endif |
| 3412 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3413 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3414 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3415 | #endif |
| 3416 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3417 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3418 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3419 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3420 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3421 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3422 | PyObject *v; |
| 3423 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3424 | BOOL result; |
| 3425 | WIN32_FIND_DATA FileData; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3426 | char namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3427 | char *bufptr = namebuf; |
| 3428 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3429 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3430 | PyObject *po = NULL; |
| 3431 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3432 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3433 | if (!path->narrow) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3434 | WIN32_FIND_DATAW wFileData; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3435 | wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3436 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3437 | if (!path->wide) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3438 | po_wchars = L"."; |
| 3439 | len = 1; |
| 3440 | } else { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3441 | po_wchars = path->wide; |
| 3442 | len = wcslen(path->wide); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3443 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3444 | /* The +5 is so we can append "\\*.*\0" */ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3445 | wnamebuf = PyMem_New(wchar_t, len + 5); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3446 | if (!wnamebuf) { |
| 3447 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3448 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3449 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3450 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3451 | if (len > 0) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3452 | wchar_t wch = wnamebuf[len-1]; |
Tim Golden | 781bbeb | 2013-10-25 20:24:06 +0100 | [diff] [blame] | 3453 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3454 | wnamebuf[len++] = SEP; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3455 | wcscpy(wnamebuf + len, L"*.*"); |
| 3456 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3457 | if ((list = PyList_New(0)) == NULL) { |
| 3458 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3459 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3460 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3461 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3462 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3463 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3464 | int error = GetLastError(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3465 | if (error == ERROR_FILE_NOT_FOUND) |
| 3466 | goto exit; |
| 3467 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3468 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3469 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3470 | } |
| 3471 | do { |
| 3472 | /* Skip over . and .. */ |
| 3473 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3474 | wcscmp(wFileData.cFileName, L"..") != 0) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3475 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3476 | wcslen(wFileData.cFileName)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3477 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3478 | Py_DECREF(list); |
| 3479 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3480 | break; |
| 3481 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3482 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3483 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3484 | Py_DECREF(list); |
| 3485 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3486 | break; |
| 3487 | } |
| 3488 | Py_DECREF(v); |
| 3489 | } |
| 3490 | Py_BEGIN_ALLOW_THREADS |
| 3491 | result = FindNextFileW(hFindFile, &wFileData); |
| 3492 | Py_END_ALLOW_THREADS |
| 3493 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3494 | it got to the end of the directory. */ |
| 3495 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3496 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3497 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3498 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3499 | } |
| 3500 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3501 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3502 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3503 | } |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3504 | strcpy(namebuf, path->narrow); |
| 3505 | len = path->length; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3506 | if (len > 0) { |
| 3507 | char ch = namebuf[len-1]; |
Tim Golden | 781bbeb | 2013-10-25 20:24:06 +0100 | [diff] [blame] | 3508 | if (ch != '\\' && ch != '/' && ch != ':') |
| 3509 | namebuf[len++] = '\\'; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3510 | strcpy(namebuf + len, "*.*"); |
| 3511 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3512 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3513 | if ((list = PyList_New(0)) == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3514 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3515 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3516 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3517 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3518 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3519 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3520 | int error = GetLastError(); |
| 3521 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3522 | goto exit; |
| 3523 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3524 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3525 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3526 | } |
| 3527 | do { |
| 3528 | /* Skip over . and .. */ |
| 3529 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 3530 | strcmp(FileData.cFileName, "..") != 0) { |
| 3531 | v = PyBytes_FromString(FileData.cFileName); |
| 3532 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3533 | Py_DECREF(list); |
| 3534 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3535 | break; |
| 3536 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3537 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3538 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3539 | Py_DECREF(list); |
| 3540 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3541 | break; |
| 3542 | } |
| 3543 | Py_DECREF(v); |
| 3544 | } |
| 3545 | Py_BEGIN_ALLOW_THREADS |
| 3546 | result = FindNextFile(hFindFile, &FileData); |
| 3547 | Py_END_ALLOW_THREADS |
| 3548 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3549 | it got to the end of the directory. */ |
| 3550 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3551 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3552 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3553 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3554 | } |
| 3555 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3556 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3557 | exit: |
| 3558 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3559 | if (FindClose(hFindFile) == FALSE) { |
| 3560 | if (list != NULL) { |
| 3561 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3562 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3563 | } |
| 3564 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3565 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3566 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3567 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3568 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3569 | } /* end of _listdir_windows_no_opendir */ |
| 3570 | |
| 3571 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3572 | |
| 3573 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3574 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3575 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3576 | PyObject *v; |
| 3577 | DIR *dirp = NULL; |
| 3578 | struct dirent *ep; |
| 3579 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3580 | #ifdef HAVE_FDOPENDIR |
| 3581 | int fd = -1; |
| 3582 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3583 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3584 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3585 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3586 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3587 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3588 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3589 | if (fd == -1) |
| 3590 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3591 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3592 | return_str = 1; |
| 3593 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3594 | Py_BEGIN_ALLOW_THREADS |
| 3595 | dirp = fdopendir(fd); |
| 3596 | Py_END_ALLOW_THREADS |
| 3597 | } |
| 3598 | else |
| 3599 | #endif |
| 3600 | { |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3601 | char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3602 | if (path->narrow) { |
| 3603 | name = path->narrow; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3604 | /* only return bytes if they specified a bytes object */ |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3605 | return_str = !(PyBytes_Check(path->object)); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3606 | } |
| 3607 | else { |
| 3608 | name = "."; |
| 3609 | return_str = 1; |
| 3610 | } |
| 3611 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3612 | Py_BEGIN_ALLOW_THREADS |
| 3613 | dirp = opendir(name); |
| 3614 | Py_END_ALLOW_THREADS |
| 3615 | } |
| 3616 | |
| 3617 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3618 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3619 | #ifdef HAVE_FDOPENDIR |
| 3620 | if (fd != -1) { |
| 3621 | Py_BEGIN_ALLOW_THREADS |
| 3622 | close(fd); |
| 3623 | Py_END_ALLOW_THREADS |
| 3624 | } |
| 3625 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3626 | goto exit; |
| 3627 | } |
| 3628 | if ((list = PyList_New(0)) == NULL) { |
| 3629 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3630 | } |
| 3631 | for (;;) { |
| 3632 | errno = 0; |
| 3633 | Py_BEGIN_ALLOW_THREADS |
| 3634 | ep = readdir(dirp); |
| 3635 | Py_END_ALLOW_THREADS |
| 3636 | if (ep == NULL) { |
| 3637 | if (errno == 0) { |
| 3638 | break; |
| 3639 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3640 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3641 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3642 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3643 | } |
| 3644 | } |
| 3645 | if (ep->d_name[0] == '.' && |
| 3646 | (NAMLEN(ep) == 1 || |
| 3647 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3648 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3649 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3650 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3651 | else |
| 3652 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3653 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3654 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3655 | break; |
| 3656 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3657 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3658 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3659 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3660 | break; |
| 3661 | } |
| 3662 | Py_DECREF(v); |
| 3663 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3664 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3665 | exit: |
| 3666 | if (dirp != NULL) { |
| 3667 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3668 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3669 | if (fd > -1) |
| 3670 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3671 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3672 | closedir(dirp); |
| 3673 | Py_END_ALLOW_THREADS |
| 3674 | } |
| 3675 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3676 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3677 | } /* end of _posix_listdir */ |
| 3678 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3679 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3680 | |
| 3681 | /*[clinic input] |
| 3682 | os.listdir |
| 3683 | |
| 3684 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
| 3685 | |
| 3686 | Return a list containing the names of the files in the directory. |
| 3687 | |
| 3688 | path can be specified as either str or bytes. If path is bytes, |
| 3689 | the filenames returned will also be bytes; in all other circumstances |
| 3690 | the filenames returned will be str. |
| 3691 | If path is None, uses the path='.'. |
| 3692 | On some platforms, path may also be specified as an open file descriptor;\ |
| 3693 | the file descriptor must refer to a directory. |
| 3694 | If this functionality is unavailable, using it raises NotImplementedError. |
| 3695 | |
| 3696 | The list is in arbitrary order. It does not include the special |
| 3697 | entries '.' and '..' even if they are present in the directory. |
| 3698 | |
| 3699 | |
| 3700 | [clinic start generated code]*/ |
| 3701 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3702 | static PyObject * |
| 3703 | os_listdir_impl(PyModuleDef *module, path_t *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3704 | /*[clinic end generated code: output=1fbe67c1f780c8b7 input=09e300416e3cd729]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3705 | { |
| 3706 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3707 | return _listdir_windows_no_opendir(path, NULL); |
| 3708 | #else |
| 3709 | return _posix_listdir(path, NULL); |
| 3710 | #endif |
| 3711 | } |
| 3712 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3713 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3714 | /* A helper function for abspath on win32 */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3715 | /* AC 3.5: probably just convert to using path converter */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3716 | static PyObject * |
| 3717 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 3718 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3719 | const char *path; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3720 | char outbuf[MAX_PATH]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3721 | char *temp; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3722 | PyObject *po; |
| 3723 | |
| 3724 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) |
| 3725 | { |
| 3726 | wchar_t *wpath; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3727 | wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3728 | wchar_t *wtemp; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3729 | DWORD result; |
| 3730 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3731 | |
| 3732 | wpath = PyUnicode_AsUnicode(po); |
| 3733 | if (wpath == NULL) |
| 3734 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3735 | result = GetFullPathNameW(wpath, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3736 | Py_ARRAY_LENGTH(woutbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3737 | woutbuf, &wtemp); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3738 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3739 | woutbufp = PyMem_New(wchar_t, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3740 | if (!woutbufp) |
| 3741 | return PyErr_NoMemory(); |
| 3742 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 3743 | } |
| 3744 | if (result) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3745 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3746 | else |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3747 | v = win32_error_object("GetFullPathNameW", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3748 | if (woutbufp != woutbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3749 | PyMem_Free(woutbufp); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3750 | return v; |
| 3751 | } |
| 3752 | /* Drop the argument parsing error as narrow strings |
| 3753 | are also valid. */ |
| 3754 | PyErr_Clear(); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3755 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3756 | if (!PyArg_ParseTuple (args, "y:_getfullpathname", |
| 3757 | &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3758 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3759 | if (win32_warn_bytes_api()) |
| 3760 | return NULL; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3761 | if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3762 | outbuf, &temp)) { |
| 3763 | win32_error("GetFullPathName", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3764 | return NULL; |
| 3765 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3766 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 3767 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 3768 | Py_FileSystemDefaultEncoding, NULL); |
| 3769 | } |
| 3770 | return PyBytes_FromString(outbuf); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3771 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3772 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3773 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3774 | /*[clinic input] |
| 3775 | os._getfinalpathname |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3776 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3777 | path: unicode |
| 3778 | / |
| 3779 | |
| 3780 | A helper function for samepath on windows. |
| 3781 | [clinic start generated code]*/ |
| 3782 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3783 | static PyObject * |
| 3784 | os__getfinalpathname_impl(PyModuleDef *module, PyObject *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3785 | /*[clinic end generated code: output=8be81a5f51a34bcf input=71d5e89334891bf4]*/ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3786 | { |
| 3787 | HANDLE hFile; |
| 3788 | int buf_size; |
| 3789 | wchar_t *target_path; |
| 3790 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3791 | PyObject *result; |
| 3792 | wchar_t *path_wchar; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3793 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3794 | path_wchar = PyUnicode_AsUnicode(path); |
| 3795 | if (path_wchar == NULL) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3796 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3797 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3798 | hFile = CreateFileW( |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3799 | path_wchar, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3800 | 0, /* desired access */ |
| 3801 | 0, /* share mode */ |
| 3802 | NULL, /* security attributes */ |
| 3803 | OPEN_EXISTING, |
| 3804 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3805 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3806 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3807 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3808 | if(hFile == INVALID_HANDLE_VALUE) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3809 | return win32_error_object("CreateFileW", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3810 | |
| 3811 | /* We have a good handle to the target, use it to determine the |
| 3812 | target path name. */ |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 3813 | buf_size = GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3814 | |
| 3815 | if(!buf_size) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3816 | return win32_error_object("GetFinalPathNameByHandle", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3817 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3818 | target_path = PyMem_New(wchar_t, buf_size+1); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3819 | if(!target_path) |
| 3820 | return PyErr_NoMemory(); |
| 3821 | |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 3822 | result_length = GetFinalPathNameByHandleW(hFile, target_path, |
| 3823 | buf_size, VOLUME_NAME_DOS); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3824 | if(!result_length) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3825 | return win32_error_object("GetFinalPathNamyByHandle", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3826 | |
| 3827 | if(!CloseHandle(hFile)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3828 | return win32_error_object("CloseHandle", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3829 | |
| 3830 | target_path[result_length] = 0; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3831 | result = PyUnicode_FromWideChar(target_path, result_length); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3832 | PyMem_Free(target_path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3833 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3834 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3835 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3836 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3837 | "Return true if the pathname refers to an existing directory."); |
| 3838 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3839 | /* AC 3.5: convert using path converter */ |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3840 | static PyObject * |
| 3841 | posix__isdir(PyObject *self, PyObject *args) |
| 3842 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3843 | const char *path; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3844 | PyObject *po; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3845 | DWORD attributes; |
| 3846 | |
| 3847 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3848 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 3849 | if (wpath == NULL) |
| 3850 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3851 | |
| 3852 | attributes = GetFileAttributesW(wpath); |
| 3853 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3854 | Py_RETURN_FALSE; |
| 3855 | goto check; |
| 3856 | } |
| 3857 | /* Drop the argument parsing error as narrow strings |
| 3858 | are also valid. */ |
| 3859 | PyErr_Clear(); |
| 3860 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3861 | if (!PyArg_ParseTuple(args, "y:_isdir", &path)) |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3862 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3863 | if (win32_warn_bytes_api()) |
| 3864 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3865 | attributes = GetFileAttributesA(path); |
| 3866 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3867 | Py_RETURN_FALSE; |
| 3868 | |
| 3869 | check: |
| 3870 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3871 | Py_RETURN_TRUE; |
| 3872 | else |
| 3873 | Py_RETURN_FALSE; |
| 3874 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3875 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3876 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3877 | /*[clinic input] |
| 3878 | os._getvolumepathname |
| 3879 | |
| 3880 | path: unicode |
| 3881 | |
| 3882 | A helper function for ismount on Win32. |
| 3883 | [clinic start generated code]*/ |
| 3884 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3885 | static PyObject * |
| 3886 | os__getvolumepathname_impl(PyModuleDef *module, PyObject *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3887 | /*[clinic end generated code: output=79a0ba729f956dbe input=7eacadc40acbda6b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3888 | { |
| 3889 | PyObject *result; |
| 3890 | wchar_t *path_wchar, *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3891 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3892 | BOOL ret; |
| 3893 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3894 | path_wchar = PyUnicode_AsUnicodeAndSize(path, &buflen); |
| 3895 | if (path_wchar == NULL) |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3896 | return NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3897 | buflen += 1; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3898 | |
| 3899 | /* Volume path should be shorter than entire path */ |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3900 | buflen = Py_MAX(buflen, MAX_PATH); |
| 3901 | |
| 3902 | if (buflen > DWORD_MAX) { |
| 3903 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 3904 | return NULL; |
| 3905 | } |
| 3906 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3907 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3908 | if (mountpath == NULL) |
| 3909 | return PyErr_NoMemory(); |
| 3910 | |
| 3911 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3912 | ret = GetVolumePathNameW(path_wchar, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 3913 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3914 | Py_END_ALLOW_THREADS |
| 3915 | |
| 3916 | if (!ret) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3917 | result = win32_error_object("_getvolumepathname", path); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3918 | goto exit; |
| 3919 | } |
| 3920 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
| 3921 | |
| 3922 | exit: |
| 3923 | PyMem_Free(mountpath); |
| 3924 | return result; |
| 3925 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 3926 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3927 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3928 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3929 | |
| 3930 | /*[clinic input] |
| 3931 | os.mkdir |
| 3932 | |
| 3933 | path : path_t |
| 3934 | |
| 3935 | mode: int = 0o777 |
| 3936 | |
| 3937 | * |
| 3938 | |
| 3939 | dir_fd : dir_fd(requires='mkdirat') = None |
| 3940 | |
| 3941 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 3942 | |
| 3943 | Create a directory. |
| 3944 | |
| 3945 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3946 | and path should be relative; path will then be relative to that directory. |
| 3947 | dir_fd may not be implemented on your platform. |
| 3948 | If it is unavailable, using it will raise a NotImplementedError. |
| 3949 | |
| 3950 | The mode argument is ignored on Windows. |
| 3951 | [clinic start generated code]*/ |
| 3952 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3953 | static PyObject * |
| 3954 | os_mkdir_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 3955 | /*[clinic end generated code: output=8bf1f738873ef2c5 input=e965f68377e9b1ce]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3956 | { |
| 3957 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3958 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3959 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3960 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3961 | if (path->wide) |
| 3962 | result = CreateDirectoryW(path->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3963 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3964 | result = CreateDirectoryA(path->narrow, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3965 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3966 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3967 | if (!result) |
| 3968 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3969 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3970 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3971 | #if HAVE_MKDIRAT |
| 3972 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3973 | result = mkdirat(dir_fd, path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3974 | else |
| 3975 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3976 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3977 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3978 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3979 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3980 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3981 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3982 | if (result < 0) |
| 3983 | return path_error(path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3984 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3985 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3986 | } |
| 3987 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3988 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3989 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3990 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3991 | #include <sys/resource.h> |
| 3992 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3993 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3994 | |
| 3995 | #ifdef HAVE_NICE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3996 | /*[clinic input] |
| 3997 | os.nice |
| 3998 | |
| 3999 | increment: int |
| 4000 | / |
| 4001 | |
| 4002 | Add increment to the priority of process and return the new priority. |
| 4003 | [clinic start generated code]*/ |
| 4004 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4005 | static PyObject * |
| 4006 | os_nice_impl(PyModuleDef *module, int increment) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4007 | /*[clinic end generated code: output=8870418a3fc07b51 input=864be2d402a21da2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4008 | { |
| 4009 | int value; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4010 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4011 | /* There are two flavours of 'nice': one that returns the new |
| 4012 | priority (as required by almost all standards out there) and the |
| 4013 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 4014 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4015 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4016 | If we are of the nice family that returns the new priority, we |
| 4017 | need to clear errno before the call, and check if errno is filled |
| 4018 | before calling posix_error() on a returnvalue of -1, because the |
| 4019 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4020 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4021 | errno = 0; |
| 4022 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4023 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4024 | if (value == 0) |
| 4025 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4026 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4027 | if (value == -1 && errno != 0) |
| 4028 | /* either nice() or getpriority() returned an error */ |
| 4029 | return posix_error(); |
| 4030 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4031 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4032 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4033 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4034 | |
| 4035 | #ifdef HAVE_GETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4036 | /*[clinic input] |
| 4037 | os.getpriority |
| 4038 | |
| 4039 | which: int |
| 4040 | who: int |
| 4041 | |
| 4042 | Return program scheduling priority. |
| 4043 | [clinic start generated code]*/ |
| 4044 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4045 | static PyObject * |
| 4046 | os_getpriority_impl(PyModuleDef *module, int which, int who) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4047 | /*[clinic end generated code: output=4759937aa5b67ed6 input=9be615d40e2544ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4048 | { |
| 4049 | int retval; |
| 4050 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4051 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4052 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4053 | if (errno != 0) |
| 4054 | return posix_error(); |
| 4055 | return PyLong_FromLong((long)retval); |
| 4056 | } |
| 4057 | #endif /* HAVE_GETPRIORITY */ |
| 4058 | |
| 4059 | |
| 4060 | #ifdef HAVE_SETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4061 | /*[clinic input] |
| 4062 | os.setpriority |
| 4063 | |
| 4064 | which: int |
| 4065 | who: int |
| 4066 | priority: int |
| 4067 | |
| 4068 | Set program scheduling priority. |
| 4069 | [clinic start generated code]*/ |
| 4070 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4071 | static PyObject * |
| 4072 | os_setpriority_impl(PyModuleDef *module, int which, int who, int priority) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4073 | /*[clinic end generated code: output=6497d3301547e7d5 input=710ccbf65b9dc513]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4074 | { |
| 4075 | int retval; |
| 4076 | |
| 4077 | retval = setpriority(which, who, priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4078 | if (retval == -1) |
| 4079 | return posix_error(); |
| 4080 | Py_RETURN_NONE; |
| 4081 | } |
| 4082 | #endif /* HAVE_SETPRIORITY */ |
| 4083 | |
| 4084 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4085 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4086 | internal_rename(path_t *src, path_t *dst, int src_dir_fd, int dst_dir_fd, int is_replace) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4087 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4088 | char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4089 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4090 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4091 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4092 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4093 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4094 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4095 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4096 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4097 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4098 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4099 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4100 | #ifndef HAVE_RENAMEAT |
| 4101 | if (dir_fd_specified) { |
| 4102 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4103 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4104 | } |
| 4105 | #endif |
| 4106 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4107 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4108 | PyErr_Format(PyExc_ValueError, |
| 4109 | "%s: src and dst must be the same type", function_name); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4110 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4111 | } |
| 4112 | |
| 4113 | #ifdef MS_WINDOWS |
| 4114 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4115 | if (src->wide) |
| 4116 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4117 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4118 | result = MoveFileExA(src->narrow, dst->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4119 | Py_END_ALLOW_THREADS |
| 4120 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4121 | if (!result) |
| 4122 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4123 | |
| 4124 | #else |
| 4125 | Py_BEGIN_ALLOW_THREADS |
| 4126 | #ifdef HAVE_RENAMEAT |
| 4127 | if (dir_fd_specified) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4128 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4129 | else |
| 4130 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4131 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4132 | Py_END_ALLOW_THREADS |
| 4133 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4134 | if (result) |
| 4135 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4136 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4137 | Py_RETURN_NONE; |
| 4138 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4139 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4140 | |
| 4141 | /*[clinic input] |
| 4142 | os.rename |
| 4143 | |
| 4144 | src : path_t |
| 4145 | dst : path_t |
| 4146 | * |
| 4147 | src_dir_fd : dir_fd = None |
| 4148 | dst_dir_fd : dir_fd = None |
| 4149 | |
| 4150 | Rename a file or directory. |
| 4151 | |
| 4152 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4153 | descriptor open to a directory, and the respective path string (src or dst) |
| 4154 | should be relative; the path will then be relative to that directory. |
| 4155 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4156 | If they are unavailable, using them will raise a NotImplementedError. |
| 4157 | [clinic start generated code]*/ |
| 4158 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4159 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4160 | os_rename_impl(PyModuleDef *module, path_t *src, path_t *dst, int src_dir_fd, int dst_dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4161 | /*[clinic end generated code: output=1bb520bf2fad186d input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4162 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4163 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4164 | } |
| 4165 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4166 | |
| 4167 | /*[clinic input] |
| 4168 | os.replace = os.rename |
| 4169 | |
| 4170 | Rename a file or directory, overwriting the destination. |
| 4171 | |
| 4172 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4173 | descriptor open to a directory, and the respective path string (src or dst) |
| 4174 | should be relative; the path will then be relative to that directory. |
| 4175 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4176 | If they are unavailable, using them will raise a NotImplementedError." |
| 4177 | [clinic start generated code]*/ |
| 4178 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4179 | static PyObject * |
| 4180 | os_replace_impl(PyModuleDef *module, path_t *src, path_t *dst, int src_dir_fd, int dst_dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4181 | /*[clinic end generated code: output=aa9ddad55fdef8e3 input=25515dfb107c8421]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4182 | { |
| 4183 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); |
| 4184 | } |
| 4185 | |
| 4186 | |
| 4187 | /*[clinic input] |
| 4188 | os.rmdir |
| 4189 | |
| 4190 | path: path_t |
| 4191 | * |
| 4192 | dir_fd: dir_fd(requires='unlinkat') = None |
| 4193 | |
| 4194 | Remove a directory. |
| 4195 | |
| 4196 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4197 | and path should be relative; path will then be relative to that directory. |
| 4198 | dir_fd may not be implemented on your platform. |
| 4199 | If it is unavailable, using it will raise a NotImplementedError. |
| 4200 | [clinic start generated code]*/ |
| 4201 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4202 | static PyObject * |
| 4203 | os_rmdir_impl(PyModuleDef *module, path_t *path, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4204 | /*[clinic end generated code: output=cabadec80d5a77c7 input=38c8b375ca34a7e2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4205 | { |
| 4206 | int result; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4207 | |
| 4208 | Py_BEGIN_ALLOW_THREADS |
| 4209 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4210 | if (path->wide) |
| 4211 | result = RemoveDirectoryW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4212 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4213 | result = RemoveDirectoryA(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4214 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4215 | #else |
| 4216 | #ifdef HAVE_UNLINKAT |
| 4217 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4218 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4219 | else |
| 4220 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4221 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4222 | #endif |
| 4223 | Py_END_ALLOW_THREADS |
| 4224 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4225 | if (result) |
| 4226 | return path_error(path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4227 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4228 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4229 | } |
| 4230 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4231 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4232 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4233 | #ifdef MS_WINDOWS |
| 4234 | /*[clinic input] |
| 4235 | os.system -> long |
| 4236 | |
| 4237 | command: Py_UNICODE |
| 4238 | |
| 4239 | Execute the command in a subshell. |
| 4240 | [clinic start generated code]*/ |
| 4241 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4242 | static long |
| 4243 | os_system_impl(PyModuleDef *module, Py_UNICODE *command) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4244 | /*[clinic end generated code: output=4c3bd5abcd9c29e7 input=303f5ce97df606b0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4245 | { |
| 4246 | long result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4247 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4248 | result = _wsystem(command); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4249 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4250 | return result; |
| 4251 | } |
| 4252 | #else /* MS_WINDOWS */ |
| 4253 | /*[clinic input] |
| 4254 | os.system -> long |
| 4255 | |
| 4256 | command: FSConverter |
| 4257 | |
| 4258 | Execute the command in a subshell. |
| 4259 | [clinic start generated code]*/ |
| 4260 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4261 | static long |
| 4262 | os_system_impl(PyModuleDef *module, PyObject *command) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4263 | /*[clinic end generated code: output=800f775e10b7be55 input=86a58554ba6094af]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4264 | { |
| 4265 | long result; |
| 4266 | char *bytes = PyBytes_AsString(command); |
| 4267 | Py_BEGIN_ALLOW_THREADS |
| 4268 | result = system(bytes); |
| 4269 | Py_END_ALLOW_THREADS |
| 4270 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4271 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4272 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4273 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4274 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4275 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4276 | /*[clinic input] |
| 4277 | os.umask |
| 4278 | |
| 4279 | mask: int |
| 4280 | / |
| 4281 | |
| 4282 | Set the current numeric umask and return the previous umask. |
| 4283 | [clinic start generated code]*/ |
| 4284 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4285 | static PyObject * |
| 4286 | os_umask_impl(PyModuleDef *module, int mask) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4287 | /*[clinic end generated code: output=9e1fe3c9f14d6a05 input=ab6bfd9b24d8a7e8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4288 | { |
| 4289 | int i = (int)umask(mask); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4290 | if (i < 0) |
| 4291 | return posix_error(); |
| 4292 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4293 | } |
| 4294 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4295 | #ifdef MS_WINDOWS |
| 4296 | |
| 4297 | /* override the default DeleteFileW behavior so that directory |
| 4298 | symlinks can be removed with this function, the same as with |
| 4299 | Unix symlinks */ |
| 4300 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4301 | { |
| 4302 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4303 | WIN32_FIND_DATAW find_data; |
| 4304 | HANDLE find_data_handle; |
| 4305 | int is_directory = 0; |
| 4306 | int is_link = 0; |
| 4307 | |
| 4308 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4309 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4310 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4311 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4312 | it is a symlink */ |
| 4313 | if(is_directory && |
| 4314 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4315 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4316 | |
| 4317 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4318 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 4319 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 4320 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 4321 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4322 | FindClose(find_data_handle); |
| 4323 | } |
| 4324 | } |
| 4325 | } |
| 4326 | |
| 4327 | if (is_directory && is_link) |
| 4328 | return RemoveDirectoryW(lpFileName); |
| 4329 | |
| 4330 | return DeleteFileW(lpFileName); |
| 4331 | } |
| 4332 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4333 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4334 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4335 | /*[clinic input] |
| 4336 | os.unlink |
| 4337 | |
| 4338 | path: path_t |
| 4339 | * |
| 4340 | dir_fd: dir_fd(requires='unlinkat')=None |
| 4341 | |
| 4342 | Remove a file (same as remove()). |
| 4343 | |
| 4344 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4345 | and path should be relative; path will then be relative to that directory. |
| 4346 | dir_fd may not be implemented on your platform. |
| 4347 | If it is unavailable, using it will raise a NotImplementedError. |
| 4348 | |
| 4349 | [clinic start generated code]*/ |
| 4350 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4351 | static PyObject * |
| 4352 | os_unlink_impl(PyModuleDef *module, path_t *path, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4353 | /*[clinic end generated code: output=474afd5cd09b237e input=d7bcde2b1b2a2552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4354 | { |
| 4355 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4356 | |
| 4357 | Py_BEGIN_ALLOW_THREADS |
| 4358 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4359 | if (path->wide) |
| 4360 | result = Py_DeleteFileW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4361 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4362 | result = DeleteFileA(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4363 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4364 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4365 | #ifdef HAVE_UNLINKAT |
| 4366 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4367 | result = unlinkat(dir_fd, path->narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4368 | else |
| 4369 | #endif /* HAVE_UNLINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4370 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4371 | #endif |
| 4372 | Py_END_ALLOW_THREADS |
| 4373 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4374 | if (result) |
| 4375 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4376 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4377 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4378 | } |
| 4379 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4380 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4381 | /*[clinic input] |
| 4382 | os.remove = os.unlink |
| 4383 | |
| 4384 | Remove a file (same as unlink()). |
| 4385 | |
| 4386 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4387 | and path should be relative; path will then be relative to that directory. |
| 4388 | dir_fd may not be implemented on your platform. |
| 4389 | If it is unavailable, using it will raise a NotImplementedError. |
| 4390 | [clinic start generated code]*/ |
| 4391 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4392 | static PyObject * |
| 4393 | os_remove_impl(PyModuleDef *module, path_t *path, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4394 | /*[clinic end generated code: output=d0d5149e64832b9e input=e05c5ab55cd30983]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4395 | { |
| 4396 | return os_unlink_impl(module, path, dir_fd); |
| 4397 | } |
| 4398 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4399 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4400 | static PyStructSequence_Field uname_result_fields[] = { |
| 4401 | {"sysname", "operating system name"}, |
| 4402 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4403 | {"release", "operating system release"}, |
| 4404 | {"version", "operating system version"}, |
| 4405 | {"machine", "hardware identifier"}, |
| 4406 | {NULL} |
| 4407 | }; |
| 4408 | |
| 4409 | PyDoc_STRVAR(uname_result__doc__, |
| 4410 | "uname_result: Result from os.uname().\n\n\ |
| 4411 | This object may be accessed either as a tuple of\n\ |
| 4412 | (sysname, nodename, release, version, machine),\n\ |
| 4413 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4414 | \n\ |
| 4415 | See os.uname for more information."); |
| 4416 | |
| 4417 | static PyStructSequence_Desc uname_result_desc = { |
| 4418 | "uname_result", /* name */ |
| 4419 | uname_result__doc__, /* doc */ |
| 4420 | uname_result_fields, |
| 4421 | 5 |
| 4422 | }; |
| 4423 | |
| 4424 | static PyTypeObject UnameResultType; |
| 4425 | |
| 4426 | |
| 4427 | #ifdef HAVE_UNAME |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4428 | /*[clinic input] |
| 4429 | os.uname |
| 4430 | |
| 4431 | Return an object identifying the current operating system. |
| 4432 | |
| 4433 | The object behaves like a named tuple with the following fields: |
| 4434 | (sysname, nodename, release, version, machine) |
| 4435 | |
| 4436 | [clinic start generated code]*/ |
| 4437 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4438 | static PyObject * |
| 4439 | os_uname_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4440 | /*[clinic end generated code: output=01e1421b757e753f input=e68bd246db3043ed]*/ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4441 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4442 | struct utsname u; |
| 4443 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4444 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4445 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4446 | Py_BEGIN_ALLOW_THREADS |
| 4447 | res = uname(&u); |
| 4448 | Py_END_ALLOW_THREADS |
| 4449 | if (res < 0) |
| 4450 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4451 | |
| 4452 | value = PyStructSequence_New(&UnameResultType); |
| 4453 | if (value == NULL) |
| 4454 | return NULL; |
| 4455 | |
| 4456 | #define SET(i, field) \ |
| 4457 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4458 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4459 | if (!o) { \ |
| 4460 | Py_DECREF(value); \ |
| 4461 | return NULL; \ |
| 4462 | } \ |
| 4463 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4464 | } \ |
| 4465 | |
| 4466 | SET(0, u.sysname); |
| 4467 | SET(1, u.nodename); |
| 4468 | SET(2, u.release); |
| 4469 | SET(3, u.version); |
| 4470 | SET(4, u.machine); |
| 4471 | |
| 4472 | #undef SET |
| 4473 | |
| 4474 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4475 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4476 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4477 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4478 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4479 | |
| 4480 | typedef struct { |
| 4481 | int now; |
| 4482 | time_t atime_s; |
| 4483 | long atime_ns; |
| 4484 | time_t mtime_s; |
| 4485 | long mtime_ns; |
| 4486 | } utime_t; |
| 4487 | |
| 4488 | /* |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4489 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4490 | * they also intentionally leak the declaration of a pointer named "time" |
| 4491 | */ |
| 4492 | #define UTIME_TO_TIMESPEC \ |
| 4493 | struct timespec ts[2]; \ |
| 4494 | struct timespec *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4495 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4496 | time = NULL; \ |
| 4497 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4498 | ts[0].tv_sec = ut->atime_s; \ |
| 4499 | ts[0].tv_nsec = ut->atime_ns; \ |
| 4500 | ts[1].tv_sec = ut->mtime_s; \ |
| 4501 | ts[1].tv_nsec = ut->mtime_ns; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4502 | time = ts; \ |
| 4503 | } \ |
| 4504 | |
| 4505 | #define UTIME_TO_TIMEVAL \ |
| 4506 | struct timeval tv[2]; \ |
| 4507 | struct timeval *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4508 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4509 | time = NULL; \ |
| 4510 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4511 | tv[0].tv_sec = ut->atime_s; \ |
| 4512 | tv[0].tv_usec = ut->atime_ns / 1000; \ |
| 4513 | tv[1].tv_sec = ut->mtime_s; \ |
| 4514 | tv[1].tv_usec = ut->mtime_ns / 1000; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4515 | time = tv; \ |
| 4516 | } \ |
| 4517 | |
| 4518 | #define UTIME_TO_UTIMBUF \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4519 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4520 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4521 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4522 | time = NULL; \ |
| 4523 | else { \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4524 | u.actime = ut->atime_s; \ |
| 4525 | u.modtime = ut->mtime_s; \ |
| 4526 | time = &u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4527 | } |
| 4528 | |
| 4529 | #define UTIME_TO_TIME_T \ |
| 4530 | time_t timet[2]; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4531 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4532 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4533 | time = NULL; \ |
| 4534 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4535 | timet[0] = ut->atime_s; \ |
| 4536 | timet[1] = ut->mtime_s; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4537 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4538 | } \ |
| 4539 | |
| 4540 | |
| 4541 | #define UTIME_HAVE_DIR_FD (defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)) |
| 4542 | |
| 4543 | #if UTIME_HAVE_DIR_FD |
| 4544 | |
| 4545 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4546 | utime_dir_fd(utime_t *ut, int dir_fd, char *path, int follow_symlinks) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4547 | { |
| 4548 | #ifdef HAVE_UTIMENSAT |
| 4549 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4550 | UTIME_TO_TIMESPEC; |
| 4551 | return utimensat(dir_fd, path, time, flags); |
| 4552 | #elif defined(HAVE_FUTIMESAT) |
| 4553 | UTIME_TO_TIMEVAL; |
| 4554 | /* |
| 4555 | * follow_symlinks will never be false here; |
| 4556 | * we only allow !follow_symlinks and dir_fd together |
| 4557 | * if we have utimensat() |
| 4558 | */ |
| 4559 | assert(follow_symlinks); |
| 4560 | return futimesat(dir_fd, path, time); |
| 4561 | #endif |
| 4562 | } |
| 4563 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4564 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
| 4565 | #else |
| 4566 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4567 | #endif |
| 4568 | |
| 4569 | #define UTIME_HAVE_FD (defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)) |
| 4570 | |
| 4571 | #if UTIME_HAVE_FD |
| 4572 | |
| 4573 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4574 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4575 | { |
| 4576 | #ifdef HAVE_FUTIMENS |
| 4577 | UTIME_TO_TIMESPEC; |
| 4578 | return futimens(fd, time); |
| 4579 | #else |
| 4580 | UTIME_TO_TIMEVAL; |
| 4581 | return futimes(fd, time); |
| 4582 | #endif |
| 4583 | } |
| 4584 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4585 | #define PATH_UTIME_HAVE_FD 1 |
| 4586 | #else |
| 4587 | #define PATH_UTIME_HAVE_FD 0 |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4588 | #endif |
| 4589 | |
| 4590 | |
| 4591 | #define UTIME_HAVE_NOFOLLOW_SYMLINKS \ |
| 4592 | (defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)) |
| 4593 | |
| 4594 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4595 | |
| 4596 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4597 | utime_nofollow_symlinks(utime_t *ut, char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4598 | { |
| 4599 | #ifdef HAVE_UTIMENSAT |
| 4600 | UTIME_TO_TIMESPEC; |
| 4601 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4602 | #else |
| 4603 | UTIME_TO_TIMEVAL; |
| 4604 | return lutimes(path, time); |
| 4605 | #endif |
| 4606 | } |
| 4607 | |
| 4608 | #endif |
| 4609 | |
| 4610 | #ifndef MS_WINDOWS |
| 4611 | |
| 4612 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4613 | utime_default(utime_t *ut, char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4614 | { |
| 4615 | #ifdef HAVE_UTIMENSAT |
| 4616 | UTIME_TO_TIMESPEC; |
| 4617 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4618 | #elif defined(HAVE_UTIMES) |
| 4619 | UTIME_TO_TIMEVAL; |
| 4620 | return utimes(path, time); |
| 4621 | #elif defined(HAVE_UTIME_H) |
| 4622 | UTIME_TO_UTIMBUF; |
| 4623 | return utime(path, time); |
| 4624 | #else |
| 4625 | UTIME_TO_TIME_T; |
| 4626 | return utime(path, time); |
| 4627 | #endif |
| 4628 | } |
| 4629 | |
| 4630 | #endif |
| 4631 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4632 | static int |
| 4633 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4634 | { |
| 4635 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4636 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4637 | divmod = PyNumber_Divmod(py_long, billion); |
| 4638 | if (!divmod) |
| 4639 | goto exit; |
| 4640 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4641 | if ((*s == -1) && PyErr_Occurred()) |
| 4642 | goto exit; |
| 4643 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4644 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4645 | goto exit; |
| 4646 | |
| 4647 | result = 1; |
| 4648 | exit: |
| 4649 | Py_XDECREF(divmod); |
| 4650 | return result; |
| 4651 | } |
| 4652 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4653 | |
| 4654 | /*[clinic input] |
| 4655 | os.utime |
| 4656 | |
| 4657 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') |
| 4658 | times: object = NULL |
| 4659 | * |
| 4660 | ns: object = NULL |
| 4661 | dir_fd: dir_fd(requires='futimensat') = None |
| 4662 | follow_symlinks: bool=True |
| 4663 | |
| 4664 | # "utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True)\n\ |
| 4665 | |
| 4666 | Set the access and modified time of path. |
| 4667 | |
| 4668 | path may always be specified as a string. |
| 4669 | On some platforms, path may also be specified as an open file descriptor. |
| 4670 | If this functionality is unavailable, using it raises an exception. |
| 4671 | |
| 4672 | If times is not None, it must be a tuple (atime, mtime); |
| 4673 | atime and mtime should be expressed as float seconds since the epoch. |
| 4674 | If ns is not None, it must be a tuple (atime_ns, mtime_ns); |
| 4675 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 4676 | since the epoch. |
| 4677 | If both times and ns are None, utime uses the current time. |
| 4678 | Specifying tuples for both times and ns is an error. |
| 4679 | |
| 4680 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4681 | and path should be relative; path will then be relative to that directory. |
| 4682 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 4683 | link, utime will modify the symbolic link itself instead of the file the |
| 4684 | link points to. |
| 4685 | It is an error to use dir_fd or follow_symlinks when specifying path |
| 4686 | as an open file descriptor. |
| 4687 | dir_fd and follow_symlinks may not be available on your platform. |
| 4688 | If they are unavailable, using them will raise a NotImplementedError. |
| 4689 | |
| 4690 | [clinic start generated code]*/ |
| 4691 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4692 | static PyObject * |
| 4693 | os_utime_impl(PyModuleDef *module, path_t *path, PyObject *times, PyObject *ns, int dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4694 | /*[clinic end generated code: output=c52d8fd0d1067f0b input=1f18c17d5941aa82]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4695 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4696 | #ifdef MS_WINDOWS |
| 4697 | HANDLE hFile; |
| 4698 | FILETIME atime, mtime; |
| 4699 | #else |
| 4700 | int result; |
| 4701 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4702 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4703 | PyObject *return_value = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4704 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4705 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4706 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4707 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4708 | if (times && (times != Py_None) && ns) { |
| 4709 | PyErr_SetString(PyExc_ValueError, |
| 4710 | "utime: you may specify either 'times'" |
| 4711 | " or 'ns' but not both"); |
| 4712 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4713 | } |
| 4714 | |
| 4715 | if (times && (times != Py_None)) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4716 | time_t a_sec, m_sec; |
| 4717 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4718 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4719 | PyErr_SetString(PyExc_TypeError, |
| 4720 | "utime: 'times' must be either" |
| 4721 | " a tuple of two ints or None"); |
| 4722 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4723 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4724 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4725 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4726 | &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4727 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4728 | &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4729 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4730 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4731 | utime.atime_s = a_sec; |
| 4732 | utime.atime_ns = a_nsec; |
| 4733 | utime.mtime_s = m_sec; |
| 4734 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4735 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4736 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4737 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4738 | PyErr_SetString(PyExc_TypeError, |
| 4739 | "utime: 'ns' must be a tuple of two ints"); |
| 4740 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4741 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4742 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4743 | 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] | 4744 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4745 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4746 | &utime.mtime_s, &utime.mtime_ns)) { |
| 4747 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4748 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4749 | } |
| 4750 | else { |
| 4751 | /* times and ns are both None/unspecified. use "now". */ |
| 4752 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4753 | } |
| 4754 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4755 | #if !UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4756 | if (follow_symlinks_specified("utime", follow_symlinks)) |
| 4757 | goto exit; |
| 4758 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4759 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4760 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
| 4761 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || |
| 4762 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4763 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4764 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4765 | #if !defined(HAVE_UTIMENSAT) |
| 4766 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4767 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4768 | "utime: cannot use dir_fd and follow_symlinks " |
| 4769 | "together on this platform"); |
| 4770 | goto exit; |
| 4771 | } |
| 4772 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4773 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4774 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4775 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4776 | if (path->wide) |
| 4777 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4778 | NULL, OPEN_EXISTING, |
| 4779 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4780 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4781 | hFile = CreateFileA(path->narrow, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4782 | NULL, OPEN_EXISTING, |
| 4783 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4784 | Py_END_ALLOW_THREADS |
| 4785 | if (hFile == INVALID_HANDLE_VALUE) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4786 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4787 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4788 | } |
| 4789 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4790 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 4791 | GetSystemTimeAsFileTime(&mtime); |
| 4792 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4793 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4794 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 4795 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4796 | _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4797 | } |
| 4798 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4799 | /* Avoid putting the file name into the error here, |
| 4800 | as that may confuse the user into believing that |
| 4801 | something is wrong with the file, when it also |
| 4802 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4803 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4804 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4805 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4806 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4807 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4808 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4809 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4810 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4811 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4812 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4813 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4814 | |
| 4815 | #if UTIME_HAVE_DIR_FD |
| 4816 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4817 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4818 | else |
| 4819 | #endif |
| 4820 | |
| 4821 | #if UTIME_HAVE_FD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4822 | if (path->fd != -1) |
| 4823 | result = utime_fd(&utime, path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4824 | else |
| 4825 | #endif |
| 4826 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4827 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4828 | |
| 4829 | Py_END_ALLOW_THREADS |
| 4830 | |
| 4831 | if (result < 0) { |
| 4832 | /* see previous comment about not putting filename in error here */ |
| 4833 | return_value = posix_error(); |
| 4834 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4835 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4836 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4837 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4838 | |
| 4839 | Py_INCREF(Py_None); |
| 4840 | return_value = Py_None; |
| 4841 | |
| 4842 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4843 | #ifdef MS_WINDOWS |
| 4844 | if (hFile != INVALID_HANDLE_VALUE) |
| 4845 | CloseHandle(hFile); |
| 4846 | #endif |
| 4847 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4848 | } |
| 4849 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4850 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4851 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4852 | |
| 4853 | /*[clinic input] |
| 4854 | os._exit |
| 4855 | |
| 4856 | status: int |
| 4857 | |
| 4858 | Exit to the system with specified status, without normal exit processing. |
| 4859 | [clinic start generated code]*/ |
| 4860 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4861 | static PyObject * |
| 4862 | os__exit_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4863 | /*[clinic end generated code: output=472a3cbaf68f3621 input=5e6d57556b0c4a62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4864 | { |
| 4865 | _exit(status); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4866 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4867 | } |
| 4868 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4869 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 4870 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4871 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4872 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4873 | Py_ssize_t i; |
| 4874 | for (i = 0; i < count; i++) |
| 4875 | PyMem_Free(array[i]); |
| 4876 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4877 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4878 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 4879 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4880 | int fsconvert_strdup(PyObject *o, char**out) |
| 4881 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4882 | PyObject *bytes; |
| 4883 | Py_ssize_t size; |
| 4884 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 4885 | return 0; |
| 4886 | size = PyBytes_GET_SIZE(bytes); |
| 4887 | *out = PyMem_Malloc(size+1); |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 4888 | if (!*out) { |
| 4889 | PyErr_NoMemory(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4890 | return 0; |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 4891 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4892 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 4893 | Py_DECREF(bytes); |
| 4894 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4895 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4896 | #endif |
| 4897 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4898 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4899 | static char** |
| 4900 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 4901 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4902 | char **envlist; |
| 4903 | Py_ssize_t i, pos, envc; |
| 4904 | PyObject *keys=NULL, *vals=NULL; |
| 4905 | PyObject *key, *val, *key2, *val2; |
| 4906 | char *p, *k, *v; |
| 4907 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4908 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4909 | i = PyMapping_Size(env); |
| 4910 | if (i < 0) |
| 4911 | return NULL; |
| 4912 | envlist = PyMem_NEW(char *, i + 1); |
| 4913 | if (envlist == NULL) { |
| 4914 | PyErr_NoMemory(); |
| 4915 | return NULL; |
| 4916 | } |
| 4917 | envc = 0; |
| 4918 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4919 | if (!keys) |
| 4920 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4921 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 4922 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4923 | goto error; |
| 4924 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 4925 | PyErr_Format(PyExc_TypeError, |
| 4926 | "env.keys() or env.values() is not a list"); |
| 4927 | goto error; |
| 4928 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4929 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4930 | for (pos = 0; pos < i; pos++) { |
| 4931 | key = PyList_GetItem(keys, pos); |
| 4932 | val = PyList_GetItem(vals, pos); |
| 4933 | if (!key || !val) |
| 4934 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4935 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4936 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 4937 | goto error; |
| 4938 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 4939 | Py_DECREF(key2); |
| 4940 | goto error; |
| 4941 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4942 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4943 | k = PyBytes_AsString(key2); |
| 4944 | v = PyBytes_AsString(val2); |
| 4945 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4946 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4947 | p = PyMem_NEW(char, len); |
| 4948 | if (p == NULL) { |
| 4949 | PyErr_NoMemory(); |
| 4950 | Py_DECREF(key2); |
| 4951 | Py_DECREF(val2); |
| 4952 | goto error; |
| 4953 | } |
| 4954 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 4955 | envlist[envc++] = p; |
| 4956 | Py_DECREF(key2); |
| 4957 | Py_DECREF(val2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4958 | } |
| 4959 | Py_DECREF(vals); |
| 4960 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4961 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4962 | envlist[envc] = 0; |
| 4963 | *envc_ptr = envc; |
| 4964 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4965 | |
| 4966 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4967 | Py_XDECREF(keys); |
| 4968 | Py_XDECREF(vals); |
| 4969 | while (--envc >= 0) |
| 4970 | PyMem_DEL(envlist[envc]); |
| 4971 | PyMem_DEL(envlist); |
| 4972 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4973 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4974 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4975 | static char** |
| 4976 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 4977 | { |
| 4978 | int i; |
| 4979 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 4980 | if (argvlist == NULL) { |
| 4981 | PyErr_NoMemory(); |
| 4982 | return NULL; |
| 4983 | } |
| 4984 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4985 | PyObject* item = PySequence_ITEM(argv, i); |
| 4986 | if (item == NULL) |
| 4987 | goto fail; |
| 4988 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 4989 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4990 | goto fail; |
| 4991 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4992 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4993 | } |
| 4994 | argvlist[*argc] = NULL; |
| 4995 | return argvlist; |
| 4996 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4997 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4998 | free_string_array(argvlist, *argc); |
| 4999 | return NULL; |
| 5000 | } |
| 5001 | #endif |
| 5002 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5003 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5004 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5005 | /*[clinic input] |
| 5006 | os.execv |
| 5007 | |
| 5008 | path: FSConverter |
| 5009 | Path of executable file. |
| 5010 | argv: object |
| 5011 | Tuple or list of strings. |
| 5012 | / |
| 5013 | |
| 5014 | Execute an executable path with arguments, replacing current process. |
| 5015 | [clinic start generated code]*/ |
| 5016 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5017 | static PyObject * |
| 5018 | os_execv_impl(PyModuleDef *module, PyObject *path, PyObject *argv) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5019 | /*[clinic end generated code: output=9221f08143146fff input=96041559925e5229]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5020 | { |
| 5021 | char *path_char; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5022 | char **argvlist; |
| 5023 | Py_ssize_t argc; |
| 5024 | |
| 5025 | /* execv has two arguments: (path, argv), where |
| 5026 | argv is a list or tuple of strings. */ |
| 5027 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5028 | path_char = PyBytes_AsString(path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5029 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 5030 | PyErr_SetString(PyExc_TypeError, |
| 5031 | "execv() arg 2 must be a tuple or list"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5032 | return NULL; |
| 5033 | } |
| 5034 | argc = PySequence_Size(argv); |
| 5035 | if (argc < 1) { |
| 5036 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5037 | return NULL; |
| 5038 | } |
| 5039 | |
| 5040 | argvlist = parse_arglist(argv, &argc); |
| 5041 | if (argvlist == NULL) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5042 | return NULL; |
| 5043 | } |
| 5044 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5045 | execv(path_char, argvlist); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5046 | |
| 5047 | /* If we get here it's definitely an error */ |
| 5048 | |
| 5049 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5050 | return posix_error(); |
| 5051 | } |
| 5052 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5053 | |
| 5054 | /*[clinic input] |
| 5055 | os.execve |
| 5056 | |
| 5057 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') |
| 5058 | Path of executable file. |
| 5059 | argv: object |
| 5060 | Tuple or list of strings. |
| 5061 | env: object |
| 5062 | Dictionary of strings mapping to strings. |
| 5063 | |
| 5064 | Execute an executable path with arguments, replacing current process. |
| 5065 | [clinic start generated code]*/ |
| 5066 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5067 | static PyObject * |
| 5068 | os_execve_impl(PyModuleDef *module, path_t *path, PyObject *argv, PyObject *env) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5069 | /*[clinic end generated code: output=7758d4f230d8aac6 input=626804fa092606d9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5070 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5071 | char **argvlist = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5072 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5073 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5074 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5075 | /* execve has three arguments: (path, argv, env), where |
| 5076 | argv is a list or tuple of strings and env is a dictionary |
| 5077 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5078 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5079 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5080 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5081 | "execve: argv must be a tuple or list"); |
| 5082 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5083 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5084 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5085 | if (!PyMapping_Check(env)) { |
| 5086 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5087 | "execve: environment must be a mapping object"); |
| 5088 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5089 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5090 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5091 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5092 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5093 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5094 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5095 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5096 | envlist = parse_envlist(env, &envc); |
| 5097 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5098 | goto fail; |
| 5099 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5100 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5101 | if (path->fd > -1) |
| 5102 | fexecve(path->fd, argvlist, envlist); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5103 | else |
| 5104 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5105 | execve(path->narrow, argvlist, envlist); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5106 | |
| 5107 | /* If we get here it's definitely an error */ |
| 5108 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5109 | path_error(path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5110 | |
| 5111 | while (--envc >= 0) |
| 5112 | PyMem_DEL(envlist[envc]); |
| 5113 | PyMem_DEL(envlist); |
| 5114 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5115 | if (argvlist) |
| 5116 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5117 | return NULL; |
| 5118 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5119 | #endif /* HAVE_EXECV */ |
| 5120 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5121 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5122 | #ifdef HAVE_SPAWNV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5123 | /*[clinic input] |
| 5124 | os.spawnv |
| 5125 | |
| 5126 | mode: int |
| 5127 | Mode of process creation. |
| 5128 | path: FSConverter |
| 5129 | Path of executable file. |
| 5130 | argv: object |
| 5131 | Tuple or list of strings. |
| 5132 | / |
| 5133 | |
| 5134 | Execute the program specified by path in a new process. |
| 5135 | [clinic start generated code]*/ |
| 5136 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5137 | static PyObject * |
| 5138 | os_spawnv_impl(PyModuleDef *module, int mode, PyObject *path, PyObject *argv) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5139 | /*[clinic end generated code: output=140a7945484c8cc5 input=042c91dfc1e6debc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5140 | { |
| 5141 | char *path_char; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5142 | char **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5143 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5144 | Py_ssize_t argc; |
| 5145 | Py_intptr_t spawnval; |
| 5146 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5147 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5148 | /* spawnv has three arguments: (mode, path, argv), where |
| 5149 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5150 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5151 | path_char = PyBytes_AsString(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5152 | if (PyList_Check(argv)) { |
| 5153 | argc = PyList_Size(argv); |
| 5154 | getitem = PyList_GetItem; |
| 5155 | } |
| 5156 | else if (PyTuple_Check(argv)) { |
| 5157 | argc = PyTuple_Size(argv); |
| 5158 | getitem = PyTuple_GetItem; |
| 5159 | } |
| 5160 | else { |
| 5161 | PyErr_SetString(PyExc_TypeError, |
| 5162 | "spawnv() arg 2 must be a tuple or list"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5163 | return NULL; |
| 5164 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5165 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5166 | argvlist = PyMem_NEW(char *, argc+1); |
| 5167 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5168 | return PyErr_NoMemory(); |
| 5169 | } |
| 5170 | for (i = 0; i < argc; i++) { |
| 5171 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5172 | &argvlist[i])) { |
| 5173 | free_string_array(argvlist, i); |
| 5174 | PyErr_SetString( |
| 5175 | PyExc_TypeError, |
| 5176 | "spawnv() arg 2 must contain only strings"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5177 | return NULL; |
| 5178 | } |
| 5179 | } |
| 5180 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5181 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5182 | if (mode == _OLD_P_OVERLAY) |
| 5183 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5184 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5185 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5186 | spawnval = _spawnv(mode, path_char, argvlist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5187 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5188 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5189 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5190 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5191 | if (spawnval == -1) |
| 5192 | return posix_error(); |
| 5193 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5194 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5195 | } |
| 5196 | |
| 5197 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5198 | /*[clinic input] |
| 5199 | os.spawnve |
| 5200 | |
| 5201 | mode: int |
| 5202 | Mode of process creation. |
| 5203 | path: FSConverter |
| 5204 | Path of executable file. |
| 5205 | argv: object |
| 5206 | Tuple or list of strings. |
| 5207 | env: object |
| 5208 | Dictionary of strings mapping to strings. |
| 5209 | / |
| 5210 | |
| 5211 | Execute the program specified by path in a new process. |
| 5212 | [clinic start generated code]*/ |
| 5213 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5214 | static PyObject * |
| 5215 | os_spawnve_impl(PyModuleDef *module, int mode, PyObject *path, PyObject *argv, PyObject *env) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5216 | /*[clinic end generated code: output=1c52955789461be8 input=02362fd937963f8f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5217 | { |
| 5218 | char *path_char; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5219 | char **argvlist; |
| 5220 | char **envlist; |
| 5221 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5222 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5223 | Py_intptr_t spawnval; |
| 5224 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 5225 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5226 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5227 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5228 | argv is a list or tuple of strings and env is a dictionary |
| 5229 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5230 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5231 | path_char = PyBytes_AsString(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5232 | if (PyList_Check(argv)) { |
| 5233 | argc = PyList_Size(argv); |
| 5234 | getitem = PyList_GetItem; |
| 5235 | } |
| 5236 | else if (PyTuple_Check(argv)) { |
| 5237 | argc = PyTuple_Size(argv); |
| 5238 | getitem = PyTuple_GetItem; |
| 5239 | } |
| 5240 | else { |
| 5241 | PyErr_SetString(PyExc_TypeError, |
| 5242 | "spawnve() arg 2 must be a tuple or list"); |
| 5243 | goto fail_0; |
| 5244 | } |
| 5245 | if (!PyMapping_Check(env)) { |
| 5246 | PyErr_SetString(PyExc_TypeError, |
| 5247 | "spawnve() arg 3 must be a mapping object"); |
| 5248 | goto fail_0; |
| 5249 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5250 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5251 | argvlist = PyMem_NEW(char *, argc+1); |
| 5252 | if (argvlist == NULL) { |
| 5253 | PyErr_NoMemory(); |
| 5254 | goto fail_0; |
| 5255 | } |
| 5256 | for (i = 0; i < argc; i++) { |
| 5257 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5258 | &argvlist[i])) |
| 5259 | { |
| 5260 | lastarg = i; |
| 5261 | goto fail_1; |
| 5262 | } |
| 5263 | } |
| 5264 | lastarg = argc; |
| 5265 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5266 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5267 | envlist = parse_envlist(env, &envc); |
| 5268 | if (envlist == NULL) |
| 5269 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5270 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5271 | if (mode == _OLD_P_OVERLAY) |
| 5272 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5273 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5274 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5275 | spawnval = _spawnve(mode, path_char, argvlist, envlist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5276 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5277 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5278 | if (spawnval == -1) |
| 5279 | (void) posix_error(); |
| 5280 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5281 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5282 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5283 | while (--envc >= 0) |
| 5284 | PyMem_DEL(envlist[envc]); |
| 5285 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5286 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5287 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5288 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5289 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5290 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5291 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5292 | #endif /* HAVE_SPAWNV */ |
| 5293 | |
| 5294 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5295 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5296 | /*[clinic input] |
| 5297 | os.fork1 |
| 5298 | |
| 5299 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 5300 | |
| 5301 | Return 0 to child process and PID of child to parent process. |
| 5302 | [clinic start generated code]*/ |
| 5303 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5304 | static PyObject * |
| 5305 | os_fork1_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5306 | /*[clinic end generated code: output=e27b4f66419c9dcf input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5307 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5308 | pid_t pid; |
| 5309 | int result = 0; |
| 5310 | _PyImport_AcquireLock(); |
| 5311 | pid = fork1(); |
| 5312 | if (pid == 0) { |
| 5313 | /* child: this clobbers and resets the import lock. */ |
| 5314 | PyOS_AfterFork(); |
| 5315 | } else { |
| 5316 | /* parent: release the import lock. */ |
| 5317 | result = _PyImport_ReleaseLock(); |
| 5318 | } |
| 5319 | if (pid == -1) |
| 5320 | return posix_error(); |
| 5321 | if (result < 0) { |
| 5322 | /* Don't clobber the OSError if the fork failed. */ |
| 5323 | PyErr_SetString(PyExc_RuntimeError, |
| 5324 | "not holding the import lock"); |
| 5325 | return NULL; |
| 5326 | } |
| 5327 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5328 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5329 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5330 | |
| 5331 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5332 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5333 | /*[clinic input] |
| 5334 | os.fork |
| 5335 | |
| 5336 | Fork a child process. |
| 5337 | |
| 5338 | Return 0 to child process and PID of child to parent process. |
| 5339 | [clinic start generated code]*/ |
| 5340 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5341 | static PyObject * |
| 5342 | os_fork_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5343 | /*[clinic end generated code: output=898b1ecd3498ba12 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5344 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5345 | pid_t pid; |
| 5346 | int result = 0; |
| 5347 | _PyImport_AcquireLock(); |
| 5348 | pid = fork(); |
| 5349 | if (pid == 0) { |
| 5350 | /* child: this clobbers and resets the import lock. */ |
| 5351 | PyOS_AfterFork(); |
| 5352 | } else { |
| 5353 | /* parent: release the import lock. */ |
| 5354 | result = _PyImport_ReleaseLock(); |
| 5355 | } |
| 5356 | if (pid == -1) |
| 5357 | return posix_error(); |
| 5358 | if (result < 0) { |
| 5359 | /* Don't clobber the OSError if the fork failed. */ |
| 5360 | PyErr_SetString(PyExc_RuntimeError, |
| 5361 | "not holding the import lock"); |
| 5362 | return NULL; |
| 5363 | } |
| 5364 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5365 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5366 | #endif /* HAVE_FORK */ |
| 5367 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5368 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5369 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5370 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5371 | /*[clinic input] |
| 5372 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5373 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5374 | policy: int |
| 5375 | |
| 5376 | Get the maximum scheduling priority for policy. |
| 5377 | [clinic start generated code]*/ |
| 5378 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5379 | static PyObject * |
| 5380 | os_sched_get_priority_max_impl(PyModuleDef *module, int policy) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5381 | /*[clinic end generated code: output=a6a30fa5071f2d81 input=2097b7998eca6874]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5382 | { |
| 5383 | int max; |
| 5384 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5385 | max = sched_get_priority_max(policy); |
| 5386 | if (max < 0) |
| 5387 | return posix_error(); |
| 5388 | return PyLong_FromLong(max); |
| 5389 | } |
| 5390 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5391 | |
| 5392 | /*[clinic input] |
| 5393 | os.sched_get_priority_min |
| 5394 | |
| 5395 | policy: int |
| 5396 | |
| 5397 | Get the minimum scheduling priority for policy. |
| 5398 | [clinic start generated code]*/ |
| 5399 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5400 | static PyObject * |
| 5401 | os_sched_get_priority_min_impl(PyModuleDef *module, int policy) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5402 | /*[clinic end generated code: output=5ca3ed6bc43e9b20 input=21bc8fa0d70983bf]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5403 | { |
| 5404 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5405 | if (min < 0) |
| 5406 | return posix_error(); |
| 5407 | return PyLong_FromLong(min); |
| 5408 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5409 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 5410 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5411 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5412 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5413 | /*[clinic input] |
| 5414 | os.sched_getscheduler |
| 5415 | pid: pid_t |
| 5416 | / |
| 5417 | |
| 5418 | Get the scheduling policy for the process identifiedy by pid. |
| 5419 | |
| 5420 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 5421 | [clinic start generated code]*/ |
| 5422 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5423 | static PyObject * |
| 5424 | os_sched_getscheduler_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5425 | /*[clinic end generated code: output=8cd63c15caf54fa9 input=5f14cfd1f189e1a0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5426 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5427 | int policy; |
| 5428 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5429 | policy = sched_getscheduler(pid); |
| 5430 | if (policy < 0) |
| 5431 | return posix_error(); |
| 5432 | return PyLong_FromLong(policy); |
| 5433 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5434 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5435 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5436 | |
| 5437 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5438 | /*[clinic input] |
| 5439 | class os.sched_param "PyObject *" "&SchedParamType" |
| 5440 | |
| 5441 | @classmethod |
| 5442 | os.sched_param.__new__ |
| 5443 | |
| 5444 | sched_priority: object |
| 5445 | A scheduling parameter. |
| 5446 | |
| 5447 | Current has only one field: sched_priority"); |
| 5448 | [clinic start generated code]*/ |
| 5449 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5450 | static PyObject * |
| 5451 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5452 | /*[clinic end generated code: output=48f4067d60f48c13 input=73a4c22f7071fc62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5453 | { |
| 5454 | PyObject *res; |
| 5455 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5456 | res = PyStructSequence_New(type); |
| 5457 | if (!res) |
| 5458 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5459 | Py_INCREF(sched_priority); |
| 5460 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5461 | return res; |
| 5462 | } |
| 5463 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5464 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5465 | PyDoc_VAR(os_sched_param__doc__); |
| 5466 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5467 | static PyStructSequence_Field sched_param_fields[] = { |
| 5468 | {"sched_priority", "the scheduling priority"}, |
| 5469 | {0} |
| 5470 | }; |
| 5471 | |
| 5472 | static PyStructSequence_Desc sched_param_desc = { |
| 5473 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5474 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5475 | sched_param_fields, |
| 5476 | 1 |
| 5477 | }; |
| 5478 | |
| 5479 | static int |
| 5480 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 5481 | { |
| 5482 | long priority; |
| 5483 | |
| 5484 | if (Py_TYPE(param) != &SchedParamType) { |
| 5485 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 5486 | return 0; |
| 5487 | } |
| 5488 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 5489 | if (priority == -1 && PyErr_Occurred()) |
| 5490 | return 0; |
| 5491 | if (priority > INT_MAX || priority < INT_MIN) { |
| 5492 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 5493 | return 0; |
| 5494 | } |
| 5495 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 5496 | return 1; |
| 5497 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5498 | #endif /* defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5499 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5500 | |
| 5501 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5502 | /*[clinic input] |
| 5503 | os.sched_setscheduler |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5504 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5505 | pid: pid_t |
| 5506 | policy: int |
| 5507 | param: sched_param |
| 5508 | / |
| 5509 | |
| 5510 | Set the scheduling policy for the process identified by pid. |
| 5511 | |
| 5512 | If pid is 0, the calling process is changed. |
| 5513 | param is an instance of sched_param. |
| 5514 | [clinic start generated code]*/ |
| 5515 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5516 | static PyObject * |
| 5517 | os_sched_setscheduler_impl(PyModuleDef *module, pid_t pid, int policy, struct sched_param *param) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5518 | /*[clinic end generated code: output=97f40f8384e554b0 input=c581f9469a5327dd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5519 | { |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5520 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 5521 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 5522 | ** scheduling policy under Solaris/Illumos, and others. |
| 5523 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5524 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5525 | if (sched_setscheduler(pid, policy, param) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5526 | return posix_error(); |
| 5527 | Py_RETURN_NONE; |
| 5528 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5529 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5530 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5531 | |
| 5532 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5533 | /*[clinic input] |
| 5534 | os.sched_getparam |
| 5535 | pid: pid_t |
| 5536 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5537 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5538 | Returns scheduling parameters for the process identified by pid. |
| 5539 | |
| 5540 | If pid is 0, returns parameters for the calling process. |
| 5541 | Return value is an instance of sched_param. |
| 5542 | [clinic start generated code]*/ |
| 5543 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5544 | static PyObject * |
| 5545 | os_sched_getparam_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5546 | /*[clinic end generated code: output=f42c5bd2604ecd08 input=18a1ef9c2efae296]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5547 | { |
| 5548 | struct sched_param param; |
| 5549 | PyObject *result; |
| 5550 | PyObject *priority; |
| 5551 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5552 | if (sched_getparam(pid, ¶m)) |
| 5553 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5554 | result = PyStructSequence_New(&SchedParamType); |
| 5555 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5556 | return NULL; |
| 5557 | priority = PyLong_FromLong(param.sched_priority); |
| 5558 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5559 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5560 | return NULL; |
| 5561 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5562 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 5563 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5564 | } |
| 5565 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5566 | |
| 5567 | /*[clinic input] |
| 5568 | os.sched_setparam |
| 5569 | pid: pid_t |
| 5570 | param: sched_param |
| 5571 | / |
| 5572 | |
| 5573 | Set scheduling parameters for the process identified by pid. |
| 5574 | |
| 5575 | If pid is 0, sets parameters for the calling process. |
| 5576 | param should be an instance of sched_param. |
| 5577 | [clinic start generated code]*/ |
| 5578 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5579 | static PyObject * |
| 5580 | os_sched_setparam_impl(PyModuleDef *module, pid_t pid, struct sched_param *param) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5581 | /*[clinic end generated code: output=c6560b34395bb343 input=6b8d6dfcecdc21bd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5582 | { |
| 5583 | if (sched_setparam(pid, param)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5584 | return posix_error(); |
| 5585 | Py_RETURN_NONE; |
| 5586 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5587 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5588 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5589 | |
| 5590 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5591 | /*[clinic input] |
| 5592 | os.sched_rr_get_interval -> double |
| 5593 | pid: pid_t |
| 5594 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5595 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5596 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 5597 | |
| 5598 | Value returned is a float. |
| 5599 | [clinic start generated code]*/ |
| 5600 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5601 | static double |
| 5602 | os_sched_rr_get_interval_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5603 | /*[clinic end generated code: output=7adc137a86dea581 input=2a973da15cca6fae]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5604 | { |
| 5605 | struct timespec interval; |
| 5606 | if (sched_rr_get_interval(pid, &interval)) { |
| 5607 | posix_error(); |
| 5608 | return -1.0; |
| 5609 | } |
| 5610 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 5611 | } |
| 5612 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5613 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5614 | |
| 5615 | /*[clinic input] |
| 5616 | os.sched_yield |
| 5617 | |
| 5618 | Voluntarily relinquish the CPU. |
| 5619 | [clinic start generated code]*/ |
| 5620 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5621 | static PyObject * |
| 5622 | os_sched_yield_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5623 | /*[clinic end generated code: output=d7bd51869c4cb6a8 input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5624 | { |
| 5625 | if (sched_yield()) |
| 5626 | return posix_error(); |
| 5627 | Py_RETURN_NONE; |
| 5628 | } |
| 5629 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5630 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5631 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 5632 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5633 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5634 | /*[clinic input] |
| 5635 | os.sched_setaffinity |
| 5636 | pid: pid_t |
| 5637 | mask : object |
| 5638 | / |
| 5639 | |
| 5640 | Set the CPU affinity of the process identified by pid to mask. |
| 5641 | |
| 5642 | mask should be an iterable of integers identifying CPUs. |
| 5643 | [clinic start generated code]*/ |
| 5644 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5645 | static PyObject * |
| 5646 | os_sched_setaffinity_impl(PyModuleDef *module, pid_t pid, PyObject *mask) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5647 | /*[clinic end generated code: output=582bcbf40d3253a9 input=a0791a597c7085ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5648 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5649 | int ncpus; |
| 5650 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5651 | cpu_set_t *cpu_set = NULL; |
| 5652 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5653 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5654 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5655 | if (iterator == NULL) |
| 5656 | return NULL; |
| 5657 | |
| 5658 | ncpus = NCPUS_START; |
| 5659 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5660 | cpu_set = CPU_ALLOC(ncpus); |
| 5661 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5662 | PyErr_NoMemory(); |
| 5663 | goto error; |
| 5664 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5665 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5666 | |
| 5667 | while ((item = PyIter_Next(iterator))) { |
| 5668 | long cpu; |
| 5669 | if (!PyLong_Check(item)) { |
| 5670 | PyErr_Format(PyExc_TypeError, |
| 5671 | "expected an iterator of ints, " |
| 5672 | "but iterator yielded %R", |
| 5673 | Py_TYPE(item)); |
| 5674 | Py_DECREF(item); |
| 5675 | goto error; |
| 5676 | } |
| 5677 | cpu = PyLong_AsLong(item); |
| 5678 | Py_DECREF(item); |
| 5679 | if (cpu < 0) { |
| 5680 | if (!PyErr_Occurred()) |
| 5681 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 5682 | goto error; |
| 5683 | } |
| 5684 | if (cpu > INT_MAX - 1) { |
| 5685 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 5686 | goto error; |
| 5687 | } |
| 5688 | if (cpu >= ncpus) { |
| 5689 | /* Grow CPU mask to fit the CPU number */ |
| 5690 | int newncpus = ncpus; |
| 5691 | cpu_set_t *newmask; |
| 5692 | size_t newsetsize; |
| 5693 | while (newncpus <= cpu) { |
| 5694 | if (newncpus > INT_MAX / 2) |
| 5695 | newncpus = cpu + 1; |
| 5696 | else |
| 5697 | newncpus = newncpus * 2; |
| 5698 | } |
| 5699 | newmask = CPU_ALLOC(newncpus); |
| 5700 | if (newmask == NULL) { |
| 5701 | PyErr_NoMemory(); |
| 5702 | goto error; |
| 5703 | } |
| 5704 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 5705 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5706 | memcpy(newmask, cpu_set, setsize); |
| 5707 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5708 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5709 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5710 | ncpus = newncpus; |
| 5711 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5712 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5713 | } |
| 5714 | Py_CLEAR(iterator); |
| 5715 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5716 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5717 | posix_error(); |
| 5718 | goto error; |
| 5719 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5720 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5721 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5722 | |
| 5723 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5724 | if (cpu_set) |
| 5725 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5726 | Py_XDECREF(iterator); |
| 5727 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5728 | } |
| 5729 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5730 | |
| 5731 | /*[clinic input] |
| 5732 | os.sched_getaffinity |
| 5733 | pid: pid_t |
| 5734 | / |
| 5735 | |
| 5736 | Return the affinity of the process identified by pid. |
| 5737 | |
| 5738 | The affinity is returned as a set of CPU identifiers. |
| 5739 | [clinic start generated code]*/ |
| 5740 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5741 | static PyObject * |
| 5742 | os_sched_getaffinity_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5743 | /*[clinic end generated code: output=b431a8f310e369e7 input=eaf161936874b8a1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5744 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5745 | int cpu, ncpus, count; |
| 5746 | size_t setsize; |
| 5747 | cpu_set_t *mask = NULL; |
| 5748 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5749 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5750 | ncpus = NCPUS_START; |
| 5751 | while (1) { |
| 5752 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5753 | mask = CPU_ALLOC(ncpus); |
| 5754 | if (mask == NULL) |
| 5755 | return PyErr_NoMemory(); |
| 5756 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 5757 | break; |
| 5758 | CPU_FREE(mask); |
| 5759 | if (errno != EINVAL) |
| 5760 | return posix_error(); |
| 5761 | if (ncpus > INT_MAX / 2) { |
| 5762 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 5763 | "a large enough CPU set"); |
| 5764 | return NULL; |
| 5765 | } |
| 5766 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5767 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5768 | |
| 5769 | res = PySet_New(NULL); |
| 5770 | if (res == NULL) |
| 5771 | goto error; |
| 5772 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 5773 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 5774 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 5775 | --count; |
| 5776 | if (cpu_num == NULL) |
| 5777 | goto error; |
| 5778 | if (PySet_Add(res, cpu_num)) { |
| 5779 | Py_DECREF(cpu_num); |
| 5780 | goto error; |
| 5781 | } |
| 5782 | Py_DECREF(cpu_num); |
| 5783 | } |
| 5784 | } |
| 5785 | CPU_FREE(mask); |
| 5786 | return res; |
| 5787 | |
| 5788 | error: |
| 5789 | if (mask) |
| 5790 | CPU_FREE(mask); |
| 5791 | Py_XDECREF(res); |
| 5792 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5793 | } |
| 5794 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5795 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5796 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5797 | #endif /* HAVE_SCHED_H */ |
| 5798 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5799 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5800 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5801 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5802 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5803 | #define DEV_PTY_FILE "/dev/ptc" |
| 5804 | #define HAVE_DEV_PTMX |
| 5805 | #else |
| 5806 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5807 | #endif |
| 5808 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5809 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5810 | #ifdef HAVE_PTY_H |
| 5811 | #include <pty.h> |
| 5812 | #else |
| 5813 | #ifdef HAVE_LIBUTIL_H |
| 5814 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5815 | #else |
| 5816 | #ifdef HAVE_UTIL_H |
| 5817 | #include <util.h> |
| 5818 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5819 | #endif /* HAVE_LIBUTIL_H */ |
| 5820 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5821 | #ifdef HAVE_STROPTS_H |
| 5822 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5823 | #endif |
| 5824 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5825 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5826 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5827 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5828 | /*[clinic input] |
| 5829 | os.openpty |
| 5830 | |
| 5831 | Open a pseudo-terminal. |
| 5832 | |
| 5833 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 5834 | for both the master and slave ends. |
| 5835 | [clinic start generated code]*/ |
| 5836 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5837 | static PyObject * |
| 5838 | os_openpty_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5839 | /*[clinic end generated code: output=358e571c1ba135ee input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5840 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5841 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5842 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5843 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5844 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5845 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5846 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5847 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5848 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5849 | #endif |
| 5850 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5851 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5852 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5853 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5854 | goto posix_error; |
| 5855 | |
| 5856 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5857 | goto error; |
| 5858 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 5859 | goto error; |
| 5860 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5861 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5862 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5863 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5864 | goto posix_error; |
| 5865 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5866 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5867 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5868 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5869 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 5870 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5871 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5872 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 5873 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5874 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5875 | goto posix_error; |
| 5876 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5877 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5878 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5879 | /* change permission of slave */ |
| 5880 | if (grantpt(master_fd) < 0) { |
| 5881 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5882 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5883 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5884 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5885 | /* unlock slave */ |
| 5886 | if (unlockpt(master_fd) < 0) { |
| 5887 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5888 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5889 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5890 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5891 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5892 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5893 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 5894 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5895 | goto posix_error; |
| 5896 | |
| 5897 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 5898 | if (slave_fd == -1) |
| 5899 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 5900 | |
| 5901 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 5902 | goto posix_error; |
| 5903 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5904 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5905 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 5906 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5907 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5908 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5909 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5910 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 5911 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5912 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5913 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5914 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5915 | posix_error: |
| 5916 | posix_error(); |
| 5917 | error: |
| 5918 | if (master_fd != -1) |
| 5919 | close(master_fd); |
| 5920 | if (slave_fd != -1) |
| 5921 | close(slave_fd); |
| 5922 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5923 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5924 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5925 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5926 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5927 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5928 | /*[clinic input] |
| 5929 | os.forkpty |
| 5930 | |
| 5931 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 5932 | |
| 5933 | Returns a tuple of (pid, master_fd). |
| 5934 | Like fork(), return pid of 0 to the child process, |
| 5935 | and pid of child to the parent process. |
| 5936 | To both, return fd of newly opened pseudo-terminal. |
| 5937 | [clinic start generated code]*/ |
| 5938 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5939 | static PyObject * |
| 5940 | os_forkpty_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5941 | /*[clinic end generated code: output=a11b8391dce3cb57 input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5942 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5943 | int master_fd = -1, result = 0; |
| 5944 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5945 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5946 | _PyImport_AcquireLock(); |
| 5947 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 5948 | if (pid == 0) { |
| 5949 | /* child: this clobbers and resets the import lock. */ |
| 5950 | PyOS_AfterFork(); |
| 5951 | } else { |
| 5952 | /* parent: release the import lock. */ |
| 5953 | result = _PyImport_ReleaseLock(); |
| 5954 | } |
| 5955 | if (pid == -1) |
| 5956 | return posix_error(); |
| 5957 | if (result < 0) { |
| 5958 | /* Don't clobber the OSError if the fork failed. */ |
| 5959 | PyErr_SetString(PyExc_RuntimeError, |
| 5960 | "not holding the import lock"); |
| 5961 | return NULL; |
| 5962 | } |
| 5963 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5964 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5965 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5966 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5967 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5968 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5969 | /*[clinic input] |
| 5970 | os.getegid |
| 5971 | |
| 5972 | Return the current process's effective group id. |
| 5973 | [clinic start generated code]*/ |
| 5974 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5975 | static PyObject * |
| 5976 | os_getegid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5977 | /*[clinic end generated code: output=90f433a8c0b1d919 input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5978 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5979 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5980 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5981 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5982 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5983 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5984 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5985 | /*[clinic input] |
| 5986 | os.geteuid |
| 5987 | |
| 5988 | Return the current process's effective user id. |
| 5989 | [clinic start generated code]*/ |
| 5990 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5991 | static PyObject * |
| 5992 | os_geteuid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5993 | /*[clinic end generated code: output=1a532c4a66874357 input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5994 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5995 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5996 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5997 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5998 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5999 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6000 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6001 | /*[clinic input] |
| 6002 | os.getgid |
| 6003 | |
| 6004 | Return the current process's group id. |
| 6005 | [clinic start generated code]*/ |
| 6006 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6007 | static PyObject * |
| 6008 | os_getgid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6009 | /*[clinic end generated code: output=91a22021b74ea46b input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6010 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6011 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6012 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6013 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6014 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6015 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6016 | /*[clinic input] |
| 6017 | os.getpid |
| 6018 | |
| 6019 | Return the current process id. |
| 6020 | [clinic start generated code]*/ |
| 6021 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6022 | static PyObject * |
| 6023 | os_getpid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6024 | /*[clinic end generated code: output=8fbf3a934ee09e62 input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6025 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6026 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6027 | } |
| 6028 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6029 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6030 | |
| 6031 | /* AC 3.5: funny apple logic below */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6032 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 6033 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 6034 | Returns a list of groups to which a user belongs.\n\n\ |
| 6035 | user: username to lookup\n\ |
| 6036 | group: base group id of the user"); |
| 6037 | |
| 6038 | static PyObject * |
| 6039 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 6040 | { |
| 6041 | #ifdef NGROUPS_MAX |
| 6042 | #define MAX_GROUPS NGROUPS_MAX |
| 6043 | #else |
| 6044 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6045 | #define MAX_GROUPS 64 |
| 6046 | #endif |
| 6047 | |
| 6048 | const char *user; |
| 6049 | int i, ngroups; |
| 6050 | PyObject *list; |
| 6051 | #ifdef __APPLE__ |
| 6052 | int *groups, basegid; |
| 6053 | #else |
| 6054 | gid_t *groups, basegid; |
| 6055 | #endif |
| 6056 | ngroups = MAX_GROUPS; |
| 6057 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6058 | #ifdef __APPLE__ |
| 6059 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6060 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6061 | #else |
| 6062 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 6063 | _Py_Gid_Converter, &basegid)) |
| 6064 | return NULL; |
| 6065 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6066 | |
| 6067 | #ifdef __APPLE__ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6068 | groups = PyMem_New(int, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6069 | #else |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6070 | groups = PyMem_New(gid_t, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6071 | #endif |
| 6072 | if (groups == NULL) |
| 6073 | return PyErr_NoMemory(); |
| 6074 | |
| 6075 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 6076 | PyMem_Del(groups); |
| 6077 | return posix_error(); |
| 6078 | } |
| 6079 | |
| 6080 | list = PyList_New(ngroups); |
| 6081 | if (list == NULL) { |
| 6082 | PyMem_Del(groups); |
| 6083 | return NULL; |
| 6084 | } |
| 6085 | |
| 6086 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6087 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6088 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6089 | #else |
| 6090 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 6091 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6092 | if (o == NULL) { |
| 6093 | Py_DECREF(list); |
| 6094 | PyMem_Del(groups); |
| 6095 | return NULL; |
| 6096 | } |
| 6097 | PyList_SET_ITEM(list, i, o); |
| 6098 | } |
| 6099 | |
| 6100 | PyMem_Del(groups); |
| 6101 | |
| 6102 | return list; |
| 6103 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6104 | #endif /* HAVE_GETGROUPLIST */ |
| 6105 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6106 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6107 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6108 | /*[clinic input] |
| 6109 | os.getgroups |
| 6110 | |
| 6111 | Return list of supplemental group IDs for the process. |
| 6112 | [clinic start generated code]*/ |
| 6113 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6114 | static PyObject * |
| 6115 | os_getgroups_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6116 | /*[clinic end generated code: output=6e7c4fd2db6d5c60 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6117 | { |
| 6118 | PyObject *result = NULL; |
| 6119 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6120 | #ifdef NGROUPS_MAX |
| 6121 | #define MAX_GROUPS NGROUPS_MAX |
| 6122 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6123 | /* 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] | 6124 | #define MAX_GROUPS 64 |
| 6125 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6126 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6127 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6128 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6129 | * This is a helper variable to store the intermediate result when |
| 6130 | * that happens. |
| 6131 | * |
| 6132 | * To keep the code readable the OSX behaviour is unconditional, |
| 6133 | * according to the POSIX spec this should be safe on all unix-y |
| 6134 | * systems. |
| 6135 | */ |
| 6136 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6137 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6138 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6139 | #ifdef __APPLE__ |
| 6140 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 6141 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 6142 | * always first call getgroups with length 0 to get the actual number |
| 6143 | * of groups. |
| 6144 | */ |
| 6145 | n = getgroups(0, NULL); |
| 6146 | if (n < 0) { |
| 6147 | return posix_error(); |
| 6148 | } else if (n <= MAX_GROUPS) { |
| 6149 | /* groups will fit in existing array */ |
| 6150 | alt_grouplist = grouplist; |
| 6151 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6152 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6153 | if (alt_grouplist == NULL) { |
| 6154 | errno = EINVAL; |
| 6155 | return posix_error(); |
| 6156 | } |
| 6157 | } |
| 6158 | |
| 6159 | n = getgroups(n, alt_grouplist); |
| 6160 | if (n == -1) { |
| 6161 | if (alt_grouplist != grouplist) { |
| 6162 | PyMem_Free(alt_grouplist); |
| 6163 | } |
| 6164 | return posix_error(); |
| 6165 | } |
| 6166 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6167 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6168 | if (n < 0) { |
| 6169 | if (errno == EINVAL) { |
| 6170 | n = getgroups(0, NULL); |
| 6171 | if (n == -1) { |
| 6172 | return posix_error(); |
| 6173 | } |
| 6174 | if (n == 0) { |
| 6175 | /* Avoid malloc(0) */ |
| 6176 | alt_grouplist = grouplist; |
| 6177 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6178 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6179 | if (alt_grouplist == NULL) { |
| 6180 | errno = EINVAL; |
| 6181 | return posix_error(); |
| 6182 | } |
| 6183 | n = getgroups(n, alt_grouplist); |
| 6184 | if (n == -1) { |
| 6185 | PyMem_Free(alt_grouplist); |
| 6186 | return posix_error(); |
| 6187 | } |
| 6188 | } |
| 6189 | } else { |
| 6190 | return posix_error(); |
| 6191 | } |
| 6192 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6193 | #endif |
| 6194 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6195 | result = PyList_New(n); |
| 6196 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6197 | int i; |
| 6198 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6199 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6200 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6201 | Py_DECREF(result); |
| 6202 | result = NULL; |
| 6203 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6204 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6205 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6206 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6207 | } |
| 6208 | |
| 6209 | if (alt_grouplist != grouplist) { |
| 6210 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6211 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6212 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6213 | return result; |
| 6214 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6215 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6216 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6217 | #ifdef HAVE_INITGROUPS |
| 6218 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 6219 | "initgroups(username, gid) -> None\n\n\ |
| 6220 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 6221 | the groups of which the specified username is a member, plus the specified\n\ |
| 6222 | group id."); |
| 6223 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6224 | /* AC 3.5: funny apple logic */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6225 | static PyObject * |
| 6226 | posix_initgroups(PyObject *self, PyObject *args) |
| 6227 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6228 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6229 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6230 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6231 | #ifdef __APPLE__ |
| 6232 | int gid; |
| 6233 | #else |
| 6234 | gid_t gid; |
| 6235 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6236 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6237 | #ifdef __APPLE__ |
| 6238 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 6239 | PyUnicode_FSConverter, &oname, |
| 6240 | &gid)) |
| 6241 | #else |
| 6242 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 6243 | PyUnicode_FSConverter, &oname, |
| 6244 | _Py_Gid_Converter, &gid)) |
| 6245 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6246 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6247 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6248 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6249 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6250 | Py_DECREF(oname); |
| 6251 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6252 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6253 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6254 | Py_INCREF(Py_None); |
| 6255 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6256 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6257 | #endif /* HAVE_INITGROUPS */ |
| 6258 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6259 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6260 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6261 | /*[clinic input] |
| 6262 | os.getpgid |
| 6263 | |
| 6264 | pid: pid_t |
| 6265 | |
| 6266 | Call the system call getpgid(), and return the result. |
| 6267 | [clinic start generated code]*/ |
| 6268 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6269 | static PyObject * |
| 6270 | os_getpgid_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6271 | /*[clinic end generated code: output=70e713b4d54b7c61 input=39d710ae3baaf1c7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6272 | { |
| 6273 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6274 | if (pgid < 0) |
| 6275 | return posix_error(); |
| 6276 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6277 | } |
| 6278 | #endif /* HAVE_GETPGID */ |
| 6279 | |
| 6280 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6281 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6282 | /*[clinic input] |
| 6283 | os.getpgrp |
| 6284 | |
| 6285 | Return the current process group id. |
| 6286 | [clinic start generated code]*/ |
| 6287 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6288 | static PyObject * |
| 6289 | os_getpgrp_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6290 | /*[clinic end generated code: output=cf3403585846811f input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6291 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6292 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6293 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6294 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6295 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6296 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6297 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6298 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6299 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6300 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6301 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6302 | /*[clinic input] |
| 6303 | os.setpgrp |
| 6304 | |
| 6305 | Make the current process the leader of its process group. |
| 6306 | [clinic start generated code]*/ |
| 6307 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6308 | static PyObject * |
| 6309 | os_setpgrp_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6310 | /*[clinic end generated code: output=59650f55a963d7ac input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6311 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6312 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6313 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6314 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6315 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6316 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6317 | return posix_error(); |
| 6318 | Py_INCREF(Py_None); |
| 6319 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6320 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6321 | #endif /* HAVE_SETPGRP */ |
| 6322 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6323 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6324 | |
| 6325 | #ifdef MS_WINDOWS |
| 6326 | #include <tlhelp32.h> |
| 6327 | |
| 6328 | static PyObject* |
| 6329 | win32_getppid() |
| 6330 | { |
| 6331 | HANDLE snapshot; |
| 6332 | pid_t mypid; |
| 6333 | PyObject* result = NULL; |
| 6334 | BOOL have_record; |
| 6335 | PROCESSENTRY32 pe; |
| 6336 | |
| 6337 | mypid = getpid(); /* This function never fails */ |
| 6338 | |
| 6339 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 6340 | if (snapshot == INVALID_HANDLE_VALUE) |
| 6341 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 6342 | |
| 6343 | pe.dwSize = sizeof(pe); |
| 6344 | have_record = Process32First(snapshot, &pe); |
| 6345 | while (have_record) { |
| 6346 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 6347 | /* We could cache the ulong value in a static variable. */ |
| 6348 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 6349 | break; |
| 6350 | } |
| 6351 | |
| 6352 | have_record = Process32Next(snapshot, &pe); |
| 6353 | } |
| 6354 | |
| 6355 | /* If our loop exits and our pid was not found (result will be NULL) |
| 6356 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 6357 | * error anyway, so let's raise it. */ |
| 6358 | if (!result) |
| 6359 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6360 | |
| 6361 | CloseHandle(snapshot); |
| 6362 | |
| 6363 | return result; |
| 6364 | } |
| 6365 | #endif /*MS_WINDOWS*/ |
| 6366 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6367 | |
| 6368 | /*[clinic input] |
| 6369 | os.getppid |
| 6370 | |
| 6371 | Return the parent's process id. |
| 6372 | |
| 6373 | If the parent process has already exited, Windows machines will still |
| 6374 | return its id; others systems will return the id of the 'init' process (1). |
| 6375 | [clinic start generated code]*/ |
| 6376 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6377 | static PyObject * |
| 6378 | os_getppid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6379 | /*[clinic end generated code: output=4e49c8e7a8738cd2 input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6380 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6381 | #ifdef MS_WINDOWS |
| 6382 | return win32_getppid(); |
| 6383 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6384 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6385 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6386 | } |
| 6387 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6388 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6389 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6390 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6391 | /*[clinic input] |
| 6392 | os.getlogin |
| 6393 | |
| 6394 | Return the actual login name. |
| 6395 | [clinic start generated code]*/ |
| 6396 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6397 | static PyObject * |
| 6398 | os_getlogin_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6399 | /*[clinic end generated code: output=037ebdb3e4b5dac1 input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6400 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6401 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6402 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6403 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6404 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6405 | |
| 6406 | if (GetUserNameW(user_name, &num_chars)) { |
| 6407 | /* num_chars is the number of unicode chars plus null terminator */ |
| 6408 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6409 | } |
| 6410 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6411 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6412 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6413 | char *name; |
| 6414 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6415 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6416 | errno = 0; |
| 6417 | name = getlogin(); |
| 6418 | if (name == NULL) { |
| 6419 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6420 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6421 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6422 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6423 | } |
| 6424 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6425 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6426 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6427 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6428 | return result; |
| 6429 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6430 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6431 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6432 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6433 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6434 | /*[clinic input] |
| 6435 | os.getuid |
| 6436 | |
| 6437 | Return the current process's user id. |
| 6438 | [clinic start generated code]*/ |
| 6439 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6440 | static PyObject * |
| 6441 | os_getuid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6442 | /*[clinic end generated code: output=03a8b894cefb3fa5 input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6443 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6444 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6445 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6446 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6447 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6448 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6449 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6450 | #define HAVE_KILL |
| 6451 | #endif /* MS_WINDOWS */ |
| 6452 | |
| 6453 | #ifdef HAVE_KILL |
| 6454 | /*[clinic input] |
| 6455 | os.kill |
| 6456 | |
| 6457 | pid: pid_t |
| 6458 | signal: Py_ssize_t |
| 6459 | / |
| 6460 | |
| 6461 | Kill a process with a signal. |
| 6462 | [clinic start generated code]*/ |
| 6463 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6464 | static PyObject * |
| 6465 | os_kill_impl(PyModuleDef *module, pid_t pid, Py_ssize_t signal) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6466 | /*[clinic end generated code: output=74f907dd00a83c26 input=61a36b86ca275ab9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6467 | #ifndef MS_WINDOWS |
| 6468 | { |
| 6469 | if (kill(pid, (int)signal) == -1) |
| 6470 | return posix_error(); |
| 6471 | Py_RETURN_NONE; |
| 6472 | } |
| 6473 | #else /* !MS_WINDOWS */ |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6474 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 6475 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6476 | DWORD sig = (DWORD)signal; |
| 6477 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6478 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6479 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6480 | /* Console processes which share a common console can be sent CTRL+C or |
| 6481 | CTRL+BREAK events, provided they handle said events. */ |
| 6482 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6483 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6484 | err = GetLastError(); |
| 6485 | PyErr_SetFromWindowsErr(err); |
| 6486 | } |
| 6487 | else |
| 6488 | Py_RETURN_NONE; |
| 6489 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6490 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6491 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 6492 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6493 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6494 | if (handle == NULL) { |
| 6495 | err = GetLastError(); |
| 6496 | return PyErr_SetFromWindowsErr(err); |
| 6497 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6498 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6499 | if (TerminateProcess(handle, sig) == 0) { |
| 6500 | err = GetLastError(); |
| 6501 | result = PyErr_SetFromWindowsErr(err); |
| 6502 | } else { |
| 6503 | Py_INCREF(Py_None); |
| 6504 | result = Py_None; |
| 6505 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6506 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6507 | CloseHandle(handle); |
| 6508 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6509 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6510 | #endif /* !MS_WINDOWS */ |
| 6511 | #endif /* HAVE_KILL */ |
| 6512 | |
| 6513 | |
| 6514 | #ifdef HAVE_KILLPG |
| 6515 | /*[clinic input] |
| 6516 | os.killpg |
| 6517 | |
| 6518 | pgid: pid_t |
| 6519 | signal: int |
| 6520 | / |
| 6521 | |
| 6522 | Kill a process group with a signal. |
| 6523 | [clinic start generated code]*/ |
| 6524 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6525 | static PyObject * |
| 6526 | os_killpg_impl(PyModuleDef *module, pid_t pgid, int signal) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6527 | /*[clinic end generated code: output=3434a766ef945f93 input=38b5449eb8faec19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6528 | { |
| 6529 | /* XXX some man pages make the `pgid` parameter an int, others |
| 6530 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 6531 | take the same type. Moreover, pid_t is always at least as wide as |
| 6532 | int (else compilation of this module fails), which is safe. */ |
| 6533 | if (killpg(pgid, signal) == -1) |
| 6534 | return posix_error(); |
| 6535 | Py_RETURN_NONE; |
| 6536 | } |
| 6537 | #endif /* HAVE_KILLPG */ |
| 6538 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6539 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6540 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6541 | #ifdef HAVE_SYS_LOCK_H |
| 6542 | #include <sys/lock.h> |
| 6543 | #endif |
| 6544 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6545 | /*[clinic input] |
| 6546 | os.plock |
| 6547 | op: int |
| 6548 | / |
| 6549 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6550 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6551 | [clinic start generated code]*/ |
| 6552 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6553 | static PyObject * |
| 6554 | os_plock_impl(PyModuleDef *module, int op) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6555 | /*[clinic end generated code: output=5cb851f81b914984 input=e6e5e348e1525f60]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6556 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6557 | if (plock(op) == -1) |
| 6558 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6559 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6560 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6561 | #endif /* HAVE_PLOCK */ |
| 6562 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6563 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6564 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6565 | /*[clinic input] |
| 6566 | os.setuid |
| 6567 | |
| 6568 | uid: uid_t |
| 6569 | / |
| 6570 | |
| 6571 | Set the current process's user id. |
| 6572 | [clinic start generated code]*/ |
| 6573 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6574 | static PyObject * |
| 6575 | os_setuid_impl(PyModuleDef *module, uid_t uid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6576 | /*[clinic end generated code: output=941ea9a8d1e5d565 input=c921a3285aa22256]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6577 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6578 | if (setuid(uid) < 0) |
| 6579 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6580 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6581 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6582 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6583 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6584 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6585 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6586 | /*[clinic input] |
| 6587 | os.seteuid |
| 6588 | |
| 6589 | euid: uid_t |
| 6590 | / |
| 6591 | |
| 6592 | Set the current process's effective user id. |
| 6593 | [clinic start generated code]*/ |
| 6594 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6595 | static PyObject * |
| 6596 | os_seteuid_impl(PyModuleDef *module, uid_t euid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6597 | /*[clinic end generated code: output=66f4f6823a648d6d input=ba93d927e4781aa9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6598 | { |
| 6599 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6600 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6601 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6602 | } |
| 6603 | #endif /* HAVE_SETEUID */ |
| 6604 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6605 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6606 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6607 | /*[clinic input] |
| 6608 | os.setegid |
| 6609 | |
| 6610 | egid: gid_t |
| 6611 | / |
| 6612 | |
| 6613 | Set the current process's effective group id. |
| 6614 | [clinic start generated code]*/ |
| 6615 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6616 | static PyObject * |
| 6617 | os_setegid_impl(PyModuleDef *module, gid_t egid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6618 | /*[clinic end generated code: output=ca094a69a081a60f input=4080526d0ccd6ce3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6619 | { |
| 6620 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6621 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6622 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6623 | } |
| 6624 | #endif /* HAVE_SETEGID */ |
| 6625 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6626 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6627 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6628 | /*[clinic input] |
| 6629 | os.setreuid |
| 6630 | |
| 6631 | ruid: uid_t |
| 6632 | euid: uid_t |
| 6633 | / |
| 6634 | |
| 6635 | Set the current process's real and effective user ids. |
| 6636 | [clinic start generated code]*/ |
| 6637 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6638 | static PyObject * |
| 6639 | os_setreuid_impl(PyModuleDef *module, uid_t ruid, uid_t euid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6640 | /*[clinic end generated code: output=b2938c3e73d27ec7 input=0ca8978de663880c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6641 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6642 | if (setreuid(ruid, euid) < 0) { |
| 6643 | return posix_error(); |
| 6644 | } else { |
| 6645 | Py_INCREF(Py_None); |
| 6646 | return Py_None; |
| 6647 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6648 | } |
| 6649 | #endif /* HAVE_SETREUID */ |
| 6650 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6651 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6652 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6653 | /*[clinic input] |
| 6654 | os.setregid |
| 6655 | |
| 6656 | rgid: gid_t |
| 6657 | egid: gid_t |
| 6658 | / |
| 6659 | |
| 6660 | Set the current process's real and effective group ids. |
| 6661 | [clinic start generated code]*/ |
| 6662 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6663 | static PyObject * |
| 6664 | os_setregid_impl(PyModuleDef *module, gid_t rgid, gid_t egid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6665 | /*[clinic end generated code: output=db18f1839ababe3d input=c59499f72846db78]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6666 | { |
| 6667 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6668 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6669 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6670 | } |
| 6671 | #endif /* HAVE_SETREGID */ |
| 6672 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6673 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6674 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6675 | /*[clinic input] |
| 6676 | os.setgid |
| 6677 | gid: gid_t |
| 6678 | / |
| 6679 | |
| 6680 | Set the current process's group id. |
| 6681 | [clinic start generated code]*/ |
| 6682 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6683 | static PyObject * |
| 6684 | os_setgid_impl(PyModuleDef *module, gid_t gid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6685 | /*[clinic end generated code: output=756cb42c6abd9d87 input=27d30c4059045dc6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6686 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6687 | if (setgid(gid) < 0) |
| 6688 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6689 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6690 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6691 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6692 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6693 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6694 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6695 | /*[clinic input] |
| 6696 | os.setgroups |
| 6697 | |
| 6698 | groups: object |
| 6699 | / |
| 6700 | |
| 6701 | Set the groups of the current process to list. |
| 6702 | [clinic start generated code]*/ |
| 6703 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6704 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6705 | os_setgroups(PyModuleDef *module, PyObject *groups) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6706 | /*[clinic end generated code: output=7945c2e3cc817c58 input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6707 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6708 | int i, len; |
| 6709 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6710 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6711 | if (!PySequence_Check(groups)) { |
| 6712 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6713 | return NULL; |
| 6714 | } |
| 6715 | len = PySequence_Size(groups); |
| 6716 | if (len > MAX_GROUPS) { |
| 6717 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6718 | return NULL; |
| 6719 | } |
| 6720 | for(i = 0; i < len; i++) { |
| 6721 | PyObject *elem; |
| 6722 | elem = PySequence_GetItem(groups, i); |
| 6723 | if (!elem) |
| 6724 | return NULL; |
| 6725 | if (!PyLong_Check(elem)) { |
| 6726 | PyErr_SetString(PyExc_TypeError, |
| 6727 | "groups must be integers"); |
| 6728 | Py_DECREF(elem); |
| 6729 | return NULL; |
| 6730 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6731 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6732 | Py_DECREF(elem); |
| 6733 | return NULL; |
| 6734 | } |
| 6735 | } |
| 6736 | Py_DECREF(elem); |
| 6737 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6738 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6739 | if (setgroups(len, grouplist) < 0) |
| 6740 | return posix_error(); |
| 6741 | Py_INCREF(Py_None); |
| 6742 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6743 | } |
| 6744 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6745 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6746 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6747 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6748 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6749 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6750 | PyObject *result; |
| 6751 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6752 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6753 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6754 | if (pid == -1) |
| 6755 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6756 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6757 | if (struct_rusage == NULL) { |
| 6758 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6759 | if (m == NULL) |
| 6760 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6761 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6762 | Py_DECREF(m); |
| 6763 | if (struct_rusage == NULL) |
| 6764 | return NULL; |
| 6765 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6766 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6767 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6768 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6769 | if (!result) |
| 6770 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6771 | |
| 6772 | #ifndef doubletime |
| 6773 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6774 | #endif |
| 6775 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6776 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6777 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6778 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6779 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6780 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6781 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6782 | SET_INT(result, 2, ru->ru_maxrss); |
| 6783 | SET_INT(result, 3, ru->ru_ixrss); |
| 6784 | SET_INT(result, 4, ru->ru_idrss); |
| 6785 | SET_INT(result, 5, ru->ru_isrss); |
| 6786 | SET_INT(result, 6, ru->ru_minflt); |
| 6787 | SET_INT(result, 7, ru->ru_majflt); |
| 6788 | SET_INT(result, 8, ru->ru_nswap); |
| 6789 | SET_INT(result, 9, ru->ru_inblock); |
| 6790 | SET_INT(result, 10, ru->ru_oublock); |
| 6791 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6792 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6793 | SET_INT(result, 13, ru->ru_nsignals); |
| 6794 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6795 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6796 | #undef SET_INT |
| 6797 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6798 | if (PyErr_Occurred()) { |
| 6799 | Py_DECREF(result); |
| 6800 | return NULL; |
| 6801 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6802 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6803 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6804 | } |
| 6805 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6806 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6807 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6808 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6809 | /*[clinic input] |
| 6810 | os.wait3 |
| 6811 | |
| 6812 | options: int |
| 6813 | Wait for completion of a child process. |
| 6814 | |
| 6815 | Returns a tuple of information about the child process: |
| 6816 | (pid, status, rusage) |
| 6817 | [clinic start generated code]*/ |
| 6818 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6819 | static PyObject * |
| 6820 | os_wait3_impl(PyModuleDef *module, int options) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6821 | /*[clinic end generated code: output=e18af4924dc54945 input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6822 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6823 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6824 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6825 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6826 | WAIT_TYPE status; |
| 6827 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6828 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6829 | do { |
| 6830 | Py_BEGIN_ALLOW_THREADS |
| 6831 | pid = wait3(&status, options, &ru); |
| 6832 | Py_END_ALLOW_THREADS |
| 6833 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6834 | if (pid < 0) |
| 6835 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6836 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6837 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6838 | } |
| 6839 | #endif /* HAVE_WAIT3 */ |
| 6840 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6841 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6842 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6843 | /*[clinic input] |
| 6844 | |
| 6845 | os.wait4 |
| 6846 | |
| 6847 | pid: pid_t |
| 6848 | options: int |
| 6849 | |
| 6850 | Wait for completion of a specific child process. |
| 6851 | |
| 6852 | Returns a tuple of information about the child process: |
| 6853 | (pid, status, rusage) |
| 6854 | [clinic start generated code]*/ |
| 6855 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6856 | static PyObject * |
| 6857 | os_wait4_impl(PyModuleDef *module, pid_t pid, int options) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6858 | /*[clinic end generated code: output=714f19e6ff01e099 input=d11deed0750600ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6859 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6860 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6861 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6862 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6863 | WAIT_TYPE status; |
| 6864 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6865 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6866 | do { |
| 6867 | Py_BEGIN_ALLOW_THREADS |
| 6868 | res = wait4(pid, &status, options, &ru); |
| 6869 | Py_END_ALLOW_THREADS |
| 6870 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6871 | if (res < 0) |
| 6872 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6873 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6874 | return wait_helper(res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6875 | } |
| 6876 | #endif /* HAVE_WAIT4 */ |
| 6877 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6878 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6879 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6880 | /*[clinic input] |
| 6881 | os.waitid |
| 6882 | |
| 6883 | idtype: idtype_t |
| 6884 | Must be one of be P_PID, P_PGID or P_ALL. |
| 6885 | id: id_t |
| 6886 | The id to wait on. |
| 6887 | options: int |
| 6888 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 6889 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 6890 | / |
| 6891 | |
| 6892 | Returns the result of waiting for a process or processes. |
| 6893 | |
| 6894 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 6895 | no children in a waitable state. |
| 6896 | [clinic start generated code]*/ |
| 6897 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6898 | static PyObject * |
| 6899 | os_waitid_impl(PyModuleDef *module, idtype_t idtype, id_t id, int options) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6900 | /*[clinic end generated code: output=5c0192750e22fa2e input=d8e7f76e052b7920]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6901 | { |
| 6902 | PyObject *result; |
| 6903 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6904 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6905 | siginfo_t si; |
| 6906 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6907 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6908 | do { |
| 6909 | Py_BEGIN_ALLOW_THREADS |
| 6910 | res = waitid(idtype, id, &si, options); |
| 6911 | Py_END_ALLOW_THREADS |
| 6912 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6913 | if (res < 0) |
| 6914 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6915 | |
| 6916 | if (si.si_pid == 0) |
| 6917 | Py_RETURN_NONE; |
| 6918 | |
| 6919 | result = PyStructSequence_New(&WaitidResultType); |
| 6920 | if (!result) |
| 6921 | return NULL; |
| 6922 | |
| 6923 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6924 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6925 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6926 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6927 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6928 | if (PyErr_Occurred()) { |
| 6929 | Py_DECREF(result); |
| 6930 | return NULL; |
| 6931 | } |
| 6932 | |
| 6933 | return result; |
| 6934 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6935 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6936 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6937 | |
| 6938 | #if defined(HAVE_WAITPID) |
| 6939 | /*[clinic input] |
| 6940 | os.waitpid |
| 6941 | pid: pid_t |
| 6942 | options: int |
| 6943 | / |
| 6944 | |
| 6945 | Wait for completion of a given child process. |
| 6946 | |
| 6947 | Returns a tuple of information regarding the child process: |
| 6948 | (pid, status) |
| 6949 | |
| 6950 | The options argument is ignored on Windows. |
| 6951 | [clinic start generated code]*/ |
| 6952 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6953 | static PyObject * |
| 6954 | os_waitpid_impl(PyModuleDef *module, pid_t pid, int options) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6955 | /*[clinic end generated code: output=5e3593353d54b15b input=0bf1666b8758fda3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6956 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6957 | pid_t res; |
| 6958 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6959 | WAIT_TYPE status; |
| 6960 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6961 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6962 | do { |
| 6963 | Py_BEGIN_ALLOW_THREADS |
| 6964 | res = waitpid(pid, &status, options); |
| 6965 | Py_END_ALLOW_THREADS |
| 6966 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6967 | if (res < 0) |
| 6968 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6969 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6970 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6971 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6972 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6973 | /* MS C has a variant of waitpid() that's usable for most purposes. */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6974 | /*[clinic input] |
| 6975 | os.waitpid |
| 6976 | pid: Py_intptr_t |
| 6977 | options: int |
| 6978 | / |
| 6979 | |
| 6980 | Wait for completion of a given process. |
| 6981 | |
| 6982 | Returns a tuple of information regarding the process: |
| 6983 | (pid, status << 8) |
| 6984 | |
| 6985 | The options argument is ignored on Windows. |
| 6986 | [clinic start generated code]*/ |
| 6987 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6988 | static PyObject * |
| 6989 | os_waitpid_impl(PyModuleDef *module, Py_intptr_t pid, int options) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6990 | /*[clinic end generated code: output=fc1d520db019625f input=444c8f51cca5b862]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6991 | { |
| 6992 | int status; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6993 | Py_intptr_t res; |
| 6994 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6995 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 6996 | do { |
| 6997 | Py_BEGIN_ALLOW_THREADS |
| 6998 | res = _cwait(&status, pid, options); |
| 6999 | Py_END_ALLOW_THREADS |
| 7000 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7001 | if (res != 0) |
| 7002 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7003 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7004 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7005 | return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7006 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7007 | #endif |
| 7008 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7009 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7010 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7011 | /*[clinic input] |
| 7012 | os.wait |
| 7013 | |
| 7014 | Wait for completion of a child process. |
| 7015 | |
| 7016 | Returns a tuple of information about the child process: |
| 7017 | (pid, status) |
| 7018 | [clinic start generated code]*/ |
| 7019 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7020 | static PyObject * |
| 7021 | os_wait_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7022 | /*[clinic end generated code: output=4a7f4978393e0654 input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7023 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7024 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7025 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7026 | WAIT_TYPE status; |
| 7027 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7028 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7029 | do { |
| 7030 | Py_BEGIN_ALLOW_THREADS |
| 7031 | pid = wait(&status); |
| 7032 | Py_END_ALLOW_THREADS |
| 7033 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7034 | if (pid < 0) |
| 7035 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7036 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7037 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7038 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7039 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7040 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7041 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7042 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
| 7043 | PyDoc_STRVAR(readlink__doc__, |
| 7044 | "readlink(path, *, dir_fd=None) -> path\n\n\ |
| 7045 | Return a string representing the path to which the symbolic link points.\n\ |
| 7046 | \n\ |
| 7047 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7048 | and path should be relative; path will then be relative to that directory.\n\ |
| 7049 | dir_fd may not be implemented on your platform.\n\ |
| 7050 | If it is unavailable, using it will raise a NotImplementedError."); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7051 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7052 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7053 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7054 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7055 | /* AC 3.5: merge win32 and not together */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7056 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7057 | posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7058 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7059 | path_t path; |
| 7060 | int dir_fd = DEFAULT_DIR_FD; |
| 7061 | char buffer[MAXPATHLEN]; |
| 7062 | ssize_t length; |
| 7063 | PyObject *return_value = NULL; |
| 7064 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7065 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7066 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7067 | path.function_name = "readlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7068 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords, |
| 7069 | path_converter, &path, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7070 | READLINKAT_DIR_FD_CONVERTER, &dir_fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7071 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7072 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7073 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7074 | #ifdef HAVE_READLINKAT |
| 7075 | if (dir_fd != DEFAULT_DIR_FD) |
| 7076 | length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer)); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 7077 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7078 | #endif |
| 7079 | length = readlink(path.narrow, buffer, sizeof(buffer)); |
| 7080 | Py_END_ALLOW_THREADS |
| 7081 | |
| 7082 | if (length < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7083 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7084 | goto exit; |
| 7085 | } |
| 7086 | |
| 7087 | if (PyUnicode_Check(path.object)) |
| 7088 | return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 7089 | else |
| 7090 | return_value = PyBytes_FromStringAndSize(buffer, length); |
| 7091 | exit: |
| 7092 | path_cleanup(&path); |
| 7093 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7094 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7095 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7096 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7097 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7098 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 7099 | |
| 7100 | static PyObject * |
| 7101 | win_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
| 7102 | { |
| 7103 | wchar_t *path; |
| 7104 | DWORD n_bytes_returned; |
| 7105 | DWORD io_result; |
| 7106 | PyObject *po, *result; |
| 7107 | int dir_fd; |
| 7108 | HANDLE reparse_point_handle; |
| 7109 | |
| 7110 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 7111 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 7112 | wchar_t *print_name; |
| 7113 | |
| 7114 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 7115 | |
| 7116 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords, |
| 7117 | &po, |
| 7118 | dir_fd_unavailable, &dir_fd |
| 7119 | )) |
| 7120 | return NULL; |
| 7121 | |
| 7122 | path = PyUnicode_AsUnicode(po); |
| 7123 | if (path == NULL) |
| 7124 | return NULL; |
| 7125 | |
| 7126 | /* First get a handle to the reparse point */ |
| 7127 | Py_BEGIN_ALLOW_THREADS |
| 7128 | reparse_point_handle = CreateFileW( |
| 7129 | path, |
| 7130 | 0, |
| 7131 | 0, |
| 7132 | 0, |
| 7133 | OPEN_EXISTING, |
| 7134 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 7135 | 0); |
| 7136 | Py_END_ALLOW_THREADS |
| 7137 | |
| 7138 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
| 7139 | return win32_error_object("readlink", po); |
| 7140 | |
| 7141 | Py_BEGIN_ALLOW_THREADS |
| 7142 | /* New call DeviceIoControl to read the reparse point */ |
| 7143 | io_result = DeviceIoControl( |
| 7144 | reparse_point_handle, |
| 7145 | FSCTL_GET_REPARSE_POINT, |
| 7146 | 0, 0, /* in buffer */ |
| 7147 | target_buffer, sizeof(target_buffer), |
| 7148 | &n_bytes_returned, |
| 7149 | 0 /* we're not using OVERLAPPED_IO */ |
| 7150 | ); |
| 7151 | CloseHandle(reparse_point_handle); |
| 7152 | Py_END_ALLOW_THREADS |
| 7153 | |
| 7154 | if (io_result==0) |
| 7155 | return win32_error_object("readlink", po); |
| 7156 | |
| 7157 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 7158 | { |
| 7159 | PyErr_SetString(PyExc_ValueError, |
| 7160 | "not a symbolic link"); |
| 7161 | return NULL; |
| 7162 | } |
| 7163 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7164 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 7165 | |
| 7166 | result = PyUnicode_FromWideChar(print_name, |
| 7167 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
| 7168 | return result; |
| 7169 | } |
| 7170 | |
| 7171 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 7172 | |
| 7173 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7174 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7175 | #ifdef HAVE_SYMLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7176 | |
| 7177 | #if defined(MS_WINDOWS) |
| 7178 | |
| 7179 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 7180 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD) = NULL; |
| 7181 | static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPSTR, LPSTR, DWORD) = NULL; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7182 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7183 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7184 | check_CreateSymbolicLink(void) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7185 | { |
| 7186 | HINSTANCE hKernel32; |
| 7187 | /* only recheck */ |
| 7188 | if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA) |
| 7189 | return 1; |
| 7190 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 7191 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 7192 | "CreateSymbolicLinkW"); |
| 7193 | *(FARPROC*)&Py_CreateSymbolicLinkA = GetProcAddress(hKernel32, |
| 7194 | "CreateSymbolicLinkA"); |
| 7195 | return (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA); |
| 7196 | } |
| 7197 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7198 | /* Remove the last portion of the path */ |
| 7199 | static void |
| 7200 | _dirnameW(WCHAR *path) |
| 7201 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7202 | WCHAR *ptr; |
| 7203 | |
| 7204 | /* walk the path from the end until a backslash is encountered */ |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7205 | for(ptr = path + wcslen(path); ptr != path; ptr--) { |
Victor Stinner | 072318b | 2013-06-05 02:07:46 +0200 | [diff] [blame] | 7206 | if (*ptr == L'\\' || *ptr == L'/') |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7207 | break; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7208 | } |
| 7209 | *ptr = 0; |
| 7210 | } |
| 7211 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7212 | /* Remove the last portion of the path */ |
| 7213 | static void |
| 7214 | _dirnameA(char *path) |
| 7215 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7216 | char *ptr; |
| 7217 | |
| 7218 | /* walk the path from the end until a backslash is encountered */ |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7219 | for(ptr = path + strlen(path); ptr != path; ptr--) { |
| 7220 | if (*ptr == '\\' || *ptr == '/') |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7221 | break; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7222 | } |
| 7223 | *ptr = 0; |
| 7224 | } |
| 7225 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7226 | /* Is this path absolute? */ |
| 7227 | static int |
| 7228 | _is_absW(const WCHAR *path) |
| 7229 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7230 | return path[0] == L'\\' || path[0] == L'/' || path[1] == L':'; |
| 7231 | |
| 7232 | } |
| 7233 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7234 | /* Is this path absolute? */ |
| 7235 | static int |
| 7236 | _is_absA(const char *path) |
| 7237 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7238 | return path[0] == '\\' || path[0] == '/' || path[1] == ':'; |
| 7239 | |
| 7240 | } |
| 7241 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7242 | /* join root and rest with a backslash */ |
| 7243 | static void |
| 7244 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 7245 | { |
Victor Stinner | e7e7eba | 2013-06-05 00:35:54 +0200 | [diff] [blame] | 7246 | size_t root_len; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7247 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7248 | if (_is_absW(rest)) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7249 | wcscpy(dest_path, rest); |
| 7250 | return; |
| 7251 | } |
| 7252 | |
| 7253 | root_len = wcslen(root); |
| 7254 | |
| 7255 | wcscpy(dest_path, root); |
| 7256 | if(root_len) { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7257 | dest_path[root_len] = L'\\'; |
| 7258 | root_len++; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7259 | } |
| 7260 | wcscpy(dest_path+root_len, rest); |
| 7261 | } |
| 7262 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7263 | /* join root and rest with a backslash */ |
| 7264 | static void |
| 7265 | _joinA(char *dest_path, const char *root, const char *rest) |
| 7266 | { |
Victor Stinner | e7e7eba | 2013-06-05 00:35:54 +0200 | [diff] [blame] | 7267 | size_t root_len; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7268 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7269 | if (_is_absA(rest)) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7270 | strcpy(dest_path, rest); |
| 7271 | return; |
| 7272 | } |
| 7273 | |
| 7274 | root_len = strlen(root); |
| 7275 | |
| 7276 | strcpy(dest_path, root); |
| 7277 | if(root_len) { |
| 7278 | dest_path[root_len] = '\\'; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7279 | root_len++; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7280 | } |
| 7281 | strcpy(dest_path+root_len, rest); |
| 7282 | } |
| 7283 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7284 | /* Return True if the path at src relative to dest is a directory */ |
| 7285 | static int |
| 7286 | _check_dirW(WCHAR *src, WCHAR *dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7287 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7288 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7289 | WCHAR dest_parent[MAX_PATH]; |
| 7290 | WCHAR src_resolved[MAX_PATH] = L""; |
| 7291 | |
| 7292 | /* dest_parent = os.path.dirname(dest) */ |
| 7293 | wcscpy(dest_parent, dest); |
| 7294 | _dirnameW(dest_parent); |
| 7295 | /* src_resolved = os.path.join(dest_parent, src) */ |
| 7296 | _joinW(src_resolved, dest_parent, src); |
| 7297 | return ( |
| 7298 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 7299 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7300 | ); |
| 7301 | } |
| 7302 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 7303 | /* Return True if the path at src relative to dest is a directory */ |
| 7304 | static int |
| 7305 | _check_dirA(char *src, char *dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7306 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7307 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 7308 | char dest_parent[MAX_PATH]; |
| 7309 | char src_resolved[MAX_PATH] = ""; |
| 7310 | |
| 7311 | /* dest_parent = os.path.dirname(dest) */ |
| 7312 | strcpy(dest_parent, dest); |
Victor Stinner | 5a43676 | 2013-06-05 00:37:12 +0200 | [diff] [blame] | 7313 | _dirnameA(dest_parent); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7314 | /* src_resolved = os.path.join(dest_parent, src) */ |
Victor Stinner | 5a43676 | 2013-06-05 00:37:12 +0200 | [diff] [blame] | 7315 | _joinA(src_resolved, dest_parent, src); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7316 | return ( |
| 7317 | GetFileAttributesExA(src_resolved, GetFileExInfoStandard, &src_info) |
| 7318 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 7319 | ); |
| 7320 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7321 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7322 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7323 | |
| 7324 | /*[clinic input] |
| 7325 | os.symlink |
| 7326 | src: path_t |
| 7327 | dst: path_t |
| 7328 | target_is_directory: bool = False |
| 7329 | * |
| 7330 | dir_fd: dir_fd(requires='symlinkat')=None |
| 7331 | |
| 7332 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 7333 | |
| 7334 | Create a symbolic link pointing to src named dst. |
| 7335 | |
| 7336 | target_is_directory is required on Windows if the target is to be |
| 7337 | interpreted as a directory. (On Windows, symlink requires |
| 7338 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 7339 | target_is_directory is ignored on non-Windows platforms. |
| 7340 | |
| 7341 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7342 | and path should be relative; path will then be relative to that directory. |
| 7343 | dir_fd may not be implemented on your platform. |
| 7344 | If it is unavailable, using it will raise a NotImplementedError. |
| 7345 | |
| 7346 | [clinic start generated code]*/ |
| 7347 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7348 | static PyObject * |
| 7349 | os_symlink_impl(PyModuleDef *module, path_t *src, path_t *dst, int target_is_directory, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7350 | /*[clinic end generated code: output=11aa03f278bb2c8a input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7351 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7352 | #ifdef MS_WINDOWS |
| 7353 | DWORD result; |
| 7354 | #else |
| 7355 | int result; |
| 7356 | #endif |
| 7357 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7358 | #ifdef MS_WINDOWS |
| 7359 | if (!check_CreateSymbolicLink()) { |
| 7360 | PyErr_SetString(PyExc_NotImplementedError, |
| 7361 | "CreateSymbolicLink functions not found"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7362 | return NULL; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7363 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7364 | if (!win32_can_symlink) { |
| 7365 | PyErr_SetString(PyExc_OSError, "symbolic link privilege not held"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7366 | return NULL; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7367 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7368 | #endif |
| 7369 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7370 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7371 | PyErr_SetString(PyExc_ValueError, |
| 7372 | "symlink: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7373 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7374 | } |
| 7375 | |
| 7376 | #ifdef MS_WINDOWS |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7377 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7378 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7379 | if (dst->wide) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7380 | /* if src is a directory, ensure target_is_directory==1 */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7381 | target_is_directory |= _check_dirW(src->wide, dst->wide); |
| 7382 | result = Py_CreateSymbolicLinkW(dst->wide, src->wide, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7383 | target_is_directory); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7384 | } |
| 7385 | else { |
| 7386 | /* if src is a directory, ensure target_is_directory==1 */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7387 | target_is_directory |= _check_dirA(src->narrow, dst->narrow); |
| 7388 | result = Py_CreateSymbolicLinkA(dst->narrow, src->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7389 | target_is_directory); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 7390 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7391 | Py_END_ALLOW_THREADS |
| 7392 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7393 | if (!result) |
| 7394 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7395 | |
| 7396 | #else |
| 7397 | |
| 7398 | Py_BEGIN_ALLOW_THREADS |
| 7399 | #if HAVE_SYMLINKAT |
| 7400 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7401 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7402 | else |
| 7403 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7404 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7405 | Py_END_ALLOW_THREADS |
| 7406 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7407 | if (result) |
| 7408 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7409 | #endif |
| 7410 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7411 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7412 | } |
| 7413 | #endif /* HAVE_SYMLINK */ |
| 7414 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7415 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7416 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7417 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7418 | static PyStructSequence_Field times_result_fields[] = { |
| 7419 | {"user", "user time"}, |
| 7420 | {"system", "system time"}, |
| 7421 | {"children_user", "user time of children"}, |
| 7422 | {"children_system", "system time of children"}, |
| 7423 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 7424 | {NULL} |
| 7425 | }; |
| 7426 | |
| 7427 | PyDoc_STRVAR(times_result__doc__, |
| 7428 | "times_result: Result from os.times().\n\n\ |
| 7429 | This object may be accessed either as a tuple of\n\ |
| 7430 | (user, system, children_user, children_system, elapsed),\n\ |
| 7431 | or via the attributes user, system, children_user, children_system,\n\ |
| 7432 | and elapsed.\n\ |
| 7433 | \n\ |
| 7434 | See os.times for more information."); |
| 7435 | |
| 7436 | static PyStructSequence_Desc times_result_desc = { |
| 7437 | "times_result", /* name */ |
| 7438 | times_result__doc__, /* doc */ |
| 7439 | times_result_fields, |
| 7440 | 5 |
| 7441 | }; |
| 7442 | |
| 7443 | static PyTypeObject TimesResultType; |
| 7444 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7445 | #ifdef MS_WINDOWS |
| 7446 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 7447 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7448 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7449 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7450 | |
| 7451 | static PyObject * |
| 7452 | build_times_result(double user, double system, |
| 7453 | double children_user, double children_system, |
| 7454 | double elapsed) |
| 7455 | { |
| 7456 | PyObject *value = PyStructSequence_New(&TimesResultType); |
| 7457 | if (value == NULL) |
| 7458 | return NULL; |
| 7459 | |
| 7460 | #define SET(i, field) \ |
| 7461 | { \ |
| 7462 | PyObject *o = PyFloat_FromDouble(field); \ |
| 7463 | if (!o) { \ |
| 7464 | Py_DECREF(value); \ |
| 7465 | return NULL; \ |
| 7466 | } \ |
| 7467 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 7468 | } \ |
| 7469 | |
| 7470 | SET(0, user); |
| 7471 | SET(1, system); |
| 7472 | SET(2, children_user); |
| 7473 | SET(3, children_system); |
| 7474 | SET(4, elapsed); |
| 7475 | |
| 7476 | #undef SET |
| 7477 | |
| 7478 | return value; |
| 7479 | } |
| 7480 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7481 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7482 | #ifndef MS_WINDOWS |
| 7483 | #define NEED_TICKS_PER_SECOND |
| 7484 | static long ticks_per_second = -1; |
| 7485 | #endif /* MS_WINDOWS */ |
| 7486 | |
| 7487 | /*[clinic input] |
| 7488 | os.times |
| 7489 | |
| 7490 | Return a collection containing process timing information. |
| 7491 | |
| 7492 | The object returned behaves like a named tuple with these fields: |
| 7493 | (utime, stime, cutime, cstime, elapsed_time) |
| 7494 | All fields are floating point numbers. |
| 7495 | [clinic start generated code]*/ |
| 7496 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7497 | static PyObject * |
| 7498 | os_times_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7499 | /*[clinic end generated code: output=df0a63ebe6e6f091 input=2bf9df3d6ab2e48b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7500 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7501 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7502 | FILETIME create, exit, kernel, user; |
| 7503 | HANDLE hProc; |
| 7504 | hProc = GetCurrentProcess(); |
| 7505 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 7506 | /* The fields of a FILETIME structure are the hi and lo part |
| 7507 | of a 64-bit value expressed in 100 nanosecond units. |
| 7508 | 1e7 is one second in such units; 1e-7 the inverse. |
| 7509 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 7510 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7511 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7512 | (double)(user.dwHighDateTime*429.4967296 + |
| 7513 | user.dwLowDateTime*1e-7), |
| 7514 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 7515 | kernel.dwLowDateTime*1e-7), |
| 7516 | (double)0, |
| 7517 | (double)0, |
| 7518 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7519 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7520 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7521 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7522 | |
| 7523 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7524 | struct tms t; |
| 7525 | clock_t c; |
| 7526 | errno = 0; |
| 7527 | c = times(&t); |
| 7528 | if (c == (clock_t) -1) |
| 7529 | return posix_error(); |
| 7530 | return build_times_result( |
| 7531 | (double)t.tms_utime / ticks_per_second, |
| 7532 | (double)t.tms_stime / ticks_per_second, |
| 7533 | (double)t.tms_cutime / ticks_per_second, |
| 7534 | (double)t.tms_cstime / ticks_per_second, |
| 7535 | (double)c / ticks_per_second); |
| 7536 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7537 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7538 | #endif /* HAVE_TIMES */ |
| 7539 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7540 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7541 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7542 | /*[clinic input] |
| 7543 | os.getsid |
| 7544 | |
| 7545 | pid: pid_t |
| 7546 | / |
| 7547 | |
| 7548 | Call the system call getsid(pid) and return the result. |
| 7549 | [clinic start generated code]*/ |
| 7550 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7551 | static PyObject * |
| 7552 | os_getsid_impl(PyModuleDef *module, pid_t pid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7553 | /*[clinic end generated code: output=a074f80c0e6bfb38 input=eeb2b923a30ce04e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7554 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7555 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7556 | sid = getsid(pid); |
| 7557 | if (sid < 0) |
| 7558 | return posix_error(); |
| 7559 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7560 | } |
| 7561 | #endif /* HAVE_GETSID */ |
| 7562 | |
| 7563 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7564 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7565 | /*[clinic input] |
| 7566 | os.setsid |
| 7567 | |
| 7568 | Call the system call setsid(). |
| 7569 | [clinic start generated code]*/ |
| 7570 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7571 | static PyObject * |
| 7572 | os_setsid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7573 | /*[clinic end generated code: output=398fc152ae327330 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7574 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7575 | if (setsid() < 0) |
| 7576 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7577 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7578 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7579 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7580 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7581 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7582 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7583 | /*[clinic input] |
| 7584 | os.setpgid |
| 7585 | |
| 7586 | pid: pid_t |
| 7587 | pgrp: pid_t |
| 7588 | / |
| 7589 | |
| 7590 | Call the system call setpgid(pid, pgrp). |
| 7591 | [clinic start generated code]*/ |
| 7592 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7593 | static PyObject * |
| 7594 | os_setpgid_impl(PyModuleDef *module, pid_t pid, pid_t pgrp) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7595 | /*[clinic end generated code: output=7079a8e932912841 input=fceb395eca572e1a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7596 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7597 | if (setpgid(pid, pgrp) < 0) |
| 7598 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7599 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7600 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7601 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7602 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7603 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7604 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7605 | /*[clinic input] |
| 7606 | os.tcgetpgrp |
| 7607 | |
| 7608 | fd: int |
| 7609 | / |
| 7610 | |
| 7611 | Return the process group associated with the terminal specified by fd. |
| 7612 | [clinic start generated code]*/ |
| 7613 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7614 | static PyObject * |
| 7615 | os_tcgetpgrp_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7616 | /*[clinic end generated code: output=ebb6dc5f111c7dc0 input=7f6c18eac10ada86]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7617 | { |
| 7618 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7619 | if (pgid < 0) |
| 7620 | return posix_error(); |
| 7621 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7622 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7623 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7624 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7625 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7626 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7627 | /*[clinic input] |
| 7628 | os.tcsetpgrp |
| 7629 | |
| 7630 | fd: int |
| 7631 | pgid: pid_t |
| 7632 | / |
| 7633 | |
| 7634 | Set the process group associated with the terminal specified by fd. |
| 7635 | [clinic start generated code]*/ |
| 7636 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7637 | static PyObject * |
| 7638 | os_tcsetpgrp_impl(PyModuleDef *module, int fd, pid_t pgid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7639 | /*[clinic end generated code: output=3e4b05177462cd22 input=5bdc997c6a619020]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7640 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7641 | if (tcsetpgrp(fd, pgid) < 0) |
| 7642 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7643 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7644 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7645 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7646 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7647 | /* Functions acting on file descriptors */ |
| 7648 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7649 | #ifdef O_CLOEXEC |
| 7650 | extern int _Py_open_cloexec_works; |
| 7651 | #endif |
| 7652 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7653 | |
| 7654 | /*[clinic input] |
| 7655 | os.open -> int |
| 7656 | path: path_t |
| 7657 | flags: int |
| 7658 | mode: int = 0o777 |
| 7659 | * |
| 7660 | dir_fd: dir_fd(requires='openat') = None |
| 7661 | |
| 7662 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 7663 | |
| 7664 | Open a file for low level IO. Returns a file descriptor (integer). |
| 7665 | |
| 7666 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7667 | and path should be relative; path will then be relative to that directory. |
| 7668 | dir_fd may not be implemented on your platform. |
| 7669 | If it is unavailable, using it will raise a NotImplementedError. |
| 7670 | [clinic start generated code]*/ |
| 7671 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7672 | static int |
| 7673 | os_open_impl(PyModuleDef *module, path_t *path, int flags, int mode, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7674 | /*[clinic end generated code: output=c95a64f0e62f199b input=ad8623b29acd2934]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7675 | { |
| 7676 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7677 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7678 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7679 | #ifdef O_CLOEXEC |
| 7680 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 7681 | #elif !defined(MS_WINDOWS) |
| 7682 | int *atomic_flag_works = NULL; |
| 7683 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7684 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7685 | #ifdef MS_WINDOWS |
| 7686 | flags |= O_NOINHERIT; |
| 7687 | #elif defined(O_CLOEXEC) |
| 7688 | flags |= O_CLOEXEC; |
| 7689 | #endif |
| 7690 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7691 | do { |
| 7692 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7693 | #ifdef MS_WINDOWS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7694 | if (path->wide) |
| 7695 | fd = _wopen(path->wide, flags, mode); |
| 7696 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7697 | #endif |
| 7698 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7699 | if (dir_fd != DEFAULT_DIR_FD) |
| 7700 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 7701 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7702 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7703 | fd = open(path->narrow, flags, mode); |
| 7704 | Py_END_ALLOW_THREADS |
| 7705 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7706 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7707 | if (fd == -1) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7708 | if (!async_err) |
| 7709 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7710 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7711 | } |
| 7712 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7713 | #ifndef MS_WINDOWS |
| 7714 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 7715 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7716 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7717 | } |
| 7718 | #endif |
| 7719 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7720 | return fd; |
| 7721 | } |
| 7722 | |
| 7723 | |
| 7724 | /*[clinic input] |
| 7725 | os.close |
| 7726 | |
| 7727 | fd: int |
| 7728 | |
| 7729 | Close a file descriptor. |
| 7730 | [clinic start generated code]*/ |
| 7731 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7732 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7733 | os_close_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7734 | /*[clinic end generated code: output=47bf2ea536445a26 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7735 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7736 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7737 | if (!_PyVerify_fd(fd)) |
| 7738 | return posix_error(); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7739 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 7740 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 7741 | * for more details. |
| 7742 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7743 | Py_BEGIN_ALLOW_THREADS |
| 7744 | res = close(fd); |
| 7745 | Py_END_ALLOW_THREADS |
| 7746 | if (res < 0) |
| 7747 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7748 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7749 | } |
| 7750 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7751 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7752 | /*[clinic input] |
| 7753 | os.closerange |
| 7754 | |
| 7755 | fd_low: int |
| 7756 | fd_high: int |
| 7757 | / |
| 7758 | |
| 7759 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 7760 | [clinic start generated code]*/ |
| 7761 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7762 | static PyObject * |
| 7763 | os_closerange_impl(PyModuleDef *module, int fd_low, int fd_high) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7764 | /*[clinic end generated code: output=70e6adb95220ba96 input=5855a3d053ebd4ec]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7765 | { |
| 7766 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7767 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7768 | for (i = fd_low; i < fd_high; i++) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7769 | if (_PyVerify_fd(i)) |
| 7770 | close(i); |
| 7771 | Py_END_ALLOW_THREADS |
| 7772 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7773 | } |
| 7774 | |
| 7775 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7776 | /*[clinic input] |
| 7777 | os.dup -> int |
| 7778 | |
| 7779 | fd: int |
| 7780 | / |
| 7781 | |
| 7782 | Return a duplicate of a file descriptor. |
| 7783 | [clinic start generated code]*/ |
| 7784 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7785 | static int |
| 7786 | os_dup_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7787 | /*[clinic end generated code: output=f4bbac8c7652d05e input=6f10f7ea97f7852a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7788 | { |
| 7789 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7790 | } |
| 7791 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7792 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7793 | /*[clinic input] |
| 7794 | os.dup2 |
| 7795 | fd: int |
| 7796 | fd2: int |
| 7797 | inheritable: bool=True |
| 7798 | |
| 7799 | Duplicate file descriptor. |
| 7800 | [clinic start generated code]*/ |
| 7801 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7802 | static PyObject * |
| 7803 | os_dup2_impl(PyModuleDef *module, int fd, int fd2, int inheritable) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7804 | /*[clinic end generated code: output=9a099d95881a7923 input=76e96f511be0352f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7805 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7806 | int res; |
| 7807 | #if defined(HAVE_DUP3) && \ |
| 7808 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 7809 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
| 7810 | int dup3_works = -1; |
| 7811 | #endif |
| 7812 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7813 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 7814 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7815 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7816 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 7817 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 7818 | * upon close(), and therefore below. |
| 7819 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7820 | #ifdef MS_WINDOWS |
| 7821 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7822 | res = dup2(fd, fd2); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7823 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7824 | if (res < 0) |
| 7825 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7826 | |
| 7827 | /* Character files like console cannot be make non-inheritable */ |
| 7828 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7829 | close(fd2); |
| 7830 | return NULL; |
| 7831 | } |
| 7832 | |
| 7833 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 7834 | Py_BEGIN_ALLOW_THREADS |
| 7835 | if (!inheritable) |
| 7836 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 7837 | else |
| 7838 | res = dup2(fd, fd2); |
| 7839 | Py_END_ALLOW_THREADS |
| 7840 | if (res < 0) |
| 7841 | return posix_error(); |
| 7842 | |
| 7843 | #else |
| 7844 | |
| 7845 | #ifdef HAVE_DUP3 |
| 7846 | if (!inheritable && dup3_works != 0) { |
| 7847 | Py_BEGIN_ALLOW_THREADS |
| 7848 | res = dup3(fd, fd2, O_CLOEXEC); |
| 7849 | Py_END_ALLOW_THREADS |
| 7850 | if (res < 0) { |
| 7851 | if (dup3_works == -1) |
| 7852 | dup3_works = (errno != ENOSYS); |
| 7853 | if (dup3_works) |
| 7854 | return posix_error(); |
| 7855 | } |
| 7856 | } |
| 7857 | |
| 7858 | if (inheritable || dup3_works == 0) |
| 7859 | { |
| 7860 | #endif |
| 7861 | Py_BEGIN_ALLOW_THREADS |
| 7862 | res = dup2(fd, fd2); |
| 7863 | Py_END_ALLOW_THREADS |
| 7864 | if (res < 0) |
| 7865 | return posix_error(); |
| 7866 | |
| 7867 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 7868 | close(fd2); |
| 7869 | return NULL; |
| 7870 | } |
| 7871 | #ifdef HAVE_DUP3 |
| 7872 | } |
| 7873 | #endif |
| 7874 | |
| 7875 | #endif |
| 7876 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7877 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7878 | } |
| 7879 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7880 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7881 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7882 | /*[clinic input] |
| 7883 | os.lockf |
| 7884 | |
| 7885 | fd: int |
| 7886 | An open file descriptor. |
| 7887 | command: int |
| 7888 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 7889 | length: Py_off_t |
| 7890 | The number of bytes to lock, starting at the current position. |
| 7891 | / |
| 7892 | |
| 7893 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 7894 | |
| 7895 | [clinic start generated code]*/ |
| 7896 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7897 | static PyObject * |
| 7898 | os_lockf_impl(PyModuleDef *module, int fd, int command, Py_off_t length) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7899 | /*[clinic end generated code: output=25ff778f9e2fbf1b input=65da41d2106e9b79]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7900 | { |
| 7901 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7902 | |
| 7903 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7904 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7905 | Py_END_ALLOW_THREADS |
| 7906 | |
| 7907 | if (res < 0) |
| 7908 | return posix_error(); |
| 7909 | |
| 7910 | Py_RETURN_NONE; |
| 7911 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7912 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7913 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7914 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7915 | /*[clinic input] |
| 7916 | os.lseek -> Py_off_t |
| 7917 | |
| 7918 | fd: int |
| 7919 | position: Py_off_t |
| 7920 | how: int |
| 7921 | / |
| 7922 | |
| 7923 | Set the position of a file descriptor. Return the new position. |
| 7924 | |
| 7925 | Return the new cursor position in number of bytes |
| 7926 | relative to the beginning of the file. |
| 7927 | [clinic start generated code]*/ |
| 7928 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7929 | static Py_off_t |
| 7930 | os_lseek_impl(PyModuleDef *module, int fd, Py_off_t position, int how) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7931 | /*[clinic end generated code: output=65d4ab96d664998c input=902654ad3f96a6d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7932 | { |
| 7933 | Py_off_t result; |
| 7934 | |
| 7935 | if (!_PyVerify_fd(fd)) { |
| 7936 | posix_error(); |
| 7937 | return -1; |
| 7938 | } |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7939 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7940 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 7941 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7942 | case 0: how = SEEK_SET; break; |
| 7943 | case 1: how = SEEK_CUR; break; |
| 7944 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7945 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7946 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7947 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7948 | if (PyErr_Occurred()) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7949 | return -1; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7950 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7951 | if (!_PyVerify_fd(fd)) { |
| 7952 | posix_error(); |
| 7953 | return -1; |
| 7954 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7955 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 7956 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7957 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7958 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7959 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7960 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7961 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7962 | if (result < 0) |
| 7963 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7964 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7965 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7966 | } |
| 7967 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7968 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7969 | /*[clinic input] |
| 7970 | os.read |
| 7971 | fd: int |
| 7972 | length: Py_ssize_t |
| 7973 | / |
| 7974 | |
| 7975 | Read from a file descriptor. Returns a bytes object. |
| 7976 | [clinic start generated code]*/ |
| 7977 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7978 | static PyObject * |
| 7979 | os_read_impl(PyModuleDef *module, int fd, Py_ssize_t length) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 7980 | /*[clinic end generated code: output=be24f44178455e8b input=1df2eaa27c0bf1d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7981 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7982 | Py_ssize_t n; |
| 7983 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7984 | |
| 7985 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7986 | errno = EINVAL; |
| 7987 | return posix_error(); |
| 7988 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7989 | |
| 7990 | #ifdef MS_WINDOWS |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 7991 | /* On Windows, the count parameter of read() is an int */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7992 | if (length > INT_MAX) |
| 7993 | length = INT_MAX; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7994 | #endif |
| 7995 | |
| 7996 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7997 | if (buffer == NULL) |
| 7998 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7999 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8000 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 8001 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8002 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8003 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8004 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8005 | |
| 8006 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8007 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8008 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8009 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8010 | } |
| 8011 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8012 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 8013 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8014 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8015 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 8016 | { |
| 8017 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8018 | Py_ssize_t blen, total = 0; |
| 8019 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8020 | *iov = PyMem_New(struct iovec, cnt); |
| 8021 | if (*iov == NULL) { |
| 8022 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8023 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8024 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8025 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8026 | *buf = PyMem_New(Py_buffer, cnt); |
| 8027 | if (*buf == NULL) { |
| 8028 | PyMem_Del(*iov); |
| 8029 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8030 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8031 | } |
| 8032 | |
| 8033 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8034 | PyObject *item = PySequence_GetItem(seq, i); |
| 8035 | if (item == NULL) |
| 8036 | goto fail; |
| 8037 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 8038 | Py_DECREF(item); |
| 8039 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8040 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8041 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8042 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8043 | blen = (*buf)[i].len; |
| 8044 | (*iov)[i].iov_len = blen; |
| 8045 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8046 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8047 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8048 | |
| 8049 | fail: |
| 8050 | PyMem_Del(*iov); |
| 8051 | for (j = 0; j < i; j++) { |
| 8052 | PyBuffer_Release(&(*buf)[j]); |
| 8053 | } |
| 8054 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8055 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8056 | } |
| 8057 | |
| 8058 | static void |
| 8059 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 8060 | { |
| 8061 | int i; |
| 8062 | PyMem_Del(iov); |
| 8063 | for (i = 0; i < cnt; i++) { |
| 8064 | PyBuffer_Release(&buf[i]); |
| 8065 | } |
| 8066 | PyMem_Del(buf); |
| 8067 | } |
| 8068 | #endif |
| 8069 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8070 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8071 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8072 | /*[clinic input] |
| 8073 | os.readv -> Py_ssize_t |
| 8074 | |
| 8075 | fd: int |
| 8076 | buffers: object |
| 8077 | / |
| 8078 | |
| 8079 | Read from a file descriptor fd into an iterable of buffers. |
| 8080 | |
| 8081 | The buffers should be mutable buffers accepting bytes. |
| 8082 | readv will transfer data into each buffer until it is full |
| 8083 | and then move on to the next buffer in the sequence to hold |
| 8084 | the rest of the data. |
| 8085 | |
| 8086 | readv returns the total number of bytes read, |
| 8087 | which may be less than the total capacity of all the buffers. |
| 8088 | [clinic start generated code]*/ |
| 8089 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8090 | static Py_ssize_t |
| 8091 | os_readv_impl(PyModuleDef *module, int fd, PyObject *buffers) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8092 | /*[clinic end generated code: output=00fc56ff1800059f input=e679eb5dbfa0357d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8093 | { |
| 8094 | int cnt; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8095 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8096 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8097 | struct iovec *iov; |
| 8098 | Py_buffer *buf; |
| 8099 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8100 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8101 | PyErr_SetString(PyExc_TypeError, |
| 8102 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8103 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8104 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8105 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8106 | cnt = PySequence_Size(buffers); |
| 8107 | |
| 8108 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 8109 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8110 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8111 | do { |
| 8112 | Py_BEGIN_ALLOW_THREADS |
| 8113 | n = readv(fd, iov, cnt); |
| 8114 | Py_END_ALLOW_THREADS |
| 8115 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8116 | |
| 8117 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8118 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8119 | if (!async_err) |
| 8120 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8121 | return -1; |
| 8122 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8123 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8124 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8125 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8126 | #endif /* HAVE_READV */ |
| 8127 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8128 | |
| 8129 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8130 | /*[clinic input] |
| 8131 | # TODO length should be size_t! but Python doesn't support parsing size_t yet. |
| 8132 | os.pread |
| 8133 | |
| 8134 | fd: int |
| 8135 | length: int |
| 8136 | offset: Py_off_t |
| 8137 | / |
| 8138 | |
| 8139 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 8140 | |
| 8141 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 8142 | the beginning of the file. The file offset remains unchanged. |
| 8143 | [clinic start generated code]*/ |
| 8144 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8145 | static PyObject * |
| 8146 | os_pread_impl(PyModuleDef *module, int fd, int length, Py_off_t offset) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8147 | /*[clinic end generated code: output=90d1fed87f68fa33 input=084948dcbaa35d4c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8148 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8149 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8150 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8151 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8152 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8153 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8154 | errno = EINVAL; |
| 8155 | return posix_error(); |
| 8156 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8157 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8158 | if (buffer == NULL) |
| 8159 | return NULL; |
| 8160 | if (!_PyVerify_fd(fd)) { |
| 8161 | Py_DECREF(buffer); |
| 8162 | return posix_error(); |
| 8163 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8164 | |
| 8165 | do { |
| 8166 | Py_BEGIN_ALLOW_THREADS |
| 8167 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
| 8168 | Py_END_ALLOW_THREADS |
| 8169 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8170 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8171 | if (n < 0) { |
| 8172 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8173 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8174 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8175 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8176 | _PyBytes_Resize(&buffer, n); |
| 8177 | return buffer; |
| 8178 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8179 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8180 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8181 | |
| 8182 | /*[clinic input] |
| 8183 | os.write -> Py_ssize_t |
| 8184 | |
| 8185 | fd: int |
| 8186 | data: Py_buffer |
| 8187 | / |
| 8188 | |
| 8189 | Write a bytes object to a file descriptor. |
| 8190 | [clinic start generated code]*/ |
| 8191 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8192 | static Py_ssize_t |
| 8193 | os_write_impl(PyModuleDef *module, int fd, Py_buffer *data) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8194 | /*[clinic end generated code: output=58845c93c9ee1dda input=3207e28963234f3c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8195 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8196 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8197 | } |
| 8198 | |
| 8199 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8200 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 8201 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 8202 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 8203 | -> byteswritten\n\ |
| 8204 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 8205 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8206 | /* AC 3.5: don't bother converting, has optional group*/ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8207 | static PyObject * |
| 8208 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 8209 | { |
| 8210 | int in, out; |
| 8211 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8212 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8213 | off_t offset; |
| 8214 | |
| 8215 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 8216 | #ifndef __APPLE__ |
| 8217 | Py_ssize_t len; |
| 8218 | #endif |
| 8219 | PyObject *headers = NULL, *trailers = NULL; |
| 8220 | Py_buffer *hbuf, *tbuf; |
| 8221 | off_t sbytes; |
| 8222 | struct sf_hdtr sf; |
| 8223 | int flags = 0; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 8224 | static char *keywords[] = {"out", "in", |
| 8225 | "offset", "count", |
| 8226 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8227 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 8228 | sf.headers = NULL; |
| 8229 | sf.trailers = NULL; |
| 8230 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8231 | #ifdef __APPLE__ |
| 8232 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8233 | keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8234 | #else |
| 8235 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8236 | keywords, &out, &in, Py_off_t_converter, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8237 | #endif |
| 8238 | &headers, &trailers, &flags)) |
| 8239 | return NULL; |
| 8240 | if (headers != NULL) { |
| 8241 | if (!PySequence_Check(headers)) { |
| 8242 | PyErr_SetString(PyExc_TypeError, |
| 8243 | "sendfile() headers must be a sequence or None"); |
| 8244 | return NULL; |
| 8245 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8246 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8247 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8248 | if (sf.hdr_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8249 | (i = iov_setup(&(sf.headers), &hbuf, |
| 8250 | headers, sf.hdr_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8251 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8252 | #ifdef __APPLE__ |
| 8253 | sbytes += i; |
| 8254 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8255 | } |
| 8256 | } |
| 8257 | if (trailers != NULL) { |
| 8258 | if (!PySequence_Check(trailers)) { |
| 8259 | PyErr_SetString(PyExc_TypeError, |
| 8260 | "sendfile() trailers must be a sequence or None"); |
| 8261 | return NULL; |
| 8262 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8263 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8264 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8265 | if (sf.trl_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8266 | (i = iov_setup(&(sf.trailers), &tbuf, |
| 8267 | trailers, sf.trl_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8268 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8269 | #ifdef __APPLE__ |
| 8270 | sbytes += i; |
| 8271 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8272 | } |
| 8273 | } |
| 8274 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8275 | do { |
| 8276 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8277 | #ifdef __APPLE__ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8278 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8279 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8280 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8281 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8282 | Py_END_ALLOW_THREADS |
| 8283 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8284 | |
| 8285 | if (sf.headers != NULL) |
| 8286 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 8287 | if (sf.trailers != NULL) |
| 8288 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 8289 | |
| 8290 | if (ret < 0) { |
| 8291 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 8292 | if (sbytes != 0) { |
| 8293 | // some data has been sent |
| 8294 | goto done; |
| 8295 | } |
| 8296 | else { |
| 8297 | // no data has been sent; upper application is supposed |
| 8298 | // to retry on EAGAIN or EBUSY |
| 8299 | return posix_error(); |
| 8300 | } |
| 8301 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8302 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8303 | } |
| 8304 | goto done; |
| 8305 | |
| 8306 | done: |
| 8307 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 8308 | return Py_BuildValue("l", sbytes); |
| 8309 | #else |
| 8310 | return Py_BuildValue("L", sbytes); |
| 8311 | #endif |
| 8312 | |
| 8313 | #else |
| 8314 | Py_ssize_t count; |
| 8315 | PyObject *offobj; |
| 8316 | static char *keywords[] = {"out", "in", |
| 8317 | "offset", "count", NULL}; |
| 8318 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 8319 | keywords, &out, &in, &offobj, &count)) |
| 8320 | return NULL; |
| 8321 | #ifdef linux |
| 8322 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8323 | do { |
| 8324 | Py_BEGIN_ALLOW_THREADS |
| 8325 | ret = sendfile(out, in, NULL, count); |
| 8326 | Py_END_ALLOW_THREADS |
| 8327 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8328 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8329 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 8330 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8331 | } |
| 8332 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8333 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 8334 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8335 | |
| 8336 | do { |
| 8337 | Py_BEGIN_ALLOW_THREADS |
| 8338 | ret = sendfile(out, in, &offset, count); |
| 8339 | Py_END_ALLOW_THREADS |
| 8340 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8341 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8342 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8343 | return Py_BuildValue("n", ret); |
| 8344 | #endif |
| 8345 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8346 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8347 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8348 | |
| 8349 | /*[clinic input] |
| 8350 | os.fstat |
| 8351 | |
| 8352 | fd : int |
| 8353 | |
| 8354 | Perform a stat system call on the given file descriptor. |
| 8355 | |
| 8356 | Like stat(), but for an open file descriptor. |
| 8357 | Equivalent to os.stat(fd). |
| 8358 | [clinic start generated code]*/ |
| 8359 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8360 | static PyObject * |
| 8361 | os_fstat_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8362 | /*[clinic end generated code: output=d71fe98bf042b626 input=27e0e0ebbe5600c9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8363 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8364 | STRUCT_STAT st; |
| 8365 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8366 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8367 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8368 | do { |
| 8369 | Py_BEGIN_ALLOW_THREADS |
| 8370 | res = FSTAT(fd, &st); |
| 8371 | Py_END_ALLOW_THREADS |
| 8372 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8373 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8374 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8375 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8376 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8377 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8378 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8379 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8380 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8381 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8382 | } |
| 8383 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8384 | |
| 8385 | /*[clinic input] |
| 8386 | os.isatty -> bool |
| 8387 | fd: int |
| 8388 | / |
| 8389 | |
| 8390 | Return True if the fd is connected to a terminal. |
| 8391 | |
| 8392 | Return True if the file descriptor is an open file descriptor |
| 8393 | connected to the slave end of a terminal. |
| 8394 | [clinic start generated code]*/ |
| 8395 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8396 | static int |
| 8397 | os_isatty_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8398 | /*[clinic end generated code: output=acec9d3c29d16d33 input=08ce94aa1eaf7b5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8399 | { |
| 8400 | if (!_PyVerify_fd(fd)) |
| 8401 | return 0; |
| 8402 | return isatty(fd); |
| 8403 | } |
| 8404 | |
| 8405 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8406 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8407 | /*[clinic input] |
| 8408 | os.pipe |
| 8409 | |
| 8410 | Create a pipe. |
| 8411 | |
| 8412 | Returns a tuple of two file descriptors: |
| 8413 | (read_fd, write_fd) |
| 8414 | [clinic start generated code]*/ |
| 8415 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8416 | static PyObject * |
| 8417 | os_pipe_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8418 | /*[clinic end generated code: output=6b0cd3f868ec3c40 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8419 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8420 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8421 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8422 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8423 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8424 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8425 | #else |
| 8426 | int res; |
| 8427 | #endif |
| 8428 | |
| 8429 | #ifdef MS_WINDOWS |
| 8430 | attr.nLength = sizeof(attr); |
| 8431 | attr.lpSecurityDescriptor = NULL; |
| 8432 | attr.bInheritHandle = FALSE; |
| 8433 | |
| 8434 | Py_BEGIN_ALLOW_THREADS |
| 8435 | ok = CreatePipe(&read, &write, &attr, 0); |
| 8436 | if (ok) { |
| 8437 | fds[0] = _open_osfhandle((Py_intptr_t)read, _O_RDONLY); |
| 8438 | fds[1] = _open_osfhandle((Py_intptr_t)write, _O_WRONLY); |
| 8439 | if (fds[0] == -1 || fds[1] == -1) { |
| 8440 | CloseHandle(read); |
| 8441 | CloseHandle(write); |
| 8442 | ok = 0; |
| 8443 | } |
| 8444 | } |
| 8445 | Py_END_ALLOW_THREADS |
| 8446 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8447 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 8448 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8449 | #else |
| 8450 | |
| 8451 | #ifdef HAVE_PIPE2 |
| 8452 | Py_BEGIN_ALLOW_THREADS |
| 8453 | res = pipe2(fds, O_CLOEXEC); |
| 8454 | Py_END_ALLOW_THREADS |
| 8455 | |
| 8456 | if (res != 0 && errno == ENOSYS) |
| 8457 | { |
| 8458 | #endif |
| 8459 | Py_BEGIN_ALLOW_THREADS |
| 8460 | res = pipe(fds); |
| 8461 | Py_END_ALLOW_THREADS |
| 8462 | |
| 8463 | if (res == 0) { |
| 8464 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 8465 | close(fds[0]); |
| 8466 | close(fds[1]); |
| 8467 | return NULL; |
| 8468 | } |
| 8469 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 8470 | close(fds[0]); |
| 8471 | close(fds[1]); |
| 8472 | return NULL; |
| 8473 | } |
| 8474 | } |
| 8475 | #ifdef HAVE_PIPE2 |
| 8476 | } |
| 8477 | #endif |
| 8478 | |
| 8479 | if (res != 0) |
| 8480 | return PyErr_SetFromErrno(PyExc_OSError); |
| 8481 | #endif /* !MS_WINDOWS */ |
| 8482 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8483 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8484 | #endif /* HAVE_PIPE */ |
| 8485 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8486 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8487 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8488 | /*[clinic input] |
| 8489 | os.pipe2 |
| 8490 | |
| 8491 | flags: int |
| 8492 | / |
| 8493 | |
| 8494 | Create a pipe with flags set atomically. |
| 8495 | |
| 8496 | Returns a tuple of two file descriptors: |
| 8497 | (read_fd, write_fd) |
| 8498 | |
| 8499 | flags can be constructed by ORing together one or more of these values: |
| 8500 | O_NONBLOCK, O_CLOEXEC. |
| 8501 | [clinic start generated code]*/ |
| 8502 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8503 | static PyObject * |
| 8504 | os_pipe2_impl(PyModuleDef *module, int flags) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8505 | /*[clinic end generated code: output=c15b6075d0c6b2e7 input=f261b6e7e63c6817]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8506 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8507 | int fds[2]; |
| 8508 | int res; |
| 8509 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8510 | res = pipe2(fds, flags); |
| 8511 | if (res != 0) |
| 8512 | return posix_error(); |
| 8513 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 8514 | } |
| 8515 | #endif /* HAVE_PIPE2 */ |
| 8516 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8517 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8518 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8519 | /*[clinic input] |
| 8520 | os.writev -> Py_ssize_t |
| 8521 | fd: int |
| 8522 | buffers: object |
| 8523 | / |
| 8524 | |
| 8525 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 8526 | |
| 8527 | Returns the total number of bytes written. |
| 8528 | buffers must be a sequence of bytes-like objects. |
| 8529 | [clinic start generated code]*/ |
| 8530 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8531 | static Py_ssize_t |
| 8532 | os_writev_impl(PyModuleDef *module, int fd, PyObject *buffers) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8533 | /*[clinic end generated code: output=a48925dbf2d5c238 input=5b8d17fe4189d2fe]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8534 | { |
| 8535 | int cnt; |
| 8536 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8537 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8538 | struct iovec *iov; |
| 8539 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8540 | |
| 8541 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8542 | PyErr_SetString(PyExc_TypeError, |
| 8543 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8544 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8545 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8546 | cnt = PySequence_Size(buffers); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8547 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8548 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 8549 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8550 | } |
| 8551 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8552 | do { |
| 8553 | Py_BEGIN_ALLOW_THREADS |
| 8554 | result = writev(fd, iov, cnt); |
| 8555 | Py_END_ALLOW_THREADS |
| 8556 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8557 | |
| 8558 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8559 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8560 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8561 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8562 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8563 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8564 | #endif /* HAVE_WRITEV */ |
| 8565 | |
| 8566 | |
| 8567 | #ifdef HAVE_PWRITE |
| 8568 | /*[clinic input] |
| 8569 | os.pwrite -> Py_ssize_t |
| 8570 | |
| 8571 | fd: int |
| 8572 | buffer: Py_buffer |
| 8573 | offset: Py_off_t |
| 8574 | / |
| 8575 | |
| 8576 | Write bytes to a file descriptor starting at a particular offset. |
| 8577 | |
| 8578 | Write buffer to fd, starting at offset bytes from the beginning of |
| 8579 | the file. Returns the number of bytes writte. Does not change the |
| 8580 | current file offset. |
| 8581 | [clinic start generated code]*/ |
| 8582 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8583 | static Py_ssize_t |
| 8584 | os_pwrite_impl(PyModuleDef *module, int fd, Py_buffer *buffer, Py_off_t offset) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8585 | /*[clinic end generated code: output=95225f3b496feaf3 input=19903f1b3dd26377]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8586 | { |
| 8587 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8588 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8589 | |
| 8590 | if (!_PyVerify_fd(fd)) { |
| 8591 | posix_error(); |
| 8592 | return -1; |
| 8593 | } |
| 8594 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8595 | do { |
| 8596 | Py_BEGIN_ALLOW_THREADS |
| 8597 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
| 8598 | Py_END_ALLOW_THREADS |
| 8599 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8600 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8601 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8602 | posix_error(); |
| 8603 | return size; |
| 8604 | } |
| 8605 | #endif /* HAVE_PWRITE */ |
| 8606 | |
| 8607 | |
| 8608 | #ifdef HAVE_MKFIFO |
| 8609 | /*[clinic input] |
| 8610 | os.mkfifo |
| 8611 | |
| 8612 | path: path_t |
| 8613 | mode: int=0o666 |
| 8614 | * |
| 8615 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 8616 | |
| 8617 | Create a "fifo" (a POSIX named pipe). |
| 8618 | |
| 8619 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8620 | and path should be relative; path will then be relative to that directory. |
| 8621 | dir_fd may not be implemented on your platform. |
| 8622 | If it is unavailable, using it will raise a NotImplementedError. |
| 8623 | [clinic start generated code]*/ |
| 8624 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8625 | static PyObject * |
| 8626 | os_mkfifo_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8627 | /*[clinic end generated code: output=8f5f5e72c630049a input=73032e98a36e0e19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8628 | { |
| 8629 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8630 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8631 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8632 | do { |
| 8633 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8634 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8635 | if (dir_fd != DEFAULT_DIR_FD) |
| 8636 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 8637 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8638 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8639 | result = mkfifo(path->narrow, mode); |
| 8640 | Py_END_ALLOW_THREADS |
| 8641 | } while (result != 0 && errno == EINTR && |
| 8642 | !(async_err = PyErr_CheckSignals())); |
| 8643 | if (result != 0) |
| 8644 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8645 | |
| 8646 | Py_RETURN_NONE; |
| 8647 | } |
| 8648 | #endif /* HAVE_MKFIFO */ |
| 8649 | |
| 8650 | |
| 8651 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 8652 | /*[clinic input] |
| 8653 | os.mknod |
| 8654 | |
| 8655 | path: path_t |
| 8656 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8657 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8658 | * |
| 8659 | dir_fd: dir_fd(requires='mknodat')=None |
| 8660 | |
| 8661 | Create a node in the file system. |
| 8662 | |
| 8663 | Create a node in the file system (file, device special file or named pipe) |
| 8664 | at path. mode specifies both the permissions to use and the |
| 8665 | type of node to be created, being combined (bitwise OR) with one of |
| 8666 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 8667 | device defines the newly created device special file (probably using |
| 8668 | os.makedev()). Otherwise device is ignored. |
| 8669 | |
| 8670 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8671 | and path should be relative; path will then be relative to that directory. |
| 8672 | dir_fd may not be implemented on your platform. |
| 8673 | If it is unavailable, using it will raise a NotImplementedError. |
| 8674 | [clinic start generated code]*/ |
| 8675 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8676 | static PyObject * |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8677 | os_mknod_impl(PyModuleDef *module, path_t *path, int mode, dev_t device, int dir_fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8678 | /*[clinic end generated code: output=f7f813e8847de12f input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8679 | { |
| 8680 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8681 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8682 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8683 | do { |
| 8684 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8685 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8686 | if (dir_fd != DEFAULT_DIR_FD) |
| 8687 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 8688 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8689 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8690 | result = mknod(path->narrow, mode, device); |
| 8691 | Py_END_ALLOW_THREADS |
| 8692 | } while (result != 0 && errno == EINTR && |
| 8693 | !(async_err = PyErr_CheckSignals())); |
| 8694 | if (result != 0) |
| 8695 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8696 | |
| 8697 | Py_RETURN_NONE; |
| 8698 | } |
| 8699 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 8700 | |
| 8701 | |
| 8702 | #ifdef HAVE_DEVICE_MACROS |
| 8703 | /*[clinic input] |
| 8704 | os.major -> unsigned_int |
| 8705 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8706 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8707 | / |
| 8708 | |
| 8709 | Extracts a device major number from a raw device number. |
| 8710 | [clinic start generated code]*/ |
| 8711 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8712 | static unsigned int |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8713 | os_major_impl(PyModuleDef *module, dev_t device) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8714 | /*[clinic end generated code: output=ba55693ab49bac34 input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8715 | { |
| 8716 | return major(device); |
| 8717 | } |
| 8718 | |
| 8719 | |
| 8720 | /*[clinic input] |
| 8721 | os.minor -> unsigned_int |
| 8722 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8723 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8724 | / |
| 8725 | |
| 8726 | Extracts a device minor number from a raw device number. |
| 8727 | [clinic start generated code]*/ |
| 8728 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8729 | static unsigned int |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8730 | os_minor_impl(PyModuleDef *module, dev_t device) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8731 | /*[clinic end generated code: output=2867219ebf274e27 input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8732 | { |
| 8733 | return minor(device); |
| 8734 | } |
| 8735 | |
| 8736 | |
| 8737 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8738 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8739 | |
| 8740 | major: int |
| 8741 | minor: int |
| 8742 | / |
| 8743 | |
| 8744 | Composes a raw device number from the major and minor device numbers. |
| 8745 | [clinic start generated code]*/ |
| 8746 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 8747 | static dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8748 | os_makedev_impl(PyModuleDef *module, int major, int minor) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8749 | /*[clinic end generated code: output=7cb6264352437660 input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8750 | { |
| 8751 | return makedev(major, minor); |
| 8752 | } |
| 8753 | #endif /* HAVE_DEVICE_MACROS */ |
| 8754 | |
| 8755 | |
| 8756 | #ifdef HAVE_FTRUNCATE |
| 8757 | /*[clinic input] |
| 8758 | os.ftruncate |
| 8759 | |
| 8760 | fd: int |
| 8761 | length: Py_off_t |
| 8762 | / |
| 8763 | |
| 8764 | Truncate a file, specified by file descriptor, to a specific length. |
| 8765 | [clinic start generated code]*/ |
| 8766 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8767 | static PyObject * |
| 8768 | os_ftruncate_impl(PyModuleDef *module, int fd, Py_off_t length) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8769 | /*[clinic end generated code: output=3666f401d76bf834 input=63b43641e52818f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8770 | { |
| 8771 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8772 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8773 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8774 | do { |
| 8775 | Py_BEGIN_ALLOW_THREADS |
| 8776 | result = ftruncate(fd, length); |
| 8777 | Py_END_ALLOW_THREADS |
| 8778 | } while (result != 0 && errno == EINTR && |
| 8779 | !(async_err = PyErr_CheckSignals())); |
| 8780 | if (result != 0) |
| 8781 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8782 | Py_RETURN_NONE; |
| 8783 | } |
| 8784 | #endif /* HAVE_FTRUNCATE */ |
| 8785 | |
| 8786 | |
| 8787 | #ifdef HAVE_TRUNCATE |
| 8788 | /*[clinic input] |
| 8789 | os.truncate |
| 8790 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 8791 | length: Py_off_t |
| 8792 | |
| 8793 | Truncate a file, specified by path, to a specific length. |
| 8794 | |
| 8795 | On some platforms, path may also be specified as an open file descriptor. |
| 8796 | If this functionality is unavailable, using it raises an exception. |
| 8797 | [clinic start generated code]*/ |
| 8798 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8799 | static PyObject * |
| 8800 | os_truncate_impl(PyModuleDef *module, path_t *path, Py_off_t length) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8801 | /*[clinic end generated code: output=f60a9e08370e9e2e input=77229cf0b50a9b77]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8802 | { |
| 8803 | int result; |
| 8804 | |
| 8805 | Py_BEGIN_ALLOW_THREADS |
| 8806 | #ifdef HAVE_FTRUNCATE |
| 8807 | if (path->fd != -1) |
| 8808 | result = ftruncate(path->fd, length); |
| 8809 | else |
| 8810 | #endif |
| 8811 | result = truncate(path->narrow, length); |
| 8812 | Py_END_ALLOW_THREADS |
| 8813 | if (result < 0) |
| 8814 | return path_error(path); |
| 8815 | |
| 8816 | Py_RETURN_NONE; |
| 8817 | } |
| 8818 | #endif /* HAVE_TRUNCATE */ |
| 8819 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8820 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 8821 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 8822 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 8823 | defined, which is the case in Python on AIX. AIX bug report: |
| 8824 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 8825 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 8826 | # define POSIX_FADVISE_AIX_BUG |
| 8827 | #endif |
| 8828 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 8829 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 8830 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8831 | /*[clinic input] |
| 8832 | os.posix_fallocate |
| 8833 | |
| 8834 | fd: int |
| 8835 | offset: Py_off_t |
| 8836 | length: Py_off_t |
| 8837 | / |
| 8838 | |
| 8839 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 8840 | |
| 8841 | Ensure that the file specified by fd encompasses a range of bytes |
| 8842 | starting at offset bytes from the beginning and continuing for length bytes. |
| 8843 | [clinic start generated code]*/ |
| 8844 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8845 | static PyObject * |
| 8846 | os_posix_fallocate_impl(PyModuleDef *module, int fd, Py_off_t offset, Py_off_t length) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8847 | /*[clinic end generated code: output=8ae5f7837004d454 input=d7a2ef0ab2ca52fb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8848 | { |
| 8849 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8850 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8851 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8852 | do { |
| 8853 | Py_BEGIN_ALLOW_THREADS |
| 8854 | result = posix_fallocate(fd, offset, length); |
| 8855 | Py_END_ALLOW_THREADS |
| 8856 | } while (result != 0 && errno == EINTR && |
| 8857 | !(async_err = PyErr_CheckSignals())); |
| 8858 | if (result != 0) |
| 8859 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8860 | Py_RETURN_NONE; |
| 8861 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 8862 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8863 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8864 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 8865 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8866 | /*[clinic input] |
| 8867 | os.posix_fadvise |
| 8868 | |
| 8869 | fd: int |
| 8870 | offset: Py_off_t |
| 8871 | length: Py_off_t |
| 8872 | advice: int |
| 8873 | / |
| 8874 | |
| 8875 | Announce an intention to access data in a specific pattern. |
| 8876 | |
| 8877 | Announce an intention to access data in a specific pattern, thus allowing |
| 8878 | the kernel to make optimizations. |
| 8879 | The advice applies to the region of the file specified by fd starting at |
| 8880 | offset and continuing for length bytes. |
| 8881 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 8882 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 8883 | POSIX_FADV_DONTNEED. |
| 8884 | [clinic start generated code]*/ |
| 8885 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8886 | static PyObject * |
| 8887 | os_posix_fadvise_impl(PyModuleDef *module, int fd, Py_off_t offset, Py_off_t length, int advice) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8888 | /*[clinic end generated code: output=0e3f09f651661257 input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8889 | { |
| 8890 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8891 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8892 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8893 | do { |
| 8894 | Py_BEGIN_ALLOW_THREADS |
| 8895 | result = posix_fadvise(fd, offset, length, advice); |
| 8896 | Py_END_ALLOW_THREADS |
| 8897 | } while (result != 0 && errno == EINTR && |
| 8898 | !(async_err = PyErr_CheckSignals())); |
| 8899 | if (result != 0) |
| 8900 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8901 | Py_RETURN_NONE; |
| 8902 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 8903 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8904 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8905 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8906 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8907 | /* Save putenv() parameters as values here, so we can collect them when they |
| 8908 | * get re-set with another call for the same key. */ |
| 8909 | static PyObject *posix_putenv_garbage; |
| 8910 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8911 | static void |
| 8912 | posix_putenv_garbage_setitem(PyObject *name, PyObject *value) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8913 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8914 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 8915 | * this will cause previous value to be collected. This has to |
| 8916 | * happen after the real putenv() call because the old value |
| 8917 | * was still accessible until then. */ |
| 8918 | if (PyDict_SetItem(posix_putenv_garbage, name, value)) |
| 8919 | /* really not much we can do; just leak */ |
| 8920 | PyErr_Clear(); |
| 8921 | else |
| 8922 | Py_DECREF(value); |
| 8923 | } |
| 8924 | |
| 8925 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8926 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8927 | /*[clinic input] |
| 8928 | os.putenv |
| 8929 | |
| 8930 | name: unicode |
| 8931 | value: unicode |
| 8932 | / |
| 8933 | |
| 8934 | Change or add an environment variable. |
| 8935 | [clinic start generated code]*/ |
| 8936 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8937 | static PyObject * |
| 8938 | os_putenv_impl(PyModuleDef *module, PyObject *name, PyObject *value) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8939 | /*[clinic end generated code: output=a2438cf95e5a0c1c input=ba586581c2e6105f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8940 | { |
| 8941 | wchar_t *env; |
| 8942 | |
| 8943 | PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 8944 | if (unicode == NULL) { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8945 | PyErr_NoMemory(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8946 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8947 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8948 | if (_MAX_ENV < PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8949 | PyErr_Format(PyExc_ValueError, |
| 8950 | "the environment variable is longer than %u characters", |
| 8951 | _MAX_ENV); |
| 8952 | goto error; |
| 8953 | } |
| 8954 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8955 | env = PyUnicode_AsUnicode(unicode); |
| 8956 | if (env == NULL) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 8957 | goto error; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8958 | if (_wputenv(env)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8959 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8960 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8961 | } |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8962 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8963 | posix_putenv_garbage_setitem(name, unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8964 | Py_RETURN_NONE; |
| 8965 | |
| 8966 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8967 | Py_DECREF(unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8968 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8969 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8970 | #else /* MS_WINDOWS */ |
| 8971 | /*[clinic input] |
| 8972 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8973 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8974 | name: FSConverter |
| 8975 | value: FSConverter |
| 8976 | / |
| 8977 | |
| 8978 | Change or add an environment variable. |
| 8979 | [clinic start generated code]*/ |
| 8980 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8981 | static PyObject * |
| 8982 | os_putenv_impl(PyModuleDef *module, PyObject *name, PyObject *value) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 8983 | /*[clinic end generated code: output=a2438cf95e5a0c1c input=a97bc6152f688d31]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8984 | { |
| 8985 | PyObject *bytes = NULL; |
| 8986 | char *env; |
| 8987 | char *name_string = PyBytes_AsString(name); |
| 8988 | char *value_string = PyBytes_AsString(value); |
| 8989 | |
| 8990 | bytes = PyBytes_FromFormat("%s=%s", name_string, value_string); |
| 8991 | if (bytes == NULL) { |
| 8992 | PyErr_NoMemory(); |
| 8993 | return NULL; |
| 8994 | } |
| 8995 | |
| 8996 | env = PyBytes_AS_STRING(bytes); |
| 8997 | if (putenv(env)) { |
| 8998 | Py_DECREF(bytes); |
| 8999 | return posix_error(); |
| 9000 | } |
| 9001 | |
| 9002 | posix_putenv_garbage_setitem(name, bytes); |
| 9003 | Py_RETURN_NONE; |
| 9004 | } |
| 9005 | #endif /* MS_WINDOWS */ |
| 9006 | #endif /* HAVE_PUTENV */ |
| 9007 | |
| 9008 | |
| 9009 | #ifdef HAVE_UNSETENV |
| 9010 | /*[clinic input] |
| 9011 | os.unsetenv |
| 9012 | name: FSConverter |
| 9013 | / |
| 9014 | |
| 9015 | Delete an environment variable. |
| 9016 | [clinic start generated code]*/ |
| 9017 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9018 | static PyObject * |
| 9019 | os_unsetenv_impl(PyModuleDef *module, PyObject *name) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9020 | /*[clinic end generated code: output=25994b57016a2dc9 input=2bb5288a599c7107]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9021 | { |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9022 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 9023 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9024 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9025 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9026 | #ifdef HAVE_BROKEN_UNSETENV |
| 9027 | unsetenv(PyBytes_AS_STRING(name)); |
| 9028 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9029 | err = unsetenv(PyBytes_AS_STRING(name)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9030 | if (err) |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 9031 | return posix_error(); |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 9032 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9033 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9034 | /* Remove the key from posix_putenv_garbage; |
| 9035 | * this will cause it to be collected. This has to |
| 9036 | * happen after the real unsetenv() call because the |
| 9037 | * old value was still accessible until then. |
| 9038 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 9039 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9040 | /* really not much we can do; just leak */ |
| 9041 | PyErr_Clear(); |
| 9042 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 9043 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9044 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9045 | #endif /* HAVE_UNSETENV */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 9046 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9047 | |
| 9048 | /*[clinic input] |
| 9049 | os.strerror |
| 9050 | |
| 9051 | code: int |
| 9052 | / |
| 9053 | |
| 9054 | Translate an error code to a message string. |
| 9055 | [clinic start generated code]*/ |
| 9056 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9057 | static PyObject * |
| 9058 | os_strerror_impl(PyModuleDef *module, int code) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9059 | /*[clinic end generated code: output=0280c6af51e5c9fe input=75a8673d97915a91]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9060 | { |
| 9061 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9062 | if (message == NULL) { |
| 9063 | PyErr_SetString(PyExc_ValueError, |
| 9064 | "strerror() argument out of range"); |
| 9065 | return NULL; |
| 9066 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 9067 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9068 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 9069 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 9070 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9071 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9072 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9073 | /*[clinic input] |
| 9074 | os.WCOREDUMP -> bool |
| 9075 | |
| 9076 | status: int |
| 9077 | / |
| 9078 | |
| 9079 | Return True if the process returning status was dumped to a core file. |
| 9080 | [clinic start generated code]*/ |
| 9081 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9082 | static int |
| 9083 | os_WCOREDUMP_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9084 | /*[clinic end generated code: output=134f70bbe63fbf41 input=8b05e7ab38528d04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9085 | { |
| 9086 | WAIT_TYPE wait_status; |
| 9087 | WAIT_STATUS_INT(wait_status) = status; |
| 9088 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9089 | } |
| 9090 | #endif /* WCOREDUMP */ |
| 9091 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9092 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9093 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9094 | /*[clinic input] |
| 9095 | os.WIFCONTINUED -> bool |
| 9096 | |
| 9097 | status: int |
| 9098 | |
| 9099 | Return True if a particular process was continued from a job control stop. |
| 9100 | |
| 9101 | Return True if the process returning status was continued from a |
| 9102 | job control stop. |
| 9103 | [clinic start generated code]*/ |
| 9104 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9105 | static int |
| 9106 | os_WIFCONTINUED_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9107 | /*[clinic end generated code: output=9cdd26543ebb6dcd input=e777e7d38eb25bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9108 | { |
| 9109 | WAIT_TYPE wait_status; |
| 9110 | WAIT_STATUS_INT(wait_status) = status; |
| 9111 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 9112 | } |
| 9113 | #endif /* WIFCONTINUED */ |
| 9114 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9115 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9116 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9117 | /*[clinic input] |
| 9118 | os.WIFSTOPPED -> bool |
| 9119 | |
| 9120 | status: int |
| 9121 | |
| 9122 | Return True if the process returning status was stopped. |
| 9123 | [clinic start generated code]*/ |
| 9124 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9125 | static int |
| 9126 | os_WIFSTOPPED_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9127 | /*[clinic end generated code: output=73bf35e44994a724 input=043cb7f1289ef904]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9128 | { |
| 9129 | WAIT_TYPE wait_status; |
| 9130 | WAIT_STATUS_INT(wait_status) = status; |
| 9131 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9132 | } |
| 9133 | #endif /* WIFSTOPPED */ |
| 9134 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9135 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9136 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9137 | /*[clinic input] |
| 9138 | os.WIFSIGNALED -> bool |
| 9139 | |
| 9140 | status: int |
| 9141 | |
| 9142 | Return True if the process returning status was terminated by a signal. |
| 9143 | [clinic start generated code]*/ |
| 9144 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9145 | static int |
| 9146 | os_WIFSIGNALED_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9147 | /*[clinic end generated code: output=2697975771872420 input=d55ba7cc9ce5dc43]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9148 | { |
| 9149 | WAIT_TYPE wait_status; |
| 9150 | WAIT_STATUS_INT(wait_status) = status; |
| 9151 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9152 | } |
| 9153 | #endif /* WIFSIGNALED */ |
| 9154 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9155 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9156 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9157 | /*[clinic input] |
| 9158 | os.WIFEXITED -> bool |
| 9159 | |
| 9160 | status: int |
| 9161 | |
| 9162 | Return True if the process returning status exited via the exit() system call. |
| 9163 | [clinic start generated code]*/ |
| 9164 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9165 | static int |
| 9166 | os_WIFEXITED_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9167 | /*[clinic end generated code: output=ca8f8c61f0b8532e input=d63775a6791586c0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9168 | { |
| 9169 | WAIT_TYPE wait_status; |
| 9170 | WAIT_STATUS_INT(wait_status) = status; |
| 9171 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9172 | } |
| 9173 | #endif /* WIFEXITED */ |
| 9174 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9175 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 9176 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9177 | /*[clinic input] |
| 9178 | os.WEXITSTATUS -> int |
| 9179 | |
| 9180 | status: int |
| 9181 | |
| 9182 | Return the process return code from status. |
| 9183 | [clinic start generated code]*/ |
| 9184 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9185 | static int |
| 9186 | os_WEXITSTATUS_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9187 | /*[clinic end generated code: output=ea54da23d9e0f6af input=e1fb4944e377585b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9188 | { |
| 9189 | WAIT_TYPE wait_status; |
| 9190 | WAIT_STATUS_INT(wait_status) = status; |
| 9191 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9192 | } |
| 9193 | #endif /* WEXITSTATUS */ |
| 9194 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9195 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9196 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9197 | /*[clinic input] |
| 9198 | os.WTERMSIG -> int |
| 9199 | |
| 9200 | status: int |
| 9201 | |
| 9202 | Return the signal that terminated the process that provided the status value. |
| 9203 | [clinic start generated code]*/ |
| 9204 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9205 | static int |
| 9206 | os_WTERMSIG_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9207 | /*[clinic end generated code: output=4d25367026cb852c input=727fd7f84ec3f243]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9208 | { |
| 9209 | WAIT_TYPE wait_status; |
| 9210 | WAIT_STATUS_INT(wait_status) = status; |
| 9211 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9212 | } |
| 9213 | #endif /* WTERMSIG */ |
| 9214 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9215 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9216 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9217 | /*[clinic input] |
| 9218 | os.WSTOPSIG -> int |
| 9219 | |
| 9220 | status: int |
| 9221 | |
| 9222 | Return the signal that stopped the process that provided the status value. |
| 9223 | [clinic start generated code]*/ |
| 9224 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9225 | static int |
| 9226 | os_WSTOPSIG_impl(PyModuleDef *module, int status) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9227 | /*[clinic end generated code: output=54eb9c13b001adb4 input=46ebf1d1b293c5c1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9228 | { |
| 9229 | WAIT_TYPE wait_status; |
| 9230 | WAIT_STATUS_INT(wait_status) = status; |
| 9231 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9232 | } |
| 9233 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 9234 | #endif /* HAVE_SYS_WAIT_H */ |
| 9235 | |
| 9236 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9237 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 9238 | #ifdef _SCO_DS |
| 9239 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 9240 | needed definitions in sys/statvfs.h */ |
| 9241 | #define _SVID3 |
| 9242 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9243 | #include <sys/statvfs.h> |
| 9244 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9245 | static PyObject* |
| 9246 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9247 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 9248 | if (v == NULL) |
| 9249 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9250 | |
| 9251 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9252 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9253 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9254 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 9255 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 9256 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 9257 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 9258 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 9259 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 9260 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9261 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9262 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9263 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 9264 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 9265 | PyStructSequence_SET_ITEM(v, 2, |
| 9266 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 9267 | PyStructSequence_SET_ITEM(v, 3, |
| 9268 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 9269 | PyStructSequence_SET_ITEM(v, 4, |
| 9270 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 9271 | PyStructSequence_SET_ITEM(v, 5, |
| 9272 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 9273 | PyStructSequence_SET_ITEM(v, 6, |
| 9274 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 9275 | PyStructSequence_SET_ITEM(v, 7, |
| 9276 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 9277 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 9278 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9279 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 9280 | if (PyErr_Occurred()) { |
| 9281 | Py_DECREF(v); |
| 9282 | return NULL; |
| 9283 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9284 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9285 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9286 | } |
| 9287 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9288 | |
| 9289 | /*[clinic input] |
| 9290 | os.fstatvfs |
| 9291 | fd: int |
| 9292 | / |
| 9293 | |
| 9294 | Perform an fstatvfs system call on the given fd. |
| 9295 | |
| 9296 | Equivalent to statvfs(fd). |
| 9297 | [clinic start generated code]*/ |
| 9298 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9299 | static PyObject * |
| 9300 | os_fstatvfs_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9301 | /*[clinic end generated code: output=584a94a754497ac0 input=d8122243ac50975e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9302 | { |
| 9303 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9304 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9305 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9306 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9307 | do { |
| 9308 | Py_BEGIN_ALLOW_THREADS |
| 9309 | result = fstatvfs(fd, &st); |
| 9310 | Py_END_ALLOW_THREADS |
| 9311 | } while (result != 0 && errno == EINTR && |
| 9312 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9313 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9314 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 9315 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9316 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9317 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9318 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9319 | |
| 9320 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9321 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9322 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9323 | /*[clinic input] |
| 9324 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9325 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9326 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 9327 | |
| 9328 | Perform a statvfs system call on the given path. |
| 9329 | |
| 9330 | path may always be specified as a string. |
| 9331 | On some platforms, path may also be specified as an open file descriptor. |
| 9332 | If this functionality is unavailable, using it raises an exception. |
| 9333 | [clinic start generated code]*/ |
| 9334 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9335 | static PyObject * |
| 9336 | os_statvfs_impl(PyModuleDef *module, path_t *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9337 | /*[clinic end generated code: output=5ced07a2cf931f41 input=3f5c35791c669bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9338 | { |
| 9339 | int result; |
| 9340 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9341 | |
| 9342 | Py_BEGIN_ALLOW_THREADS |
| 9343 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9344 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9345 | #ifdef __APPLE__ |
| 9346 | /* handle weak-linking on Mac OS X 10.3 */ |
| 9347 | if (fstatvfs == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9348 | fd_specified("statvfs", path->fd); |
| 9349 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9350 | } |
| 9351 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9352 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9353 | } |
| 9354 | else |
| 9355 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9356 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9357 | Py_END_ALLOW_THREADS |
| 9358 | |
| 9359 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9360 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9361 | } |
| 9362 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9363 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9364 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9365 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 9366 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9367 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9368 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9369 | /*[clinic input] |
| 9370 | os._getdiskusage |
| 9371 | |
| 9372 | path: Py_UNICODE |
| 9373 | |
| 9374 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 9375 | [clinic start generated code]*/ |
| 9376 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9377 | static PyObject * |
| 9378 | os__getdiskusage_impl(PyModuleDef *module, Py_UNICODE *path) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9379 | /*[clinic end generated code: output=60a9cf33449db1dd input=6458133aed893c78]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9380 | { |
| 9381 | BOOL retval; |
| 9382 | ULARGE_INTEGER _, total, free; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9383 | |
| 9384 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 9385 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9386 | Py_END_ALLOW_THREADS |
| 9387 | if (retval == 0) |
| 9388 | return PyErr_SetFromWindowsErr(0); |
| 9389 | |
| 9390 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 9391 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9392 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9393 | |
| 9394 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9395 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 9396 | * It maps strings representing configuration variable names to |
| 9397 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 9398 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9399 | * rarely-used constants. There are three separate tables that use |
| 9400 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9401 | * |
| 9402 | * This code is always included, even if none of the interfaces that |
| 9403 | * need it are included. The #if hackery needed to avoid it would be |
| 9404 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9405 | */ |
| 9406 | struct constdef { |
| 9407 | char *name; |
| 9408 | long value; |
| 9409 | }; |
| 9410 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9411 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9412 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 9413 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9414 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9415 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9416 | *valuep = PyLong_AS_LONG(arg); |
| 9417 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9418 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 9419 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9420 | /* look up the value in the table using a binary search */ |
| 9421 | size_t lo = 0; |
| 9422 | size_t mid; |
| 9423 | size_t hi = tablesize; |
| 9424 | int cmp; |
| 9425 | const char *confname; |
| 9426 | if (!PyUnicode_Check(arg)) { |
| 9427 | PyErr_SetString(PyExc_TypeError, |
| 9428 | "configuration names must be strings or integers"); |
| 9429 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9430 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9431 | confname = _PyUnicode_AsString(arg); |
| 9432 | if (confname == NULL) |
| 9433 | return 0; |
| 9434 | while (lo < hi) { |
| 9435 | mid = (lo + hi) / 2; |
| 9436 | cmp = strcmp(confname, table[mid].name); |
| 9437 | if (cmp < 0) |
| 9438 | hi = mid; |
| 9439 | else if (cmp > 0) |
| 9440 | lo = mid + 1; |
| 9441 | else { |
| 9442 | *valuep = table[mid].value; |
| 9443 | return 1; |
| 9444 | } |
| 9445 | } |
| 9446 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 9447 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9448 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9449 | } |
| 9450 | |
| 9451 | |
| 9452 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 9453 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9454 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9455 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9456 | #endif |
| 9457 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9458 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9459 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9460 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9461 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9462 | #endif |
| 9463 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9464 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9465 | #endif |
| 9466 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9467 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9468 | #endif |
| 9469 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9470 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9471 | #endif |
| 9472 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9473 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9474 | #endif |
| 9475 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9476 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9477 | #endif |
| 9478 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9479 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9480 | #endif |
| 9481 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9482 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9483 | #endif |
| 9484 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9485 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9486 | #endif |
| 9487 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9488 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9489 | #endif |
| 9490 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9491 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9492 | #endif |
| 9493 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9494 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9495 | #endif |
| 9496 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9497 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9498 | #endif |
| 9499 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9500 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9501 | #endif |
| 9502 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9503 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9504 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 9505 | #ifdef _PC_ACL_ENABLED |
| 9506 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 9507 | #endif |
| 9508 | #ifdef _PC_MIN_HOLE_SIZE |
| 9509 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 9510 | #endif |
| 9511 | #ifdef _PC_ALLOC_SIZE_MIN |
| 9512 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 9513 | #endif |
| 9514 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 9515 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 9516 | #endif |
| 9517 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 9518 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 9519 | #endif |
| 9520 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 9521 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 9522 | #endif |
| 9523 | #ifdef _PC_REC_XFER_ALIGN |
| 9524 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 9525 | #endif |
| 9526 | #ifdef _PC_SYMLINK_MAX |
| 9527 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 9528 | #endif |
| 9529 | #ifdef _PC_XATTR_ENABLED |
| 9530 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 9531 | #endif |
| 9532 | #ifdef _PC_XATTR_EXISTS |
| 9533 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 9534 | #endif |
| 9535 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 9536 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 9537 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9538 | }; |
| 9539 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9540 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9541 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9542 | { |
| 9543 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 9544 | sizeof(posix_constants_pathconf) |
| 9545 | / sizeof(struct constdef)); |
| 9546 | } |
| 9547 | #endif |
| 9548 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9549 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9550 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9551 | /*[clinic input] |
| 9552 | os.fpathconf -> long |
| 9553 | |
| 9554 | fd: int |
| 9555 | name: path_confname |
| 9556 | / |
| 9557 | |
| 9558 | Return the configuration limit name for the file descriptor fd. |
| 9559 | |
| 9560 | If there is no limit, return -1. |
| 9561 | [clinic start generated code]*/ |
| 9562 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9563 | static long |
| 9564 | os_fpathconf_impl(PyModuleDef *module, int fd, int name) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9565 | /*[clinic end generated code: output=082b2922d4441de7 input=5942a024d3777810]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9566 | { |
| 9567 | long limit; |
| 9568 | |
| 9569 | errno = 0; |
| 9570 | limit = fpathconf(fd, name); |
| 9571 | if (limit == -1 && errno != 0) |
| 9572 | posix_error(); |
| 9573 | |
| 9574 | return limit; |
| 9575 | } |
| 9576 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9577 | |
| 9578 | |
| 9579 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9580 | /*[clinic input] |
| 9581 | os.pathconf -> long |
| 9582 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 9583 | name: path_confname |
| 9584 | |
| 9585 | Return the configuration limit name for the file or directory path. |
| 9586 | |
| 9587 | If there is no limit, return -1. |
| 9588 | On some platforms, path may also be specified as an open file descriptor. |
| 9589 | If this functionality is unavailable, using it raises an exception. |
| 9590 | [clinic start generated code]*/ |
| 9591 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9592 | static long |
| 9593 | os_pathconf_impl(PyModuleDef *module, path_t *path, int name) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9594 | /*[clinic end generated code: output=3713029e9501f5ab input=bc3e2a985af27e5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9595 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9596 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9597 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9598 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9599 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9600 | if (path->fd != -1) |
| 9601 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9602 | else |
| 9603 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9604 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9605 | if (limit == -1 && errno != 0) { |
| 9606 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 9607 | /* could be a path or name problem */ |
| 9608 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9609 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9610 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9611 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9612 | |
| 9613 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9614 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9615 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9616 | |
| 9617 | #ifdef HAVE_CONFSTR |
| 9618 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9619 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9620 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9621 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9622 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9623 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9624 | #endif |
| 9625 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9626 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9627 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9628 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9629 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9630 | #endif |
| 9631 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9632 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9633 | #endif |
| 9634 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9635 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9636 | #endif |
| 9637 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9638 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9639 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9640 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9641 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9642 | #endif |
| 9643 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9644 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9645 | #endif |
| 9646 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9647 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9648 | #endif |
| 9649 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9650 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9651 | #endif |
| 9652 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9653 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9654 | #endif |
| 9655 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9656 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9657 | #endif |
| 9658 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9659 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9660 | #endif |
| 9661 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9662 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9663 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9664 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9665 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9666 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9667 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9668 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9669 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9670 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9671 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9672 | #endif |
| 9673 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9674 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9675 | #endif |
| 9676 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9677 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9678 | #endif |
| 9679 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9680 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9681 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9682 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9683 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9684 | #endif |
| 9685 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9686 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9687 | #endif |
| 9688 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9689 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9690 | #endif |
| 9691 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9692 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9693 | #endif |
| 9694 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9695 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9696 | #endif |
| 9697 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9698 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9699 | #endif |
| 9700 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9701 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9702 | #endif |
| 9703 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9704 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9705 | #endif |
| 9706 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9707 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9708 | #endif |
| 9709 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9710 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9711 | #endif |
| 9712 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9713 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9714 | #endif |
| 9715 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9716 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9717 | #endif |
| 9718 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9719 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9720 | #endif |
| 9721 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9722 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9723 | #endif |
| 9724 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9725 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9726 | #endif |
| 9727 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9728 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9729 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9730 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9731 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9732 | #endif |
| 9733 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9734 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9735 | #endif |
| 9736 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9737 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9738 | #endif |
| 9739 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9740 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9741 | #endif |
| 9742 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9743 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9744 | #endif |
| 9745 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9746 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9747 | #endif |
| 9748 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9749 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9750 | #endif |
| 9751 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9752 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9753 | #endif |
| 9754 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9755 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9756 | #endif |
| 9757 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9758 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9759 | #endif |
| 9760 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9761 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9762 | #endif |
| 9763 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9764 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9765 | #endif |
| 9766 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9767 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9768 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9769 | }; |
| 9770 | |
| 9771 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9772 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9773 | { |
| 9774 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 9775 | sizeof(posix_constants_confstr) |
| 9776 | / sizeof(struct constdef)); |
| 9777 | } |
| 9778 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9779 | |
| 9780 | /*[clinic input] |
| 9781 | os.confstr |
| 9782 | |
| 9783 | name: confstr_confname |
| 9784 | / |
| 9785 | |
| 9786 | Return a string-valued system configuration variable. |
| 9787 | [clinic start generated code]*/ |
| 9788 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9789 | static PyObject * |
| 9790 | os_confstr_impl(PyModuleDef *module, int name) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9791 | /*[clinic end generated code: output=6ff79c9eed8c2daf input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9792 | { |
| 9793 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9794 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9795 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9796 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9797 | errno = 0; |
| 9798 | len = confstr(name, buffer, sizeof(buffer)); |
| 9799 | if (len == 0) { |
| 9800 | if (errno) { |
| 9801 | posix_error(); |
| 9802 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9803 | } |
| 9804 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9805 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9806 | } |
| 9807 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9808 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 9809 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 9810 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9811 | char *buf = PyMem_Malloc(len); |
| 9812 | if (buf == NULL) |
| 9813 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 9814 | len2 = confstr(name, buf, len); |
| 9815 | assert(len == len2); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9816 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 9817 | PyMem_Free(buf); |
| 9818 | } |
| 9819 | else |
| 9820 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9821 | return result; |
| 9822 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9823 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9824 | |
| 9825 | |
| 9826 | #ifdef HAVE_SYSCONF |
| 9827 | static struct constdef posix_constants_sysconf[] = { |
| 9828 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9829 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9830 | #endif |
| 9831 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9832 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9833 | #endif |
| 9834 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9835 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9836 | #endif |
| 9837 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9838 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9839 | #endif |
| 9840 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9841 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9842 | #endif |
| 9843 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9844 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9845 | #endif |
| 9846 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9847 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9848 | #endif |
| 9849 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9850 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9851 | #endif |
| 9852 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9853 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9854 | #endif |
| 9855 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9856 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9857 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9858 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9859 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9860 | #endif |
| 9861 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9862 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9863 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9864 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9865 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9866 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9867 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9868 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9869 | #endif |
| 9870 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9871 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9872 | #endif |
| 9873 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9874 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9875 | #endif |
| 9876 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9877 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9878 | #endif |
| 9879 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9880 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9881 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9882 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9883 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9884 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9885 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9886 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9887 | #endif |
| 9888 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9889 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9890 | #endif |
| 9891 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9892 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9893 | #endif |
| 9894 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9895 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9896 | #endif |
| 9897 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9898 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9899 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9900 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9901 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9902 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9903 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9904 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9905 | #endif |
| 9906 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9907 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9908 | #endif |
| 9909 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9910 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9911 | #endif |
| 9912 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9913 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9914 | #endif |
| 9915 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9916 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9917 | #endif |
| 9918 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9919 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9920 | #endif |
| 9921 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9922 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9923 | #endif |
| 9924 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9925 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9926 | #endif |
| 9927 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9928 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9929 | #endif |
| 9930 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9931 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9932 | #endif |
| 9933 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9934 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9935 | #endif |
| 9936 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9937 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9938 | #endif |
| 9939 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9940 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9941 | #endif |
| 9942 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9943 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9944 | #endif |
| 9945 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9946 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9947 | #endif |
| 9948 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9949 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9950 | #endif |
| 9951 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9952 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9953 | #endif |
| 9954 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9955 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9956 | #endif |
| 9957 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9958 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9959 | #endif |
| 9960 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9961 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9962 | #endif |
| 9963 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9964 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9965 | #endif |
| 9966 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9967 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9968 | #endif |
| 9969 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9970 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9971 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9972 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9973 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9974 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9975 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9976 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9977 | #endif |
| 9978 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9979 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9980 | #endif |
| 9981 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9982 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9983 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9984 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9985 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9986 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9987 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9988 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9989 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9990 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9991 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9992 | #endif |
| 9993 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9994 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9995 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9996 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9997 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9998 | #endif |
| 9999 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10000 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10001 | #endif |
| 10002 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10003 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10004 | #endif |
| 10005 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10006 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10007 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10008 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10009 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10010 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10011 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10012 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10013 | #endif |
| 10014 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10015 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10016 | #endif |
| 10017 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10018 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10019 | #endif |
| 10020 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10021 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10022 | #endif |
| 10023 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10024 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10025 | #endif |
| 10026 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10027 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10028 | #endif |
| 10029 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10030 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10031 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10032 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10033 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10034 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10035 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10036 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10037 | #endif |
| 10038 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10039 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10040 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10041 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10042 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10043 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10044 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10045 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10046 | #endif |
| 10047 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10048 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10049 | #endif |
| 10050 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10051 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10052 | #endif |
| 10053 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10054 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10055 | #endif |
| 10056 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10057 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10058 | #endif |
| 10059 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10060 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10061 | #endif |
| 10062 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10063 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10064 | #endif |
| 10065 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10066 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10067 | #endif |
| 10068 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10069 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10070 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10071 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10072 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10073 | #endif |
| 10074 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10075 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10076 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10077 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10078 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10079 | #endif |
| 10080 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10081 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10082 | #endif |
| 10083 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10084 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10085 | #endif |
| 10086 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10087 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10088 | #endif |
| 10089 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10090 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10091 | #endif |
| 10092 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10093 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10094 | #endif |
| 10095 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10096 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10097 | #endif |
| 10098 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10099 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10100 | #endif |
| 10101 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10102 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10103 | #endif |
| 10104 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10105 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10106 | #endif |
| 10107 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10108 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10109 | #endif |
| 10110 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10111 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10112 | #endif |
| 10113 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10114 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10115 | #endif |
| 10116 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10117 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10118 | #endif |
| 10119 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10120 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10121 | #endif |
| 10122 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10123 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10124 | #endif |
| 10125 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10126 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10127 | #endif |
| 10128 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10129 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10130 | #endif |
| 10131 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10132 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10133 | #endif |
| 10134 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10135 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10136 | #endif |
| 10137 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10138 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10139 | #endif |
| 10140 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10141 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10142 | #endif |
| 10143 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10144 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10145 | #endif |
| 10146 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10147 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10148 | #endif |
| 10149 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10150 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10151 | #endif |
| 10152 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10153 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10154 | #endif |
| 10155 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10156 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10157 | #endif |
| 10158 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10159 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10160 | #endif |
| 10161 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10162 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10163 | #endif |
| 10164 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10165 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10166 | #endif |
| 10167 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10168 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10169 | #endif |
| 10170 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10171 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10172 | #endif |
| 10173 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10174 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10175 | #endif |
| 10176 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10177 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10178 | #endif |
| 10179 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10180 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10181 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10182 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10183 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10184 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10185 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10186 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10187 | #endif |
| 10188 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10189 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10190 | #endif |
| 10191 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10192 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10193 | #endif |
| 10194 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10195 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10196 | #endif |
| 10197 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10198 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10199 | #endif |
| 10200 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10201 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10202 | #endif |
| 10203 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10204 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10205 | #endif |
| 10206 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10207 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10208 | #endif |
| 10209 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10210 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10211 | #endif |
| 10212 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10213 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10214 | #endif |
| 10215 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10216 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10217 | #endif |
| 10218 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10219 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10220 | #endif |
| 10221 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10222 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10223 | #endif |
| 10224 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10225 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10226 | #endif |
| 10227 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10228 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10229 | #endif |
| 10230 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10231 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10232 | #endif |
| 10233 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10234 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10235 | #endif |
| 10236 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10237 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10238 | #endif |
| 10239 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10240 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10241 | #endif |
| 10242 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10243 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10244 | #endif |
| 10245 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10246 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10247 | #endif |
| 10248 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10249 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10250 | #endif |
| 10251 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10252 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10253 | #endif |
| 10254 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10255 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10256 | #endif |
| 10257 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10258 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10259 | #endif |
| 10260 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10261 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10262 | #endif |
| 10263 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10264 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10265 | #endif |
| 10266 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10267 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10268 | #endif |
| 10269 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10270 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10271 | #endif |
| 10272 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10273 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10274 | #endif |
| 10275 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10276 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10277 | #endif |
| 10278 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10279 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10280 | #endif |
| 10281 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10282 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10283 | #endif |
| 10284 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10285 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10286 | #endif |
| 10287 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10288 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10289 | #endif |
| 10290 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10291 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10292 | #endif |
| 10293 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10294 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10295 | #endif |
| 10296 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10297 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10298 | #endif |
| 10299 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10300 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10301 | #endif |
| 10302 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10303 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10304 | #endif |
| 10305 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10306 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10307 | #endif |
| 10308 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10309 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10310 | #endif |
| 10311 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10312 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10313 | #endif |
| 10314 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10315 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10316 | #endif |
| 10317 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10318 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10319 | #endif |
| 10320 | }; |
| 10321 | |
| 10322 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10323 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10324 | { |
| 10325 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 10326 | sizeof(posix_constants_sysconf) |
| 10327 | / sizeof(struct constdef)); |
| 10328 | } |
| 10329 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10330 | |
| 10331 | /*[clinic input] |
| 10332 | os.sysconf -> long |
| 10333 | name: sysconf_confname |
| 10334 | / |
| 10335 | |
| 10336 | Return an integer-valued system configuration variable. |
| 10337 | [clinic start generated code]*/ |
| 10338 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10339 | static long |
| 10340 | os_sysconf_impl(PyModuleDef *module, int name) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10341 | /*[clinic end generated code: output=ed567306f58d69c4 input=279e3430a33f29e4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10342 | { |
| 10343 | long value; |
| 10344 | |
| 10345 | errno = 0; |
| 10346 | value = sysconf(name); |
| 10347 | if (value == -1 && errno != 0) |
| 10348 | posix_error(); |
| 10349 | return value; |
| 10350 | } |
| 10351 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10352 | |
| 10353 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10354 | /* This code is used to ensure that the tables of configuration value names |
Serhiy Storchaka | 56a6d85 | 2014-12-01 18:28:43 +0200 | [diff] [blame] | 10355 | * are in sorted order as required by conv_confname(), and also to build |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10356 | * the exported dictionaries that are used to publish information about the |
| 10357 | * names available on the host platform. |
| 10358 | * |
| 10359 | * Sorting the table at runtime ensures that the table is properly ordered |
| 10360 | * when used, even for platforms we're not able to test on. It also makes |
| 10361 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10362 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10363 | |
| 10364 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10365 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10366 | { |
| 10367 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10368 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10369 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10370 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10371 | |
| 10372 | return strcmp(c1->name, c2->name); |
| 10373 | } |
| 10374 | |
| 10375 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10376 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10377 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10378 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10379 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10380 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10381 | |
| 10382 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 10383 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10384 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10385 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10386 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10387 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10388 | PyObject *o = PyLong_FromLong(table[i].value); |
| 10389 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 10390 | Py_XDECREF(o); |
| 10391 | Py_DECREF(d); |
| 10392 | return -1; |
| 10393 | } |
| 10394 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10395 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10396 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10397 | } |
| 10398 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10399 | /* Return -1 on failure, 0 on success. */ |
| 10400 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10401 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10402 | { |
| 10403 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10404 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10405 | sizeof(posix_constants_pathconf) |
| 10406 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10407 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10408 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10409 | #endif |
| 10410 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10411 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10412 | sizeof(posix_constants_confstr) |
| 10413 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10414 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10415 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10416 | #endif |
| 10417 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10418 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10419 | sizeof(posix_constants_sysconf) |
| 10420 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10421 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10422 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10423 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10424 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10425 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10426 | |
| 10427 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10428 | /*[clinic input] |
| 10429 | os.abort |
| 10430 | |
| 10431 | Abort the interpreter immediately. |
| 10432 | |
| 10433 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 10434 | on the hosting operating system. This function never returns. |
| 10435 | [clinic start generated code]*/ |
| 10436 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10437 | static PyObject * |
| 10438 | os_abort_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10439 | /*[clinic end generated code: output=486bb96647c299b3 input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10440 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10441 | abort(); |
| 10442 | /*NOTREACHED*/ |
| 10443 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 10444 | return NULL; |
| 10445 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10446 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10447 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10448 | /* AC 3.5: change to path_t? but that might change exceptions */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10449 | PyDoc_STRVAR(win32_startfile__doc__, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10450 | "startfile(filepath [, operation])\n\ |
| 10451 | \n\ |
| 10452 | Start a file with its associated application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10453 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 10454 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 10455 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 10456 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 10457 | application (if any) its extension is associated.\n\ |
| 10458 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 10459 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10460 | \n\ |
| 10461 | startfile returns as soon as the associated application is launched.\n\ |
| 10462 | There is no option to wait for the application to close, and no way\n\ |
| 10463 | to retrieve the application's exit status.\n\ |
| 10464 | \n\ |
| 10465 | The filepath is relative to the current directory. If you want to use\n\ |
| 10466 | 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] | 10467 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10468 | |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10469 | /* Grab ShellExecute dynamically from shell32 */ |
| 10470 | static int has_ShellExecute = -1; |
| 10471 | static HINSTANCE (CALLBACK *Py_ShellExecuteA)(HWND, LPCSTR, LPCSTR, LPCSTR, |
| 10472 | LPCSTR, INT); |
| 10473 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 10474 | LPCWSTR, INT); |
| 10475 | static int |
| 10476 | check_ShellExecute() |
| 10477 | { |
| 10478 | HINSTANCE hShell32; |
| 10479 | |
| 10480 | /* only recheck */ |
| 10481 | if (-1 == has_ShellExecute) { |
| 10482 | Py_BEGIN_ALLOW_THREADS |
| 10483 | hShell32 = LoadLibraryW(L"SHELL32"); |
| 10484 | Py_END_ALLOW_THREADS |
| 10485 | if (hShell32) { |
| 10486 | *(FARPROC*)&Py_ShellExecuteA = GetProcAddress(hShell32, |
| 10487 | "ShellExecuteA"); |
| 10488 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 10489 | "ShellExecuteW"); |
| 10490 | has_ShellExecute = Py_ShellExecuteA && |
| 10491 | Py_ShellExecuteW; |
| 10492 | } else { |
| 10493 | has_ShellExecute = 0; |
| 10494 | } |
| 10495 | } |
| 10496 | return has_ShellExecute; |
| 10497 | } |
| 10498 | |
| 10499 | |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10500 | static PyObject * |
| 10501 | win32_startfile(PyObject *self, PyObject *args) |
| 10502 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10503 | PyObject *ofilepath; |
| 10504 | char *filepath; |
| 10505 | char *operation = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10506 | wchar_t *wpath, *woperation; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10507 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 10508 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10509 | PyObject *unipath, *uoperation = NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10510 | |
| 10511 | if(!check_ShellExecute()) { |
| 10512 | /* If the OS doesn't have ShellExecute, return a |
| 10513 | NotImplementedError. */ |
| 10514 | return PyErr_Format(PyExc_NotImplementedError, |
| 10515 | "startfile not available on this platform"); |
| 10516 | } |
| 10517 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10518 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 10519 | &unipath, &operation)) { |
| 10520 | PyErr_Clear(); |
| 10521 | goto normal; |
| 10522 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10523 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10524 | if (operation) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10525 | uoperation = PyUnicode_DecodeASCII(operation, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10526 | strlen(operation), NULL); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10527 | if (!uoperation) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10528 | PyErr_Clear(); |
| 10529 | operation = NULL; |
| 10530 | goto normal; |
| 10531 | } |
| 10532 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10533 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10534 | wpath = PyUnicode_AsUnicode(unipath); |
| 10535 | if (wpath == NULL) |
| 10536 | goto normal; |
| 10537 | if (uoperation) { |
| 10538 | woperation = PyUnicode_AsUnicode(uoperation); |
| 10539 | if (woperation == NULL) |
| 10540 | goto normal; |
| 10541 | } |
| 10542 | else |
| 10543 | woperation = NULL; |
| 10544 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10545 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10546 | rc = Py_ShellExecuteW((HWND)0, woperation, wpath, |
| 10547 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10548 | Py_END_ALLOW_THREADS |
| 10549 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10550 | Py_XDECREF(uoperation); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10551 | if (rc <= (HINSTANCE)32) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10552 | win32_error_object("startfile", unipath); |
| 10553 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10554 | } |
| 10555 | Py_INCREF(Py_None); |
| 10556 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10557 | |
| 10558 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10559 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 10560 | PyUnicode_FSConverter, &ofilepath, |
| 10561 | &operation)) |
| 10562 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 10563 | if (win32_warn_bytes_api()) { |
| 10564 | Py_DECREF(ofilepath); |
| 10565 | return NULL; |
| 10566 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10567 | filepath = PyBytes_AsString(ofilepath); |
| 10568 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 10569 | rc = Py_ShellExecuteA((HWND)0, operation, filepath, |
| 10570 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10571 | Py_END_ALLOW_THREADS |
| 10572 | if (rc <= (HINSTANCE)32) { |
| 10573 | PyObject *errval = win32_error("startfile", filepath); |
| 10574 | Py_DECREF(ofilepath); |
| 10575 | return errval; |
| 10576 | } |
| 10577 | Py_DECREF(ofilepath); |
| 10578 | Py_INCREF(Py_None); |
| 10579 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10580 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10581 | #endif /* MS_WINDOWS */ |
| 10582 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10583 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10584 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10585 | /*[clinic input] |
| 10586 | os.getloadavg |
| 10587 | |
| 10588 | Return average recent system load information. |
| 10589 | |
| 10590 | Return the number of processes in the system run queue averaged over |
| 10591 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 10592 | Raises OSError if the load average was unobtainable. |
| 10593 | [clinic start generated code]*/ |
| 10594 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10595 | static PyObject * |
| 10596 | os_getloadavg_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10597 | /*[clinic end generated code: output=2b64c5b675d74c14 input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10598 | { |
| 10599 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10600 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10601 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 10602 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10603 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10604 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10605 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10606 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10607 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10608 | |
| 10609 | /*[clinic input] |
| 10610 | os.device_encoding |
| 10611 | fd: int |
| 10612 | |
| 10613 | Return a string describing the encoding of a terminal's file descriptor. |
| 10614 | |
| 10615 | The file descriptor must be attached to a terminal. |
| 10616 | If the device is not a terminal, return None. |
| 10617 | [clinic start generated code]*/ |
| 10618 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10619 | static PyObject * |
| 10620 | os_device_encoding_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10621 | /*[clinic end generated code: output=34f14e33468419c1 input=9e1d4a42b66df312]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10622 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10623 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 10624 | } |
| 10625 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10626 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10627 | #ifdef HAVE_SETRESUID |
| 10628 | /*[clinic input] |
| 10629 | os.setresuid |
| 10630 | |
| 10631 | ruid: uid_t |
| 10632 | euid: uid_t |
| 10633 | suid: uid_t |
| 10634 | / |
| 10635 | |
| 10636 | Set the current process's real, effective, and saved user ids. |
| 10637 | [clinic start generated code]*/ |
| 10638 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10639 | static PyObject * |
| 10640 | os_setresuid_impl(PyModuleDef *module, uid_t ruid, uid_t euid, uid_t suid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10641 | /*[clinic end generated code: output=92cc330812c6ed0f input=9e33cb79a82792f3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10642 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10643 | if (setresuid(ruid, euid, suid) < 0) |
| 10644 | return posix_error(); |
| 10645 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10646 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10647 | #endif /* HAVE_SETRESUID */ |
| 10648 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10649 | |
| 10650 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10651 | /*[clinic input] |
| 10652 | os.setresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10653 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10654 | rgid: gid_t |
| 10655 | egid: gid_t |
| 10656 | sgid: gid_t |
| 10657 | / |
| 10658 | |
| 10659 | Set the current process's real, effective, and saved group ids. |
| 10660 | [clinic start generated code]*/ |
| 10661 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10662 | static PyObject * |
| 10663 | os_setresgid_impl(PyModuleDef *module, gid_t rgid, gid_t egid, gid_t sgid) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10664 | /*[clinic end generated code: output=e91dc4842a604429 input=33e9e0785ef426b1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10665 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10666 | if (setresgid(rgid, egid, sgid) < 0) |
| 10667 | return posix_error(); |
| 10668 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10669 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10670 | #endif /* HAVE_SETRESGID */ |
| 10671 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10672 | |
| 10673 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10674 | /*[clinic input] |
| 10675 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10676 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10677 | Return a tuple of the current process's real, effective, and saved user ids. |
| 10678 | [clinic start generated code]*/ |
| 10679 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10680 | static PyObject * |
| 10681 | os_getresuid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10682 | /*[clinic end generated code: output=9ddef62faae8e477 input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10683 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10684 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10685 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 10686 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10687 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 10688 | _PyLong_FromUid(euid), |
| 10689 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10690 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10691 | #endif /* HAVE_GETRESUID */ |
| 10692 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10693 | |
| 10694 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10695 | /*[clinic input] |
| 10696 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10697 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10698 | Return a tuple of the current process's real, effective, and saved group ids. |
| 10699 | [clinic start generated code]*/ |
| 10700 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10701 | static PyObject * |
| 10702 | os_getresgid_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10703 | /*[clinic end generated code: output=e1a553cbcf16234c input=517e68db9ca32df6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10704 | { |
| 10705 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10706 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 10707 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 10708 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 10709 | _PyLong_FromGid(egid), |
| 10710 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10711 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10712 | #endif /* HAVE_GETRESGID */ |
| 10713 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10714 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10715 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10716 | /*[clinic input] |
| 10717 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10718 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10719 | path: path_t(allow_fd=True) |
| 10720 | attribute: path_t |
| 10721 | * |
| 10722 | follow_symlinks: bool = True |
| 10723 | |
| 10724 | Return the value of extended attribute attribute on path. |
| 10725 | |
| 10726 | path may be either a string or an open file descriptor. |
| 10727 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10728 | link, getxattr will examine the symbolic link itself instead of the file |
| 10729 | the link points to. |
| 10730 | |
| 10731 | [clinic start generated code]*/ |
| 10732 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10733 | static PyObject * |
| 10734 | os_getxattr_impl(PyModuleDef *module, path_t *path, path_t *attribute, int follow_symlinks) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10735 | /*[clinic end generated code: output=d90086b314859f8b input=8c8ea3bab78d89c2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10736 | { |
| 10737 | Py_ssize_t i; |
| 10738 | PyObject *buffer = NULL; |
| 10739 | |
| 10740 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 10741 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10742 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10743 | for (i = 0; ; i++) { |
| 10744 | void *ptr; |
| 10745 | ssize_t result; |
| 10746 | static Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
| 10747 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10748 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10749 | path_error(path); |
| 10750 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10751 | } |
| 10752 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 10753 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10754 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10755 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10756 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10757 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10758 | if (path->fd >= 0) |
| 10759 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10760 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10761 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10762 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10763 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10764 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10765 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10766 | if (result < 0) { |
| 10767 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10768 | if (errno == ERANGE) |
| 10769 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10770 | path_error(path); |
| 10771 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10772 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10773 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10774 | if (result != buffer_size) { |
| 10775 | /* Can only shrink. */ |
| 10776 | _PyBytes_Resize(&buffer, result); |
| 10777 | } |
| 10778 | break; |
| 10779 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10780 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10781 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10782 | } |
| 10783 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10784 | |
| 10785 | /*[clinic input] |
| 10786 | os.setxattr |
| 10787 | |
| 10788 | path: path_t(allow_fd=True) |
| 10789 | attribute: path_t |
| 10790 | value: Py_buffer |
| 10791 | flags: int = 0 |
| 10792 | * |
| 10793 | follow_symlinks: bool = True |
| 10794 | |
| 10795 | Set extended attribute attribute on path to value. |
| 10796 | |
| 10797 | path may be either a string or an open file descriptor. |
| 10798 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10799 | link, setxattr will modify the symbolic link itself instead of the file |
| 10800 | the link points to. |
| 10801 | |
| 10802 | [clinic start generated code]*/ |
| 10803 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10804 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10805 | os_setxattr_impl(PyModuleDef *module, path_t *path, path_t *attribute, Py_buffer *value, int flags, int follow_symlinks) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10806 | /*[clinic end generated code: output=e3defa5c4b1ad0ae input=f0d26833992015c2]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10807 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10808 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10809 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10810 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10811 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10812 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10813 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10814 | if (path->fd > -1) |
| 10815 | result = fsetxattr(path->fd, attribute->narrow, |
| 10816 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10817 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10818 | result = setxattr(path->narrow, attribute->narrow, |
| 10819 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10820 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10821 | result = lsetxattr(path->narrow, attribute->narrow, |
| 10822 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10823 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10824 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10825 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10826 | path_error(path); |
| 10827 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10828 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10829 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10830 | Py_RETURN_NONE; |
| 10831 | } |
| 10832 | |
| 10833 | |
| 10834 | /*[clinic input] |
| 10835 | os.removexattr |
| 10836 | |
| 10837 | path: path_t(allow_fd=True) |
| 10838 | attribute: path_t |
| 10839 | * |
| 10840 | follow_symlinks: bool = True |
| 10841 | |
| 10842 | Remove extended attribute attribute on path. |
| 10843 | |
| 10844 | path may be either a string or an open file descriptor. |
| 10845 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10846 | link, removexattr will modify the symbolic link itself instead of the file |
| 10847 | the link points to. |
| 10848 | |
| 10849 | [clinic start generated code]*/ |
| 10850 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10851 | static PyObject * |
| 10852 | os_removexattr_impl(PyModuleDef *module, path_t *path, path_t *attribute, int follow_symlinks) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10853 | /*[clinic end generated code: output=4870ec90249af875 input=cdb54834161e3329]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10854 | { |
| 10855 | ssize_t result; |
| 10856 | |
| 10857 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 10858 | return NULL; |
| 10859 | |
| 10860 | Py_BEGIN_ALLOW_THREADS; |
| 10861 | if (path->fd > -1) |
| 10862 | result = fremovexattr(path->fd, attribute->narrow); |
| 10863 | else if (follow_symlinks) |
| 10864 | result = removexattr(path->narrow, attribute->narrow); |
| 10865 | else |
| 10866 | result = lremovexattr(path->narrow, attribute->narrow); |
| 10867 | Py_END_ALLOW_THREADS; |
| 10868 | |
| 10869 | if (result) { |
| 10870 | return path_error(path); |
| 10871 | } |
| 10872 | |
| 10873 | Py_RETURN_NONE; |
| 10874 | } |
| 10875 | |
| 10876 | |
| 10877 | /*[clinic input] |
| 10878 | os.listxattr |
| 10879 | |
| 10880 | path: path_t(allow_fd=True, nullable=True) = None |
| 10881 | * |
| 10882 | follow_symlinks: bool = True |
| 10883 | |
| 10884 | Return a list of extended attributes on path. |
| 10885 | |
| 10886 | path may be either None, a string, or an open file descriptor. |
| 10887 | if path is None, listxattr will examine the current directory. |
| 10888 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 10889 | link, listxattr will examine the symbolic link itself instead of the file |
| 10890 | the link points to. |
| 10891 | [clinic start generated code]*/ |
| 10892 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10893 | static PyObject * |
| 10894 | os_listxattr_impl(PyModuleDef *module, path_t *path, int follow_symlinks) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10895 | /*[clinic end generated code: output=a87ad6ce56e42a4f input=08cca53ac0b07c13]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10896 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10897 | Py_ssize_t i; |
| 10898 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10899 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10900 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10901 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10902 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10903 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10904 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10905 | name = path->narrow ? path->narrow : "."; |
| 10906 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10907 | for (i = 0; ; i++) { |
| 10908 | char *start, *trace, *end; |
| 10909 | ssize_t length; |
| 10910 | static Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
| 10911 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10912 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 10913 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10914 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10915 | break; |
| 10916 | } |
| 10917 | buffer = PyMem_MALLOC(buffer_size); |
| 10918 | if (!buffer) { |
| 10919 | PyErr_NoMemory(); |
| 10920 | break; |
| 10921 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10922 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10923 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10924 | if (path->fd > -1) |
| 10925 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10926 | else if (follow_symlinks) |
| 10927 | length = listxattr(name, buffer, buffer_size); |
| 10928 | else |
| 10929 | length = llistxattr(name, buffer, buffer_size); |
| 10930 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10931 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10932 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 10933 | if (errno == ERANGE) { |
| 10934 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 10935 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10936 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 10937 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10938 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10939 | break; |
| 10940 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10941 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10942 | result = PyList_New(0); |
| 10943 | if (!result) { |
| 10944 | goto exit; |
| 10945 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10946 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10947 | end = buffer + length; |
| 10948 | for (trace = start = buffer; trace != end; trace++) { |
| 10949 | if (!*trace) { |
| 10950 | int error; |
| 10951 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 10952 | trace - start); |
| 10953 | if (!attribute) { |
| 10954 | Py_DECREF(result); |
| 10955 | result = NULL; |
| 10956 | goto exit; |
| 10957 | } |
| 10958 | error = PyList_Append(result, attribute); |
| 10959 | Py_DECREF(attribute); |
| 10960 | if (error) { |
| 10961 | Py_DECREF(result); |
| 10962 | result = NULL; |
| 10963 | goto exit; |
| 10964 | } |
| 10965 | start = trace + 1; |
| 10966 | } |
| 10967 | } |
| 10968 | break; |
| 10969 | } |
| 10970 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10971 | if (buffer) |
| 10972 | PyMem_FREE(buffer); |
| 10973 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10974 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10975 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10976 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10977 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10978 | /*[clinic input] |
| 10979 | os.urandom |
| 10980 | |
| 10981 | size: Py_ssize_t |
| 10982 | / |
| 10983 | |
| 10984 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 10985 | [clinic start generated code]*/ |
| 10986 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10987 | static PyObject * |
| 10988 | os_urandom_impl(PyModuleDef *module, Py_ssize_t size) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 10989 | /*[clinic end generated code: output=e0011f021501f03b input=4067cdb1b6776c29]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10990 | { |
| 10991 | PyObject *bytes; |
| 10992 | int result; |
| 10993 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10994 | if (size < 0) |
| 10995 | return PyErr_Format(PyExc_ValueError, |
| 10996 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10997 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 10998 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10999 | return NULL; |
| 11000 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11001 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), |
| 11002 | PyBytes_GET_SIZE(bytes)); |
| 11003 | if (result == -1) { |
| 11004 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11005 | return NULL; |
| 11006 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11007 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11008 | } |
| 11009 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11010 | /* Terminal size querying */ |
| 11011 | |
| 11012 | static PyTypeObject TerminalSizeType; |
| 11013 | |
| 11014 | PyDoc_STRVAR(TerminalSize_docstring, |
| 11015 | "A tuple of (columns, lines) for holding terminal window size"); |
| 11016 | |
| 11017 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 11018 | {"columns", "width of the terminal window in characters"}, |
| 11019 | {"lines", "height of the terminal window in characters"}, |
| 11020 | {NULL, NULL} |
| 11021 | }; |
| 11022 | |
| 11023 | static PyStructSequence_Desc TerminalSize_desc = { |
| 11024 | "os.terminal_size", |
| 11025 | TerminalSize_docstring, |
| 11026 | TerminalSize_fields, |
| 11027 | 2, |
| 11028 | }; |
| 11029 | |
| 11030 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11031 | /* AC 3.5: fd should accept None */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11032 | PyDoc_STRVAR(termsize__doc__, |
| 11033 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 11034 | "\n" \ |
| 11035 | "The optional argument fd (default standard output) specifies\n" \ |
| 11036 | "which file descriptor should be queried.\n" \ |
| 11037 | "\n" \ |
| 11038 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 11039 | "is thrown.\n" \ |
| 11040 | "\n" \ |
| 11041 | "This function will only be defined if an implementation is\n" \ |
| 11042 | "available for this system.\n" \ |
| 11043 | "\n" \ |
| 11044 | "shutil.get_terminal_size is the high-level function which should \n" \ |
| 11045 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 11046 | |
| 11047 | static PyObject* |
| 11048 | get_terminal_size(PyObject *self, PyObject *args) |
| 11049 | { |
| 11050 | int columns, lines; |
| 11051 | PyObject *termsize; |
| 11052 | |
| 11053 | int fd = fileno(stdout); |
| 11054 | /* Under some conditions stdout may not be connected and |
| 11055 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 11056 | * GUI apps don't have valid standard streams by default. |
| 11057 | * |
| 11058 | * If this happens, and the optional fd argument is not present, |
| 11059 | * the ioctl below will fail returning EBADF. This is what we want. |
| 11060 | */ |
| 11061 | |
| 11062 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 11063 | return NULL; |
| 11064 | |
| 11065 | #ifdef TERMSIZE_USE_IOCTL |
| 11066 | { |
| 11067 | struct winsize w; |
| 11068 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 11069 | return PyErr_SetFromErrno(PyExc_OSError); |
| 11070 | columns = w.ws_col; |
| 11071 | lines = w.ws_row; |
| 11072 | } |
| 11073 | #endif /* TERMSIZE_USE_IOCTL */ |
| 11074 | |
| 11075 | #ifdef TERMSIZE_USE_CONIO |
| 11076 | { |
| 11077 | DWORD nhandle; |
| 11078 | HANDLE handle; |
| 11079 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 11080 | switch (fd) { |
| 11081 | case 0: nhandle = STD_INPUT_HANDLE; |
| 11082 | break; |
| 11083 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 11084 | break; |
| 11085 | case 2: nhandle = STD_ERROR_HANDLE; |
| 11086 | break; |
| 11087 | default: |
| 11088 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 11089 | } |
| 11090 | handle = GetStdHandle(nhandle); |
| 11091 | if (handle == NULL) |
| 11092 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 11093 | if (handle == INVALID_HANDLE_VALUE) |
| 11094 | return PyErr_SetFromWindowsErr(0); |
| 11095 | |
| 11096 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 11097 | return PyErr_SetFromWindowsErr(0); |
| 11098 | |
| 11099 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 11100 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 11101 | } |
| 11102 | #endif /* TERMSIZE_USE_CONIO */ |
| 11103 | |
| 11104 | termsize = PyStructSequence_New(&TerminalSizeType); |
| 11105 | if (termsize == NULL) |
| 11106 | return NULL; |
| 11107 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 11108 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 11109 | if (PyErr_Occurred()) { |
| 11110 | Py_DECREF(termsize); |
| 11111 | return NULL; |
| 11112 | } |
| 11113 | return termsize; |
| 11114 | } |
| 11115 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 11116 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11117 | |
| 11118 | /*[clinic input] |
| 11119 | os.cpu_count |
| 11120 | |
| 11121 | Return the number of CPUs in the system; return None if indeterminable. |
| 11122 | [clinic start generated code]*/ |
| 11123 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11124 | static PyObject * |
| 11125 | os_cpu_count_impl(PyModuleDef *module) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 11126 | /*[clinic end generated code: output=c59ee7f6bce832b8 input=d55e2f8f3823a628]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11127 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11128 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11129 | #ifdef MS_WINDOWS |
| 11130 | SYSTEM_INFO sysinfo; |
| 11131 | GetSystemInfo(&sysinfo); |
| 11132 | ncpu = sysinfo.dwNumberOfProcessors; |
| 11133 | #elif defined(__hpux) |
| 11134 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 11135 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 11136 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11137 | #elif defined(__DragonFly__) || \ |
| 11138 | defined(__OpenBSD__) || \ |
| 11139 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 11140 | defined(__NetBSD__) || \ |
| 11141 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 11142 | int mib[2]; |
| 11143 | size_t len = sizeof(ncpu); |
| 11144 | mib[0] = CTL_HW; |
| 11145 | mib[1] = HW_NCPU; |
| 11146 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 11147 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 11148 | #endif |
| 11149 | if (ncpu >= 1) |
| 11150 | return PyLong_FromLong(ncpu); |
| 11151 | else |
| 11152 | Py_RETURN_NONE; |
| 11153 | } |
| 11154 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11155 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11156 | /*[clinic input] |
| 11157 | os.get_inheritable -> bool |
| 11158 | |
| 11159 | fd: int |
| 11160 | / |
| 11161 | |
| 11162 | Get the close-on-exe flag of the specified file descriptor. |
| 11163 | [clinic start generated code]*/ |
| 11164 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11165 | static int |
| 11166 | os_get_inheritable_impl(PyModuleDef *module, int fd) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 11167 | /*[clinic end generated code: output=36110bb36efaa21e input=89ac008dc9ab6b95]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11168 | { |
| 11169 | if (!_PyVerify_fd(fd)){ |
| 11170 | posix_error(); |
| 11171 | return -1; |
| 11172 | } |
| 11173 | |
| 11174 | return _Py_get_inheritable(fd); |
| 11175 | } |
| 11176 | |
| 11177 | |
| 11178 | /*[clinic input] |
| 11179 | os.set_inheritable |
| 11180 | fd: int |
| 11181 | inheritable: int |
| 11182 | / |
| 11183 | |
| 11184 | Set the inheritable flag of the specified file descriptor. |
| 11185 | [clinic start generated code]*/ |
| 11186 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11187 | static PyObject * |
| 11188 | os_set_inheritable_impl(PyModuleDef *module, int fd, int inheritable) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 11189 | /*[clinic end generated code: output=2ac5c6ce8623f045 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11190 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11191 | if (!_PyVerify_fd(fd)) |
| 11192 | return posix_error(); |
| 11193 | |
| 11194 | if (_Py_set_inheritable(fd, inheritable, NULL) < 0) |
| 11195 | return NULL; |
| 11196 | Py_RETURN_NONE; |
| 11197 | } |
| 11198 | |
| 11199 | |
| 11200 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11201 | /*[clinic input] |
| 11202 | os.get_handle_inheritable -> bool |
| 11203 | handle: Py_intptr_t |
| 11204 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11205 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11206 | Get the close-on-exe flag of the specified file descriptor. |
| 11207 | [clinic start generated code]*/ |
| 11208 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11209 | static int |
| 11210 | os_get_handle_inheritable_impl(PyModuleDef *module, Py_intptr_t handle) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 11211 | /*[clinic end generated code: output=3b7b3e1b43f312b6 input=5f7759443aae3dc5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11212 | { |
| 11213 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11214 | |
| 11215 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 11216 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11217 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11218 | } |
| 11219 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11220 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11221 | } |
| 11222 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11223 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11224 | /*[clinic input] |
| 11225 | os.set_handle_inheritable |
| 11226 | handle: Py_intptr_t |
| 11227 | inheritable: bool |
| 11228 | / |
| 11229 | |
| 11230 | Set the inheritable flag of the specified handle. |
| 11231 | [clinic start generated code]*/ |
| 11232 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11233 | static PyObject * |
| 11234 | os_set_handle_inheritable_impl(PyModuleDef *module, Py_intptr_t handle, int inheritable) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 11235 | /*[clinic end generated code: output=627aa5b158b69338 input=e64b2b2730469def]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11236 | { |
| 11237 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11238 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 11239 | PyErr_SetFromWindowsErr(0); |
| 11240 | return NULL; |
| 11241 | } |
| 11242 | Py_RETURN_NONE; |
| 11243 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11244 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11245 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 11246 | #ifndef MS_WINDOWS |
| 11247 | PyDoc_STRVAR(get_blocking__doc__, |
| 11248 | "get_blocking(fd) -> bool\n" \ |
| 11249 | "\n" \ |
| 11250 | "Get the blocking mode of the file descriptor:\n" \ |
| 11251 | "False if the O_NONBLOCK flag is set, True if the flag is cleared."); |
| 11252 | |
| 11253 | static PyObject* |
| 11254 | posix_get_blocking(PyObject *self, PyObject *args) |
| 11255 | { |
| 11256 | int fd; |
| 11257 | int blocking; |
| 11258 | |
| 11259 | if (!PyArg_ParseTuple(args, "i:get_blocking", &fd)) |
| 11260 | return NULL; |
| 11261 | |
| 11262 | if (!_PyVerify_fd(fd)) |
| 11263 | return posix_error(); |
| 11264 | |
| 11265 | blocking = _Py_get_blocking(fd); |
| 11266 | if (blocking < 0) |
| 11267 | return NULL; |
| 11268 | return PyBool_FromLong(blocking); |
| 11269 | } |
| 11270 | |
| 11271 | PyDoc_STRVAR(set_blocking__doc__, |
| 11272 | "set_blocking(fd, blocking)\n" \ |
| 11273 | "\n" \ |
| 11274 | "Set the blocking mode of the specified file descriptor.\n" \ |
| 11275 | "Set the O_NONBLOCK flag if blocking is False,\n" \ |
| 11276 | "clear the O_NONBLOCK flag otherwise."); |
| 11277 | |
| 11278 | static PyObject* |
| 11279 | posix_set_blocking(PyObject *self, PyObject *args) |
| 11280 | { |
| 11281 | int fd, blocking; |
| 11282 | |
| 11283 | if (!PyArg_ParseTuple(args, "ii:set_blocking", &fd, &blocking)) |
| 11284 | return NULL; |
| 11285 | |
| 11286 | if (!_PyVerify_fd(fd)) |
| 11287 | return posix_error(); |
| 11288 | |
| 11289 | if (_Py_set_blocking(fd, blocking) < 0) |
| 11290 | return NULL; |
| 11291 | Py_RETURN_NONE; |
| 11292 | } |
| 11293 | #endif /* !MS_WINDOWS */ |
| 11294 | |
| 11295 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11296 | PyDoc_STRVAR(posix_scandir__doc__, |
| 11297 | "scandir(path='.') -> iterator of DirEntry objects for given path"); |
| 11298 | |
| 11299 | static char *follow_symlinks_keywords[] = {"follow_symlinks", NULL}; |
| 11300 | |
| 11301 | typedef struct { |
| 11302 | PyObject_HEAD |
| 11303 | PyObject *name; |
| 11304 | PyObject *path; |
| 11305 | PyObject *stat; |
| 11306 | PyObject *lstat; |
| 11307 | #ifdef MS_WINDOWS |
| 11308 | struct _Py_stat_struct win32_lstat; |
| 11309 | __int64 win32_file_index; |
| 11310 | int got_file_index; |
| 11311 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11312 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11313 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11314 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11315 | ino_t d_ino; |
| 11316 | #endif |
| 11317 | } DirEntry; |
| 11318 | |
| 11319 | static void |
| 11320 | DirEntry_dealloc(DirEntry *entry) |
| 11321 | { |
| 11322 | Py_XDECREF(entry->name); |
| 11323 | Py_XDECREF(entry->path); |
| 11324 | Py_XDECREF(entry->stat); |
| 11325 | Py_XDECREF(entry->lstat); |
| 11326 | Py_TYPE(entry)->tp_free((PyObject *)entry); |
| 11327 | } |
| 11328 | |
| 11329 | /* Forward reference */ |
| 11330 | static int |
| 11331 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits); |
| 11332 | |
| 11333 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 11334 | static int |
| 11335 | DirEntry_is_symlink(DirEntry *self) |
| 11336 | { |
| 11337 | #ifdef MS_WINDOWS |
| 11338 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11339 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 11340 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11341 | if (self->d_type != DT_UNKNOWN) |
| 11342 | return self->d_type == DT_LNK; |
| 11343 | else |
| 11344 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11345 | #else |
| 11346 | /* POSIX without d_type */ |
| 11347 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11348 | #endif |
| 11349 | } |
| 11350 | |
| 11351 | static PyObject * |
| 11352 | DirEntry_py_is_symlink(DirEntry *self) |
| 11353 | { |
| 11354 | int result; |
| 11355 | |
| 11356 | result = DirEntry_is_symlink(self); |
| 11357 | if (result == -1) |
| 11358 | return NULL; |
| 11359 | return PyBool_FromLong(result); |
| 11360 | } |
| 11361 | |
| 11362 | static PyObject * |
| 11363 | DirEntry_fetch_stat(DirEntry *self, int follow_symlinks) |
| 11364 | { |
| 11365 | int result; |
| 11366 | struct _Py_stat_struct st; |
| 11367 | |
| 11368 | #ifdef MS_WINDOWS |
| 11369 | wchar_t *path; |
| 11370 | |
| 11371 | path = PyUnicode_AsUnicode(self->path); |
| 11372 | if (!path) |
| 11373 | return NULL; |
| 11374 | |
| 11375 | if (follow_symlinks) |
| 11376 | result = win32_stat_w(path, &st); |
| 11377 | else |
| 11378 | result = win32_lstat_w(path, &st); |
| 11379 | |
| 11380 | if (result != 0) { |
| 11381 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 11382 | 0, self->path); |
| 11383 | } |
| 11384 | #else /* POSIX */ |
| 11385 | PyObject *bytes; |
| 11386 | char *path; |
| 11387 | |
| 11388 | if (!PyUnicode_FSConverter(self->path, &bytes)) |
| 11389 | return NULL; |
| 11390 | path = PyBytes_AS_STRING(bytes); |
| 11391 | |
| 11392 | if (follow_symlinks) |
| 11393 | result = STAT(path, &st); |
| 11394 | else |
| 11395 | result = LSTAT(path, &st); |
| 11396 | Py_DECREF(bytes); |
| 11397 | |
| 11398 | if (result != 0) |
| 11399 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, self->path); |
| 11400 | #endif |
| 11401 | |
| 11402 | return _pystat_fromstructstat(&st); |
| 11403 | } |
| 11404 | |
| 11405 | static PyObject * |
| 11406 | DirEntry_get_lstat(DirEntry *self) |
| 11407 | { |
| 11408 | if (!self->lstat) { |
| 11409 | #ifdef MS_WINDOWS |
| 11410 | self->lstat = _pystat_fromstructstat(&self->win32_lstat); |
| 11411 | #else /* POSIX */ |
| 11412 | self->lstat = DirEntry_fetch_stat(self, 0); |
| 11413 | #endif |
| 11414 | } |
| 11415 | Py_XINCREF(self->lstat); |
| 11416 | return self->lstat; |
| 11417 | } |
| 11418 | |
| 11419 | static PyObject * |
| 11420 | DirEntry_get_stat(DirEntry *self, int follow_symlinks) |
| 11421 | { |
| 11422 | if (!follow_symlinks) |
| 11423 | return DirEntry_get_lstat(self); |
| 11424 | |
| 11425 | if (!self->stat) { |
| 11426 | int result = DirEntry_is_symlink(self); |
| 11427 | if (result == -1) |
| 11428 | return NULL; |
| 11429 | else if (result) |
| 11430 | self->stat = DirEntry_fetch_stat(self, 1); |
| 11431 | else |
| 11432 | self->stat = DirEntry_get_lstat(self); |
| 11433 | } |
| 11434 | |
| 11435 | Py_XINCREF(self->stat); |
| 11436 | return self->stat; |
| 11437 | } |
| 11438 | |
| 11439 | static PyObject * |
| 11440 | DirEntry_stat(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 11441 | { |
| 11442 | int follow_symlinks = 1; |
| 11443 | |
| 11444 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.stat", |
| 11445 | follow_symlinks_keywords, &follow_symlinks)) |
| 11446 | return NULL; |
| 11447 | |
| 11448 | return DirEntry_get_stat(self, follow_symlinks); |
| 11449 | } |
| 11450 | |
| 11451 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 11452 | static int |
| 11453 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 11454 | { |
| 11455 | PyObject *stat = NULL; |
| 11456 | PyObject *st_mode = NULL; |
| 11457 | long mode; |
| 11458 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11459 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11460 | int is_symlink; |
| 11461 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11462 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11463 | #ifdef MS_WINDOWS |
| 11464 | unsigned long dir_bits; |
| 11465 | #endif |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11466 | _Py_IDENTIFIER(st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11467 | |
| 11468 | #ifdef MS_WINDOWS |
| 11469 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 11470 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11471 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11472 | is_symlink = self->d_type == DT_LNK; |
| 11473 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 11474 | #endif |
| 11475 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11476 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11477 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11478 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11479 | stat = DirEntry_get_stat(self, follow_symlinks); |
| 11480 | if (!stat) { |
| 11481 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 11482 | /* If file doesn't exist (anymore), then return False |
| 11483 | (i.e., say it's not a file/directory) */ |
| 11484 | PyErr_Clear(); |
| 11485 | return 0; |
| 11486 | } |
| 11487 | goto error; |
| 11488 | } |
| 11489 | st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode); |
| 11490 | if (!st_mode) |
| 11491 | goto error; |
| 11492 | |
| 11493 | mode = PyLong_AsLong(st_mode); |
| 11494 | if (mode == -1 && PyErr_Occurred()) |
| 11495 | goto error; |
| 11496 | Py_CLEAR(st_mode); |
| 11497 | Py_CLEAR(stat); |
| 11498 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11499 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11500 | } |
| 11501 | else if (is_symlink) { |
| 11502 | assert(mode_bits != S_IFLNK); |
| 11503 | result = 0; |
| 11504 | } |
| 11505 | else { |
| 11506 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 11507 | #ifdef MS_WINDOWS |
| 11508 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 11509 | if (mode_bits == S_IFDIR) |
| 11510 | result = dir_bits != 0; |
| 11511 | else |
| 11512 | result = dir_bits == 0; |
| 11513 | #else /* POSIX */ |
| 11514 | if (mode_bits == S_IFDIR) |
| 11515 | result = self->d_type == DT_DIR; |
| 11516 | else |
| 11517 | result = self->d_type == DT_REG; |
| 11518 | #endif |
| 11519 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11520 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11521 | |
| 11522 | return result; |
| 11523 | |
| 11524 | error: |
| 11525 | Py_XDECREF(st_mode); |
| 11526 | Py_XDECREF(stat); |
| 11527 | return -1; |
| 11528 | } |
| 11529 | |
| 11530 | static PyObject * |
| 11531 | DirEntry_py_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 11532 | { |
| 11533 | int result; |
| 11534 | |
| 11535 | result = DirEntry_test_mode(self, follow_symlinks, mode_bits); |
| 11536 | if (result == -1) |
| 11537 | return NULL; |
| 11538 | return PyBool_FromLong(result); |
| 11539 | } |
| 11540 | |
| 11541 | static PyObject * |
| 11542 | DirEntry_is_dir(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 11543 | { |
| 11544 | int follow_symlinks = 1; |
| 11545 | |
| 11546 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_dir", |
| 11547 | follow_symlinks_keywords, &follow_symlinks)) |
| 11548 | return NULL; |
| 11549 | |
| 11550 | return DirEntry_py_test_mode(self, follow_symlinks, S_IFDIR); |
| 11551 | } |
| 11552 | |
| 11553 | static PyObject * |
| 11554 | DirEntry_is_file(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 11555 | { |
| 11556 | int follow_symlinks = 1; |
| 11557 | |
| 11558 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_file", |
| 11559 | follow_symlinks_keywords, &follow_symlinks)) |
| 11560 | return NULL; |
| 11561 | |
| 11562 | return DirEntry_py_test_mode(self, follow_symlinks, S_IFREG); |
| 11563 | } |
| 11564 | |
| 11565 | static PyObject * |
| 11566 | DirEntry_inode(DirEntry *self) |
| 11567 | { |
| 11568 | #ifdef MS_WINDOWS |
| 11569 | if (!self->got_file_index) { |
| 11570 | wchar_t *path; |
| 11571 | struct _Py_stat_struct stat; |
| 11572 | |
| 11573 | path = PyUnicode_AsUnicode(self->path); |
| 11574 | if (!path) |
| 11575 | return NULL; |
| 11576 | |
| 11577 | if (win32_lstat_w(path, &stat) != 0) { |
| 11578 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 11579 | 0, self->path); |
| 11580 | } |
| 11581 | |
| 11582 | self->win32_file_index = stat.st_ino; |
| 11583 | self->got_file_index = 1; |
| 11584 | } |
| 11585 | return PyLong_FromLongLong((PY_LONG_LONG)self->win32_file_index); |
| 11586 | #else /* POSIX */ |
| 11587 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 11588 | return PyLong_FromLongLong((PY_LONG_LONG)self->d_ino); |
| 11589 | #else |
| 11590 | return PyLong_FromLong((long)self->d_ino); |
| 11591 | #endif |
| 11592 | #endif |
| 11593 | } |
| 11594 | |
| 11595 | static PyObject * |
| 11596 | DirEntry_repr(DirEntry *self) |
| 11597 | { |
| 11598 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 11599 | } |
| 11600 | |
| 11601 | static PyMemberDef DirEntry_members[] = { |
| 11602 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 11603 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 11604 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 11605 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 11606 | {NULL} |
| 11607 | }; |
| 11608 | |
| 11609 | static PyMethodDef DirEntry_methods[] = { |
| 11610 | {"is_dir", (PyCFunction)DirEntry_is_dir, METH_VARARGS | METH_KEYWORDS, |
| 11611 | "return True if the entry is a directory; cached per entry" |
| 11612 | }, |
| 11613 | {"is_file", (PyCFunction)DirEntry_is_file, METH_VARARGS | METH_KEYWORDS, |
| 11614 | "return True if the entry is a file; cached per entry" |
| 11615 | }, |
| 11616 | {"is_symlink", (PyCFunction)DirEntry_py_is_symlink, METH_NOARGS, |
| 11617 | "return True if the entry is a symbolic link; cached per entry" |
| 11618 | }, |
| 11619 | {"stat", (PyCFunction)DirEntry_stat, METH_VARARGS | METH_KEYWORDS, |
| 11620 | "return stat_result object for the entry; cached per entry" |
| 11621 | }, |
| 11622 | {"inode", (PyCFunction)DirEntry_inode, METH_NOARGS, |
| 11623 | "return inode of the entry; cached per entry", |
| 11624 | }, |
| 11625 | {NULL} |
| 11626 | }; |
| 11627 | |
| 11628 | PyTypeObject DirEntryType = { |
| 11629 | PyVarObject_HEAD_INIT(NULL, 0) |
| 11630 | MODNAME ".DirEntry", /* tp_name */ |
| 11631 | sizeof(DirEntry), /* tp_basicsize */ |
| 11632 | 0, /* tp_itemsize */ |
| 11633 | /* methods */ |
| 11634 | (destructor)DirEntry_dealloc, /* tp_dealloc */ |
| 11635 | 0, /* tp_print */ |
| 11636 | 0, /* tp_getattr */ |
| 11637 | 0, /* tp_setattr */ |
| 11638 | 0, /* tp_compare */ |
| 11639 | (reprfunc)DirEntry_repr, /* tp_repr */ |
| 11640 | 0, /* tp_as_number */ |
| 11641 | 0, /* tp_as_sequence */ |
| 11642 | 0, /* tp_as_mapping */ |
| 11643 | 0, /* tp_hash */ |
| 11644 | 0, /* tp_call */ |
| 11645 | 0, /* tp_str */ |
| 11646 | 0, /* tp_getattro */ |
| 11647 | 0, /* tp_setattro */ |
| 11648 | 0, /* tp_as_buffer */ |
| 11649 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 11650 | 0, /* tp_doc */ |
| 11651 | 0, /* tp_traverse */ |
| 11652 | 0, /* tp_clear */ |
| 11653 | 0, /* tp_richcompare */ |
| 11654 | 0, /* tp_weaklistoffset */ |
| 11655 | 0, /* tp_iter */ |
| 11656 | 0, /* tp_iternext */ |
| 11657 | DirEntry_methods, /* tp_methods */ |
| 11658 | DirEntry_members, /* tp_members */ |
| 11659 | }; |
| 11660 | |
| 11661 | #ifdef MS_WINDOWS |
| 11662 | |
| 11663 | static wchar_t * |
| 11664 | join_path_filenameW(wchar_t *path_wide, wchar_t* filename) |
| 11665 | { |
| 11666 | Py_ssize_t path_len; |
| 11667 | Py_ssize_t size; |
| 11668 | wchar_t *result; |
| 11669 | wchar_t ch; |
| 11670 | |
| 11671 | if (!path_wide) { /* Default arg: "." */ |
| 11672 | path_wide = L"."; |
| 11673 | path_len = 1; |
| 11674 | } |
| 11675 | else { |
| 11676 | path_len = wcslen(path_wide); |
| 11677 | } |
| 11678 | |
| 11679 | /* The +1's are for the path separator and the NUL */ |
| 11680 | size = path_len + 1 + wcslen(filename) + 1; |
| 11681 | result = PyMem_New(wchar_t, size); |
| 11682 | if (!result) { |
| 11683 | PyErr_NoMemory(); |
| 11684 | return NULL; |
| 11685 | } |
| 11686 | wcscpy(result, path_wide); |
| 11687 | if (path_len > 0) { |
| 11688 | ch = result[path_len - 1]; |
| 11689 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 11690 | result[path_len++] = SEP; |
| 11691 | wcscpy(result + path_len, filename); |
| 11692 | } |
| 11693 | return result; |
| 11694 | } |
| 11695 | |
| 11696 | static PyObject * |
| 11697 | DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW) |
| 11698 | { |
| 11699 | DirEntry *entry; |
| 11700 | BY_HANDLE_FILE_INFORMATION file_info; |
| 11701 | ULONG reparse_tag; |
| 11702 | wchar_t *joined_path; |
| 11703 | |
| 11704 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 11705 | if (!entry) |
| 11706 | return NULL; |
| 11707 | entry->name = NULL; |
| 11708 | entry->path = NULL; |
| 11709 | entry->stat = NULL; |
| 11710 | entry->lstat = NULL; |
| 11711 | entry->got_file_index = 0; |
| 11712 | |
| 11713 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 11714 | if (!entry->name) |
| 11715 | goto error; |
| 11716 | |
| 11717 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 11718 | if (!joined_path) |
| 11719 | goto error; |
| 11720 | |
| 11721 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 11722 | PyMem_Free(joined_path); |
| 11723 | if (!entry->path) |
| 11724 | goto error; |
| 11725 | |
| 11726 | find_data_to_file_info_w(dataW, &file_info, &reparse_tag); |
| 11727 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 11728 | |
| 11729 | return (PyObject *)entry; |
| 11730 | |
| 11731 | error: |
| 11732 | Py_DECREF(entry); |
| 11733 | return NULL; |
| 11734 | } |
| 11735 | |
| 11736 | #else /* POSIX */ |
| 11737 | |
| 11738 | static char * |
| 11739 | join_path_filename(char *path_narrow, char* filename, Py_ssize_t filename_len) |
| 11740 | { |
| 11741 | Py_ssize_t path_len; |
| 11742 | Py_ssize_t size; |
| 11743 | char *result; |
| 11744 | |
| 11745 | if (!path_narrow) { /* Default arg: "." */ |
| 11746 | path_narrow = "."; |
| 11747 | path_len = 1; |
| 11748 | } |
| 11749 | else { |
| 11750 | path_len = strlen(path_narrow); |
| 11751 | } |
| 11752 | |
| 11753 | if (filename_len == -1) |
| 11754 | filename_len = strlen(filename); |
| 11755 | |
| 11756 | /* The +1's are for the path separator and the NUL */ |
| 11757 | size = path_len + 1 + filename_len + 1; |
| 11758 | result = PyMem_New(char, size); |
| 11759 | if (!result) { |
| 11760 | PyErr_NoMemory(); |
| 11761 | return NULL; |
| 11762 | } |
| 11763 | strcpy(result, path_narrow); |
| 11764 | if (path_len > 0 && result[path_len - 1] != '/') |
| 11765 | result[path_len++] = '/'; |
| 11766 | strcpy(result + path_len, filename); |
| 11767 | return result; |
| 11768 | } |
| 11769 | |
| 11770 | static PyObject * |
| 11771 | DirEntry_from_posix_info(path_t *path, char *name, Py_ssize_t name_len, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11772 | ino_t d_ino |
| 11773 | #ifdef HAVE_DIRENT_D_TYPE |
| 11774 | , unsigned char d_type |
| 11775 | #endif |
| 11776 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11777 | { |
| 11778 | DirEntry *entry; |
| 11779 | char *joined_path; |
| 11780 | |
| 11781 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 11782 | if (!entry) |
| 11783 | return NULL; |
| 11784 | entry->name = NULL; |
| 11785 | entry->path = NULL; |
| 11786 | entry->stat = NULL; |
| 11787 | entry->lstat = NULL; |
| 11788 | |
| 11789 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 11790 | if (!joined_path) |
| 11791 | goto error; |
| 11792 | |
| 11793 | if (!path->narrow || !PyBytes_Check(path->object)) { |
| 11794 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
| 11795 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
| 11796 | } |
| 11797 | else { |
| 11798 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
| 11799 | entry->path = PyBytes_FromString(joined_path); |
| 11800 | } |
| 11801 | PyMem_Free(joined_path); |
| 11802 | if (!entry->name || !entry->path) |
| 11803 | goto error; |
| 11804 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11805 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11806 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11807 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11808 | entry->d_ino = d_ino; |
| 11809 | |
| 11810 | return (PyObject *)entry; |
| 11811 | |
| 11812 | error: |
| 11813 | Py_XDECREF(entry); |
| 11814 | return NULL; |
| 11815 | } |
| 11816 | |
| 11817 | #endif |
| 11818 | |
| 11819 | |
| 11820 | typedef struct { |
| 11821 | PyObject_HEAD |
| 11822 | path_t path; |
| 11823 | #ifdef MS_WINDOWS |
| 11824 | HANDLE handle; |
| 11825 | WIN32_FIND_DATAW file_data; |
| 11826 | int first_time; |
| 11827 | #else /* POSIX */ |
| 11828 | DIR *dirp; |
| 11829 | #endif |
| 11830 | } ScandirIterator; |
| 11831 | |
| 11832 | #ifdef MS_WINDOWS |
| 11833 | |
| 11834 | static void |
| 11835 | ScandirIterator_close(ScandirIterator *iterator) |
| 11836 | { |
| 11837 | if (iterator->handle == INVALID_HANDLE_VALUE) |
| 11838 | return; |
| 11839 | |
| 11840 | Py_BEGIN_ALLOW_THREADS |
| 11841 | FindClose(iterator->handle); |
| 11842 | Py_END_ALLOW_THREADS |
| 11843 | iterator->handle = INVALID_HANDLE_VALUE; |
| 11844 | } |
| 11845 | |
| 11846 | static PyObject * |
| 11847 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 11848 | { |
| 11849 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 11850 | BOOL success; |
| 11851 | |
| 11852 | /* Happens if the iterator is iterated twice */ |
| 11853 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 11854 | PyErr_SetNone(PyExc_StopIteration); |
| 11855 | return NULL; |
| 11856 | } |
| 11857 | |
| 11858 | while (1) { |
| 11859 | if (!iterator->first_time) { |
| 11860 | Py_BEGIN_ALLOW_THREADS |
| 11861 | success = FindNextFileW(iterator->handle, file_data); |
| 11862 | Py_END_ALLOW_THREADS |
| 11863 | if (!success) { |
| 11864 | if (GetLastError() != ERROR_NO_MORE_FILES) |
| 11865 | return path_error(&iterator->path); |
| 11866 | /* No more files found in directory, stop iterating */ |
| 11867 | break; |
| 11868 | } |
| 11869 | } |
| 11870 | iterator->first_time = 0; |
| 11871 | |
| 11872 | /* Skip over . and .. */ |
| 11873 | if (wcscmp(file_data->cFileName, L".") != 0 && |
| 11874 | wcscmp(file_data->cFileName, L"..") != 0) |
| 11875 | return DirEntry_from_find_data(&iterator->path, file_data); |
| 11876 | |
| 11877 | /* Loop till we get a non-dot directory or finish iterating */ |
| 11878 | } |
| 11879 | |
| 11880 | ScandirIterator_close(iterator); |
| 11881 | |
| 11882 | PyErr_SetNone(PyExc_StopIteration); |
| 11883 | return NULL; |
| 11884 | } |
| 11885 | |
| 11886 | #else /* POSIX */ |
| 11887 | |
| 11888 | static void |
| 11889 | ScandirIterator_close(ScandirIterator *iterator) |
| 11890 | { |
| 11891 | if (!iterator->dirp) |
| 11892 | return; |
| 11893 | |
| 11894 | Py_BEGIN_ALLOW_THREADS |
| 11895 | closedir(iterator->dirp); |
| 11896 | Py_END_ALLOW_THREADS |
| 11897 | iterator->dirp = NULL; |
| 11898 | return; |
| 11899 | } |
| 11900 | |
| 11901 | static PyObject * |
| 11902 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 11903 | { |
| 11904 | struct dirent *direntp; |
| 11905 | Py_ssize_t name_len; |
| 11906 | int is_dot; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11907 | |
| 11908 | /* Happens if the iterator is iterated twice */ |
| 11909 | if (!iterator->dirp) { |
| 11910 | PyErr_SetNone(PyExc_StopIteration); |
| 11911 | return NULL; |
| 11912 | } |
| 11913 | |
| 11914 | while (1) { |
| 11915 | errno = 0; |
| 11916 | Py_BEGIN_ALLOW_THREADS |
| 11917 | direntp = readdir(iterator->dirp); |
| 11918 | Py_END_ALLOW_THREADS |
| 11919 | |
| 11920 | if (!direntp) { |
| 11921 | if (errno != 0) |
| 11922 | return path_error(&iterator->path); |
| 11923 | /* No more files found in directory, stop iterating */ |
| 11924 | break; |
| 11925 | } |
| 11926 | |
| 11927 | /* Skip over . and .. */ |
| 11928 | name_len = NAMLEN(direntp); |
| 11929 | is_dot = direntp->d_name[0] == '.' && |
| 11930 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 11931 | if (!is_dot) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11932 | return DirEntry_from_posix_info(&iterator->path, direntp->d_name, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 11933 | name_len, direntp->d_ino |
| 11934 | #ifdef HAVE_DIRENT_D_TYPE |
| 11935 | , direntp->d_type |
| 11936 | #endif |
| 11937 | ); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 11938 | } |
| 11939 | |
| 11940 | /* Loop till we get a non-dot directory or finish iterating */ |
| 11941 | } |
| 11942 | |
| 11943 | ScandirIterator_close(iterator); |
| 11944 | |
| 11945 | PyErr_SetNone(PyExc_StopIteration); |
| 11946 | return NULL; |
| 11947 | } |
| 11948 | |
| 11949 | #endif |
| 11950 | |
| 11951 | static void |
| 11952 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 11953 | { |
| 11954 | ScandirIterator_close(iterator); |
| 11955 | Py_XDECREF(iterator->path.object); |
| 11956 | path_cleanup(&iterator->path); |
| 11957 | Py_TYPE(iterator)->tp_free((PyObject *)iterator); |
| 11958 | } |
| 11959 | |
| 11960 | PyTypeObject ScandirIteratorType = { |
| 11961 | PyVarObject_HEAD_INIT(NULL, 0) |
| 11962 | MODNAME ".ScandirIterator", /* tp_name */ |
| 11963 | sizeof(ScandirIterator), /* tp_basicsize */ |
| 11964 | 0, /* tp_itemsize */ |
| 11965 | /* methods */ |
| 11966 | (destructor)ScandirIterator_dealloc, /* tp_dealloc */ |
| 11967 | 0, /* tp_print */ |
| 11968 | 0, /* tp_getattr */ |
| 11969 | 0, /* tp_setattr */ |
| 11970 | 0, /* tp_compare */ |
| 11971 | 0, /* tp_repr */ |
| 11972 | 0, /* tp_as_number */ |
| 11973 | 0, /* tp_as_sequence */ |
| 11974 | 0, /* tp_as_mapping */ |
| 11975 | 0, /* tp_hash */ |
| 11976 | 0, /* tp_call */ |
| 11977 | 0, /* tp_str */ |
| 11978 | 0, /* tp_getattro */ |
| 11979 | 0, /* tp_setattro */ |
| 11980 | 0, /* tp_as_buffer */ |
| 11981 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 11982 | 0, /* tp_doc */ |
| 11983 | 0, /* tp_traverse */ |
| 11984 | 0, /* tp_clear */ |
| 11985 | 0, /* tp_richcompare */ |
| 11986 | 0, /* tp_weaklistoffset */ |
| 11987 | PyObject_SelfIter, /* tp_iter */ |
| 11988 | (iternextfunc)ScandirIterator_iternext, /* tp_iternext */ |
| 11989 | }; |
| 11990 | |
| 11991 | static PyObject * |
| 11992 | posix_scandir(PyObject *self, PyObject *args, PyObject *kwargs) |
| 11993 | { |
| 11994 | ScandirIterator *iterator; |
| 11995 | static char *keywords[] = {"path", NULL}; |
| 11996 | #ifdef MS_WINDOWS |
| 11997 | wchar_t *path_strW; |
| 11998 | #else |
| 11999 | char *path; |
| 12000 | #endif |
| 12001 | |
| 12002 | iterator = PyObject_New(ScandirIterator, &ScandirIteratorType); |
| 12003 | if (!iterator) |
| 12004 | return NULL; |
| 12005 | memset(&iterator->path, 0, sizeof(path_t)); |
| 12006 | iterator->path.function_name = "scandir"; |
| 12007 | iterator->path.nullable = 1; |
| 12008 | |
| 12009 | #ifdef MS_WINDOWS |
| 12010 | iterator->handle = INVALID_HANDLE_VALUE; |
| 12011 | #else |
| 12012 | iterator->dirp = NULL; |
| 12013 | #endif |
| 12014 | |
| 12015 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:scandir", keywords, |
| 12016 | path_converter, &iterator->path)) |
| 12017 | goto error; |
| 12018 | |
| 12019 | /* path_converter doesn't keep path.object around, so do it |
| 12020 | manually for the lifetime of the iterator here (the refcount |
| 12021 | is decremented in ScandirIterator_dealloc) |
| 12022 | */ |
| 12023 | Py_XINCREF(iterator->path.object); |
| 12024 | |
| 12025 | #ifdef MS_WINDOWS |
| 12026 | if (iterator->path.narrow) { |
| 12027 | PyErr_SetString(PyExc_TypeError, |
| 12028 | "os.scandir() doesn't support bytes path on Windows, use Unicode instead"); |
| 12029 | goto error; |
| 12030 | } |
| 12031 | iterator->first_time = 1; |
| 12032 | |
| 12033 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 12034 | if (!path_strW) |
| 12035 | goto error; |
| 12036 | |
| 12037 | Py_BEGIN_ALLOW_THREADS |
| 12038 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 12039 | Py_END_ALLOW_THREADS |
| 12040 | |
| 12041 | PyMem_Free(path_strW); |
| 12042 | |
| 12043 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 12044 | path_error(&iterator->path); |
| 12045 | goto error; |
| 12046 | } |
| 12047 | #else /* POSIX */ |
| 12048 | if (iterator->path.narrow) |
| 12049 | path = iterator->path.narrow; |
| 12050 | else |
| 12051 | path = "."; |
| 12052 | |
| 12053 | errno = 0; |
| 12054 | Py_BEGIN_ALLOW_THREADS |
| 12055 | iterator->dirp = opendir(path); |
| 12056 | Py_END_ALLOW_THREADS |
| 12057 | |
| 12058 | if (!iterator->dirp) { |
| 12059 | path_error(&iterator->path); |
| 12060 | goto error; |
| 12061 | } |
| 12062 | #endif |
| 12063 | |
| 12064 | return (PyObject *)iterator; |
| 12065 | |
| 12066 | error: |
| 12067 | Py_DECREF(iterator); |
| 12068 | return NULL; |
| 12069 | } |
| 12070 | |
| 12071 | |
Serhiy Storchaka | a4c6bad | 2015-04-04 23:35:52 +0300 | [diff] [blame] | 12072 | #include "clinic/posixmodule.c.h" |
| 12073 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 12074 | /*[clinic input] |
| 12075 | dump buffer |
| 12076 | [clinic start generated code]*/ |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 12077 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/ |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 12078 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12079 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12080 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12081 | |
| 12082 | OS_STAT_METHODDEF |
| 12083 | OS_ACCESS_METHODDEF |
| 12084 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12085 | OS_CHDIR_METHODDEF |
| 12086 | OS_CHFLAGS_METHODDEF |
| 12087 | OS_CHMOD_METHODDEF |
| 12088 | OS_FCHMOD_METHODDEF |
| 12089 | OS_LCHMOD_METHODDEF |
| 12090 | OS_CHOWN_METHODDEF |
| 12091 | OS_FCHOWN_METHODDEF |
| 12092 | OS_LCHOWN_METHODDEF |
| 12093 | OS_LCHFLAGS_METHODDEF |
| 12094 | OS_CHROOT_METHODDEF |
| 12095 | OS_CTERMID_METHODDEF |
| 12096 | OS_GETCWD_METHODDEF |
| 12097 | OS_GETCWDB_METHODDEF |
| 12098 | OS_LINK_METHODDEF |
| 12099 | OS_LISTDIR_METHODDEF |
| 12100 | OS_LSTAT_METHODDEF |
| 12101 | OS_MKDIR_METHODDEF |
| 12102 | OS_NICE_METHODDEF |
| 12103 | OS_GETPRIORITY_METHODDEF |
| 12104 | OS_SETPRIORITY_METHODDEF |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12105 | #ifdef HAVE_READLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12106 | {"readlink", (PyCFunction)posix_readlink, |
| 12107 | METH_VARARGS | METH_KEYWORDS, |
| 12108 | readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 12109 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 12110 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12111 | {"readlink", (PyCFunction)win_readlink, |
| 12112 | METH_VARARGS | METH_KEYWORDS, |
| 12113 | readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 12114 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12115 | OS_RENAME_METHODDEF |
| 12116 | OS_REPLACE_METHODDEF |
| 12117 | OS_RMDIR_METHODDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12118 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12119 | OS_SYMLINK_METHODDEF |
| 12120 | OS_SYSTEM_METHODDEF |
| 12121 | OS_UMASK_METHODDEF |
| 12122 | OS_UNAME_METHODDEF |
| 12123 | OS_UNLINK_METHODDEF |
| 12124 | OS_REMOVE_METHODDEF |
| 12125 | OS_UTIME_METHODDEF |
| 12126 | OS_TIMES_METHODDEF |
| 12127 | OS__EXIT_METHODDEF |
| 12128 | OS_EXECV_METHODDEF |
| 12129 | OS_EXECVE_METHODDEF |
| 12130 | OS_SPAWNV_METHODDEF |
| 12131 | OS_SPAWNVE_METHODDEF |
| 12132 | OS_FORK1_METHODDEF |
| 12133 | OS_FORK_METHODDEF |
| 12134 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 12135 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 12136 | OS_SCHED_GETPARAM_METHODDEF |
| 12137 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 12138 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 12139 | OS_SCHED_SETPARAM_METHODDEF |
| 12140 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 12141 | OS_SCHED_YIELD_METHODDEF |
| 12142 | OS_SCHED_SETAFFINITY_METHODDEF |
| 12143 | OS_SCHED_GETAFFINITY_METHODDEF |
| 12144 | OS_OPENPTY_METHODDEF |
| 12145 | OS_FORKPTY_METHODDEF |
| 12146 | OS_GETEGID_METHODDEF |
| 12147 | OS_GETEUID_METHODDEF |
| 12148 | OS_GETGID_METHODDEF |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 12149 | #ifdef HAVE_GETGROUPLIST |
| 12150 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 12151 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12152 | OS_GETGROUPS_METHODDEF |
| 12153 | OS_GETPID_METHODDEF |
| 12154 | OS_GETPGRP_METHODDEF |
| 12155 | OS_GETPPID_METHODDEF |
| 12156 | OS_GETUID_METHODDEF |
| 12157 | OS_GETLOGIN_METHODDEF |
| 12158 | OS_KILL_METHODDEF |
| 12159 | OS_KILLPG_METHODDEF |
| 12160 | OS_PLOCK_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 12161 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12162 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 12163 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12164 | OS_SETUID_METHODDEF |
| 12165 | OS_SETEUID_METHODDEF |
| 12166 | OS_SETREUID_METHODDEF |
| 12167 | OS_SETGID_METHODDEF |
| 12168 | OS_SETEGID_METHODDEF |
| 12169 | OS_SETREGID_METHODDEF |
| 12170 | OS_SETGROUPS_METHODDEF |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 12171 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12172 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 12173 | #endif /* HAVE_INITGROUPS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12174 | OS_GETPGID_METHODDEF |
| 12175 | OS_SETPGRP_METHODDEF |
| 12176 | OS_WAIT_METHODDEF |
| 12177 | OS_WAIT3_METHODDEF |
| 12178 | OS_WAIT4_METHODDEF |
| 12179 | OS_WAITID_METHODDEF |
| 12180 | OS_WAITPID_METHODDEF |
| 12181 | OS_GETSID_METHODDEF |
| 12182 | OS_SETSID_METHODDEF |
| 12183 | OS_SETPGID_METHODDEF |
| 12184 | OS_TCGETPGRP_METHODDEF |
| 12185 | OS_TCSETPGRP_METHODDEF |
| 12186 | OS_OPEN_METHODDEF |
| 12187 | OS_CLOSE_METHODDEF |
| 12188 | OS_CLOSERANGE_METHODDEF |
| 12189 | OS_DEVICE_ENCODING_METHODDEF |
| 12190 | OS_DUP_METHODDEF |
| 12191 | OS_DUP2_METHODDEF |
| 12192 | OS_LOCKF_METHODDEF |
| 12193 | OS_LSEEK_METHODDEF |
| 12194 | OS_READ_METHODDEF |
| 12195 | OS_READV_METHODDEF |
| 12196 | OS_PREAD_METHODDEF |
| 12197 | OS_WRITE_METHODDEF |
| 12198 | OS_WRITEV_METHODDEF |
| 12199 | OS_PWRITE_METHODDEF |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12200 | #ifdef HAVE_SENDFILE |
| 12201 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 12202 | posix_sendfile__doc__}, |
| 12203 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12204 | OS_FSTAT_METHODDEF |
| 12205 | OS_ISATTY_METHODDEF |
| 12206 | OS_PIPE_METHODDEF |
| 12207 | OS_PIPE2_METHODDEF |
| 12208 | OS_MKFIFO_METHODDEF |
| 12209 | OS_MKNOD_METHODDEF |
| 12210 | OS_MAJOR_METHODDEF |
| 12211 | OS_MINOR_METHODDEF |
| 12212 | OS_MAKEDEV_METHODDEF |
| 12213 | OS_FTRUNCATE_METHODDEF |
| 12214 | OS_TRUNCATE_METHODDEF |
| 12215 | OS_POSIX_FALLOCATE_METHODDEF |
| 12216 | OS_POSIX_FADVISE_METHODDEF |
| 12217 | OS_PUTENV_METHODDEF |
| 12218 | OS_UNSETENV_METHODDEF |
| 12219 | OS_STRERROR_METHODDEF |
| 12220 | OS_FCHDIR_METHODDEF |
| 12221 | OS_FSYNC_METHODDEF |
| 12222 | OS_SYNC_METHODDEF |
| 12223 | OS_FDATASYNC_METHODDEF |
| 12224 | OS_WCOREDUMP_METHODDEF |
| 12225 | OS_WIFCONTINUED_METHODDEF |
| 12226 | OS_WIFSTOPPED_METHODDEF |
| 12227 | OS_WIFSIGNALED_METHODDEF |
| 12228 | OS_WIFEXITED_METHODDEF |
| 12229 | OS_WEXITSTATUS_METHODDEF |
| 12230 | OS_WTERMSIG_METHODDEF |
| 12231 | OS_WSTOPSIG_METHODDEF |
| 12232 | OS_FSTATVFS_METHODDEF |
| 12233 | OS_STATVFS_METHODDEF |
| 12234 | OS_CONFSTR_METHODDEF |
| 12235 | OS_SYSCONF_METHODDEF |
| 12236 | OS_FPATHCONF_METHODDEF |
| 12237 | OS_PATHCONF_METHODDEF |
| 12238 | OS_ABORT_METHODDEF |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 12239 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12240 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 12241 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 12242 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12243 | OS__GETDISKUSAGE_METHODDEF |
| 12244 | OS__GETFINALPATHNAME_METHODDEF |
| 12245 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 12246 | OS_GETLOADAVG_METHODDEF |
| 12247 | OS_URANDOM_METHODDEF |
| 12248 | OS_SETRESUID_METHODDEF |
| 12249 | OS_SETRESGID_METHODDEF |
| 12250 | OS_GETRESUID_METHODDEF |
| 12251 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12252 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12253 | OS_GETXATTR_METHODDEF |
| 12254 | OS_SETXATTR_METHODDEF |
| 12255 | OS_REMOVEXATTR_METHODDEF |
| 12256 | OS_LISTXATTR_METHODDEF |
| 12257 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12258 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 12259 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 12260 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12261 | OS_CPU_COUNT_METHODDEF |
| 12262 | OS_GET_INHERITABLE_METHODDEF |
| 12263 | OS_SET_INHERITABLE_METHODDEF |
| 12264 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 12265 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12266 | #ifndef MS_WINDOWS |
| 12267 | {"get_blocking", posix_get_blocking, METH_VARARGS, get_blocking__doc__}, |
| 12268 | {"set_blocking", posix_set_blocking, METH_VARARGS, set_blocking__doc__}, |
| 12269 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12270 | {"scandir", (PyCFunction)posix_scandir, |
| 12271 | METH_VARARGS | METH_KEYWORDS, |
| 12272 | posix_scandir__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12273 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 12274 | }; |
| 12275 | |
| 12276 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12277 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12278 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12279 | enable_symlink() |
| 12280 | { |
| 12281 | HANDLE tok; |
| 12282 | TOKEN_PRIVILEGES tok_priv; |
| 12283 | LUID luid; |
| 12284 | int meth_idx = 0; |
| 12285 | |
| 12286 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12287 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12288 | |
| 12289 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12290 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12291 | |
| 12292 | tok_priv.PrivilegeCount = 1; |
| 12293 | tok_priv.Privileges[0].Luid = luid; |
| 12294 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 12295 | |
| 12296 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 12297 | sizeof(TOKEN_PRIVILEGES), |
| 12298 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12299 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12300 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12301 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 12302 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12303 | } |
| 12304 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 12305 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12306 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12307 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12308 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12309 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12310 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12311 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12312 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12313 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12314 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12315 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12316 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12317 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 12318 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12319 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12320 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12321 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12322 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12323 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12324 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12325 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12326 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12327 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12328 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12329 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12330 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12331 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12332 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12333 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12334 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 12335 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12336 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12337 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12338 | #endif |
| 12339 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12340 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12341 | #endif |
| 12342 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12343 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12344 | #endif |
| 12345 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12346 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12347 | #endif |
| 12348 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12349 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12350 | #endif |
| 12351 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12352 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12353 | #endif |
| 12354 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12355 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12356 | #endif |
| 12357 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12358 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12359 | #endif |
| 12360 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12361 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12362 | #endif |
| 12363 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12364 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12365 | #endif |
| 12366 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12367 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12368 | #endif |
| 12369 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12370 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12371 | #endif |
| 12372 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12373 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12374 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 12375 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12376 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 12377 | #endif |
| 12378 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12379 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 12380 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12381 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12382 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12383 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12384 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12385 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12386 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 12387 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12388 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 12389 | #endif |
| 12390 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12391 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 12392 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12393 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12394 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12395 | #endif |
| 12396 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12397 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12398 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 12399 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12400 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 12401 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12402 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12403 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 12404 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 12405 | #ifdef O_TMPFILE |
| 12406 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 12407 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12408 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12409 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12410 | #endif |
| 12411 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12412 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12413 | #endif |
| 12414 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12415 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12416 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 12417 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12418 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 12419 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12420 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12421 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12422 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 12423 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12424 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 12425 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12426 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 12427 | #endif |
| 12428 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12429 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 12430 | #endif |
| 12431 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12432 | /* MS Windows */ |
| 12433 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12434 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12435 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12436 | #endif |
| 12437 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12438 | /* Optimize for short life (keep in memory). */ |
| 12439 | /* 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] | 12440 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12441 | #endif |
| 12442 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12443 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12444 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12445 | #endif |
| 12446 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12447 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12448 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12449 | #endif |
| 12450 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12451 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12452 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12453 | #endif |
| 12454 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12455 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 12456 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12457 | /* Send a SIGIO signal whenever input or output |
| 12458 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12459 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 12460 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12461 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12462 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12463 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12464 | #endif |
| 12465 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12466 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12467 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12468 | #endif |
| 12469 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12470 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12471 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 12472 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12473 | #ifdef O_NOLINKS |
| 12474 | /* 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] | 12475 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 12476 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 12477 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12478 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12479 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 12480 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 12481 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12482 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12483 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12484 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12485 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12486 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12487 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12488 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12489 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12490 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12491 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12492 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12493 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12494 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12495 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12496 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12497 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12498 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12499 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12500 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12501 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12502 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12503 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12504 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12505 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12506 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12507 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12508 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12509 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12510 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12511 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12512 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12513 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12514 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12515 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12516 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12517 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12518 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12519 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12520 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12521 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12522 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12523 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12524 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12525 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12526 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12527 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12528 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12529 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12530 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12531 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12532 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 12533 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 12534 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 12535 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 12536 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12537 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 12538 | #endif /* ST_RDONLY */ |
| 12539 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12540 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 12541 | #endif /* ST_NOSUID */ |
| 12542 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 12543 | /* GNU extensions */ |
| 12544 | #ifdef ST_NODEV |
| 12545 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 12546 | #endif /* ST_NODEV */ |
| 12547 | #ifdef ST_NOEXEC |
| 12548 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 12549 | #endif /* ST_NOEXEC */ |
| 12550 | #ifdef ST_SYNCHRONOUS |
| 12551 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 12552 | #endif /* ST_SYNCHRONOUS */ |
| 12553 | #ifdef ST_MANDLOCK |
| 12554 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 12555 | #endif /* ST_MANDLOCK */ |
| 12556 | #ifdef ST_WRITE |
| 12557 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 12558 | #endif /* ST_WRITE */ |
| 12559 | #ifdef ST_APPEND |
| 12560 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 12561 | #endif /* ST_APPEND */ |
| 12562 | #ifdef ST_NOATIME |
| 12563 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 12564 | #endif /* ST_NOATIME */ |
| 12565 | #ifdef ST_NODIRATIME |
| 12566 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 12567 | #endif /* ST_NODIRATIME */ |
| 12568 | #ifdef ST_RELATIME |
| 12569 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 12570 | #endif /* ST_RELATIME */ |
| 12571 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12572 | /* FreeBSD sendfile() constants */ |
| 12573 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12574 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12575 | #endif |
| 12576 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12577 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12578 | #endif |
| 12579 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12580 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 12581 | #endif |
| 12582 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12583 | /* constants for posix_fadvise */ |
| 12584 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12585 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12586 | #endif |
| 12587 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12588 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12589 | #endif |
| 12590 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12591 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12592 | #endif |
| 12593 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12594 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12595 | #endif |
| 12596 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12597 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12598 | #endif |
| 12599 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12600 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12601 | #endif |
| 12602 | |
| 12603 | /* constants for waitid */ |
| 12604 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12605 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 12606 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 12607 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12608 | #endif |
| 12609 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12610 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12611 | #endif |
| 12612 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12613 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12614 | #endif |
| 12615 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12616 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12617 | #endif |
| 12618 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12619 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12620 | #endif |
| 12621 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12622 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12623 | #endif |
| 12624 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12625 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12626 | #endif |
| 12627 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12628 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12629 | #endif |
| 12630 | |
| 12631 | /* constants for lockf */ |
| 12632 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12633 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12634 | #endif |
| 12635 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12636 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12637 | #endif |
| 12638 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12639 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12640 | #endif |
| 12641 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12642 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12643 | #endif |
| 12644 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 12645 | #ifdef HAVE_SPAWNV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12646 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 12647 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
| 12648 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
| 12649 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
| 12650 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 12651 | #endif |
| 12652 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12653 | #ifdef HAVE_SCHED_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12654 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
| 12655 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
| 12656 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12657 | #ifdef SCHED_SPORADIC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12658 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12659 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12660 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12661 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12662 | #endif |
| 12663 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12664 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12665 | #endif |
| 12666 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12667 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12668 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12669 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12670 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12671 | #endif |
| 12672 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12673 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12674 | #endif |
| 12675 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12676 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12677 | #endif |
| 12678 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12679 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 12680 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12681 | #endif |
| 12682 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12683 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12684 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 12685 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 12686 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12687 | #endif |
| 12688 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12689 | #ifdef RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12690 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12691 | #endif |
| 12692 | #ifdef RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12693 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12694 | #endif |
| 12695 | #ifdef RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12696 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12697 | #endif |
| 12698 | #ifdef RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12699 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12700 | #endif |
| 12701 | #ifdef RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12702 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12703 | #endif |
| 12704 | #ifdef RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12705 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12706 | #endif |
| 12707 | #ifdef RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 12708 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 12709 | #endif |
| 12710 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12711 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12712 | } |
| 12713 | |
| 12714 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12715 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12716 | PyModuleDef_HEAD_INIT, |
| 12717 | MODNAME, |
| 12718 | posix__doc__, |
| 12719 | -1, |
| 12720 | posix_methods, |
| 12721 | NULL, |
| 12722 | NULL, |
| 12723 | NULL, |
| 12724 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 12725 | }; |
| 12726 | |
| 12727 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12728 | static char *have_functions[] = { |
| 12729 | |
| 12730 | #ifdef HAVE_FACCESSAT |
| 12731 | "HAVE_FACCESSAT", |
| 12732 | #endif |
| 12733 | |
| 12734 | #ifdef HAVE_FCHDIR |
| 12735 | "HAVE_FCHDIR", |
| 12736 | #endif |
| 12737 | |
| 12738 | #ifdef HAVE_FCHMOD |
| 12739 | "HAVE_FCHMOD", |
| 12740 | #endif |
| 12741 | |
| 12742 | #ifdef HAVE_FCHMODAT |
| 12743 | "HAVE_FCHMODAT", |
| 12744 | #endif |
| 12745 | |
| 12746 | #ifdef HAVE_FCHOWN |
| 12747 | "HAVE_FCHOWN", |
| 12748 | #endif |
| 12749 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 12750 | #ifdef HAVE_FCHOWNAT |
| 12751 | "HAVE_FCHOWNAT", |
| 12752 | #endif |
| 12753 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12754 | #ifdef HAVE_FEXECVE |
| 12755 | "HAVE_FEXECVE", |
| 12756 | #endif |
| 12757 | |
| 12758 | #ifdef HAVE_FDOPENDIR |
| 12759 | "HAVE_FDOPENDIR", |
| 12760 | #endif |
| 12761 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12762 | #ifdef HAVE_FPATHCONF |
| 12763 | "HAVE_FPATHCONF", |
| 12764 | #endif |
| 12765 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12766 | #ifdef HAVE_FSTATAT |
| 12767 | "HAVE_FSTATAT", |
| 12768 | #endif |
| 12769 | |
| 12770 | #ifdef HAVE_FSTATVFS |
| 12771 | "HAVE_FSTATVFS", |
| 12772 | #endif |
| 12773 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12774 | #ifdef HAVE_FTRUNCATE |
| 12775 | "HAVE_FTRUNCATE", |
| 12776 | #endif |
| 12777 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12778 | #ifdef HAVE_FUTIMENS |
| 12779 | "HAVE_FUTIMENS", |
| 12780 | #endif |
| 12781 | |
| 12782 | #ifdef HAVE_FUTIMES |
| 12783 | "HAVE_FUTIMES", |
| 12784 | #endif |
| 12785 | |
| 12786 | #ifdef HAVE_FUTIMESAT |
| 12787 | "HAVE_FUTIMESAT", |
| 12788 | #endif |
| 12789 | |
| 12790 | #ifdef HAVE_LINKAT |
| 12791 | "HAVE_LINKAT", |
| 12792 | #endif |
| 12793 | |
| 12794 | #ifdef HAVE_LCHFLAGS |
| 12795 | "HAVE_LCHFLAGS", |
| 12796 | #endif |
| 12797 | |
| 12798 | #ifdef HAVE_LCHMOD |
| 12799 | "HAVE_LCHMOD", |
| 12800 | #endif |
| 12801 | |
| 12802 | #ifdef HAVE_LCHOWN |
| 12803 | "HAVE_LCHOWN", |
| 12804 | #endif |
| 12805 | |
| 12806 | #ifdef HAVE_LSTAT |
| 12807 | "HAVE_LSTAT", |
| 12808 | #endif |
| 12809 | |
| 12810 | #ifdef HAVE_LUTIMES |
| 12811 | "HAVE_LUTIMES", |
| 12812 | #endif |
| 12813 | |
| 12814 | #ifdef HAVE_MKDIRAT |
| 12815 | "HAVE_MKDIRAT", |
| 12816 | #endif |
| 12817 | |
| 12818 | #ifdef HAVE_MKFIFOAT |
| 12819 | "HAVE_MKFIFOAT", |
| 12820 | #endif |
| 12821 | |
| 12822 | #ifdef HAVE_MKNODAT |
| 12823 | "HAVE_MKNODAT", |
| 12824 | #endif |
| 12825 | |
| 12826 | #ifdef HAVE_OPENAT |
| 12827 | "HAVE_OPENAT", |
| 12828 | #endif |
| 12829 | |
| 12830 | #ifdef HAVE_READLINKAT |
| 12831 | "HAVE_READLINKAT", |
| 12832 | #endif |
| 12833 | |
| 12834 | #ifdef HAVE_RENAMEAT |
| 12835 | "HAVE_RENAMEAT", |
| 12836 | #endif |
| 12837 | |
| 12838 | #ifdef HAVE_SYMLINKAT |
| 12839 | "HAVE_SYMLINKAT", |
| 12840 | #endif |
| 12841 | |
| 12842 | #ifdef HAVE_UNLINKAT |
| 12843 | "HAVE_UNLINKAT", |
| 12844 | #endif |
| 12845 | |
| 12846 | #ifdef HAVE_UTIMENSAT |
| 12847 | "HAVE_UTIMENSAT", |
| 12848 | #endif |
| 12849 | |
| 12850 | #ifdef MS_WINDOWS |
| 12851 | "MS_WINDOWS", |
| 12852 | #endif |
| 12853 | |
| 12854 | NULL |
| 12855 | }; |
| 12856 | |
| 12857 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 12858 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 12859 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12860 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12861 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12862 | PyObject *list; |
| 12863 | char **trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12864 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12865 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 12866 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 12867 | #endif |
| 12868 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12869 | m = PyModule_Create(&posixmodule); |
| 12870 | if (m == NULL) |
| 12871 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 12872 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12873 | /* Initialize environ dictionary */ |
| 12874 | v = convertenviron(); |
| 12875 | Py_XINCREF(v); |
| 12876 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 12877 | return NULL; |
| 12878 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12879 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12880 | if (all_ins(m)) |
| 12881 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 12882 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12883 | if (setup_confname_tables(m)) |
| 12884 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12885 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12886 | Py_INCREF(PyExc_OSError); |
| 12887 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 12888 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12889 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12890 | if (posix_putenv_garbage == NULL) |
| 12891 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 12892 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 12893 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12894 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12895 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 12896 | waitid_result_desc.name = MODNAME ".waitid_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12897 | if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0) |
| 12898 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12899 | #endif |
| 12900 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 12901 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12902 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 12903 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 12904 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12905 | if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0) |
| 12906 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12907 | structseq_new = StatResultType.tp_new; |
| 12908 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12909 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 12910 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12911 | if (PyStructSequence_InitType2(&StatVFSResultType, |
| 12912 | &statvfs_result_desc) < 0) |
| 12913 | return NULL; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12914 | #ifdef NEED_TICKS_PER_SECOND |
| 12915 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12916 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12917 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12918 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12919 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12920 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 12921 | # endif |
| 12922 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12923 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 12924 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12925 | sched_param_desc.name = MODNAME ".sched_param"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12926 | if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0) |
| 12927 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12928 | SchedParamType.tp_new = os_sched_param; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12929 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12930 | |
| 12931 | /* initialize TerminalSize_info */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12932 | if (PyStructSequence_InitType2(&TerminalSizeType, |
| 12933 | &TerminalSize_desc) < 0) |
| 12934 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12935 | |
| 12936 | /* initialize scandir types */ |
| 12937 | if (PyType_Ready(&ScandirIteratorType) < 0) |
| 12938 | return NULL; |
| 12939 | if (PyType_Ready(&DirEntryType) < 0) |
| 12940 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12941 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12942 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 12943 | Py_INCREF((PyObject*) &WaitidResultType); |
| 12944 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 12945 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12946 | Py_INCREF((PyObject*) &StatResultType); |
| 12947 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 12948 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 12949 | PyModule_AddObject(m, "statvfs_result", |
| 12950 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 12951 | |
| 12952 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 12953 | Py_INCREF(&SchedParamType); |
| 12954 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 12955 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12956 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 12957 | times_result_desc.name = MODNAME ".times_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12958 | if (PyStructSequence_InitType2(&TimesResultType, ×_result_desc) < 0) |
| 12959 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 12960 | PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType); |
| 12961 | |
| 12962 | uname_result_desc.name = MODNAME ".uname_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 12963 | if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0) |
| 12964 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 12965 | PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType); |
| 12966 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12967 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12968 | /* |
| 12969 | * Step 2 of weak-linking support on Mac OS X. |
| 12970 | * |
| 12971 | * The code below removes functions that are not available on the |
| 12972 | * currently active platform. |
| 12973 | * |
| 12974 | * 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] | 12975 | * 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] | 12976 | * OSX 10.4. |
| 12977 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12978 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12979 | if (fstatvfs == NULL) { |
| 12980 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 12981 | return NULL; |
| 12982 | } |
| 12983 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12984 | #endif /* HAVE_FSTATVFS */ |
| 12985 | |
| 12986 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12987 | if (statvfs == NULL) { |
| 12988 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 12989 | return NULL; |
| 12990 | } |
| 12991 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12992 | #endif /* HAVE_STATVFS */ |
| 12993 | |
| 12994 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12995 | if (lchown == NULL) { |
| 12996 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 12997 | return NULL; |
| 12998 | } |
| 12999 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13000 | #endif /* HAVE_LCHOWN */ |
| 13001 | |
| 13002 | |
| 13003 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13004 | |
Antoine Pitrou | 9d20e0e | 2012-09-12 18:01:36 +0200 | [diff] [blame] | 13005 | Py_INCREF(&TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13006 | PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); |
| 13007 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 13008 | billion = PyLong_FromLong(1000000000); |
| 13009 | if (!billion) |
| 13010 | return NULL; |
| 13011 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13012 | /* suppress "function not used" warnings */ |
| 13013 | { |
| 13014 | int ignored; |
| 13015 | fd_specified("", -1); |
| 13016 | follow_symlinks_specified("", 1); |
| 13017 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 13018 | dir_fd_converter(Py_None, &ignored); |
| 13019 | dir_fd_unavailable(Py_None, &ignored); |
| 13020 | } |
| 13021 | |
| 13022 | /* |
| 13023 | * provide list of locally available functions |
| 13024 | * so os.py can populate support_* lists |
| 13025 | */ |
| 13026 | list = PyList_New(0); |
| 13027 | if (!list) |
| 13028 | return NULL; |
| 13029 | for (trace = have_functions; *trace; trace++) { |
| 13030 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 13031 | if (!unicode) |
| 13032 | return NULL; |
| 13033 | if (PyList_Append(list, unicode)) |
| 13034 | return NULL; |
| 13035 | Py_DECREF(unicode); |
| 13036 | } |
| 13037 | PyModule_AddObject(m, "_have_functions", list); |
| 13038 | |
| 13039 | initialized = 1; |
| 13040 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13041 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 13042 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13043 | |
| 13044 | #ifdef __cplusplus |
| 13045 | } |
| 13046 | #endif |